Skip to content

Commit

Permalink
Refactor more tests to use cli-testlab (#3640)
Browse files Browse the repository at this point in the history
  • Loading branch information
kibertoad committed Jan 28, 2020
1 parent 2f42191 commit c14252d
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 109 deletions.
4 changes: 3 additions & 1 deletion lib/util/parse-connection.js
@@ -1,11 +1,13 @@
const url = require('url');
const { parse } = require('pg-connection-string');
const parsePG = parse;
const isWindows = process && process.platform && process.platform === 'win32';

module.exports = function parseConnectionString(str) {
const parsed = url.parse(str, true);
let { protocol } = parsed;
if (protocol === null) {
const isDriveLetter = isWindows && protocol.length === 2;
if (protocol === null || isDriveLetter) {
return {
client: 'sqlite3',
connection: {
Expand Down
9 changes: 6 additions & 3 deletions test/cli/cli-test-utils.js
Expand Up @@ -63,10 +63,12 @@ function expectContentMatchesStub(stubPath, globPath, fileHelper) {
expect(content).equals(stubContent);
}

function getRootDir() {
return path.resolve(__dirname, '../jake-util');
}

function setupFileHelper() {
const fileHelper = new FileTestHelper(
path.resolve(__dirname, '../jake-util')
);
const fileHelper = new FileTestHelper(getRootDir());
fileHelper.deleteFile('test.sqlite3');
fileHelper.registerForCleanup('test.sqlite3');

Expand All @@ -75,6 +77,7 @@ function setupFileHelper() {

module.exports = {
expectContentMatchesStub,
getRootDir,
migrationStubOptionSetup,
seedStubOptionSetup,
setupFileHelper,
Expand Down
59 changes: 59 additions & 0 deletions test/cli/migrate.spec.js
@@ -0,0 +1,59 @@
'use strict';

const path = require('path');
const sqlite3 = require('sqlite3');
const { expect } = require('chai');
const { execCommand } = require('cli-testlab');
const { getRootDir, setupFileHelper } = require('./cli-test-utils');

const KNEX = path.normalize(__dirname + '/../../bin/cli.js');

describe('migrate:latest', () => {
before(() => {
process.env.KNEX_PATH = '../knex.js';
});

let fileHelper;
const rootDir = getRootDir();
const dbPath = path.resolve(rootDir, 'db');
beforeEach(() => {
fileHelper = setupFileHelper();
fileHelper.registerForCleanup(dbPath);
});
afterEach(() => {
fileHelper.cleanup();
});

it('Run migrations', async () => {
fileHelper.createFile(
'migrations/000_create_rule_table.js',
`
exports.up = (knex)=> knex.schema.createTable('rules', (table)=> {
table.string('name');
});
exports.down = (knex)=> knex.schema.dropTable('rules');
`,
{ willBeCleanedUp: true, isPathAbsolute: false }
);

expect(fileHelper.fileExists(dbPath)).to.equal(false);
await execCommand(`node ${KNEX} migrate:latest \
--client=sqlite3 --connection=${dbPath} \
--migrations-directory=${rootDir}/migrations \
create_rule_table`);
expect(fileHelper.fileExists(dbPath)).to.equal(true);
const db = await new sqlite3.Database(dbPath);

const getPromise = new Promise((resolve, reject) => {
db.get('SELECT name FROM knex_migrations', {}, (err, row) => {
if (err) {
reject(err);
}
resolve(row);
});
});
const row = await getPromise;
expect(row.name).to.equal('000_create_rule_table.js');
db.close();
});
});
60 changes: 60 additions & 0 deletions test/cli/seed.spec.js
@@ -0,0 +1,60 @@
'use strict';

const path = require('path');
const { execCommand } = require('cli-testlab');

const KNEX = path.normalize(__dirname + '/../../bin/cli.js');

describe('seed:run', () => {
before(() => {
process.env.KNEX_PATH = '../knex.js';
});

it('prints non verbose logs', () => {
return execCommand(
`node ${KNEX} seed:run --knexfile=test/jake-util/seeds-knexfile.js`,
{
expectedOutput: 'Ran 2 seed files',
notExpectedOutput: ['first.js', 'second.js'],
}
);
});

it('prints verbose logs', () => {
return execCommand(
`node ${KNEX} seed:run --knexfile=test/jake-util/seeds-knexfile.js --verbose`,
{
expectedOutput: ['Ran 2 seed files', 'first.js', 'second.js'],
}
);
});

it('runs specific file', () => {
return execCommand(
`node ${KNEX} seed:run --knexfile=test/jake-util/seeds-knexfile.js --specific=second.js`,
{
expectedOutput: 'Ran 1 seed files',
notExpectedOutput: ['first.js', 'second.js'],
}
);
});

it('handles seeding errors correctly', () => {
return execCommand(
`node ${KNEX} seed:run --knexfile=test/jake-util/seeds-error-knexfile.js`,
{
expectedErrorMessage: ['Error while executing', 'seeds.js', 'Boom'],
}
);
});

it('seed:run runs "esm" files', () => {
return execCommand(
`node ${KNEX} --esm seed:run --knexfile=test/jake-util/knexfile-esm/knexfile.js`,
{
expectedOutput: 'Ran 1 seed files',
notExpectedOutput: ['first.js', 'second.js'],
}
);
});
});
4 changes: 3 additions & 1 deletion test/index.js
Expand Up @@ -89,9 +89,11 @@ if (config.sqlite3) {

describe('CLI tests', function() {
this.timeout(process.env.KNEX_TEST_TIMEOUT || 5000);
require('./cli/help.spec');
require('./cli/knexfile-test.spec');
require('./cli/migrate.spec');
require('./cli/migrate-make.spec');
require('./cli/seed.spec');
require('./cli/seed-make.spec');
require('./cli/version.spec');
require('./cli/help.spec');
});
4 changes: 1 addition & 3 deletions test/jake/Jakefile
Expand Up @@ -4,11 +4,9 @@
/* eslint-disable no-console */

const migrateTests = require('./jakelib/migrate-test').taskList;
const seedTests = require('./jakelib/seed-test').taskList;

const tests = [
...migrateTests,
...seedTests
...migrateTests
];

task('default', tests, () => {
Expand Down
31 changes: 0 additions & 31 deletions test/jake/jakelib/migrate-test.js
Expand Up @@ -82,37 +82,6 @@ test('Create a migration file without client passed', (temp) =>
)
));

test('Run migrations', (temp) =>
new Promise((resolve, reject) =>
fs.writeFile(
temp + '/migrations/000_create_rule_table.js',
`
exports.up = (knex)=> knex.schema.createTable('rules', (table)=> {
table.string('name');
});
exports.down = (knex)=> knex.schema.dropTable('rules');
`,
(err) => (err ? reject(err) : resolve())
)
)
.then(() =>
assertExec(`${KNEX} migrate:latest \
--client=sqlite3 --connection=${temp}/db \
--migrations-directory=${temp}/migrations \
create_rule_table`)
)
.then(() => assertExec(`ls ${temp}/db`, 'Find the database file'))
.then(() => new sqlite3.Database(temp + '/db'))
.then(
(db) =>
new Promise((resolve, reject) =>
db.get('SELECT name FROM knex_migrations', function(err, row) {
err ? reject(err) : resolve(row);
})
)
)
.then((row) => assert.equal(row.name, '000_create_rule_table.js')));

test('run migrations without knexfile and with --migrations-table-name', (temp) =>
assertExec(`${KNEX} migrate:latest \
--client=sqlite3 --connection=${temp}/db \
Expand Down
70 changes: 0 additions & 70 deletions test/jake/jakelib/seed-test.js

This file was deleted.

0 comments on commit c14252d

Please sign in to comment.