Skip to content

Commit 7473e2a

Browse files
committed
seeding csv
1 parent a0dac47 commit 7473e2a

File tree

2 files changed

+41
-66
lines changed

2 files changed

+41
-66
lines changed

packages/rls-demo/__tests__/seeding/data/seed-data.sql

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,14 @@
22
-- This file can be used to populate the database with test data
33

44
-- Insert test users
5-
INSERT INTO rls_test.user_profiles (id, email, name) VALUES
6-
('550e8400-e29b-41d4-a716-446655440001', '[email protected]', 'Alice Johnson'),
7-
('550e8400-e29b-41d4-a716-446655440002', '[email protected]', 'Bob Smith'),
8-
('550e8400-e29b-41d4-a716-446655440003', '[email protected]', 'Charlie Brown'),
9-
('550e8400-e29b-41d4-a716-446655440004', '[email protected]', 'Diana Prince');
5+
INSERT INTO auth.users (id, email) VALUES
6+
('550e8400-e29b-41d4-a716-446655440001', '[email protected]'),
7+
('550e8400-e29b-41d4-a716-446655440002', '[email protected]'),
8+
('550e8400-e29b-41d4-a716-446655440003', '[email protected]'),
9+
('550e8400-e29b-41d4-a716-446655440004', '[email protected]');
1010

1111
-- Insert test products
12-
INSERT INTO rls_test.products (id, name, description, price, owner_id) VALUES
13-
('660e8400-e29b-41d4-a716-446655440001', 'Laptop Pro', 'High-performance laptop for developers', 1299.99, '550e8400-e29b-41d4-a716-446655440001'),
14-
('660e8400-e29b-41d4-a716-446655440002', 'Wireless Mouse', 'Ergonomic wireless mouse', 49.99, '550e8400-e29b-41d4-a716-446655440001'),
15-
('660e8400-e29b-41d4-a716-446655440003', 'Mechanical Keyboard', 'RGB mechanical keyboard', 149.99, '550e8400-e29b-41d4-a716-446655440002'),
16-
('660e8400-e29b-41d4-a716-446655440004', 'Monitor 4K', '27-inch 4K monitor', 399.99, '550e8400-e29b-41d4-a716-446655440002'),
17-
('660e8400-e29b-41d4-a716-446655440005', 'Webcam HD', '1080p webcam for video calls', 89.99, '550e8400-e29b-41d4-a716-446655440003'),
18-
('660e8400-e29b-41d4-a716-446655440006', 'Headphones', 'Noise-cancelling headphones', 199.99, '550e8400-e29b-41d4-a716-446655440003'),
19-
('660e8400-e29b-41d4-a716-446655440007', 'Standing Desk', 'Adjustable height standing desk', 599.99, '550e8400-e29b-41d4-a716-446655440004'),
20-
('660e8400-e29b-41d4-a716-446655440008', 'Desk Lamp', 'LED desk lamp with USB charging', 79.99, '550e8400-e29b-41d4-a716-446655440004');
21-
22-
-- Verify the data
23-
SELECT
24-
u.name as user_name,
25-
u.email,
26-
COUNT(p.id) as product_count,
27-
COALESCE(SUM(p.price), 0) as total_value
28-
FROM rls_test.user_profiles u
29-
LEFT JOIN rls_test.products p ON u.id = p.owner_id
30-
GROUP BY u.id, u.name, u.email
31-
ORDER BY u.name;
12+
INSERT INTO rls_test.pets (id, name, breed, user_id) VALUES
13+
('660e8400-e29b-41d4-a716-446655440001', 'Fido', 'Labrador', '550e8400-e29b-41d4-a716-446655440001'),
14+
('660e8400-e29b-41d4-a716-446655440002', 'Buddy', 'Golden Retriever', '550e8400-e29b-41d4-a716-446655440002'),
15+
('660e8400-e29b-41d4-a716-446655440003', 'Rex', 'German Shepherd', '550e8400-e29b-41d4-a716-446655440003');

packages/rls-demo/__tests__/seeding/seeding.sql.test.ts

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ const cwd = path.resolve(__dirname, '../../');
1111
beforeAll(async () => {
1212
({ db, teardown } = await getConnections(
1313
{}, [
14-
seed.launchql(cwd),
15-
seed.sqlfile([
16-
sql('seed-data.sql'),
17-
]) ]
14+
seed.launchql(cwd),
15+
seed.sqlfile([
16+
sql('seed-data.sql'),
17+
])
18+
]
1819
));
1920
});
2021

@@ -30,55 +31,45 @@ afterEach(async () => {
3031
await db.afterEach();
3132
});
3233

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+
3352
describe('tutorial: testing with sql file seeding', () => {
3453
it('should work with sql file seed function', async () => {
3554

36-
db.setContext({ role: 'service_role' });
37-
38-
const user = await db.one(
39-
`INSERT INTO rls_test.user_profiles (email, name)
40-
VALUES ($1, $2)
41-
RETURNING id`,
42-
['[email protected]', 'SQL Seed User 1']
43-
);
44-
4555
db.setContext({
4656
role: 'authenticated',
47-
'request.jwt.claim.sub': user.id
57+
'request.jwt.claim.sub': users[0].id
4858
});
4959

50-
await db.one(
51-
`INSERT INTO rls_test.products (name, description, price, owner_id)
52-
VALUES ($1, $2, $3, $4)
53-
RETURNING id`,
54-
['Secret Product', 'Should not be visible', 100.00, user.id]
60+
const verifiedPets = await db.any(
61+
`SELECT id FROM rls_test.pets WHERE user_id = $1`,
62+
[users[0].id]
5563
);
56-
57-
const verifiedUsers = await db.any(
58-
`SELECT id FROM rls_test.user_profiles WHERE id = $1`,
59-
[user.id]
60-
);
61-
expect(verifiedUsers.length).toBe(1);
62-
63-
const verifiedProducts = await db.any(
64-
`SELECT id FROM rls_test.products WHERE owner_id = $1`,
65-
[user.id]
66-
);
67-
expect(verifiedProducts.length).toBe(1);
64+
expect(verifiedPets.length).toBe(1);
6865

6966
db.clearContext();
70-
71-
const anonUsers = await db.any(
72-
`SELECT id FROM rls_test.user_profiles WHERE id = $1`,
73-
[user.id]
74-
);
75-
expect(anonUsers.length).toBe(0);
7667

77-
const anonProducts = await db.any(
78-
`SELECT id FROM rls_test.products WHERE owner_id = $1`,
79-
[user.id]
68+
const anonPets = await db.any(
69+
`SELECT id FROM rls_test.pets WHERE user_id = $1`,
70+
[users[0].id]
8071
);
81-
expect(anonProducts.length).toBe(0);
72+
expect(anonPets.length).toBe(0);
8273

8374
});
8475

0 commit comments

Comments
 (0)