From 94c5fbe7bb5f118869cd35d34e9326ce166d6cc7 Mon Sep 17 00:00:00 2001 From: Changyu Moon <121847433+window9u@users.noreply.github.com> Date: Sun, 21 Jul 2024 11:58:55 +0900 Subject: [PATCH] Add test --- .../backend/database/testcases/testcases.go | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/server/backend/database/testcases/testcases.go b/server/backend/database/testcases/testcases.go index 0b9e42b19..da3919471 100644 --- a/server/backend/database/testcases/testcases.go +++ b/server/backend/database/testcases/testcases.go @@ -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.