Improved integration test framework (#4161)

This commit is contained in:
Igor Savin 2020-12-14 23:47:02 +02:00 committed by GitHub
parent 5e95a6afb8
commit 704853d605
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 235 additions and 5 deletions

View File

@ -15,11 +15,11 @@
"lint": "eslint \"lib/**/*.js\" \"test/**/*.js\"",
"lint:types": "tsd && dtslint types",
"lint:everything": "npm run lint:types && npm run lint",
"test:unit": "mocha --exit -t 10000 test/db-less-test-suite.js && npm run test:tape && npm run test:cli",
"test:db": "mocha --exit -t 10000 test/integration-test-suite.js",
"test:db:no-oracle": "cross-env DB=\"mssql mysql mysql2 postgres sqlite3\" mocha --exit -t 10000 test/integration-test-suite.js",
"test": "mocha --exit -t 10000 test/all-tests-suite.js && npm run test:tape && npm run test:cli",
"test:coverage": "nyc mocha --exit --check-leaks --globals __core-js_shared__ -t 10000 test/all-tests-suite.js && npm run test:tape && npm run test:cli",
"test:unit": "mocha --exit -t 10000 --config test/mocha-unit-config-test.js && npm run test:tape && npm run test:cli",
"test:db": "mocha --exit -t 10000 --config test/mocha-integration-config-test.js",
"test:db:no-oracle": "cross-env DB=\"mssql mysql mysql2 postgres sqlite3\" mocha --exit -t 10000 --config test/mocha-integration-config-test.js",
"test": "mocha --exit -t 10000 --config test/mocha-all-config-test.js && npm run test:tape && npm run test:cli",
"test:coverage": "nyc mocha --exit --check-leaks --globals __core-js_shared__ -t 10000 --config test/mocha-all-config-test.js && npm run test:tape && npm run test:cli",
"test:everything": "npm run lint:everything && npm run test:coverage",
"test:sqlite": "cross-env DB=sqlite3 npm run test:db",
"test:postgres": "cross-env DB=postgres npm run test:db",

View File

@ -0,0 +1,78 @@
const { expect } = require('chai');
const { getAllDbs, getKnexForDb } = require('../util/knex-instance-provider');
describe.skip('Schema', () => {
describe('Foreign keys', () => {
getAllDbs().forEach((db) => {
describe(db, () => {
let knex;
before(() => {
knex = getKnexForDb(db);
});
after(() => {
return knex.destroy();
});
beforeEach(async () => {
await knex.schema
.createTable('foreign_keys_table_two', (table) => {
table.increments();
})
.createTable('foreign_keys_table_three', (table) => {
table.increments();
})
.createTable('foreign_keys_table_one', (table) => {
table.increments();
table.integer('fkey_two').unsigned().notNull();
table.integer('fkey_three').unsigned().notNull();
});
});
afterEach(async () => {
await knex.schema
.dropTable('foreign_keys_table_one')
.dropTable('foreign_keys_table_two')
.dropTable('foreign_keys_table_three');
});
describe('createForeignKey', () => {
it('creates new foreign key', async () => {
await knex.schema.alterTable('foreign_keys_table_one', (table) => {
table
.foreign('fkey_three')
.references('foreign_keys_table_three.id')
.withKeyName('fk_fkey_threeee');
});
await knex('foreign_keys_table_two').insert({});
await knex('foreign_keys_table_three').insert({});
await knex('foreign_keys_table_one').insert({
fkey_two: 1,
fkey_three: 1,
});
try {
await knex('foreign_keys_table_one').insert({
fkey_two: 9999,
fkey_three: 99,
});
throw new Error("Shouldn't reach this");
} catch (err) {
if (knex.client.driverName === 'sqlite3') {
expect(err.message).to.equal(
`insert into \`foreign_keys_table_one\` (\`fkey_three\`, \`fkey_two\`) values (99, 9999) - SQLITE_CONSTRAINT: FOREIGN KEY constraint failed`
);
}
if (knex.client.driverName === 'postgres') {
expect(err.message).to.equal(
`insert into "foreign_keys_table_one" ("fkey_three", "fkey_two") values ($1, $2) - insert or update on table "foreign_keys_table_one" violates foreign key constraint "foreign_keys_table_one_fkey_two_foreign"`
);
}
expect(err.message).to.include('constraint');
}
});
});
});
});
});
});

View File

@ -0,0 +1,143 @@
const { promisify } = require('util');
const knex = require('../../../lib');
const Db = {
PostgresSQL: 'postgres',
MySQL: 'mysql',
MySQL2: 'mysql2',
MSSQL: 'mssql',
SQLite: 'sqlite3',
Oracle: 'oracledb',
};
const defaultDbs = [Db.PostgresSQL, Db.MySQL, Db.MySQL2, Db.SQLite, Db.MSSQL];
function getAllDbs() {
return process.env.DB ? process.env.DB.split(' ') : defaultDbs;
}
const pool = {
afterCreate: function (connection, callback) {
callback(null, connection);
},
};
const poolSqlite = {
min: 0,
max: 1,
acquireTimeoutMillis: 1000,
afterCreate: function (connection, callback) {
connection.run('PRAGMA foreign_keys = ON', callback);
},
};
const mysqlPool = Object.assign({}, pool, {
afterCreate: function (connection, callback) {
promisify(connection.query)
.call(connection, "SET sql_mode='TRADITIONAL';", [])
.then(function () {
callback(null, connection);
});
},
});
const migrations = {
directory: 'test/integration/migrate/migration',
};
const seeds = {
directory: 'test/integration/seed/seeds',
};
const testConfigs = {
mysql: {
client: 'mysql',
connection: {
port: 23306,
database: 'knex_test',
host: 'localhost',
user: 'testuser',
password: 'testpassword',
charset: 'utf8',
},
pool: mysqlPool,
migrations,
seeds,
},
mysql2: {
client: 'mysql2',
connection: {
port: 23306,
database: 'knex_test',
host: 'localhost',
user: 'testuser',
password: 'testpassword',
charset: 'utf8',
},
pool: mysqlPool,
migrations,
seeds,
},
postgres: {
client: 'postgres',
connection: {
adapter: 'postgresql',
port: 25432,
host: 'localhost',
database: 'knex_test',
user: 'testuser',
password: 'knextest',
},
pool,
migrations,
seeds,
},
sqlite3: {
client: 'sqlite3',
connection: ':memory:',
pool: poolSqlite,
migrations,
seeds,
},
mssql: {
client: 'mssql',
connection: {
user: 'sa',
password: 'S0meVeryHardPassword',
server: 'localhost',
port: 21433,
database: 'knex_test',
},
pool: pool,
migrations,
seeds,
},
oracledb: {
client: 'oracledb',
connection: {
user: 'system',
password: 'Oracle18',
connectString: 'localhost:21521/XE',
// https://github.com/oracle/node-oracledb/issues/525
stmtCacheSize: 0,
},
pool,
migrations,
},
};
function getKnexForDb(db) {
const config = testConfigs[db];
return knex(config);
}
module.exports = {
Db,
getAllDbs,
getKnexForDb,
};

View File

@ -0,0 +1,3 @@
module.exports = {
spec: ['test/all-tests-suite.js', 'test/integration2/**/*.spec.js'],
};

View File

@ -0,0 +1,3 @@
module.exports = {
spec: ['test/integration-test-suite.js', 'test/integration2/**/*.spec.js'],
};

View File

@ -0,0 +1,3 @@
module.exports = {
spec: ['test/db-less-test-suite.js'],
};