Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests suite for storage adapters #307

Merged
merged 28 commits into from
Mar 27, 2024
Merged
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
15ba961
test: NodeFSStorageAdapter
bijela-gora Mar 8, 2024
97e6415
fix: after calls to the save method three times the loadRange method …
bijela-gora Mar 8, 2024
98b7bba
refactor: make the tests for a storage reusable
bijela-gora Mar 8, 2024
16771e1
refactor: move runStorageAdapterTests to separate file
bijela-gora Mar 8, 2024
032739b
style: remove all semicolons
bijela-gora Mar 8, 2024
fa3d572
fix: keys from loadRange should be returned unmodified
bijela-gora Mar 8, 2024
048a392
test: DummyStorageAdapter
bijela-gora Mar 8, 2024
0fb2cfc
test: use node env to run NodeFSStorageAdapter adapter
bijela-gora Mar 8, 2024
3b1f161
chore: remove unused imports
bijela-gora Mar 8, 2024
178b84e
refactor: reduce number of loops
bijela-gora Mar 8, 2024
434c680
fix: fs keys should be the same as fs keys on windows also
bijela-gora Mar 8, 2024
a7f0f97
style: remove semicolons
bijela-gora Mar 8, 2024
1c72305
style: use the mkdtempSync function as it is used in StorageSubsystem…
bijela-gora Mar 8, 2024
b5513fe
chore: the jsx compiler option is not mandatory here
bijela-gora Mar 9, 2024
a759033
chore: remove unused import
bijela-gora Mar 9, 2024
ef0a7c4
test: add test cases for loadRange and removeRange methods
bijela-gora Mar 10, 2024
d0a4007
test: fix test
bijela-gora Mar 10, 2024
fcde512
rename runAdapterTests to runNetworkAdapterTests
HerbCaudill Mar 19, 2024
2a48ef1
refactor params
HerbCaudill Mar 19, 2024
845d65e
refactor to take setup function
HerbCaudill Mar 19, 2024
67b8cfa
abbreviate keys & payloads for legibility
HerbCaudill Mar 19, 2024
dcc6f23
refactor
HerbCaudill Mar 19, 2024
ae4685a
add large payload test
HerbCaudill Mar 19, 2024
649341f
test names
HerbCaudill Mar 24, 2024
fdb0b5c
prettier
HerbCaudill Mar 24, 2024
62ac01c
missed a teardown
HerbCaudill Mar 24, 2024
a6ac292
prettier
HerbCaudill Mar 24, 2024
847bbcd
convert PAYLOAD_A etc to functions
HerbCaudill Mar 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 39 additions & 31 deletions packages/automerge-repo/src/helpers/tests/storage-adapter-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ import { describe, expect, it } from "vitest"

import type { StorageAdapterInterface } from "../../storage/StorageAdapterInterface.js"

const PAYLOAD_A = new Uint8Array([
56, 97, 51, 53, 99, 57, 98, 52, 45, 49, 48, 57, 101, 45, 52, 97, 55, 102, 45,
97, 51, 53, 101, 45, 97, 53, 52, 54, 52, 49, 50, 49, 98, 54, 100, 100,
])
const PAYLOAD_B = new Uint8Array([0, 1, 127, 99, 154, 235])
const PAYLOAD_C = new Uint8Array([1, 76, 160, 53, 57, 10, 230])
const PAYLOAD_D = new Uint8Array([2, 111, 74, 131, 236, 96, 142, 193])
const PAYLOAD_A = new Uint8Array([0, 1, 127, 99, 154, 235])
const PAYLOAD_B = new Uint8Array([1, 76, 160, 53, 57, 10, 230])
const PAYLOAD_C = new Uint8Array([2, 111, 74, 131, 236, 96, 142, 193])

const LARGE_PAYLOAD = new Uint8Array(100000).map((_, i) => Math.random() * 256)
bijela-gora marked this conversation as resolved.
Show resolved Hide resolved

export function runStorageAdapterTests(_setup: SetupFn, title?: string): void {
const setup = async () => {
Expand Down Expand Up @@ -44,9 +42,19 @@ export function runStorageAdapterTests(_setup: SetupFn, title?: string): void {
it("should work with composite keys", async () => {
const { adapter, teardown } = await setup()

await adapter.save(["AAAAA", "sync-state", "xxxxx"], PAYLOAD_B)
await adapter.save(["AAAAA", "sync-state", "xxxxx"], PAYLOAD_A)
const actual = await adapter.load(["AAAAA", "sync-state", "xxxxx"])
expect(actual).toStrictEqual(PAYLOAD_A)

teardown()
})

it("should work with a large payload", async () => {
const { adapter, teardown } = await setup()

await adapter.save(["AAAAA", "sync-state", "xxxxx"], LARGE_PAYLOAD)
const actual = await adapter.load(["AAAAA", "sync-state", "xxxxx"])
expect(actual).toStrictEqual(PAYLOAD_B)
expect(actual).toStrictEqual(LARGE_PAYLOAD)

teardown()
})
Expand All @@ -66,41 +74,41 @@ export function runStorageAdapterTests(_setup: SetupFn, title?: string): void {
it("should return all the data that is present", async () => {
const { adapter, teardown } = await setup()

await adapter.save(["AAAAA", "sync-state", "xxxxx"], PAYLOAD_B)
await adapter.save(["AAAAA", "snapshot", "yyyyy"], PAYLOAD_C)
await adapter.save(["AAAAA", "sync-state", "zzzzz"], PAYLOAD_D)
await adapter.save(["AAAAA", "sync-state", "xxxxx"], PAYLOAD_A)
await adapter.save(["AAAAA", "snapshot", "yyyyy"], PAYLOAD_B)
await adapter.save(["AAAAA", "sync-state", "zzzzz"], PAYLOAD_C)

expect(await adapter.loadRange(["AAAAA"])).toStrictEqual(
expect.arrayContaining([
{ key: ["AAAAA", "sync-state", "xxxxx"], data: PAYLOAD_B },
{ key: ["AAAAA", "snapshot", "yyyyy"], data: PAYLOAD_C },
{ key: ["AAAAA", "sync-state", "zzzzz"], data: PAYLOAD_D },
{ key: ["AAAAA", "sync-state", "xxxxx"], data: PAYLOAD_A },
{ key: ["AAAAA", "snapshot", "yyyyy"], data: PAYLOAD_B },
{ key: ["AAAAA", "sync-state", "zzzzz"], data: PAYLOAD_C },
])
)

expect(await adapter.loadRange(["AAAAA", "sync-state"])).toStrictEqual(
expect.arrayContaining([
{ key: ["AAAAA", "sync-state", "xxxxx"], data: PAYLOAD_B },
{ key: ["AAAAA", "sync-state", "zzzzz"], data: PAYLOAD_D },
{ key: ["AAAAA", "sync-state", "xxxxx"], data: PAYLOAD_A },
{ key: ["AAAAA", "sync-state", "zzzzz"], data: PAYLOAD_C },
])
)
})

it("does not includes values which shouldn't be there", async () => {
const { adapter, teardown } = await setup()

await adapter.save(["AAAAA", "sync-state", "xxxxx"], PAYLOAD_B)
await adapter.save(["BBBBB", "sync-state", "zzzzz"], PAYLOAD_D)
await adapter.save(["AAAAA", "sync-state", "xxxxx"], PAYLOAD_A)
await adapter.save(["BBBBB", "sync-state", "zzzzz"], PAYLOAD_C)

const actual = await adapter.loadRange(["AAAAA"])
expect(actual).toStrictEqual(
expect.arrayContaining([
{ key: ["AAAAA", "sync-state", "xxxxx"], data: PAYLOAD_B },
{ key: ["AAAAA", "sync-state", "xxxxx"], data: PAYLOAD_A },
])
)
expect(actual).toStrictEqual(
expect.not.arrayContaining([
{ key: ["BBBBB", "sync-state", "zzzzz"], data: PAYLOAD_D },
{ key: ["BBBBB", "sync-state", "zzzzz"], data: PAYLOAD_C },
])
)

Expand All @@ -112,7 +120,7 @@ export function runStorageAdapterTests(_setup: SetupFn, title?: string): void {
it("should be no data", async () => {
const { adapter, teardown } = await setup()

await adapter.save(["AAAAA", "snapshot", "xxxxx"], PAYLOAD_B)
await adapter.save(["AAAAA", "snapshot", "xxxxx"], PAYLOAD_A)
await adapter.remove(["AAAAA", "snapshot", "xxxxx"])

expect(await adapter.loadRange(["AAAAA"])).toStrictEqual([])
Expand All @@ -128,11 +136,11 @@ export function runStorageAdapterTests(_setup: SetupFn, title?: string): void {
it("should overwrite data saved with the same key", async () => {
const { adapter, teardown } = await setup()

await adapter.save(["AAAAA", "sync-state", "xxxxx"], PAYLOAD_A)
await adapter.save(["AAAAA", "sync-state", "xxxxx"], PAYLOAD_B)
await adapter.save(["AAAAA", "sync-state", "xxxxx"], PAYLOAD_C)

expect(await adapter.loadRange(["AAAAA", "sync-state"])).toStrictEqual([
{ key: ["AAAAA", "sync-state", "xxxxx"], data: PAYLOAD_C },
{ key: ["AAAAA", "sync-state", "xxxxx"], data: PAYLOAD_B },
])

teardown()
Expand All @@ -143,14 +151,14 @@ export function runStorageAdapterTests(_setup: SetupFn, title?: string): void {
it("should remove a range of records", async () => {
const { adapter, teardown } = await setup()

await adapter.save(["AAAAA", "sync-state", "xxxxx"], PAYLOAD_B)
await adapter.save(["AAAAA", "snapshot", "yyyyy"], PAYLOAD_C)
await adapter.save(["AAAAA", "sync-state", "zzzzz"], PAYLOAD_D)
await adapter.save(["AAAAA", "sync-state", "xxxxx"], PAYLOAD_A)
await adapter.save(["AAAAA", "snapshot", "yyyyy"], PAYLOAD_B)
await adapter.save(["AAAAA", "sync-state", "zzzzz"], PAYLOAD_C)

await adapter.removeRange(["AAAAA", "sync-state"])

expect(await adapter.loadRange(["AAAAA"])).toStrictEqual([
{ key: ["AAAAA", "snapshot", "yyyyy"], data: PAYLOAD_C },
{ key: ["AAAAA", "snapshot", "yyyyy"], data: PAYLOAD_B },
])

teardown()
Expand All @@ -159,14 +167,14 @@ export function runStorageAdapterTests(_setup: SetupFn, title?: string): void {
it("should not remove records that doesn't match", async () => {
const { adapter, teardown } = await setup()

await adapter.save(["AAAAA", "sync-state", "xxxxx"], PAYLOAD_B)
await adapter.save(["BBBBB", "sync-state", "zzzzz"], PAYLOAD_C)
await adapter.save(["AAAAA", "sync-state", "xxxxx"], PAYLOAD_A)
await adapter.save(["BBBBB", "sync-state", "zzzzz"], PAYLOAD_B)

await adapter.removeRange(["AAAAA"])

const actual = await adapter.loadRange(["BBBBB"])
expect(actual).toStrictEqual([
{ key: ["BBBBB", "sync-state", "zzzzz"], data: PAYLOAD_C },
{ key: ["BBBBB", "sync-state", "zzzzz"], data: PAYLOAD_B },
])

teardown()
Expand Down