Skip to content

Commit

Permalink
Various mostly generated tests via Cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
dcramer committed Aug 24, 2024
1 parent d4f74a0 commit 171fc84
Show file tree
Hide file tree
Showing 10 changed files with 720 additions and 87 deletions.
166 changes: 85 additions & 81 deletions apps/server/src/trpc/routes/countryUpdate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,104 +5,108 @@ import waitError from "@peated/server/lib/test/waitError";
import { eq } from "drizzle-orm";
import { createCaller } from "../router";

test("requires authentication", async () => {
const caller = createCaller({ user: null });
const err = await waitError(
caller.countryUpdate({
slug: "test-country",
}),
);
expect(err).toMatchInlineSnapshot(`[TRPCError: UNAUTHORIZED]`);
});

test("requires mod", async ({ defaults }) => {
const caller = createCaller({ user: defaults.user });
const err = await waitError(
caller.countryUpdate({
slug: "test-country",
}),
);
expect(err).toMatchInlineSnapshot(`[TRPCError: UNAUTHORIZED]`);
});

test("no changes", async ({ fixtures }) => {
const country = await fixtures.Country();

const caller = createCaller({
user: await fixtures.User({ mod: true }),
describe("authentication and authorization", () => {
test("requires authentication", async () => {
const caller = createCaller({ user: null });
const err = await waitError(
caller.countryUpdate({
slug: "test-country",
}),
);
expect(err).toMatchInlineSnapshot(`[TRPCError: UNAUTHORIZED]`);
});
const data = await caller.countryUpdate({
slug: country.slug,

test("requires mod", async ({ defaults }) => {
const caller = createCaller({ user: defaults.user });
const err = await waitError(
caller.countryUpdate({
slug: "test-country",
}),
);
expect(err).toMatchInlineSnapshot(`[TRPCError: UNAUTHORIZED]`);
});
});

expect(data.id).toBeDefined();
describe("country updates", () => {
test("no changes", async ({ fixtures }) => {
const country = await fixtures.Country();

const [newCountry] = await db
.select()
.from(countries)
.where(eq(countries.id, data.id));
const caller = createCaller({
user: await fixtures.User({ mod: true }),
});
const data = await caller.countryUpdate({
slug: country.slug,
});

expect(country).toEqual(newCountry);
});
expect(data.id).toBeDefined();

test("can change description", async ({ fixtures }) => {
const country = await fixtures.Country();
const [newCountry] = await db
.select()
.from(countries)
.where(eq(countries.id, data.id));

const caller = createCaller({
user: await fixtures.User({ mod: true }),
});
const data = await caller.countryUpdate({
slug: country.slug,
description: "New description",
expect(country).toEqual(newCountry);
});

expect(data.id).toBeDefined();
test("can change description", async ({ fixtures }) => {
const country = await fixtures.Country();

const [newCountry] = await db
.select()
.from(countries)
.where(eq(countries.id, data.id));
const caller = createCaller({
user: await fixtures.User({ mod: true }),
});
const data = await caller.countryUpdate({
slug: country.slug,
description: "New description",
});

expect(omit(country, "description", "descriptionSrc", "updatedAt")).toEqual(
omit(newCountry, "description", "descriptionSrc", "updatedAt"),
);
expect(newCountry.description).toBe("New description");
expect(newCountry.descriptionSrc).toBe("user");
});
expect(data.id).toBeDefined();

test("can change summary", async ({ fixtures }) => {
const country = await fixtures.Country();
const [newCountry] = await db
.select()
.from(countries)
.where(eq(countries.id, data.id));

const caller = createCaller({
user: await fixtures.User({ mod: true }),
});
const data = await caller.countryUpdate({
slug: country.slug,
summary: "New summary",
expect(omit(country, "description", "descriptionSrc", "updatedAt")).toEqual(
omit(newCountry, "description", "descriptionSrc", "updatedAt"),
);
expect(newCountry.description).toBe("New description");
expect(newCountry.descriptionSrc).toBe("user");
});

expect(data.id).toBeDefined();
test("can change summary", async ({ fixtures }) => {
const country = await fixtures.Country();

const [newCountry] = await db
.select()
.from(countries)
.where(eq(countries.id, data.id));
const caller = createCaller({
user: await fixtures.User({ mod: true }),
});
const data = await caller.countryUpdate({
slug: country.slug,
summary: "New summary",
});

expect(omit(country, "summary", "updatedAt")).toEqual(
omit(newCountry, "summary", "updatedAt"),
);
expect(newCountry.summary).toBe("New summary");
});
expect(data.id).toBeDefined();

test("throws error for invalid country slug", async ({ fixtures }) => {
const caller = createCaller({
user: await fixtures.User({ mod: true }),
const [newCountry] = await db
.select()
.from(countries)
.where(eq(countries.id, data.id));

expect(omit(country, "summary", "updatedAt")).toEqual(
omit(newCountry, "summary", "updatedAt"),
);
expect(newCountry.summary).toBe("New summary");
});

test("throws error for invalid country slug", async ({ fixtures }) => {
const caller = createCaller({
user: await fixtures.User({ mod: true }),
});
const err = await waitError(
caller.countryUpdate({
slug: "nonexistent-country",
description: "New description",
}),
);
expect(err).toMatchInlineSnapshot(`[TRPCError: NOT_FOUND]`);
});
const err = await waitError(
caller.countryUpdate({
slug: "nonexistent-country",
description: "New description",
}),
);
expect(err).toMatchInlineSnapshot(`[TRPCError: NOT_FOUND]`);
});
104 changes: 104 additions & 0 deletions apps/server/src/trpc/routes/regionBySlug.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { TRPCError } from "@trpc/server";
import { createCaller } from "../router";

describe("regionBySlug", () => {
test("retrieves a region by slug and country id", async ({
fixtures,
expect,
}) => {
const country = await fixtures.Country();
const region = await fixtures.Region({ countryId: country.id });

const caller = createCaller({ user: null });
const result = await caller.regionBySlug({
country: country.id,
slug: region.slug,
});

expect(result.id).toBe(region.id);
expect(result.name).toBe(region.name);
expect(result.slug).toBe(region.slug);
});

test("retrieves a region by slug and country slug", async ({
fixtures,
expect,
}) => {
const country = await fixtures.Country();
const region = await fixtures.Region({ countryId: country.id });

const caller = createCaller({ user: null });
const result = await caller.regionBySlug({
country: country.slug,
slug: region.slug,
});

expect(result.id).toBe(region.id);
expect(result.name).toBe(region.name);
expect(result.slug).toBe(region.slug);
});

test("throws BAD_REQUEST for invalid country slug", async ({
fixtures,
expect,
}) => {
const caller = createCaller({ user: null });
await expect(
caller.regionBySlug({
country: "nonexistent-country",
slug: "some-region",
}),
).rejects.toThrow(TRPCError);
});

test("throws NOT_FOUND for non-existent region", async ({
fixtures,
expect,
}) => {
const country = await fixtures.Country();

const caller = createCaller({ user: null });
await expect(
caller.regionBySlug({
country: country.id,
slug: "nonexistent-region",
}),
).rejects.toThrow(TRPCError);
});

test("is case-insensitive for country and region slugs", async ({
fixtures,
expect,
}) => {
const country = await fixtures.Country({ slug: "United-States" });
const region = await fixtures.Region({
countryId: country.id,
slug: "California",
});

const caller = createCaller({ user: null });
const result = await caller.regionBySlug({
country: "united-states",
slug: "california",
});

expect(result.id).toBe(region.id);
expect(result.name).toBe(region.name);
expect(result.slug).toBe(region.slug);
});

test("returns serialized region data", async ({ fixtures, expect }) => {
const country = await fixtures.Country();
const region = await fixtures.Region({ countryId: country.id });

const caller = createCaller({ user: null });
const result = await caller.regionBySlug({
country: country.id,
slug: region.slug,
});

expect(result.id).toBe(region.id);
expect(result.name).toBe(region.name);
expect(result.slug).toBe(region.slug);
});
});
5 changes: 3 additions & 2 deletions apps/server/src/trpc/routes/regionCreate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ test("handles duplicate name", async ({ fixtures }) => {
}),
);

expect(err).toBeInstanceOf(Error);
expect(err.message).toContain("Conflict");
expect(err).toMatchInlineSnapshot(
`[TRPCError: Conflicting object already exists (ID=1).]`,
);
});

test("creates region with minimal data", async ({ fixtures }) => {
Expand Down
Loading

0 comments on commit 171fc84

Please sign in to comment.