Skip to content

Commit

Permalink
Merge pull request #385 from kraenhansen/refactored-storage-adapter-t…
Browse files Browse the repository at this point in the history
…ests

Refactor the storage adapter tests to use a `beforeEach` hook
  • Loading branch information
HerbCaudill authored Sep 21, 2024
2 parents 3817343 + 4185f6b commit a052bb5
Showing 1 changed file with 31 additions and 62 deletions.
93 changes: 31 additions & 62 deletions packages/automerge-repo/src/helpers/tests/storage-adapter-tests.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from "vitest"
import { describe, expect, beforeEach, it as _it } from "vitest"

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

Expand All @@ -8,72 +8,61 @@ const PAYLOAD_C = () => new Uint8Array([2, 111, 74, 131, 236, 96, 142, 193])

const LARGE_PAYLOAD = new Uint8Array(100000).map(() => Math.random() * 256)

export function runStorageAdapterTests(_setup: SetupFn, title?: string): void {
const setup = async () => {
const { adapter, teardown = NO_OP } = await _setup()
return { adapter, teardown }
}
type AdapterTestContext = {
adapter: StorageAdapterInterface
}

const it = _it<AdapterTestContext>

export function runStorageAdapterTests(setup: SetupFn, title?: string): void {
beforeEach<AdapterTestContext>(async ctx => {
const { adapter, teardown = NO_OP } = await setup()
ctx.adapter = adapter
return teardown
})

describe(`Storage adapter acceptance tests ${
title ? `(${title})` : ""
}`, () => {
describe("load", () => {
it("should return undefined if there is no data", async () => {
const { adapter, teardown } = await setup()

it("should return undefined if there is no data", async ({ adapter }) => {
const actual = await adapter.load(["AAAAA", "sync-state", "xxxxx"])
expect(actual).toBeUndefined()

teardown()
})
})

describe("save and load", () => {
it("should return data that was saved", async () => {
const { adapter, teardown } = await setup()

it("should return data that was saved", async ({ adapter }) => {
await adapter.save(["storage-adapter-id"], PAYLOAD_A())
const actual = await adapter.load(["storage-adapter-id"])
expect(actual).toStrictEqual(PAYLOAD_A())

teardown()
})

it("should work with composite keys", async () => {
const { adapter, teardown } = await setup()

it("should work with composite keys", async ({ adapter }) => {
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()

it("should work with a large payload", async ({ adapter }) => {
await adapter.save(["AAAAA", "sync-state", "xxxxx"], LARGE_PAYLOAD)
const actual = await adapter.load(["AAAAA", "sync-state", "xxxxx"])
expect(actual).toStrictEqual(LARGE_PAYLOAD)

teardown()
})
})

describe("loadRange", () => {
it("should return an empty array if there is no data", async () => {
const { adapter, teardown } = await setup()

it("should return an empty array if there is no data", async ({
adapter,
}) => {
expect(await adapter.loadRange(["AAAAA"])).toStrictEqual([])

teardown()
})
})

describe("save and loadRange", () => {
it("should return all the data that matches the key", async () => {
const { adapter, teardown } = await setup()

it("should return all the data that matches the key", async ({
adapter,
}) => {
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())
Expand All @@ -92,13 +81,9 @@ export function runStorageAdapterTests(_setup: SetupFn, title?: string): void {
{ key: ["AAAAA", "sync-state", "zzzzz"], data: PAYLOAD_C() },
])
)

teardown()
})

it("should only load values that match they key", async () => {
const { adapter, teardown } = await setup()

it("should only load values that match they key", async ({ adapter }) => {
await adapter.save(["AAAAA", "sync-state", "xxxxx"], PAYLOAD_A())
await adapter.save(["BBBBB", "sync-state", "zzzzz"], PAYLOAD_C())

Expand All @@ -113,46 +98,36 @@ export function runStorageAdapterTests(_setup: SetupFn, title?: string): void {
{ key: ["BBBBB", "sync-state", "zzzzz"], data: PAYLOAD_C() },
])
)

teardown()
})
})

describe("save and remove", () => {
it("after removing, should be empty", async () => {
const { adapter, teardown } = await setup()

it("after removing, should be empty", async ({ adapter }) => {
await adapter.save(["AAAAA", "snapshot", "xxxxx"], PAYLOAD_A())
await adapter.remove(["AAAAA", "snapshot", "xxxxx"])

expect(await adapter.loadRange(["AAAAA"])).toStrictEqual([])
expect(
await adapter.load(["AAAAA", "snapshot", "xxxxx"])
).toBeUndefined()

teardown()
})
})

describe("save and save", () => {
it("should overwrite data saved with the same key", async () => {
const { adapter, teardown } = await setup()

it("should overwrite data saved with the same key", async ({
adapter,
}) => {
await adapter.save(["AAAAA", "sync-state", "xxxxx"], PAYLOAD_A())
await adapter.save(["AAAAA", "sync-state", "xxxxx"], PAYLOAD_B())

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

teardown()
})
})

describe("removeRange", () => {
it("should remove a range of records", async () => {
const { adapter, teardown } = await setup()

it("should remove a range of records", async ({ adapter }) => {
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())
Expand All @@ -162,13 +137,9 @@ export function runStorageAdapterTests(_setup: SetupFn, title?: string): void {
expect(await adapter.loadRange(["AAAAA"])).toStrictEqual([
{ key: ["AAAAA", "snapshot", "yyyyy"], data: PAYLOAD_B() },
])

teardown()
})

it("should not remove records that don't match", async () => {
const { adapter, teardown } = await setup()

it("should not remove records that don't match", async ({ adapter }) => {
await adapter.save(["AAAAA", "sync-state", "xxxxx"], PAYLOAD_A())
await adapter.save(["BBBBB", "sync-state", "zzzzz"], PAYLOAD_B())

Expand All @@ -178,8 +149,6 @@ export function runStorageAdapterTests(_setup: SetupFn, title?: string): void {
expect(actual).toStrictEqual([
{ key: ["BBBBB", "sync-state", "zzzzz"], data: PAYLOAD_B() },
])

teardown()
})
})
})
Expand All @@ -189,5 +158,5 @@ const NO_OP = () => {}

export type SetupFn = () => Promise<{
adapter: StorageAdapterInterface
teardown?: () => void
teardown?: () => void | Promise<void>
}>

0 comments on commit a052bb5

Please sign in to comment.