Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CHE-197] Add PG to BE Test Setup #155

Merged
merged 6 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions docker-compose-test-solo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,32 @@ services:
- client_node_modules:/usr/src/app/client/node_modules
depends_on:
- ch-mongo-test
- ch-pg-test
environment:
- JWT_SECRET=${JWT_SECRET}
- NODE_ENV=test
- JWT_SECRET=testJwtSecret
- MONGO_URI=mongodb://ch-mongo-test:27017/ch-testdb
- POSTGRES_USER=postgres
- POSTGRES_DB=ch-dev-database
- POSTGRES_PASSWORD=ch-dev
- POSTGRES_DB=ch-test-database
- POSTGRES_PASSWORD=ch-test
# suppress aws sdk v2 deprecation warning
- AWS_SDK_JS_SUPPRESS_MAINTENANCE_MODE_MESSAGE=1;
- TEST_CMD=${TEST_CMD}
- TEST_FILE=${TEST_FILE}
command: npm run ${TEST_CMD} ${TEST_FILE}

ch-pg-test:
image: postgres:16.3
container_name: ch-pg-test
environment:
- POSTGRES_USER=postgres
- POSTGRES_DB=ch-test-database
- POSTGRES_PASSWORD=ch-test
volumes:
- ./scripts/db/postgres/sql_db_init_test.sql:/docker-entrypoint-initdb.d/sql_db_init_test.sql
ports:
- '5432:5432'

ch-mongo-test:
image: mongo
container_name: ch-mongo-test
Expand Down
20 changes: 17 additions & 3 deletions docker-compose-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,30 @@ services:
- client_node_modules:/usr/src/app/client/node_modules
depends_on:
- ch-mongo-test
- ch-pg-test
environment:
- JWT_SECRET=${JWT_SECRET}
- NODE_ENV=test
- JWT_SECRET=testJwtSecret
- MONGO_URI=mongodb://ch-mongo-test:27017/ch-testdb
- POSTGRES_USER=postgres
- POSTGRES_DB=ch-dev-database
- POSTGRES_PASSWORD=ch-dev
- POSTGRES_DB=ch-test-database
- POSTGRES_PASSWORD=ch-test
# suppress aws sdk v2 deprecation warning
- AWS_SDK_JS_SUPPRESS_MAINTENANCE_MODE_MESSAGE=1;
command: npm run test:all

ch-pg-test:
image: postgres:16.3
container_name: ch-pg-test
environment:
- POSTGRES_USER=postgres
- POSTGRES_DB=ch-test-database
- POSTGRES_PASSWORD=ch-test
volumes:
- ./scripts/db/postgres/sql_db_init_test.sql:/docker-entrypoint-initdb.d/sql_db_init_test.sql
ports:
- '5432:5432'

ch-mongo-test:
image: mongo
container_name: ch-mongo-test
Expand Down
75 changes: 75 additions & 0 deletions scripts/db/postgres/sql_db_init_test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
-- DROP EXISTING TABLES
DROP TABLE IF EXISTS follow_ups;
DROP TABLE IF EXISTS applications;
DROP TABLE IF EXISTS jobs;
DROP TABLE IF EXISTS statuses;
DROP TABLE IF EXISTS follow_up_types;

-- CREATE JOBS TABLE
CREATE TABLE jobs (
id SERIAL PRIMARY KEY,
title VARCHAR(255) NOT NULL,
company VARCHAR(255) NOT NULL,
location VARCHAR(255),
description TEXT,
url VARCHAR(255),
created_at TIMESTAMPTZ DEFAULT NOW()
);

-- CREATE STATUSES TABLE
CREATE TABLE statuses (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL
);

-- INSERT DEFAULT STATUSES
INSERT INTO statuses (name) VALUES
('Applied'),
('Phone Screen'),
('Interviewing'),
('Offer Received'),
('Rejected'),
('Withdrawn');

-- CREATE FOLLOW-UP TYPES TABLE
CREATE TABLE follow_up_types (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL
);

-- INSERT DEFAULT FOLLOW-UP TYPES
INSERT INTO follow_up_types (name) VALUES
('After Apply'),
('After Phone Screen'),
('After Interview'),
('After Technical Interview'),
('After Offer Received'),
('After Rejection'),
('After Withdrawal');

-- CREATE APPLICATIONS TABLE
CREATE TABLE applications (
id SERIAL PRIMARY KEY,
job_id INT NOT NULL,
status_id INT NOT NULL,
user_id VARCHAR(255) NOT NULL,
quick_apply BOOLEAN NOT NULL,
date_applied TIMESTAMPTZ DEFAULT NOW(),
general_notes TEXT,
last_updated TIMESTAMPTZ DEFAULT NOW(),
notification_period INT DEFAULT 3,
notifications_paused BOOLEAN DEFAULT FALSE,
FOREIGN KEY (job_id) REFERENCES jobs(id),
FOREIGN KEY (status_id) REFERENCES statuses(id)
);

-- CREATE FOLLOW-UPS TABLE
CREATE TABLE follow_ups (
id SERIAL PRIMARY KEY,
application_id INT NOT NULL,
follow_up_date TIMESTAMPTZ DEFAULT NOW(),
follow_up_type_id INT NOT NULL,
notes TEXT,
FOREIGN KEY (application_id) REFERENCES applications(id),
FOREIGN KEY (follow_up_type_id) REFERENCES follow_up_types(id)
);
5 changes: 1 addition & 4 deletions server/config/sql-db.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { Pool } from 'pg';
import dotenv from 'dotenv';

dotenv.config();

const pool = new Pool({
user: process.env.POSTGRES_USER,
host: 'postgres',
host: process.env.NODE_ENV === 'test' ? 'ch-pg-test' : 'postgres',
database: process.env.POSTGRES_DB,
password: process.env.POSTGRES_PASSWORD,
port: 5432,
Expand Down
20 changes: 19 additions & 1 deletion server/test/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import mongoose from 'mongoose';
import app from '../app';
import request from 'supertest';
import User from '../models/userModel';
import { pool } from '../config/sql-db';

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

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

Expand All @@ -21,10 +21,28 @@ beforeEach(async () => {
for (const collection of collections) {
await collection.deleteMany({});
}

await pool.query(`
DO
$$
DECLARE
table_name text;
BEGIN
FOR table_name IN
SELECT tablename
FROM pg_tables
WHERE schemaname = 'public'
LOOP
EXECUTE format('TRUNCATE TABLE %I CASCADE;', table_name);
END LOOP;
END
$$;
`);
});

afterAll(async () => {
await mongoose.connection.close();
pool.end();
});

global.login = async () => {
Expand Down
Loading