New API tested
This commit is contained in:
@@ -396,3 +396,50 @@ func fillBoltDB(t *testing.T, bdb *BoltDB) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplySet(t *testing.T) {
|
||||
cases := map[string]struct {
|
||||
doc bson.M
|
||||
operator bson.M
|
||||
want bson.M
|
||||
}{
|
||||
"noop on empty": {},
|
||||
"noop on full": {
|
||||
doc: bson.M{"hello": "world"},
|
||||
want: bson.M{"hello": "world"},
|
||||
},
|
||||
"add new field on full": {
|
||||
operator: bson.M{"hi": "mom"},
|
||||
doc: bson.M{"hello": "world"},
|
||||
want: bson.M{"hello": "world", "hi": "mom"},
|
||||
},
|
||||
"change only field on full": {
|
||||
operator: bson.M{"hello": "lol jk not world"},
|
||||
doc: bson.M{"hello": "world"},
|
||||
want: bson.M{"hello": "lol jk not world"},
|
||||
},
|
||||
"set existing, nested field": {
|
||||
operator: bson.M{"hello.world": "hi"},
|
||||
doc: bson.M{"hello": bson.M{"world": "not hi"}},
|
||||
want: bson.M{"hello": bson.M{"world": "hi"}},
|
||||
},
|
||||
"add to existing, nested field": {
|
||||
operator: bson.M{"hello.notworld": "hi"},
|
||||
doc: bson.M{"hello": bson.M{"world": "not hi"}},
|
||||
want: bson.M{"hello": bson.M{"world": "not hi", "notworld": "hi"}},
|
||||
},
|
||||
}
|
||||
|
||||
for name, d := range cases {
|
||||
c := d
|
||||
t.Run(name, func(t *testing.T) {
|
||||
out, err := applySet(c.doc, c.operator)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if fmt.Sprint(out) != fmt.Sprint(c.want) {
|
||||
t.Fatalf("(%+v, %+v) => want \n%+v\n, got \n%+v", c.doc, c.operator, c.want, out)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user