Skip to content

Commit

Permalink
Merge pull request #198 from oliver-oloughlin/async-id-generator-tests
Browse files Browse the repository at this point in the history
chore: added async id generator tests
  • Loading branch information
oliver-oloughlin authored Mar 21, 2024
2 parents 77b799b + 46c8dd4 commit fd257dc
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 3 deletions.
4 changes: 2 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "@olli/kvdex",
"version": "0.35.2",
"version": "0.35.3",
"exports": {
".": "./mod.ts",
"./ext/zod": "./ext/zod.ts",
"./ext/migrate": "./ext/migrate.ts"
},
"tasks": {
"check": "deno check mod.ts src/*.ts ext/*.ts tests/*.ts tests/**/*.ts benchmarks/**/*ts",
"test": "deno test --allow-write --allow-read --unstable-kv --trace-ops",
"test": "deno test --allow-write --allow-read --unstable-kv --trace-leaks",
"bench": "deno bench --unstable-kv",
"prep": "deno task check && deno fmt && deno lint && deno publish --dry-run && deno task test",
"cache": "deno cache -r mod.ts && deno cache -r ext/zod.ts && deno cache -r ext/migrate.ts"
Expand Down
2 changes: 1 addition & 1 deletion src/atomic_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ export class AtomicBuilder<
)
)
) {
// Ff collisions are detected, return commit error
// If collisions are detected, return commit error
return {
ok: false,
}
Expand Down
28 changes: 28 additions & 0 deletions tests/collection/properties.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,32 @@ Deno.test("collection - properties", async (t) => {
})
},
)

await t.step("Should successfully generate id asynchronously", async () => {
await useKv(async (kv) => {
const db = kvdex(kv, {
test: collection(model<User>(), {
idGenerator: async (user) => {
const buffer = await crypto.subtle.digest(
"SHA-256",
new ArrayBuffer(user.age),
)
return Math.random() * buffer.byteLength
},
}),
})

const cr1 = await db.test.add(mockUser1)
const cr2 = await db.atomic((s) => s.test).add(mockUser2).commit()
const doc2 = await db.test.getOne({
filter: (doc) => doc.value.username === mockUser2.username,
})

assert(cr1.ok)
assert(typeof cr1.id === "number")
assert(cr2.ok)
assert(doc2 !== null)
assert(typeof doc2.id === "number")
})
})
})
32 changes: 32 additions & 0 deletions tests/indexable_collection/properties.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -882,4 +882,36 @@ Deno.test("indexable_collection - properties", async (t) => {
assert(bySecondary26.length === 1)
})
})

await t.step("Should successfully generate id asynchronously", async () => {
await useKv(async (kv) => {
const db = kvdex(kv, {
test: collection(model<User>(), {
indices: {
username: "primary",
age: "secondary",
},
idGenerator: async (user) => {
const buffer = await crypto.subtle.digest(
"SHA-256",
new ArrayBuffer(user.age),
)
return Math.random() * buffer.byteLength
},
}),
})

const cr1 = await db.test.add(mockUser1)
const cr2 = await db.atomic((s) => s.test).add(mockUser2).commit()
const doc2 = await db.test.getOne({
filter: (doc) => doc.value.username === mockUser2.username,
})

assert(cr1.ok)
assert(typeof cr1.id === "number")
assert(cr2.ok)
assert(doc2 !== null)
assert(typeof doc2.id === "number")
})
})
})
22 changes: 22 additions & 0 deletions tests/serialized_collection/properties.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,4 +285,26 @@ Deno.test("serialized_collection - properties", async (t) => {
})
},
)

await t.step("Should successfully generate id asynchronously", async () => {
await useKv(async (kv) => {
const db = kvdex(kv, {
test: collection(model<User>(), {
serialize: "json",
idGenerator: async (user) => {
const buffer = await crypto.subtle.digest(
"SHA-256",
new ArrayBuffer(user.age),
)
return Math.random() * buffer.byteLength
},
}),
})

const cr1 = await db.test.add(mockUser1)

assert(cr1.ok)
assert(typeof cr1.id === "number")
})
})
})
26 changes: 26 additions & 0 deletions tests/serialized_indexable_collection/properties.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -914,4 +914,30 @@ Deno.test("serialized_indexable_collection - properties", async (t) => {
assert(bySecondary26.length === 1)
})
})

await t.step("Should successfully generate id asynchronously", async () => {
await useKv(async (kv) => {
const db = kvdex(kv, {
test: collection(model<User>(), {
serialize: "json",
indices: {
username: "primary",
age: "secondary",
},
idGenerator: async (user) => {
const buffer = await crypto.subtle.digest(
"SHA-256",
new ArrayBuffer(user.age),
)
return Math.random() * buffer.byteLength
},
}),
})

const cr1 = await db.test.add(mockUser1)

assert(cr1.ok)
assert(typeof cr1.id === "number")
})
})
})

0 comments on commit fd257dc

Please sign in to comment.