Skip to content

Commit d4d940d

Browse files
Add test for document update with mergeObjects option (#381)
1 parent 06a0f56 commit d4d940d

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

test/document_update_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,97 @@ func TestUpdateDocumentReturnNew(t *testing.T) {
125125
}
126126
}
127127

128+
// TestUpdateDocumentKeepNullTrue creates a document, updates it with MergeObjects(true) and then checks the update has succeeded.
129+
func TestUpdateDocumentWithMergeObjectsTrue(t *testing.T) {
130+
ctx := context.Background()
131+
// don't use disallowUnknownFields in this test - we have here custom structs defined
132+
c := createClient(t, true, false)
133+
db := ensureDatabase(ctx, c, "document_test", nil, t)
134+
col := ensureCollection(ctx, db, "document_test", nil, t)
135+
type account struct {
136+
ID string `json:"id"`
137+
User UserDocWithKeyWithOmit `json:"user"`
138+
}
139+
doc := account{
140+
ID: "1234",
141+
User: UserDocWithKeyWithOmit{
142+
Name: "Mathilda",
143+
},
144+
}
145+
meta, err := col.CreateDocument(ctx, doc)
146+
if err != nil {
147+
t.Fatalf("Failed to create new document: %s", describe(err))
148+
}
149+
150+
// Update document
151+
const newUserAge = 30
152+
update := map[string]interface{}{
153+
"user": UserDocWithKeyWithOmit{
154+
Age: newUserAge,
155+
},
156+
}
157+
if _, err := col.UpdateDocument(driver.WithMergeObjects(ctx, true), meta.Key, update); err != nil {
158+
t.Fatalf("Failed to update document '%s': %s", meta.Key, describe(err))
159+
}
160+
161+
// Read updated document
162+
var readDoc account
163+
if _, err := col.ReadDocument(ctx, meta.Key, &readDoc); err != nil {
164+
t.Fatalf("Failed to read document '%s': %s", meta.Key, describe(err))
165+
}
166+
doc.User.Key = readDoc.User.Key
167+
doc.User.Age = newUserAge
168+
if !reflect.DeepEqual(doc, readDoc) {
169+
t.Errorf("Got wrong document. Expected %+v, got %+v", doc, readDoc)
170+
}
171+
}
172+
173+
// TestUpdateDocumentKeepNullTrue creates a document, updates it with WithMerge(false) and then checks the update has succeeded.
174+
func TestUpdateDocumentWithMergeObjectsFalse(t *testing.T) {
175+
ctx := context.Background()
176+
// don't use disallowUnknownFields in this test - we have here custom structs defined
177+
c := createClient(t, true, false)
178+
db := ensureDatabase(ctx, c, "document_test", nil, t)
179+
col := ensureCollection(ctx, db, "document_test", nil, t)
180+
type account struct {
181+
ID string `json:"id"`
182+
User UserDocWithKeyWithOmit `json:"user"`
183+
}
184+
doc := account{
185+
ID: "1234",
186+
User: UserDocWithKeyWithOmit{
187+
Name: "Mathilda",
188+
},
189+
}
190+
meta, err := col.CreateDocument(ctx, doc)
191+
if err != nil {
192+
t.Fatalf("Failed to create new document: %s", describe(err))
193+
}
194+
195+
// Update document
196+
const newUserAge = 30
197+
update := map[string]interface{}{
198+
"user": UserDocWithKeyWithOmit{
199+
Age: newUserAge,
200+
},
201+
}
202+
if _, err := col.UpdateDocument(driver.WithMergeObjects(ctx, false), meta.Key, update); err != nil {
203+
t.Fatalf("Failed to update document '%s': %s", meta.Key, describe(err))
204+
}
205+
206+
// Read updated document
207+
var readDoc account
208+
if _, err := col.ReadDocument(ctx, meta.Key, &readDoc); err != nil {
209+
t.Fatalf("Failed to read document '%s': %s", meta.Key, describe(err))
210+
}
211+
doc.User = UserDocWithKeyWithOmit{
212+
Age: newUserAge,
213+
}
214+
if !reflect.DeepEqual(doc, readDoc) {
215+
t.Errorf("Got wrong document. Expected %+v, got %+v", doc, readDoc)
216+
}
217+
}
218+
128219
// TestUpdateDocumentKeepNullTrue creates a document, updates it with KeepNull(true) and then checks the update has succeeded.
129220
func TestUpdateDocumentKeepNullTrue(t *testing.T) {
130221
ctx := context.Background()

0 commit comments

Comments
 (0)