Skip to content

Commit

Permalink
fixed bug that caused gogm to fail when saving one node with no rels
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Solender committed Dec 12, 2019
1 parent 178f634 commit ce54cc5
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 20 deletions.
14 changes: 14 additions & 0 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,18 @@ func testSave(sess *Session, req *require.Assertions) {
req.Nil(sess.Commit())
req.Nil(a2.SingleSpecA)
req.Nil(b2.SingleSpec)


//test save something that isn't connected to anything
singleSave := &a{
TestField: "test",
TestTypeDefString: "dasdfas",
TestTypeDefInt: 600,
ManyA: []*b{},
MultiA: []*b{},
}

req.Nil(sess.Begin())
req.Nil(sess.SaveDepth(singleSave, 1))
req.Nil(sess.Commit())
}
23 changes: 3 additions & 20 deletions save.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,9 @@ func generateCurRels(parentId string, current *reflect.Value, currentDepth, maxD
if _, ok := (*curRels)[uuid]; ok {
//this node has already been seen
return nil
} else {
//create the record for it
(*curRels)[uuid] = map[string]*RelationConfig{}
}

//get the type
Expand Down Expand Up @@ -556,16 +559,6 @@ func generateCurRels(parentId string, current *reflect.Value, currentDepth, maxD
return err
}

//makes us go backwards
//if skip {
// continue
//}

//check that the map is there for this id
if _, ok := (*curRels)[uuid]; !ok {
(*curRels)[uuid] = map[string]*RelationConfig{}
}

//check the config is there for the specified field
if _, ok = (*curRels)[uuid][conf.FieldName]; !ok {
(*curRels)[uuid][conf.FieldName] = &RelationConfig{
Expand All @@ -587,16 +580,6 @@ func generateCurRels(parentId string, current *reflect.Value, currentDepth, maxD
return err
}

//makes us go backwards
//if skip {
// continue
//}

//check that the map is there for this id
if _, ok := (*curRels)[uuid]; !ok {
(*curRels)[uuid] = map[string]*RelationConfig{}
}

//check the config is there for the specified field
if _, ok = (*curRels)[uuid][conf.FieldName]; !ok {
(*curRels)[uuid][conf.FieldName] = &RelationConfig{
Expand Down
58 changes: 58 additions & 0 deletions save_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,64 @@ func parseM2M(req *require.Assertions) {
req.EqualValues(oldRels, curRels)
}


func TestCalculateCurRels(t *testing.T) {
req := require.New(t)

req.Nil(setupInit(true, nil, &a{}, &b{}, &c{}))

//test single save
a1 := &a{
TestField: "test",
TestTypeDefString: "dasdfas",
TestTypeDefInt: 600,
BaseNode: BaseNode{
Id: 1,
UUID: "a1uuid",
LoadMap: map[string]*RelationConfig{
"MultiA": {
Ids: []int64{2},
RelationType: Multi,
},
},
},
ManyA: []*b{},
MultiA: []*b{},
}

//b1 := &b{
// TestField: "test",
// BaseNode: BaseNode{
// Id: 2,
// UUID: "b1uuid",
// LoadMap: map[string]*RelationConfig{
// "Multi": {
// Ids: []int64{1},
// RelationType: Multi,
// },
// },
// },
// Multi: []*a{},
//}

//b1.Multi = append(b1.Multi, a1)
//a1.MultiA = append(a1.MultiA, b1)

nodes := map[string]map[string]nodeCreateConf{}
relations := map[string][]relCreateConf{}
oldRels := map[string]map[string]*RelationConfig{}
curRels := map[string]map[string]*RelationConfig{}
ids := []*string{}

nodeRef := map[string]*reflect.Value{}

val := reflect.ValueOf(a1)

req.Nil(parseStruct("", "", false, 0, nil, &val, 0, 5, &nodes, &relations, &oldRels, &ids, &nodeRef))
req.Nil(generateCurRels("", &val, 0, 5, &curRels))
req.Equal(1, len(curRels))
}

func TestCalculateDels(t *testing.T) {
req := require.New(t)

Expand Down

0 comments on commit ce54cc5

Please sign in to comment.