MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / Test_getUpdatedDocument

Function Test_getUpdatedDocument

db/database_test.go:4135–4197  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

4133}
4134
4135func Test_getUpdatedDocument(t *testing.T) {
4136 t.Run("Non Sync document is not processed", func(t *testing.T) {
4137 db, ctx := setupTestDB(t)
4138 defer db.Close(ctx)
4139
4140 docID := "testDoc"
4141
4142 body := `{"val": "nonsyncdoc"}`
4143 added, err := db.Bucket.DefaultDataStore().AddRaw(docID, 0, []byte(body))
4144 require.NoError(t, err)
4145 assert.True(t, added)
4146
4147 raw, _, err := db.Bucket.DefaultDataStore().GetRaw(docID)
4148 require.NoError(t, err)
4149 doc, err := unmarshalDocument(docID, raw)
4150 require.NoError(t, err)
4151
4152 collection, ctx := GetSingleDatabaseCollectionWithUser(ctx, t, db)
4153 _, _, _, _, _, err = collection.getResyncedDocument(ctx, doc, false, []uint64{})
4154 assert.Equal(t, base.ErrUpdateCancel, err)
4155 })
4156
4157 t.Run("Sync Document", func(t *testing.T) {
4158 db, ctx := setupTestDB(t)
4159 defer db.Close(ctx)
4160 collection, ctx := GetSingleDatabaseCollectionWithUser(ctx, t, db)
4161 syncFn := `
4162 function sync(doc, oldDoc){
4163 channel("channel." + "ABC");
4164 }
4165`
4166 _, err := collection.UpdateSyncFun(ctx, syncFn)
4167 require.NoError(t, err)
4168
4169 docID := uuid.NewString()
4170 updateBody := make(map[string]interface{})
4171 updateBody["val"] = "value"
4172 _, doc, err := collection.Put(ctx, docID, updateBody)
4173 require.NoError(t, err)
4174 assert.NotNil(t, doc)
4175
4176 syncFn = `
4177 function sync(doc, oldDoc){
4178 channel("channel." + "ABC12332423234");
4179 }
4180 `
4181 _, err = collection.UpdateSyncFun(ctx, syncFn)
4182 require.NoError(t, err)
4183
4184 updatedDoc, shouldUpdate, _, highSeq, _, err := collection.getResyncedDocument(ctx, doc, false, []uint64{})
4185 require.NoError(t, err)
4186 assert.True(t, shouldUpdate)
4187 assert.Equal(t, doc.Sequence, highSeq)
4188 assert.Equal(t, 2, int(db.DbStats.Database().SyncFunctionCount.Value()))
4189
4190 // Rerunning same resync function should mark doc not to be updated
4191 _, shouldUpdate, _, _, _, err = collection.getResyncedDocument(ctx, updatedDoc, false, []uint64{})
4192 require.NoError(t, err)

Callers

nothing calls this directly

Calls 14

setupTestDBFunction · 0.85
unmarshalDocumentFunction · 0.85
getResyncedDocumentMethod · 0.80
UpdateSyncFunMethod · 0.80
RunMethod · 0.65
CloseMethod · 0.65
PutMethod · 0.65
AddRawMethod · 0.45
DefaultDataStoreMethod · 0.45
GetRawMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected