Skip to content

Commit

Permalink
Merge branch 'feature/WEOS-1324' into akeem-poc
Browse files Browse the repository at this point in the history
# Conflicts:
#	projections/gorm.go
  • Loading branch information
akeemphilbert committed Feb 13, 2022
2 parents 586bfd9 + 62ead6d commit 2fa197a
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 20 deletions.
4 changes: 2 additions & 2 deletions controllers/rest/middleware_initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func newSchema(ref *openapi3.Schema, logger echo.Logger) (ds.Builder, map[string

relations := make(map[string]string)
for name, p := range ref.Properties {
tagString := `json:"` + utils.SnakeCase(name) + `"`
tagString := `json:"` + name + `"`
var gormParts []string
for _, req := range ref.Required {
if strings.EqualFold(req, name) {
Expand Down Expand Up @@ -203,7 +203,7 @@ func addRelations(struc ds.Builder, relations map[string]string, structs map[str
keystring += strings.Title(name) + strings.Title(k)
}

struc.AddField(name, instance, `json:"`+utils.SnakeCase(name)+`" gorm:"foreignKey:`+keystring+`; references `+strings.Join(key, ",")+`"`)
struc.AddField(name, instance, `json:"`+name+`" gorm:"foreignKey:`+keystring+`; references `+strings.Join(key, ",")+`"`)
}
}
return struc, nil
Expand Down
2 changes: 1 addition & 1 deletion end2end_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,7 @@ func TestBDD(t *testing.T) {
Options: &godog.Options{
Format: "pretty",
Tags: "~skipped && ~long",
//Tags: "WEOS-1131",
//Tags: "focus1",
//Tags: "WEOS-1110 && ~skipped",
},
}.Run()
Expand Down
5 changes: 3 additions & 2 deletions features/edit-content.feature
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,15 @@ Feature: Edit content
And the "Blog" is updated
| title | description |
| Some New Title | Some Description |


@focus1
Scenario: Update item with invalid data

If the content type validation fails then a 422 response code should be returned (the request could have a valid
format but the contents are invalid)

Given "Sojourner" is on the "Blog" edit screen with id "1234"
And "Sojourner" enters "Some New Title" in the "last_updated" field
And "Sojourner" enters "Some New Title" in the "lastUpdated" field

When the "Blog" is submitted
Then a 422 response should be returned
Expand Down
32 changes: 17 additions & 15 deletions projections/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package projections
import (
"encoding/json"
"fmt"
"strings"
"time"

ds "github.com/ompluscator/dynamic-struct"
weos "github.com/wepala/weos/model"
"golang.org/x/net/context"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"strings"
"time"
)

//GORMDB interface struct
Expand Down Expand Up @@ -77,16 +78,17 @@ func (p *GORMDB) GetByKey(ctxt context.Context, entityFactory weos.EntityFactory
}

func (p *GORMDB) GetByEntityID(ctx context.Context, entityFactory weos.EntityFactory, id string) (map[string]interface{}, error) {
scheme, err := entityFactory.NewEntity(ctx)
if err != nil {
return nil, err
}
result := p.db.Table(entityFactory.Name()).Scopes(ContentQuery()).Find(scheme.Property, "weos_id = ?", id)
//scheme, err := entityFactory.NewEntity(ctx)
tstruct := entityFactory.DynamicStruct(ctx).New()
//if err != nil {
// return nil, err
//}
result := p.db.Table(entityFactory.Name()).Scopes(ContentQuery()).Find(tstruct, "weos_id = ?", id)

if result.Error != nil {
return nil, result.Error
}
data, err := json.Marshal(scheme.Property)
data, err := json.Marshal(tstruct)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -216,17 +218,17 @@ func (p *GORMDB) GetEventHandler() weos.EventHandler {
}

func (p *GORMDB) GetContentEntity(ctx context.Context, entityFactory weos.EntityFactory, weosID string) (*weos.ContentEntity, error) {
row := map[string]interface{}{}
result := p.db.Table(entityFactory.TableName()).Find(&row, "weos_id = ? ", weosID)
if result.Error != nil {
p.logger.Errorf("unexpected error retrieving created blog, got: '%s'", result.Error)
}
//set result to entity
newEntity, err := entityFactory.NewEntity(ctx)
if err != nil {
return nil, err
}
rowData, err := json.Marshal(row)

result := p.db.Table(entityFactory.TableName()).Find(newEntity.Property, "weos_id = ? ", weosID)
if result.Error != nil {
p.logger.Errorf("unexpected error retrieving created blog, got: '%s'", result.Error)
}
//set result to entity
rowData, err := json.Marshal(newEntity.Property)
if err != nil {
return nil, err
}
Expand Down
102 changes: 102 additions & 0 deletions projections/projections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2750,3 +2750,105 @@ components:
}
})
}

func TestProjections_GetContentWithCorrectCasing(t *testing.T) {
openAPI := `openapi: 3.0.3
info:
title: Blog
description: Blog example
version: 1.0.0
servers:
- url: https://prod1.weos.sh/blog/dev
description: WeOS Dev
- url: https://prod1.weos.sh/blog/v1
components:
schemas:
Post:
type: object
properties:
title:
type: string
description: blog title
description:
type: string
Blog:
type: object
properties:
title:
type: string
description: blog title
lastUpdated:
type: string
posts:
type: array
items:
$ref: "#/components/schemas/Post"
`

api, err := rest.New(openAPI)
if err != nil {
t.Fatalf("error loading api config '%s'", err)
}
schemes := rest.CreateSchema(context.Background(), echo.New(), api.Swagger)
p, err := projections.NewProjection(context.Background(), gormDB, api.EchoInstance().Logger)
if err != nil {
t.Fatal(err)
}

err = p.Migrate(context.Background(), schemes)
if err != nil {
t.Fatal(err)
}

if !gormDB.Migrator().HasTable("Blog") {
t.Errorf("expected to get a table 'Blog'")
}

if !gormDB.Migrator().HasTable("Post") {
t.Errorf("expected to get a table 'Post'")
}

if !gormDB.Migrator().HasTable("blog_posts") {
t.Errorf("expected to get a table 'blog_posts'")
}

columns, _ := gormDB.Migrator().ColumnTypes("Blog")

found := false
for _, c := range columns {
if c.Name() == "last_updated" {
found = true
}
}

if !found {
t.Fatal("not all fields found")
}
gormDB.Table("Blog").Create(map[string]interface{}{"weos_id": "5678", "sequence_no": 1, "title": "hugs", "last_updated": "Test"})

blogEntityFactory := new(weos.DefaultEntityFactory).FromSchemaAndBuilder("Blog", api.Swagger.Components.Schemas["Blog"].Value, schemes["Blog"])
r, err := p.GetByEntityID(context.Background(), blogEntityFactory, "5678")
if err != nil {
t.Fatalf("error querying '%s' '%s'", "Blog", err)
}
if r["title"] != "hugs" {
t.Errorf("expected the blog title to be %s got %v", "hugs", r["titles"])
}

if r["lastUpdated"] != "Test" {
t.Errorf("expected the lastUpdated to be %s got %v", "Test", r["lastUpdated"])
}

err = gormDB.Migrator().DropTable("blog_posts")
if err != nil {
t.Errorf("error removing table '%s' '%s'", "blog_posts", err)
}
err = gormDB.Migrator().DropTable("Post")
if err != nil {
t.Errorf("error removing table '%s' '%s'", "Post", err)
}
err = gormDB.Migrator().DropTable("Blog")
if err != nil {
t.Errorf("error removing table '%s' '%s'", "Blog", err)
}
}

0 comments on commit 2fa197a

Please sign in to comment.