Skip to content

Commit a59881d

Browse files
authored
Merge pull request #155 from Code-Hammers/CHE-197/subtask/Add-PG-to-BE-Test-Setup
[CHE-197] Add PG to BE Test Setup
2 parents f53b887 + bbe957e commit a59881d

File tree

5 files changed

+129
-11
lines changed

5 files changed

+129
-11
lines changed

docker-compose-test-solo.yml

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,32 @@ services:
1111
- client_node_modules:/usr/src/app/client/node_modules
1212
depends_on:
1313
- ch-mongo-test
14+
- ch-pg-test
1415
environment:
15-
- JWT_SECRET=${JWT_SECRET}
16+
- NODE_ENV=test
17+
- JWT_SECRET=testJwtSecret
1618
- MONGO_URI=mongodb://ch-mongo-test:27017/ch-testdb
1719
- POSTGRES_USER=postgres
18-
- POSTGRES_DB=ch-dev-database
19-
- POSTGRES_PASSWORD=ch-dev
20+
- POSTGRES_DB=ch-test-database
21+
- POSTGRES_PASSWORD=ch-test
2022
# suppress aws sdk v2 deprecation warning
2123
- AWS_SDK_JS_SUPPRESS_MAINTENANCE_MODE_MESSAGE=1;
2224
- TEST_CMD=${TEST_CMD}
2325
- TEST_FILE=${TEST_FILE}
2426
command: npm run ${TEST_CMD} ${TEST_FILE}
2527

28+
ch-pg-test:
29+
image: postgres:16.3
30+
container_name: ch-pg-test
31+
environment:
32+
- POSTGRES_USER=postgres
33+
- POSTGRES_DB=ch-test-database
34+
- POSTGRES_PASSWORD=ch-test
35+
volumes:
36+
- ./scripts/db/postgres/sql_db_init_test.sql:/docker-entrypoint-initdb.d/sql_db_init_test.sql
37+
ports:
38+
- '5432:5432'
39+
2640
ch-mongo-test:
2741
image: mongo
2842
container_name: ch-mongo-test

docker-compose-test.yml

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,30 @@ services:
1212
- client_node_modules:/usr/src/app/client/node_modules
1313
depends_on:
1414
- ch-mongo-test
15+
- ch-pg-test
1516
environment:
16-
- JWT_SECRET=${JWT_SECRET}
17+
- NODE_ENV=test
18+
- JWT_SECRET=testJwtSecret
1719
- MONGO_URI=mongodb://ch-mongo-test:27017/ch-testdb
1820
- POSTGRES_USER=postgres
19-
- POSTGRES_DB=ch-dev-database
20-
- POSTGRES_PASSWORD=ch-dev
21+
- POSTGRES_DB=ch-test-database
22+
- POSTGRES_PASSWORD=ch-test
2123
# suppress aws sdk v2 deprecation warning
2224
- AWS_SDK_JS_SUPPRESS_MAINTENANCE_MODE_MESSAGE=1;
2325
command: npm run test:all
2426

27+
ch-pg-test:
28+
image: postgres:16.3
29+
container_name: ch-pg-test
30+
environment:
31+
- POSTGRES_USER=postgres
32+
- POSTGRES_DB=ch-test-database
33+
- POSTGRES_PASSWORD=ch-test
34+
volumes:
35+
- ./scripts/db/postgres/sql_db_init_test.sql:/docker-entrypoint-initdb.d/sql_db_init_test.sql
36+
ports:
37+
- '5432:5432'
38+
2539
ch-mongo-test:
2640
image: mongo
2741
container_name: ch-mongo-test
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
-- DROP EXISTING TABLES
2+
DROP TABLE IF EXISTS follow_ups;
3+
DROP TABLE IF EXISTS applications;
4+
DROP TABLE IF EXISTS jobs;
5+
DROP TABLE IF EXISTS statuses;
6+
DROP TABLE IF EXISTS follow_up_types;
7+
8+
-- CREATE JOBS TABLE
9+
CREATE TABLE jobs (
10+
id SERIAL PRIMARY KEY,
11+
title VARCHAR(255) NOT NULL,
12+
company VARCHAR(255) NOT NULL,
13+
location VARCHAR(255),
14+
description TEXT,
15+
url VARCHAR(255),
16+
created_at TIMESTAMPTZ DEFAULT NOW()
17+
);
18+
19+
-- CREATE STATUSES TABLE
20+
CREATE TABLE statuses (
21+
id SERIAL PRIMARY KEY,
22+
name VARCHAR(255) NOT NULL
23+
);
24+
25+
-- INSERT DEFAULT STATUSES
26+
INSERT INTO statuses (name) VALUES
27+
('Applied'),
28+
('Phone Screen'),
29+
('Interviewing'),
30+
('Offer Received'),
31+
('Rejected'),
32+
('Withdrawn');
33+
34+
-- CREATE FOLLOW-UP TYPES TABLE
35+
CREATE TABLE follow_up_types (
36+
id SERIAL PRIMARY KEY,
37+
name VARCHAR(255) NOT NULL
38+
);
39+
40+
-- INSERT DEFAULT FOLLOW-UP TYPES
41+
INSERT INTO follow_up_types (name) VALUES
42+
('After Apply'),
43+
('After Phone Screen'),
44+
('After Interview'),
45+
('After Technical Interview'),
46+
('After Offer Received'),
47+
('After Rejection'),
48+
('After Withdrawal');
49+
50+
-- CREATE APPLICATIONS TABLE
51+
CREATE TABLE applications (
52+
id SERIAL PRIMARY KEY,
53+
job_id INT NOT NULL,
54+
status_id INT NOT NULL,
55+
user_id VARCHAR(255) NOT NULL,
56+
quick_apply BOOLEAN NOT NULL,
57+
date_applied TIMESTAMPTZ DEFAULT NOW(),
58+
general_notes TEXT,
59+
last_updated TIMESTAMPTZ DEFAULT NOW(),
60+
notification_period INT DEFAULT 3,
61+
notifications_paused BOOLEAN DEFAULT FALSE,
62+
FOREIGN KEY (job_id) REFERENCES jobs(id),
63+
FOREIGN KEY (status_id) REFERENCES statuses(id)
64+
);
65+
66+
-- CREATE FOLLOW-UPS TABLE
67+
CREATE TABLE follow_ups (
68+
id SERIAL PRIMARY KEY,
69+
application_id INT NOT NULL,
70+
follow_up_date TIMESTAMPTZ DEFAULT NOW(),
71+
follow_up_type_id INT NOT NULL,
72+
notes TEXT,
73+
FOREIGN KEY (application_id) REFERENCES applications(id),
74+
FOREIGN KEY (follow_up_type_id) REFERENCES follow_up_types(id)
75+
);

server/config/sql-db.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import { Pool } from 'pg';
2-
import dotenv from 'dotenv';
3-
4-
dotenv.config();
52

63
const pool = new Pool({
74
user: process.env.POSTGRES_USER,
8-
host: 'postgres',
5+
host: process.env.NODE_ENV === 'test' ? 'ch-pg-test' : 'postgres',
96
database: process.env.POSTGRES_DB,
107
password: process.env.POSTGRES_PASSWORD,
118
port: 5432,

server/test/setup.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import mongoose from 'mongoose';
22
import app from '../app';
33
import request from 'supertest';
44
import User from '../models/userModel';
5+
import { pool } from '../config/sql-db';
56

67
declare global {
78
function login(): Promise<string[]>;
@@ -11,7 +12,6 @@ const testUserEmail = '[email protected]';
1112
const testUserPassword = 'jackieTreehorn';
1213

1314
beforeAll(async () => {
14-
process.env.JWT_SECRET = 'asdfasdfasdf';
1515
await mongoose.connect('mongodb://ch-mongo-test:27017/ch-testdb', {});
1616
});
1717

@@ -21,10 +21,28 @@ beforeEach(async () => {
2121
for (const collection of collections) {
2222
await collection.deleteMany({});
2323
}
24+
25+
await pool.query(`
26+
DO
27+
$$
28+
DECLARE
29+
table_name text;
30+
BEGIN
31+
FOR table_name IN
32+
SELECT tablename
33+
FROM pg_tables
34+
WHERE schemaname = 'public'
35+
LOOP
36+
EXECUTE format('TRUNCATE TABLE %I CASCADE;', table_name);
37+
END LOOP;
38+
END
39+
$$;
40+
`);
2441
});
2542

2643
afterAll(async () => {
2744
await mongoose.connection.close();
45+
pool.end();
2846
});
2947

3048
global.login = async () => {

0 commit comments

Comments
 (0)