Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor more tests to use cli-testlab #3640

Merged
merged 2 commits into from Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original implementation didn't support sqlite paths to begin with drive letter, e. g. "c:/knex/migrations"

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: 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.