From 2ff44266ed883086fce28961c97163db41760e38 Mon Sep 17 00:00:00 2001 From: typedarray <90073088+0xOlias@users.noreply.github.com> Date: Tue, 27 Jun 2023 20:32:28 -0400 Subject: [PATCH] test: add timestamp tests for graphql server --- packages/core/src/server/service.test.ts | 100 ++++++++++++++++++++++- 1 file changed, 98 insertions(+), 2 deletions(-) diff --git a/packages/core/src/server/service.test.ts b/packages/core/src/server/service.test.ts index 23963ce5c..b1d6a0131 100644 --- a/packages/core/src/server/service.test.ts +++ b/packages/core/src/server/service.test.ts @@ -62,7 +62,7 @@ const setup = async ({ userStore }: { userStore: UserStore }) => { const createTestEntity = async ({ id }: { id: number }) => { await userStore.create({ modelName: "TestEntity", - timestamp: id, + timestamp: 0, id: String(id), data: { string: String(id), @@ -90,7 +90,7 @@ const setup = async ({ userStore }: { userStore: UserStore }) => { }) => { await userStore.create({ modelName: "EntityWithBigIntId", - timestamp: Number(id), + timestamp: 0, id, data: { testEntity: testEntityId, @@ -1299,3 +1299,99 @@ test("limits and skips together as expected", async (context) => { await service.teardown(); await userStore.teardown(); }); + +test("serves singular entity versioned at specified timestamp", async (context) => { + const { userStore } = context; + const { service, gql, createTestEntity } = await setup({ userStore }); + + await createTestEntity({ id: 1 }); + await userStore.update({ + modelName: "TestEntity", + timestamp: 10, + id: String(1), + data: { + string: "updated", + }, + }); + + const responseOld = await gql(` + testEntity(id: "1", timestamp: 5) { + id + string + } + `); + expect(responseOld.body.errors).toBe(undefined); + expect(responseOld.statusCode).toBe(200); + const testEntityOld = responseOld.body.data.testEntity; + expect(testEntityOld.string).toBe("1"); + + const response = await gql(` + testEntity(id: "1", timestamp: 15) { + id + string + } + `); + expect(response.body.errors).toBe(undefined); + expect(response.statusCode).toBe(200); + const testEntity = response.body.data.testEntity; + expect(testEntity.string).toBe("updated"); + + await service.teardown(); + await userStore.teardown(); +}); + +test("serves plural entities versioned at specified timestamp", async (context) => { + const { userStore } = context; + const { service, gql, createTestEntity } = await setup({ userStore }); + + await createTestEntity({ id: 1 }); + await createTestEntity({ id: 2 }); + + await userStore.update({ + modelName: "TestEntity", + timestamp: 10, + id: String(1), + data: { + string: "updated", + }, + }); + await userStore.update({ + modelName: "TestEntity", + timestamp: 15, + id: String(2), + data: { + string: "updated", + }, + }); + + const responseOld = await gql(` + testEntitys(timestamp: 12, orderBy: "int") { + id + string + } + `); + expect(responseOld.body.errors).toBe(undefined); + expect(responseOld.statusCode).toBe(200); + const testEntitysOld = responseOld.body.data.testEntitys; + expect(testEntitysOld).toMatchObject([ + { id: "1", string: "updated" }, + { id: "2", string: "2" }, + ]); + + const response = await gql(` + testEntitys(orderBy: "int") { + id + string + } + `); + expect(response.body.errors).toBe(undefined); + expect(response.statusCode).toBe(200); + const testEntitys = response.body.data.testEntitys; + expect(testEntitys).toMatchObject([ + { id: "1", string: "updated" }, + { id: "2", string: "updated" }, + ]); + + await service.teardown(); + await userStore.teardown(); +});