Skip to content

Commit 48f998b

Browse files
committed
fix CSV tests
1 parent 4671f75 commit 48f998b

File tree

6 files changed

+64
-46
lines changed

6 files changed

+64
-46
lines changed

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,12 @@ describe('tutorial: basic rls crud operations', () => {
101101
});
102102

103103
it('should allow users to see only their own data in list queries', async () => {
104-
// db.setContext({role: 'service_role'});
105-
106104
// set context to user1
107105
db.setContext({
108106
role: 'authenticated',
109107
'request.jwt.claim.sub': user1.id
110108
});
111109

112-
113110
// create multiple users as admin
114111
await db.one(
115112
`INSERT INTO rls_test.pets (name, breed, user_id)
@@ -124,7 +121,6 @@ describe('tutorial: basic rls crud operations', () => {
124121
'request.jwt.claim.sub': user2.id
125122
});
126123

127-
128124
await db.one(
129125
`INSERT INTO rls_test.pets (name, breed, user_id)
130126
VALUES ($1, $2, $3)
@@ -138,7 +134,6 @@ describe('tutorial: basic rls crud operations', () => {
138134
'request.jwt.claim.sub': user3.id
139135
});
140136

141-
142137
await db.one(
143138
`INSERT INTO rls_test.pets (name, breed, user_id)
144139
VALUES ($1, $2, $3)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
id,name,breed,user_id
2+
660e8400-e29b-41d4-a716-446655440001,Fido,Labrador,550e8400-e29b-41d4-a716-446655440001
3+
660e8400-e29b-41d4-a716-446655440002,Buddy,Golden Retriever,550e8400-e29b-41d4-a716-446655440002
4+
660e8400-e29b-41d4-a716-446655440003,Rex,German Shepherd,550e8400-e29b-41d4-a716-446655440003

packages/hello-world/__tests__/seeding/data/seed-data.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ INSERT INTO auth.users (id, email) VALUES
88
('550e8400-e29b-41d4-a716-446655440003', '[email protected]'),
99
('550e8400-e29b-41d4-a716-446655440004', '[email protected]');
1010

11-
-- Insert test products
11+
-- Insert test pets
1212
INSERT INTO rls_test.pets (id, name, breed, user_id) VALUES
1313
('660e8400-e29b-41d4-a716-446655440001', 'Fido', 'Labrador', '550e8400-e29b-41d4-a716-446655440001'),
1414
('660e8400-e29b-41d4-a716-446655440002', 'Buddy', 'Golden Retriever', '550e8400-e29b-41d4-a716-446655440002'),
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
export const users = [
2+
{
3+
id: '550e8400-e29b-41d4-a716-446655440001',
4+
5+
name: 'Tutorial User 1'
6+
},
7+
{
8+
id: '550e8400-e29b-41d4-a716-446655440002',
9+
10+
name: 'Tutorial User 2'
11+
},
12+
{
13+
id: '550e8400-e29b-41d4-a716-446655440003',
14+
15+
name: 'Tutorial User 3'
16+
}
17+
];
18+
19+
export const pets = [
20+
{
21+
id: '660e8400-e29b-41d4-a716-446655440001',
22+
name: 'Fido',
23+
breed: 'Labrador',
24+
user_id: '550e8400-e29b-41d4-a716-446655440001'
25+
},
26+
{
27+
id: '660e8400-e29b-41d4-a716-446655440002',
28+
name: 'Buddy',
29+
breed: 'Golden Retriever',
30+
user_id: '550e8400-e29b-41d4-a716-446655440002'
31+
},
32+
{
33+
id: '660e8400-e29b-41d4-a716-446655440003',
34+
name: 'Rex',
35+
breed: 'German Shepherd',
36+
user_id: '550e8400-e29b-41d4-a716-446655440003'
37+
}
38+
];
39+
Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { getConnections, PgTestClient, seed } from 'supabase-test';
22
import path from 'path';
3+
import { users } from './data/seed-data';
34

45
let pg: PgTestClient;
56
let db: PgTestClient;
@@ -14,10 +15,11 @@ beforeAll(async () => {
1415
[
1516
// create schema
1617
seed.launchql(cwd),
18+
1719
// load from csv
1820
seed.csv({
19-
'auth.users': csv('users.csv')
20-
// 'rls_test.products': csv('products.csv')
21+
'auth.users': csv('users.csv'),
22+
'rls_test.pets': csv('pets.csv')
2123
})
2224
]
2325
));
@@ -27,31 +29,26 @@ afterAll(async () => {
2729
await teardown();
2830
});
2931

30-
// TODO: make issue for db/pg on csv seeding
31-
// TODO: consider the supabase full schema
3232
describe('csv seeding', () => {
3333
it('has loaded rows from csv files', async () => {
34-
// db.setContext({ role: 'service_role' });
35-
3634
const usersRes = await pg.query('SELECT COUNT(*) FROM auth.users');
37-
console.log('usersRes', usersRes);
3835
expect(+usersRes.rows[0].count).toBeGreaterThan(0);
3936

40-
// const productsRes = await pg.query('SELECT COUNT(*) FROM rls_test.products');
41-
// expect(+productsRes.rows[0].count).toBeGreaterThan(0);
42-
43-
// // verify specific data was loaded
44-
// const alice = await pg.one(
45-
// `SELECT * FROM rls_test.user_profiles WHERE email = $1`,
46-
47-
// );
48-
// expect(alice.name).toBe('Alice Johnson');
49-
50-
// const aliceProducts = await pg.query(
51-
// `SELECT COUNT(*) FROM rls_test.products WHERE owner_id = $1`,
52-
// [alice.id]
53-
// );
54-
// expect(+aliceProducts.rows[0].count).toBe(2);
37+
const petsRes = await pg.query('SELECT COUNT(*) FROM rls_test.pets');
38+
expect(+petsRes.rows[0].count).toBeGreaterThan(0);
39+
40+
// verify specific data was loaded
41+
const alice = await pg.one(
42+
`SELECT * FROM rls_test.pets WHERE user_id = $1`,
43+
[users[0].id]
44+
);
45+
expect(alice.name).toBe('Fido');
46+
47+
const alicePets = await pg.query(
48+
`SELECT COUNT(*) FROM rls_test.pets WHERE user_id = $1`,
49+
[users[0].id]
50+
);
51+
expect(+alicePets.rows[0].count).toBe(1);
5552
});
5653
});
5754

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

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { getConnections, PgTestClient, seed } from 'supabase-test';
22
import path from 'path';
3+
import { users } from './data/seed-data';
34

45
let db: PgTestClient;
56
let teardown: () => Promise<void>;
@@ -31,24 +32,6 @@ afterEach(async () => {
3132
await db.afterEach();
3233
});
3334

34-
const users = [
35-
{
36-
id: '550e8400-e29b-41d4-a716-446655440001',
37-
38-
name: 'Tutorial User 1'
39-
},
40-
{
41-
id: '550e8400-e29b-41d4-a716-446655440002',
42-
43-
name: 'Tutorial User 2'
44-
},
45-
{
46-
id: '550e8400-e29b-41d4-a716-446655440003',
47-
48-
name: 'Tutorial User 3'
49-
}
50-
];
51-
5235
describe('tutorial: testing with sql file seeding', () => {
5336
it('should work with sql file seed function', async () => {
5437

0 commit comments

Comments
 (0)