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

chore: added async id generator tests #198

Merged
merged 2 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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")
})
})
})
Loading