Skip to content

Commit

Permalink
test: add regression test for #44
Browse files Browse the repository at this point in the history
  • Loading branch information
skoshx committed Dec 20, 2023
1 parent 0342387 commit 4d4e9fb
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 9 deletions.
5 changes: 2 additions & 3 deletions examples/demo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ deno task start

This will watch the project directory and restart as necessary.


### Testing locally

Swap out "x-pentagon" and "pentagon" in the `import_map.json` to switch between testing the local library or the latest
release.
Swap out "x-pentagon" and "pentagon" in the `import_map.json` to switch between
testing the local library or the latest release.
8 changes: 4 additions & 4 deletions examples/demo/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {Head} from "$fresh/runtime.ts";
import {Handlers, PageProps} from "$fresh/server.ts";
import {z} from "zod";
import { Head } from "$fresh/runtime.ts";
import { Handlers, PageProps } from "$fresh/server.ts";
import { z } from "zod";

import {db, TodoTask, User} from "../lib/db.ts";
import { db, TodoTask, User } from "../lib/db.ts";
import Input from "../components/ui/input.tsx";
import Button from "../components/ui/button.tsx";

Expand Down
62 changes: 60 additions & 2 deletions test/regression_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import {
assert,
assertEquals,
} from "https://deno.land/[email protected]/testing/asserts.ts";
import { clearMocks, createMockDatabase, User } from "./util.ts";
import { removeVersionstamp } from "../src/util.ts";
import { clearMocks, createMockDatabase, Post, User } from "./util.ts";
import { removeVersionstamp, removeVersionstamps } from "../src/util.ts";
import { createPentagon } from "../mod.ts";
import { z } from "../deps.ts";
import { schemaToKeys } from "../src/keys.ts";
import { ZodSchema } from "https://deno.land/x/[email protected]/types.ts";

Deno.test("include", async (t) => {
const db = createMockDatabase();
Expand Down Expand Up @@ -295,3 +296,60 @@ Deno.test("regression #33", async () => {
assertEquals(typeof meeting.updatedAt, "string");
kv.close();
});

Deno.test("regression #44", async () => {
const kv = await Deno.openKv();

const userWithSecondaryIndex = User.extend({
// @todo: this should work with `index` too??
name: z.string().describe("unique"),
});

const postWithUniqueIndex = Post.extend({
title: z.string().describe("unique"),
});

const db = createPentagon(kv, {
users: {
schema: userWithSecondaryIndex,
},
posts: {
schema: postWithUniqueIndex,
},
});

await db.users.deleteMany({});
await db.posts.deleteMany({});
await clearMocks();

const userId = crypto.randomUUID();
const createdAt = new Date();

await db.users.create({
data: {
id: userId,
createdAt,
name: "John Doe",
},
});

// Update by secondary index
await db.users.update({
where: { name: "John Doe" },
data: {
name: "Doe John",
},
});

// Check that user got updated when updating by secondary index
const updatedUsersList = await db.users.findMany({});
assertEquals(removeVersionstamps(updatedUsersList), [
{
id: userId,
createdAt,
name: "Doe John",
},
]);

kv.close();
});
3 changes: 3 additions & 0 deletions test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ export async function clearMocks() {
for await (const x of kv.list({ prefix: ["users"] })) {
await kv.delete(x.key);
}
for await (const x of kv.list({ prefix: ["posts"] })) {
await kv.delete(x.key);
}
for await (const x of kv.list({ prefix: ["orders"] })) {
await kv.delete(x.key);
}
Expand Down

0 comments on commit 4d4e9fb

Please sign in to comment.