2018-06-29 10:47:06 +03:00
|
|
|
'use strict';
|
|
|
|
const tape = require('tape')
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Collection of tests for making sure that certain features are cross database compatible
|
|
|
|
*/
|
|
|
|
module.exports = function(knex) {
|
|
|
|
|
2018-07-08 14:10:51 +03:00
|
|
|
const driverName = knex.client.driverName;
|
2018-06-29 10:47:06 +03:00
|
|
|
|
2018-07-08 14:10:51 +03:00
|
|
|
if (driverName === 'oracledb') {
|
2018-06-29 10:47:06 +03:00
|
|
|
// TODO: FIX ORACLE TO WORK THE SAME WAY WITH OTHER DIALECTS IF POSSIBLE
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-08 14:10:51 +03:00
|
|
|
tape(driverName + ' - crossdb compatibility: setup test table', function(t) {
|
2018-06-29 10:47:06 +03:00
|
|
|
knex.schema.dropTableIfExists('test_table')
|
|
|
|
.createTable('test_table', function(t) {
|
|
|
|
t.integer('id');
|
|
|
|
t.string('first');
|
|
|
|
t.string('second');
|
|
|
|
t.string('third').unique();
|
|
|
|
t.unique(['first', 'second']);
|
|
|
|
})
|
|
|
|
.finally(function() {
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-07-08 14:10:51 +03:00
|
|
|
tape(driverName + ' - crossdb compatibility: table may have multiple nulls in unique constrainted column', function (t) {
|
2018-06-29 10:47:06 +03:00
|
|
|
t.plan(3);
|
|
|
|
|
|
|
|
knex('test_table').insert([
|
|
|
|
{ third: 'foo' },
|
|
|
|
{ third: 'foo' }
|
|
|
|
]).catch(err => {
|
|
|
|
t.assert(true, 'unique constraint prevents adding rows');
|
|
|
|
return knex('test_table').insert([
|
|
|
|
{ first: 'foo2', second: 'bar2' },
|
|
|
|
{ first: 'foo2', second: 'bar2' }
|
|
|
|
]);
|
|
|
|
}).catch(err => {
|
|
|
|
t.assert(true, 'two column unique constraint prevents adding rows');
|
|
|
|
|
|
|
|
// even one null makes index to not match, thus allows adding the row
|
|
|
|
return knex('test_table').insert([
|
|
|
|
{ first: 'fo', second: null, third: null },
|
|
|
|
{ first: 'fo', second: null, third: null },
|
|
|
|
{ first: null, second: 'fo', third: null },
|
|
|
|
{ first: null, second: 'fo', third: null },
|
|
|
|
{ first: null, second: null, third: null },
|
|
|
|
]);
|
|
|
|
}).then(() => {
|
|
|
|
return knex('test_table');
|
|
|
|
}).then(res => {
|
|
|
|
t.assert(res.length == 5, 'multiple rows with nulls could be added despite of unique constraints')
|
|
|
|
}).finally(() => {
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-07-08 14:10:51 +03:00
|
|
|
tape(driverName + ' - create and drop index works in different cases', t => {
|
2018-06-29 10:47:06 +03:00
|
|
|
t.plan(1);
|
|
|
|
knex.schema.dropTableIfExists('test_table_drop_unique')
|
|
|
|
.createTable('test_table_drop_unique', t => {
|
|
|
|
t.integer('id');
|
|
|
|
t.string('first');
|
|
|
|
t.string('second');
|
|
|
|
t.string('third').unique();
|
|
|
|
t.string('fourth');
|
|
|
|
t.unique(['first', 'second']);
|
|
|
|
t.unique('fourth');
|
|
|
|
}).alterTable('test_table_drop_unique', t => {
|
|
|
|
t.dropUnique('third');
|
|
|
|
t.dropUnique('fourth');
|
|
|
|
t.dropUnique(['first', 'second']);
|
|
|
|
}).alterTable('test_table_drop_unique', t => {
|
|
|
|
t.unique(['first', 'second']);
|
|
|
|
t.unique('third');
|
|
|
|
t.unique('fourth');
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
t.assert(true, 'Creating / dropping / creating unique constraint was a success');
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
t.end();
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|