Skip to content

Commit 1b39912

Browse files
committed
examples
1 parent 24acc67 commit 1b39912

File tree

4 files changed

+128
-26
lines changed

4 files changed

+128
-26
lines changed

packages/hello-world/__tests__/seeding/seeding.csv.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
import { getConnections, PgTestClient, seed } from 'supabase-test';
21
import path from 'path';
2+
import { getConnections, PgTestClient, seed } from 'supabase-test';
33
import { users } from './data/seed-data';
44

55
let pg: PgTestClient;
66
let db: PgTestClient;
77
let teardown: () => Promise<void>;
88

99
const csv = (file: string) => path.resolve(__dirname, './data', file);
10-
const cwd = path.resolve(__dirname, '../../');
1110

1211
beforeAll(async () => {
1312
({ pg, db, teardown } = await getConnections(
1413
{},
1514
[
16-
// create schema
17-
seed.launchql(cwd),
15+
// load schema and it's dependencies (supabase full schema)
16+
seed.launchql(),
1817

1918
// load from csv
2019
seed.csv({

packages/hello-world/__tests__/seeding/seeding.json.test.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
import { getConnections, PgTestClient, seed } from 'supabase-test';
2-
import path from 'path';
32
import { pets, users } from './data/seed-data';
43

54
let db: PgTestClient;
65
let teardown: () => Promise<void>;
76

8-
const sql = (f: string) => path.join(__dirname, 'data', f);
9-
10-
const cwd = path.resolve(__dirname, '../../');
11-
127
beforeAll(async () => {
138
({ db, teardown } = await getConnections(
14-
{}, [
15-
seed.launchql(cwd),
16-
seed.json({
17-
'auth.users': users,
18-
'rls_test.pets': pets
19-
})
20-
]
9+
{},
10+
[
11+
// load schema and it's dependencies (supabase full schema)
12+
seed.launchql(),
13+
14+
// load data from json files
15+
seed.json({
16+
'auth.users': users,
17+
'rls_test.pets': pets
18+
})
19+
]
2120
));
2221
});
2322

@@ -34,7 +33,7 @@ afterEach(async () => {
3433
});
3534

3635
describe('tutorial: testing with sql file seeding', () => {
37-
it('should work with sql file seed function', async () => {
36+
it('data should be loaded into the database', async () => {
3837

3938
db.setContext({
4039
role: 'authenticated',
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import { getConnections, PgTestClient, seed } from 'supabase-test';
2+
import { pets, users } from './data/seed-data';
3+
4+
let db: PgTestClient;
5+
let teardown: () => Promise<void>;
6+
7+
beforeAll(async () => {
8+
({ db, teardown } = await getConnections(
9+
{},
10+
[
11+
// load schema and it's dependencies (supabase full schema)
12+
seed.launchql(),
13+
14+
// load data from json files
15+
seed.fn(async ({ pg }) => {
16+
await pg.query(`
17+
-- Insert test users
18+
INSERT INTO auth.users (id, email) VALUES
19+
('550e8400-e29b-41d4-a716-446655440001', '[email protected]'),
20+
('550e8400-e29b-41d4-a716-446655440002', '[email protected]'),
21+
('550e8400-e29b-41d4-a716-446655440003', '[email protected]'),
22+
('550e8400-e29b-41d4-a716-446655440004', '[email protected]');
23+
24+
-- Insert test pets
25+
INSERT INTO rls_test.pets (id, name, breed, user_id) VALUES
26+
('660e8400-e29b-41d4-a716-446655440001', 'Fido', 'Labrador', '550e8400-e29b-41d4-a716-446655440001'),
27+
('660e8400-e29b-41d4-a716-446655440002', 'Buddy', 'Golden Retriever', '550e8400-e29b-41d4-a716-446655440002'),
28+
('660e8400-e29b-41d4-a716-446655440003', 'Rex', 'German Shepherd', '550e8400-e29b-41d4-a716-446655440003');
29+
`);
30+
}),
31+
]
32+
));
33+
});
34+
35+
afterAll(async () => {
36+
await teardown();
37+
});
38+
39+
beforeEach(async () => {
40+
await db.beforeEach();
41+
});
42+
43+
afterEach(async () => {
44+
await db.afterEach();
45+
});
46+
47+
describe('tutorial: testing with pg function seeding', () => {
48+
it('data should be loaded into the database', async () => {
49+
50+
db.setContext({
51+
role: 'authenticated',
52+
'request.jwt.claim.sub': users[0].id
53+
});
54+
55+
const verifiedPets = await db.any(
56+
`SELECT id FROM rls_test.pets WHERE user_id = $1`,
57+
[users[0].id]
58+
);
59+
expect(verifiedPets.length).toBe(1);
60+
61+
db.clearContext();
62+
63+
const anonPets = await db.any(
64+
`SELECT id FROM rls_test.pets WHERE user_id = $1`,
65+
[users[0].id]
66+
);
67+
expect(anonPets.length).toBe(0);
68+
69+
});
70+
it('should enforce RLS - users can only see their own pets', async () => {
71+
// set context to first user
72+
db.setContext({
73+
role: 'authenticated',
74+
'request.jwt.claim.sub': users[0].id
75+
});
76+
77+
// user1 should only see their own pet (Fido)
78+
const user1Pets = await db.many(
79+
`SELECT id, name, breed, user_id FROM rls_test.pets ORDER BY name`
80+
);
81+
82+
expect(user1Pets.length).toBe(1);
83+
expect(user1Pets[0].user_id).toBe(users[0].id);
84+
expect(user1Pets[0].name).toBe('Fido');
85+
86+
// set context to second user
87+
db.setContext({
88+
role: 'authenticated',
89+
'request.jwt.claim.sub': users[1].id
90+
});
91+
92+
// user2 should only see their own pet (Buddy)
93+
const user2Pets = await db.many(
94+
`SELECT id, name, breed, user_id FROM rls_test.pets ORDER BY name`
95+
);
96+
97+
expect(user2Pets.length).toBe(1);
98+
expect(user2Pets[0].user_id).toBe(users[1].id);
99+
expect(user2Pets[0].name).toBe('Buddy');
100+
});
101+
});
102+

packages/hello-world/__tests__/seeding/seeding.sql.test.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@ let teardown: () => Promise<void>;
77

88
const sql = (f: string) => path.join(__dirname, 'data', f);
99

10-
const cwd = path.resolve(__dirname, '../../');
11-
1210
beforeAll(async () => {
1311
({ db, teardown } = await getConnections(
14-
{}, [
15-
seed.launchql(cwd),
16-
seed.sqlfile([
17-
sql('seed-data.sql'),
18-
])
19-
]
12+
{},
13+
[
14+
// load schema and it's dependencies (supabase full schema)
15+
seed.launchql(),
16+
17+
// load data from sql file
18+
seed.sqlfile([
19+
sql('seed-data.sql'),
20+
])
21+
]
2022
));
2123
});
2224

@@ -33,7 +35,7 @@ afterEach(async () => {
3335
});
3436

3537
describe('tutorial: testing with sql file seeding', () => {
36-
it('should work with sql file seed function', async () => {
38+
it('data should be loaded into the database', async () => {
3739

3840
db.setContext({
3941
role: 'authenticated',

0 commit comments

Comments
 (0)