Skip to content

Commit d894f4b

Browse files
authored
test: add edge-runtime e2e (#458)
* test: add edge-runtime e2e * update * add cleanup * update * simplify typing
1 parent 3c22e34 commit d894f4b

File tree

20 files changed

+467
-145
lines changed

20 files changed

+467
-145
lines changed

pnpm-lock.yaml

Lines changed: 88 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/bun/e2e.test.ts renamed to tests/runtimes/bun/bun-e2e.test.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { PostgresDialect } from '@zenstackhq/orm/dialects/postgres';
44
import { PolicyPlugin } from '@zenstackhq/plugin-policy';
55
import { TEST_PG_URL } from '@zenstackhq/testtools';
66
import { Database } from 'bun:sqlite';
7-
import { describe, expect, it } from 'bun:test';
7+
import { afterEach, describe, expect, it } from 'bun:test';
88
import type { Dialect } from 'kysely';
99
import { BunSqliteDialect } from 'kysely-bun-sqlite';
1010
import { Client, Pool } from 'pg';
@@ -13,8 +13,14 @@ import { schema } from './schemas/schema';
1313
describe('Bun e2e tests', () => {
1414
const provider = (process.env['TEST_DB_PROVIDER'] ?? 'sqlite') as 'sqlite' | 'postgresql';
1515

16+
let _db: any;
17+
18+
afterEach(async () => {
19+
await _db?.$disconnect();
20+
});
21+
1622
it('works with simple CRUD', async () => {
17-
const db = await createClient(provider, 'bun-e2e-crud');
23+
const db = (_db = await createClient(provider, 'bun-e2e-crud'));
1824

1925
const user = await db.user.create({
2026
data: {
@@ -44,7 +50,7 @@ describe('Bun e2e tests', () => {
4450
});
4551

4652
it('enforces policies', async () => {
47-
const db = await createClient(provider, 'bun-e2e-policies');
53+
const db = (_db = await createClient(provider, 'bun-e2e-policies'));
4854
const authDb = db.$use(new PolicyPlugin());
4955

5056
// create a user
@@ -58,13 +64,11 @@ describe('Bun e2e tests', () => {
5864
{
5965
id: 'p1',
6066
title: 'First Post',
61-
content: 'Hello World',
6267
published: true,
6368
},
6469
{
6570
id: 'p2',
6671
title: 'Second Post',
67-
content: 'Draft Post',
6872
published: false,
6973
},
7074
],
File renamed without changes.
File renamed without changes.

tests/bun/schemas/models.ts renamed to tests/runtimes/bun/schemas/models.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55

66
/* eslint-disable */
77

8-
import { schema as $schema, type SchemaType as $Schema } from "./schema";
9-
import { type ModelResult as $ModelResult, type TypeDefResult as $TypeDefResult } from "@zenstackhq/orm";
8+
import { type SchemaType as $Schema } from "./schema";
9+
import { type ModelResult as $ModelResult } from "@zenstackhq/orm";
1010
export type User = $ModelResult<$Schema, "User">;
1111
export type Post = $ModelResult<$Schema, "Post">;
12-
export type CommonFields = $TypeDefResult<$Schema, "CommonFields">;
13-
export const Role = $schema.enums.Role.values;
14-
export type Role = (typeof Role)[keyof typeof Role];

tests/bun/schemas/schema.ts renamed to tests/runtimes/bun/schemas/schema.ts

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,6 @@ const _schema = {
2121
attributes: [{ name: "@id" }, { name: "@default", args: [{ name: "value", value: ExpressionUtils.call("cuid") }] }],
2222
default: ExpressionUtils.call("cuid")
2323
},
24-
createdAt: {
25-
name: "createdAt",
26-
type: "DateTime",
27-
attributes: [{ name: "@default", args: [{ name: "value", value: ExpressionUtils.call("now") }] }],
28-
default: ExpressionUtils.call("now")
29-
},
30-
updatedAt: {
31-
name: "updatedAt",
32-
type: "DateTime",
33-
updatedAt: true,
34-
attributes: [{ name: "@updatedAt" }]
35-
},
3624
email: {
3725
name: "email",
3826
type: "String",
@@ -44,22 +32,11 @@ const _schema = {
4432
type: "String",
4533
optional: true
4634
},
47-
role: {
48-
name: "role",
49-
type: "Role",
50-
attributes: [{ name: "@default", args: [{ name: "value", value: ExpressionUtils.literal("USER") }] }],
51-
default: "USER"
52-
},
5335
posts: {
5436
name: "posts",
5537
type: "Post",
5638
array: true,
5739
relation: { opposite: "author" }
58-
},
59-
meta: {
60-
name: "meta",
61-
type: "Json",
62-
optional: true
6340
}
6441
},
6542
attributes: [
@@ -82,27 +59,10 @@ const _schema = {
8259
attributes: [{ name: "@id" }, { name: "@default", args: [{ name: "value", value: ExpressionUtils.call("cuid") }] }],
8360
default: ExpressionUtils.call("cuid")
8461
},
85-
createdAt: {
86-
name: "createdAt",
87-
type: "DateTime",
88-
attributes: [{ name: "@default", args: [{ name: "value", value: ExpressionUtils.call("now") }] }],
89-
default: ExpressionUtils.call("now")
90-
},
91-
updatedAt: {
92-
name: "updatedAt",
93-
type: "DateTime",
94-
updatedAt: true,
95-
attributes: [{ name: "@updatedAt" }]
96-
},
9762
title: {
9863
name: "title",
9964
type: "String"
10065
},
101-
content: {
102-
name: "content",
103-
type: "String",
104-
optional: true
105-
},
10666
published: {
10767
name: "published",
10868
type: "Boolean",
@@ -134,39 +94,6 @@ const _schema = {
13494
}
13595
}
13696
},
137-
typeDefs: {
138-
CommonFields: {
139-
name: "CommonFields",
140-
fields: {
141-
id: {
142-
name: "id",
143-
type: "String",
144-
attributes: [{ name: "@id" }, { name: "@default", args: [{ name: "value", value: ExpressionUtils.call("cuid") }] }],
145-
default: ExpressionUtils.call("cuid")
146-
},
147-
createdAt: {
148-
name: "createdAt",
149-
type: "DateTime",
150-
attributes: [{ name: "@default", args: [{ name: "value", value: ExpressionUtils.call("now") }] }],
151-
default: ExpressionUtils.call("now")
152-
},
153-
updatedAt: {
154-
name: "updatedAt",
155-
type: "DateTime",
156-
updatedAt: true,
157-
attributes: [{ name: "@updatedAt" }]
158-
}
159-
}
160-
}
161-
},
162-
enums: {
163-
Role: {
164-
values: {
165-
ADMIN: "ADMIN",
166-
USER: "USER"
167-
}
168-
}
169-
},
17097
authType: "User",
17198
plugins: {}
17299
} as const satisfies SchemaDef;
Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,25 @@
11
datasource db {
22
provider = "sqlite"
3-
url = "file:./dev.db"
43
}
54

65
plugin policy {
76
provider = "../../../packages/plugins/policy"
87
}
98

10-
enum Role {
11-
ADMIN
12-
USER
13-
}
14-
15-
type CommonFields {
16-
id String @id @default(cuid())
17-
createdAt DateTime @default(now())
18-
updatedAt DateTime @updatedAt
19-
}
20-
21-
model User with CommonFields {
22-
email String @unique
23-
name String?
24-
password String @ignore
25-
role Role @default(USER)
26-
posts Post[]
27-
meta Json?
9+
model User {
10+
id String @id @default(cuid())
11+
email String @unique
12+
name String?
13+
posts Post[]
2814

2915
// Access policies
3016
@@allow('all', auth().id == id)
3117
@@allow('read', auth() != null)
3218
}
3319

34-
model Post with CommonFields {
20+
model Post {
21+
id String @id @default(cuid())
3522
title String
36-
content String?
3723
published Boolean @default(false)
3824
author User @relation(fields: [authorId], references: [id], onUpdate: Cascade, onDelete: Cascade)
3925
authorId String
@@ -43,4 +29,3 @@ model Post with CommonFields {
4329
@@allow('all', auth().id == authorId)
4430
@@allow('read', published)
4531
}
46-
File renamed without changes.
File renamed without changes.
6 KB
Binary file not shown.

0 commit comments

Comments
 (0)