Skip to content

Commit

Permalink
Tests improvements (#130)
Browse files Browse the repository at this point in the history
* Tests improvements

* Fixes in jest config

* Fix tests

* Change test db cleanup method

* Remove comment

* Return dist/run folder
  • Loading branch information
imsitnikov authored Jun 19, 2024
1 parent d28d25a commit 8be88eb
Show file tree
Hide file tree
Showing 37 changed files with 220 additions and 479 deletions.
2 changes: 1 addition & 1 deletion api/middlewares.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export {
decodeId,
waitDatabase,
setCiEnv,
dlContext,
finalRequestHandler,
logError,
checkReadOnlyMode,
resolveWorkbookId,
setCiEnv,
} from '../src/components/middlewares';
1 change: 1 addition & 0 deletions api/tests.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export {testTenantId} from '../src/tests/int/constants';
export {testDbConfig, getTestDsnList} from '../src/tests/int/db';
2 changes: 1 addition & 1 deletion dev/env/opensource/int-testing.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ APP_INSTALLATION=opensource
APP_ENV=int-testing

MASTER_TOKEN=int-testing-master-token
CONTROL_MASTER_TOKEN=int-testing-control-master-token

APP_LOGGING_LEVEL=silent
US_SURPRESS_DB_STATUS_LOGS=true
43 changes: 43 additions & 0 deletions jest/int/prepare-test-db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const DSNParser = require('dsn-parser');
const knexBuilder = require('knex');
const _ = require('lodash');

const {getKnexOptions} = require('../../dist/server/db/init-db');
const {getTestDsnList} = require('../../dist/server/tests/int/db');

const prepareTestUsDb = async ({dsnList}) => {
const knexOptions = _.merge({}, getKnexOptions(), {
connection: dsnList,
});

const knexInstance = knexBuilder(knexOptions);

await knexInstance.raw(`
DROP SCHEMA public CASCADE;
CREATE SCHEMA public;
`);

await knexInstance.raw(`
CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE EXTENSION IF NOT EXISTS btree_gin;
CREATE EXTENSION IF NOT EXISTS btree_gist;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
`);

await knexInstance.migrate.latest();
await knexInstance.destroy();
};

const prepareTestDb = async () => {
const dsnList = getTestDsnList();

const testDbName = 'int-testing_us_ci_purgeable';
const parsedDsn = new DSNParser(dsnList);
if (parsedDsn.getParts().database !== testDbName) {
throw new Error(`Database for tests should be named \`${testDbName}\`!`);
}

await prepareTestUsDb({dsnList});
};

module.exports = {prepareTestDb};
5 changes: 5 additions & 0 deletions jest/int/setup-after-env.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
require('events').EventEmitter.defaultMaxListeners = 1000;

require('../../dist/server');

const {db} = require('../../dist/server/db');

const {prepareTestDb} = require('./prepare-test-db');

global.beforeAll(async () => {
await db.ready();

await prepareTestDb();
});

global.afterAll(async () => {
Expand Down
60 changes: 0 additions & 60 deletions jest/int/test-environment.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,8 @@
const {TestcontainersEnvironment} = require('@trendyol/jest-testcontainers');
const knexBuilder = require('knex');

const {getKnexOptions} = require('../../dist/server/db/init-db');
const {getTestDsnList} = require('../../dist/server/tests/int/db');

class TestEnvironment extends TestcontainersEnvironment {
static prepearedDbPromise;

static prepareDb = ({host, port}) => {
if (TestEnvironment.prepearedDbPromise) {
return TestEnvironment.prepearedDbPromise;
}

const getPreparedDbPromise = async () => {
const dsnList = getTestDsnList({host, port});

const testDbName = 'test_us_ci_purgeable';
if (!dsnList.endsWith(`/${testDbName}`)) {
throw new Error(`Database for tests should be named \`${testDbName}\`!`);
}

const knexInstance = knexBuilder({
...getKnexOptions(),
connection: dsnList,
});

const tenantsExistsResult = await knexInstance.raw(`
SELECT EXISTS (
SELECT FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name = 'tenants'
);
`);

if (tenantsExistsResult.rows[0].exists === true) {
throw new Error(
"Database is not empty, `tenants` table already exists. Don't run tests on databases with data!",
);
}

await knexInstance.raw(`
CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE EXTENSION IF NOT EXISTS btree_gin;
CREATE EXTENSION IF NOT EXISTS btree_gist;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
`);

await knexInstance.migrate.latest();
await knexInstance.seed.run();

await knexInstance.destroy();
};

TestEnvironment.prepearedDbPromise = getPreparedDbPromise();
return TestEnvironment.prepearedDbPromise;
};

async setup() {
await super.setup();

await TestEnvironment.prepareDb({
host: this.global.__TESTCONTAINERS_POSTGRE_IP__,
port: this.global.__TESTCONTAINERS_POSTGRE_PORT_5432__,
});
}

async teardown() {
Expand Down
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@types/pg": "^7.14.11",
"@types/supertest": "^2.0.10",
"@types/uuid": "^8.3.0",
"dsn-parser": "^1.0.3",
"eslint": "^8.35.0",
"husky": "^8.0.3",
"jest": "^29.7.0",
Expand All @@ -57,7 +58,7 @@
"dev": "node ./scripts/setup-dev-env.js opensource && npm run clear:dist && APP_DEV_MODE=1 tsc-watch --onSuccess 'node dist/server'",
"test:unit": "jest -c './jest/unit/jest.config.js'",
"build:test:unit": "npm run build && npm run test:unit",
"test:int": "ENV=int-testing node ./scripts/setup-dev-env.js opensource && JEST_TESTCONTAINERS_CONFIG_PATH='./jest/int/testcontainers-config.js' NODE_TLS_REJECT_UNAUTHORIZED=0 jest -c './jest/int/jest.config.js' --testMatch '**/dist/server/tests/int/opensource/**/(*.)+(test|spec).[tj]s' --detectOpenHandles",
"test:int": "ENV=int-testing node ./scripts/setup-dev-env.js opensource && JEST_TESTCONTAINERS_CONFIG_PATH='./jest/int/testcontainers-config.js' NODE_TLS_REJECT_UNAUTHORIZED=0 jest -c './jest/int/jest.config.js' --testMatch '**/dist/server/tests/int/suites/**/(*.)+(test|spec).[tj]s' --detectOpenHandles",
"build:test:int": "npm run build && npm run test:int",
"test": "npm run build && npm run test:unit && npm run test:int",
"db:migrate": "node dist/server/db/scripts/migrate.js",
Expand Down
10 changes: 2 additions & 8 deletions src/components/middlewares/set-ci-env.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import {Request, Response, NextFunction} from '@gravity-ui/expresskit';
import {
testUserId,
testUserLogin,
TEST_USER_ID_HEADER,
TEST_USER_LOGIN_HEADER,
} from '../../tests/int/constants';

export const setCiEnv = (req: Request, res: Response, next: NextFunction) => {
res.locals.userId = req.headers[TEST_USER_ID_HEADER] ?? testUserId;
res.locals.login = req.headers[TEST_USER_LOGIN_HEADER] ?? testUserLogin;
res.locals.userId = 'dev-user-id';
res.locals.login = 'dev-user-login';

next();
};
5 changes: 1 addition & 4 deletions src/configs/int-testing.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import {AuthPolicy} from '@gravity-ui/expresskit';
import {AppConfig} from '@gravity-ui/nodekit';

export default {
appAuthPolicy: AuthPolicy.disabled,
} as Partial<AppConfig>;
export default {} as Partial<AppConfig>;
2 changes: 0 additions & 2 deletions src/configs/opensource/int-testing.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import {AppConfig} from '@gravity-ui/nodekit';
import {testTenantId} from '../../tests/int/constants';
import {features} from './common';

export default {
tenantIdOverride: testTenantId,
features: {
...features,
},
Expand Down
13 changes: 1 addition & 12 deletions src/db/init-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ export const getKnexOptions = () => ({
extension: 'js',
loadExtensions: ['.js'],
},
seeds: {
directory: path.resolve(__dirname, 'seeds'),
loadExtensions: ['.js'],
},
postProcessResponse: (result: any): any => {
let dataFormed;

Expand All @@ -69,14 +65,7 @@ export const getKnexOptions = () => ({
export function initDB(nodekit: NodeKit) {
let dsnList: string;
if (nodekit.config.appEnv === AppEnv.IntTesting) {
const globals = global as unknown as {
__TESTCONTAINERS_POSTGRE_IP__: string;
__TESTCONTAINERS_POSTGRE_PORT_5432__: string;
};
dsnList = getTestDsnList({
host: globals.__TESTCONTAINERS_POSTGRE_IP__,
port: globals.__TESTCONTAINERS_POSTGRE_PORT_5432__,
});
dsnList = getTestDsnList();
} else {
dsnList = Utils.getDsnList();
}
Expand Down
Loading

0 comments on commit 8be88eb

Please sign in to comment.