Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #206: Removed address reference to model in gorm query #212

Merged
merged 1 commit into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions projections/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (p *GORMDB) GetByKey(ctxt context.Context, entityFactory weos.EntityFactory

model, err := p.GORMModel(entityFactory.Name(), entityFactory.Schema(), nil)

result := p.db.Table(entityFactory.Name()).Model(model).Preload(clause.Associations).Scopes(ContentQuery()).Find(&model, identifiers)
result := p.db.Table(entityFactory.Name()).Model(model).Preload(clause.Associations).Scopes(ContentQuery()).Find(model, identifiers)
if result.Error != nil {
return nil, result.Error
}
Expand Down Expand Up @@ -120,7 +120,7 @@ func (p *GORMDB) Persist(entities []weos.Entity) error {
if err != nil {
return err
}
result := transaction.Table(tableName).Save(tmodel)
result := transaction.Table(tableName).Debug().Save(tmodel)
if result.Error != nil {
transaction.Rollback()
return result.Error
Expand Down Expand Up @@ -658,7 +658,7 @@ func (p *GORMDB) GetContentEntity(ctx context.Context, entityFactory weos.Entity

model, err := p.GORMModel(entityFactory.Name(), entityFactory.Schema(), nil)

result := p.db.Table(entityFactory.TableName()).Model(model).Preload(clause.Associations).Find(&model, "weos_id = ? ", weosID)
result := p.db.Table(entityFactory.TableName()).Model(model).Preload(clause.Associations).Find(model, "weos_id = ? ", weosID)
if result.Error != nil {
p.logger.Errorf("unexpected error retrieving entity , got: '%s'", result.Error)
return nil, result.Error
Expand Down
139 changes: 139 additions & 0 deletions projections/gorm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,3 +487,142 @@ func TestGORMDB_Remove(t *testing.T) {
}
})
}

func TestGORMDB_GetByKey(t *testing.T) {
err := gormDB.Migrator().DropTable("Blog")
if err != nil {
t.Errorf("error removing table '%s' '%s'", "Post", err)
}

defer gormDB.Migrator().DropTable("Blog")

api, err := rest.New("../controllers/rest/fixtures/blog.yaml")
if err != nil {
t.Fatalf("unexpected error setting up api: %s", err)
}
projection, err := projections.NewProjection(context.Background(), gormDB, api.EchoInstance().Logger)
if err != nil {
t.Fatal(err)
}
projection.Migrate(context.TODO(), api.GetConfig())

t.Run("get entity with associations", func(t *testing.T) {
api.RegisterEntityFactory("Blog", new(model.DefaultEntityFactory).FromSchema("Blog", api.GetConfig().Components.Schemas["Blog"].Value))
ef, err := api.GetEntityFactory("Blog")
if err != nil {
t.Fatalf("unexpected error getting entity factory '%s'", err)
}
blog := `{"title":"My Blog","url":"http://wepala.com","description":"This is my blog","author":{"id":"test","email":"test.example.org", "firstName":"John", "lastName":"Doe"}}`
contentEntity, err := ef.CreateEntityWithValues(context.Background(), []byte(blog))
if err != nil {
t.Fatalf("unexpected error creating entity '%s'", err)
}
err = projection.Persist([]model.Entity{contentEntity})
if err != nil {
t.Fatalf("unexpected error persisting entity '%s'", err)
}
//check Blog is created
tmodel, err := projection.GORMModel("Blog", ef.Schema(), []byte(`{}`))
if err != nil {
t.Fatalf("unexpected error creating model '%s'", err)
}
projection.DB().Table(contentEntity.Name).Find(&tmodel, "title = ?", "My Blog")
reader := ds.NewReader(tmodel)
if !reader.HasField("Id") {
t.Fatalf("expected id to be present")
}
//use the generated id to look up the blog by key
tblog, err := projection.GetByKey(context.TODO(), ef, map[string]interface{}{
"id": reader.GetField("Id").Uint(),
})
if err != nil {
t.Fatalf("unexpected error getting entity '%s'", err)
}
if tblog == nil {
t.Fatalf("expected entity to be found")
}
if author := tblog.GetInterface("author"); author != nil {
var authorMap map[string]interface{}
authorRaw, err := json.Marshal(author)
if err != nil {
t.Errorf("unexpected error marshalling author '%s'", err)
}
err = json.Unmarshal(authorRaw, &authorMap)
if authorMap["firstName"] != "John" {
t.Errorf("expected author to be '%s', got '%s'", "John", authorMap["firstName"])
}
} else {
t.Errorf("expected author to be '%s', got '%T'", "John Doe", tblog.GetInterface("author"))
}

})

}

func TestGORMDB_GetContentEntity(t *testing.T) {
err := gormDB.Migrator().DropTable("Blog")
if err != nil {
t.Errorf("error removing table '%s' '%s'", "Post", err)
}

defer gormDB.Migrator().DropTable("Blog")

api, err := rest.New("../controllers/rest/fixtures/blog.yaml")
if err != nil {
t.Fatalf("unexpected error setting up api: %s", err)
}
projection, err := projections.NewProjection(context.Background(), gormDB, api.EchoInstance().Logger)
if err != nil {
t.Fatal(err)
}
projection.Migrate(context.TODO(), api.GetConfig())

t.Run("get entity with associations", func(t *testing.T) {
api.RegisterEntityFactory("Blog", new(model.DefaultEntityFactory).FromSchema("Blog", api.GetConfig().Components.Schemas["Blog"].Value))
ef, err := api.GetEntityFactory("Blog")
if err != nil {
t.Fatalf("unexpected error getting entity factory '%s'", err)
}
blog := `{"title":"My Blog","url":"http://wepala.com","description":"This is my blog","author":{"id":"test","email":"test.example.org", "firstName":"John", "lastName":"Doe"}}`
contentEntity, err := ef.CreateEntityWithValues(context.Background(), []byte(blog))
if err != nil {
t.Fatalf("unexpected error creating entity '%s'", err)
}
err = projection.Persist([]model.Entity{contentEntity})
if err != nil {
t.Fatalf("unexpected error persisting entity '%s'", err)
}
//check Blog is created
tmodel, err := projection.GORMModel("Blog", ef.Schema(), []byte(`{}`))
if err != nil {
t.Fatalf("unexpected error creating model '%s'", err)
}
projection.DB().Table(contentEntity.Name).Find(&tmodel, "title = ?", "My Blog")
reader := ds.NewReader(tmodel)
if !reader.HasField("WeosID") {
t.Fatalf("expected Weos_id to be present")
}
//use the generated id to look up the blog by key
tblog, err := projection.GetContentEntity(context.TODO(), ef, reader.GetField("WeosID").String())
if err != nil {
t.Fatalf("unexpected error getting entity '%s'", err)
}
if tblog == nil {
t.Fatalf("expected entity to be found")
}
if author := tblog.GetInterface("author"); author != nil {
var authorMap map[string]interface{}
authorRaw, err := json.Marshal(author)
if err != nil {
t.Errorf("unexpected error marshalling author '%s'", err)
}
err = json.Unmarshal(authorRaw, &authorMap)
if authorMap["firstName"] != "John" {
t.Errorf("expected author to be '%s', got '%s'", "John", authorMap["firstName"])
}
} else {
t.Errorf("expected author to be '%s', got '%T'", "John Doe", tblog.GetInterface("author"))
}

})
}