From ae9ef3acf59cfcba7823c1ebace6f7227b3460d1 Mon Sep 17 00:00:00 2001 From: seantokuzo Date: Mon, 24 Jun 2024 13:04:14 -0700 Subject: [PATCH 1/5] add pg db init for tests --- scripts/db/postgres/sql_db_init_test.sql | 75 ++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 scripts/db/postgres/sql_db_init_test.sql diff --git a/scripts/db/postgres/sql_db_init_test.sql b/scripts/db/postgres/sql_db_init_test.sql new file mode 100644 index 0000000..151cf5e --- /dev/null +++ b/scripts/db/postgres/sql_db_init_test.sql @@ -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) +); From 0ab02a35fc3c0c9adf562cddf266dee2fde428e6 Mon Sep 17 00:00:00 2001 From: seantokuzo Date: Mon, 24 Jun 2024 13:04:59 -0700 Subject: [PATCH 2/5] add node env dependent host - remove unused dotenv --- server/config/sql-db.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/server/config/sql-db.ts b/server/config/sql-db.ts index 55860bd..0c5bc95 100644 --- a/server/config/sql-db.ts +++ b/server/config/sql-db.ts @@ -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, From 23affe585f25ce82bc0cba6e0e5b166fa94eb57e Mon Sep 17 00:00:00 2001 From: seantokuzo Date: Mon, 24 Jun 2024 13:06:21 -0700 Subject: [PATCH 3/5] add pg service for tests with test env vars --- docker-compose-test-solo.yml | 20 +++++++++++++++++--- docker-compose-test.yml | 20 +++++++++++++++++--- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/docker-compose-test-solo.yml b/docker-compose-test-solo.yml index 8e6291b..64ef6af 100644 --- a/docker-compose-test-solo.yml +++ b/docker-compose-test-solo.yml @@ -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 diff --git a/docker-compose-test.yml b/docker-compose-test.yml index a7ed2af..b6f79b9 100644 --- a/docker-compose-test.yml +++ b/docker-compose-test.yml @@ -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 From 9bf5f72f4319fe12f14bf86858ab3b40046bd281 Mon Sep 17 00:00:00 2001 From: seantokuzo Date: Mon, 24 Jun 2024 13:06:52 -0700 Subject: [PATCH 4/5] remove unnecessary env var definition - gets from docker env --- server/test/setup.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/server/test/setup.ts b/server/test/setup.ts index a83a74f..e30a5bb 100644 --- a/server/test/setup.ts +++ b/server/test/setup.ts @@ -11,7 +11,6 @@ const testUserEmail = 'theDude@Duderino.com'; const testUserPassword = 'jackieTreehorn'; beforeAll(async () => { - process.env.JWT_SECRET = 'asdfasdfasdf'; await mongoose.connect('mongodb://ch-mongo-test:27017/ch-testdb', {}); }); From bbe957e6b26c36d3c76b635eadccfe3a8a0f5c1d Mon Sep 17 00:00:00 2001 From: seantokuzo Date: Tue, 2 Jul 2024 11:51:21 -0700 Subject: [PATCH 5/5] clear out pg tables before each test and call end on pool after all --- server/test/setup.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/server/test/setup.ts b/server/test/setup.ts index e30a5bb..825a325 100644 --- a/server/test/setup.ts +++ b/server/test/setup.ts @@ -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; @@ -20,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 () => {