Skip to content

Commit

Permalink
Refactor test teardown function
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuupertunut committed Mar 17, 2024
1 parent 1f3abd2 commit 83b1797
Show file tree
Hide file tree
Showing 17 changed files with 33 additions and 44 deletions.
27 changes: 18 additions & 9 deletions src/db/knex.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import config from '../../knexfile.js';
const { Client } = pkg;

const environment = process.env.NODE_ENV || 'development';
const cfg = config[environment];

export const drop_database = async (db_name) => {
const cfg = config[environment];
const drop_database = async (db_name) => {
const client = new Client({
host: cfg.connection.host,
port: cfg.connection.port,
Expand All @@ -17,15 +17,9 @@ export const drop_database = async (db_name) => {
await client.connect();
await client.query('DROP DATABASE ' + db_name);
await client.end();
cfg.connection.database = db_name;
};

const cfg = config[environment];

if (environment == 'test') {
// Each test runs in its own separate process, use pid to avoid db conflicts.
const db_name = 'rv_test_' + process.pid;

const create_database = async (db_name) => {
const client = new Client({
host: cfg.connection.host,
port: cfg.connection.port,
Expand All @@ -37,8 +31,23 @@ if (environment == 'test') {
await client.query('DROP DATABASE IF EXISTS ' + db_name);
await client.query('CREATE DATABASE ' + db_name);
await client.end();
};

/* In test environment, a new database is created for every process to allow parallel test runs. In development and
* production environments there is already a single database per environment */
if (environment === 'test') {
// Each test runs in its own separate process, use pid to avoid db conflicts.
const db_name = 'rv_test_' + process.pid;
await create_database(db_name);
cfg.connection.database = db_name;
}

const knex = Knex(cfg);

/* In test environment, a new database is created for every process, so they need to be deleted after the test is run. */
export const test_teardown = async () => {
await knex.destroy();
await drop_database('rv_test_' + process.pid);
};

export default knex;
3 changes: 1 addition & 2 deletions test/admin/routes.adminauth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import chai from 'chai';
import chaiHttp from 'chai-http';

import app from '../../src/app.js';
import knex from '../../src/db/knex.js';
import knex, { test_teardown } from '../../src/db/knex.js';
import userStore from '../../src/db/userStore.js';
import jwt from '../../src/jwt/token.js';

import { after, afterEach, beforeEach, describe, it } from 'node:test';
import { test_teardown } from '../utils.js';

const expect = chai.expect;

Expand Down
3 changes: 1 addition & 2 deletions test/admin/routes.adminboxes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import chaiHttp from 'chai-http';

import app from '../../src/app.js';
import boxStore from '../../src/db/boxStore.js';
import knex from '../../src/db/knex.js';
import knex, { test_teardown } from '../../src/db/knex.js';
import jwt from '../../src/jwt/token.js';

import { after, afterEach, beforeEach, describe, it } from 'node:test';
import { test_teardown } from '../utils.js';

const expect = chai.expect;

Expand Down
3 changes: 1 addition & 2 deletions test/admin/routes.admincategories.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import chaiHttp from 'chai-http';

import app from '../../src/app.js';
import categoryStore from '../../src/db/categoryStore.js';
import knex from '../../src/db/knex.js';
import knex, { test_teardown } from '../../src/db/knex.js';
import jwt from '../../src/jwt/token.js';

import { after, afterEach, beforeEach, describe, it } from 'node:test';
import { test_teardown } from '../utils.js';

const expect = chai.expect;

Expand Down
3 changes: 1 addition & 2 deletions test/admin/routes.adminhistory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import chai from 'chai';
import chaiHttp from 'chai-http';

import app from '../../src/app.js';
import knex from '../../src/db/knex.js';
import knex, { test_teardown } from '../../src/db/knex.js';
import jwt from '../../src/jwt/token.js';

import { after, afterEach, beforeEach, describe, it } from 'node:test';
import { test_teardown } from '../utils.js';

const expect = chai.expect;

Expand Down
3 changes: 1 addition & 2 deletions test/admin/routes.adminpreferences.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import chai from 'chai';
import chaiHttp from 'chai-http';

import app from '../../src/app.js';
import knex from '../../src/db/knex.js';
import knex, { test_teardown } from '../../src/db/knex.js';
import jwt from '../../src/jwt/token.js';

import { after, afterEach, beforeEach, describe, it } from 'node:test';
import { test_teardown } from '../utils.js';

const expect = chai.expect;

Expand Down
3 changes: 1 addition & 2 deletions test/admin/routes.adminproducts.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import chai from 'chai';
import chaiHttp from 'chai-http';
import app from '../../src/app.js';
import knex from '../../src/db/knex.js';
import knex, { test_teardown } from '../../src/db/knex.js';
import productStore from '../../src/db/productStore.js';
import jwt from '../../src/jwt/token.js';

import { after, afterEach, beforeEach, describe, it } from 'node:test';
import { test_teardown } from '../utils.js';

const expect = chai.expect;

Expand Down
3 changes: 1 addition & 2 deletions test/admin/routes.adminusers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import chai from 'chai';
import chaiHttp from 'chai-http';

import app from '../../src/app.js';
import knex from '../../src/db/knex.js';
import knex, { test_teardown } from '../../src/db/knex.js';
import userStore from '../../src/db/userStore.js';
import jwt from '../../src/jwt/token.js';

import { after, afterEach, beforeEach, describe, it } from 'node:test';
import { test_teardown } from '../utils.js';

const expect = chai.expect;

Expand Down
3 changes: 1 addition & 2 deletions test/routes.auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import chai from 'chai';
import chaiHttp from 'chai-http';

import app from '../src/app.js';
import knex from '../src/db/knex.js';
import knex, { test_teardown } from '../src/db/knex.js';
import userStore from '../src/db/userStore.js';
import jwt from '../src/jwt/token.js';

import { after, afterEach, beforeEach, describe, it } from 'node:test';
import { test_teardown } from './utils.js';

const expect = chai.expect;

Expand Down
3 changes: 1 addition & 2 deletions test/routes.categories.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import chai from 'chai';
import chaiHttp from 'chai-http';

import app from '../src/app.js';
import knex from '../src/db/knex.js';
import knex, { test_teardown } from '../src/db/knex.js';
import jwt from '../src/jwt/token.js';

import { after, afterEach, beforeEach, describe, it } from 'node:test';
import { test_teardown } from './utils.js';

const expect = chai.expect;

Expand Down
3 changes: 1 addition & 2 deletions test/routes.products.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import chaiHttp from 'chai-http';

import app from '../src/app.js';
import historyStore from '../src/db/historyStore.js';
import knex from '../src/db/knex.js';
import knex, { test_teardown } from '../src/db/knex.js';
import productStore from '../src/db/productStore.js';
import userStore from '../src/db/userStore.js';
import jwt from '../src/jwt/token.js';

import { after, afterEach, beforeEach, describe, it } from 'node:test';
import { test_teardown } from './utils.js';

const expect = chai.expect;

Expand Down
3 changes: 1 addition & 2 deletions test/routes.register.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import chai from 'chai';
import chaiHttp from 'chai-http';

import app from '../src/app.js';
import knex from '../src/db/knex.js';
import knex, { test_teardown } from '../src/db/knex.js';
import userStore from '../src/db/userStore.js';

import { after, afterEach, beforeEach, describe, it } from 'node:test';
import { test_teardown } from './utils.js';

const expect = chai.expect;

Expand Down
3 changes: 1 addition & 2 deletions test/routes.user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ import chaiHttp from 'chai-http';

import app from '../src/app.js';
import historyStore from '../src/db/historyStore.js';
import knex from '../src/db/knex.js';
import knex, { test_teardown } from '../src/db/knex.js';
import userStore from '../src/db/userStore.js';
import jwt from '../src/jwt/token.js';

import { after, afterEach, beforeEach, describe, it } from 'node:test';
import { test_teardown } from './utils.js';

const expect = chai.expect;

Expand Down
3 changes: 1 addition & 2 deletions test/routes.userDepositHistory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import chai from 'chai';
import chaiHttp from 'chai-http';

import app from '../src/app.js';
import knex from '../src/db/knex.js';
import knex, { test_teardown } from '../src/db/knex.js';
import jwt from '../src/jwt/token.js';

import { after, afterEach, beforeEach, describe, it } from 'node:test';
import { test_teardown } from './utils.js';

const expect = chai.expect;

Expand Down
3 changes: 1 addition & 2 deletions test/routes.userPurchaseHistory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import chai from 'chai';
import chaiHttp from 'chai-http';

import app from '../src/app.js';
import knex from '../src/db/knex.js';
import knex, { test_teardown } from '../src/db/knex.js';
import jwt from '../src/jwt/token.js';

import { after, afterEach, beforeEach, describe, it } from 'node:test';
import { test_teardown } from './utils.js';

const expect = chai.expect;

Expand Down
2 changes: 1 addition & 1 deletion test/test_env/api_data_reset.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const expect = chai.expect;
chai.use(chaiHttp);

import { after, describe, it } from 'node:test';
import { test_teardown } from '../utils.js';
import { test_teardown } from '../../src/db/knex.js';

after(async () => {
await test_teardown();
Expand Down
6 changes: 0 additions & 6 deletions test/utils.js

This file was deleted.

0 comments on commit 83b1797

Please sign in to comment.