knex/test/unit/seed/seeder.js

100 lines
2.3 KiB
JavaScript
Raw Normal View History

/*eslint no-var:0, indent:0, max-len:0 */
'use strict';
const { expect } = require('chai');
const mockFs = require('mock-fs');
const knex = require('../../../knex');
2020-04-19 00:40:23 +02:00
describe('Seeder.loadExtensions', function () {
const config = {
client: 'postgres',
connection: {
user: 'postgres',
password: '',
host: '127.0.0.1',
database: 'knex_test',
},
seeds: {
directory: 'test/integration/seed/seeds',
},
};
let seeder;
2020-04-19 00:40:23 +02:00
before(function () {
mockFs({
'test/integration/seed/seeds': {
'co-seed.co': 'co seed content',
'coffee-seed.coffee': 'coffee seed content',
'eg-seed.eg': 'eg seed content',
'iced-seed.iced': 'iced seed content',
'js-seed.js': 'js seed content',
'litcoffee-seed.litcoffee': 'litcoffee seed content',
'ls-seed.ls': 'ls seed content',
'ts-seed.ts': 'ts seed content',
'useless.txt': 'i am not a seed',
},
});
});
2020-04-19 00:40:23 +02:00
after(function () {
mockFs.restore();
});
2020-04-19 00:40:23 +02:00
beforeEach(function () {
seeder = knex(config).seed;
});
2020-04-19 00:40:23 +02:00
it('should include all supported extensions by default', function () {
return seeder._listAll().then(function (list) {
expect(list).to.eql([
'co-seed.co',
'coffee-seed.coffee',
'eg-seed.eg',
'iced-seed.iced',
'js-seed.js',
'litcoffee-seed.litcoffee',
'ls-seed.ls',
'ts-seed.ts',
]);
});
});
2020-04-19 00:40:23 +02:00
it('should list only files with specified extensions', function () {
return seeder
._listAll({ loadExtensions: ['.ts', '.js'] })
2020-04-19 00:40:23 +02:00
.then(function (list) {
expect(list).to.eql(['js-seed.js', 'ts-seed.ts']);
});
});
});
2020-04-19 00:40:23 +02:00
describe('Seeder._waterfallBatch', function () {
const config = {
client: 'postgres',
connection: {
user: 'postgres',
password: '',
host: '127.0.0.1',
database: 'knex_test',
},
seeds: {
directory: 'test/unit/seed/test',
},
};
let seeder;
2020-04-19 00:40:23 +02:00
beforeEach(function () {
seeder = knex(config).seed;
});
it('should throw an error with correct file name', (done) => {
seeder._waterfallBatch(['1-first.js', '2-second.js']).catch((error) => {
expect(error.message).to.match(
/^Error while executing .*1-first.js" seed: throwing in first file$/
);
done();
});
});
});