-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✅ Test for Repository.atomicCompareAndSet (#525)
- Loading branch information
1 parent
f1dcb15
commit 3de9829
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
tdrive/backend/node/test/e2e/documents/editing-session.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { describe, beforeAll, beforeEach, it, expect, afterAll } from "@jest/globals"; | ||
|
||
import { init, TestPlatform } from "../setup"; | ||
import UserApi from "../common/user-api"; | ||
|
||
import { DriveFile, TYPE as DriveFileType } from "../../../src/services/documents/entities/drive-file"; | ||
|
||
describe("the Drive's documents' editing session kind-of-lock", () => { | ||
let platform: TestPlatform | null; | ||
let currentUser: UserApi; | ||
let currentUserRoot: string | undefined; | ||
let temporaryDocument: DriveFile; | ||
|
||
beforeAll(async () => { | ||
platform = await init({ | ||
services: [ | ||
"webserver", | ||
"database", | ||
"applications", | ||
"search", | ||
"storage", | ||
"message-queue", | ||
"user", | ||
"search", | ||
"files", | ||
"websocket", | ||
"messages", | ||
"auth", | ||
"realtime", | ||
"channels", | ||
"counter", | ||
"statistics", | ||
"platform-services", | ||
"documents", | ||
], | ||
}); | ||
currentUser = await UserApi.getInstance(platform); | ||
currentUserRoot = `user_${currentUser.user.id}`; | ||
}); | ||
|
||
afterAll(async () => { | ||
await platform?.tearDown(); | ||
platform = null; | ||
}); | ||
|
||
beforeEach(async () => { | ||
temporaryDocument = await currentUser.createDefaultDocument({ | ||
parent_id: currentUserRoot, | ||
scope: "personal", | ||
}); | ||
}); | ||
|
||
it("atomicCompareAndSet allows a single value at a time", async () => { | ||
const driveFileRepository = await platform?.database.getRepository<DriveFile>(DriveFileType, DriveFile); | ||
|
||
let result = await driveFileRepository?.atomicCompareAndSet(temporaryDocument, "editing_session_key", null, "123"); | ||
expect(result).toEqual({ didSet: true, currentValue: "123" }); | ||
|
||
result = await driveFileRepository?.atomicCompareAndSet(temporaryDocument, "editing_session_key", null, "124"); | ||
expect(result).toEqual({ didSet: false, currentValue: "123" }); | ||
|
||
expect(driveFileRepository?.atomicCompareAndSet(temporaryDocument, "editing_session_key", "124", "124")). | ||
rejects.toThrow("Previous and new values are identical"); | ||
|
||
result = await driveFileRepository?.atomicCompareAndSet(temporaryDocument, "editing_session_key", "122", "124"); | ||
expect(result).toEqual({ didSet: false, currentValue: "123" }); | ||
|
||
result = await driveFileRepository?.atomicCompareAndSet(temporaryDocument, "editing_session_key", "123", "124"); | ||
expect(result).toEqual({ didSet: true, currentValue: "124" }); | ||
|
||
result = await driveFileRepository?.atomicCompareAndSet(temporaryDocument, "editing_session_key", "124", null); | ||
expect(result).toEqual({ didSet: true, currentValue: null }); | ||
|
||
temporaryDocument.id = "00000000-0000-0000-0000-000000000000"; | ||
expect(driveFileRepository?.atomicCompareAndSet(temporaryDocument, "editing_session_key", "124", null)). | ||
rejects.toThrow("no row matched PK"); | ||
}); | ||
}); |