Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
window9u committed Jul 21, 2024
1 parent fe91dac commit 94c5fbe
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions server/backend/database/testcases/testcases.go
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,46 @@ func RunCreateChangeInfosTest(t *testing.T, db database.Database, projectID type
assert.NoError(t, err)
assert.NotEqual(t, database.DocumentRemoved, clientInfo.Documents[docInfo.ID].Status)
})

t.Run("set updated_at in docInfo test", func(t *testing.T) {
ctx := context.Background()
docKey := helper.TestDocKey(t)

// 01. Create a client and a document then attach the document to the client.
clientInfo, _ := db.ActivateClient(ctx, projectID, t.Name())
docInfo1, _ := db.FindDocInfoByKeyAndOwner(ctx, clientInfo.RefKey(), docKey, true)
docRefKey := docInfo1.RefKey()
assert.NoError(t, clientInfo.AttachDocument(docRefKey.DocID, false))
assert.NoError(t, db.UpdateClientInfoAfterPushPull(ctx, clientInfo, docInfo1))

bytesID, _ := clientInfo.ID.Bytes()
actorID, _ := time.ActorIDFromBytes(bytesID)
doc := document.New(key.Key(t.Name()))
doc.SetActor(actorID)

// 02. update document only presence
assert.NoError(t, doc.Update(func(root *json.Object, p *presence.Presence) error {
p.Set("key", "val")
return nil
}))
pack := doc.CreateChangePack()
updatedAt := docInfo1.UpdatedAt
assert.NoError(t, db.CreateChangeInfos(ctx, projectID, docInfo1, 0, pack.Changes, false))
docInfo2, _ := db.FindDocInfoByKeyAndOwner(ctx, clientInfo.RefKey(), docKey, true)
assert.Equal(t, updatedAt, docInfo2.UpdatedAt)

// 03. update document presence and operation
assert.NoError(t, doc.Update(func(root *json.Object, p *presence.Presence) error {
p.Set("key", "val")
root.SetNewArray("array")
return nil
}))
pack = doc.CreateChangePack()
updatedAt = docInfo2.UpdatedAt
assert.NoError(t, db.CreateChangeInfos(ctx, projectID, docInfo2, 0, pack.Changes, false))
docInfo3, _ := db.FindDocInfoByKeyAndOwner(ctx, clientInfo.RefKey(), docKey, true)
assert.NotEqual(t, updatedAt, docInfo3.UpdatedAt)
})
}

// RunUpdateClientInfoAfterPushPullTest runs the UpdateClientInfoAfterPushPull tests for the given db.
Expand Down

0 comments on commit 94c5fbe

Please sign in to comment.