2013-09-11 23:36:55 -04:00
|
|
|
var uuid = require('node-uuid');
|
|
|
|
|
2013-09-08 15:57:32 -04:00
|
|
|
module.exports = function(knex) {
|
|
|
|
|
2013-09-12 13:30:47 -04:00
|
|
|
describe('Inserts', function() {
|
2013-09-11 23:36:55 -04:00
|
|
|
|
|
|
|
it("should handle simple inserts", function() {
|
|
|
|
|
2013-09-12 13:30:47 -04:00
|
|
|
return knex('accounts').logMe().insert({
|
2013-09-11 23:36:55 -04:00
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User',
|
|
|
|
email:'test@example.com',
|
|
|
|
logins: 1,
|
|
|
|
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
|
|
|
created_at: new Date(),
|
|
|
|
updated_at: new Date()
|
|
|
|
}, 'id');
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle multi inserts', function() {
|
|
|
|
|
|
|
|
return knex('accounts')
|
2013-09-12 13:30:47 -04:00
|
|
|
.logMe()
|
2013-09-11 23:36:55 -04:00
|
|
|
.insert([{
|
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User',
|
|
|
|
email:'test2@example.com',
|
|
|
|
logins: 1,
|
|
|
|
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
|
|
|
created_at: new Date(),
|
|
|
|
updated_at: new Date()
|
|
|
|
}, {
|
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User',
|
|
|
|
email:'test3@example.com',
|
|
|
|
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
|
|
|
logins: 2,
|
|
|
|
created_at: new Date(),
|
|
|
|
updated_at: new Date()
|
|
|
|
}], 'id');
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow for using the `exec` interface', function(ok) {
|
|
|
|
|
|
|
|
knex('test_table_two').insert([{
|
|
|
|
account_id: 1,
|
|
|
|
details: 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.',
|
|
|
|
status: 0
|
|
|
|
}, {
|
|
|
|
account_id: 2,
|
|
|
|
details: 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.',
|
|
|
|
status: 1
|
|
|
|
}, {
|
|
|
|
account_id: 3,
|
|
|
|
details: '',
|
|
|
|
status: 1
|
|
|
|
}], 'id').exec(function(err, resp) {
|
|
|
|
if (err) return ok(err);
|
|
|
|
ok();
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should take hashes passed into insert and keep them in the correct order', function() {
|
|
|
|
|
2013-09-12 13:30:47 -04:00
|
|
|
return knex('accounts').logMe().insert([{
|
2013-09-11 23:36:55 -04:00
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User',
|
|
|
|
email:'test4@example.com',
|
|
|
|
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
|
|
|
logins: 2,
|
|
|
|
created_at: new Date(),
|
|
|
|
updated_at: new Date()
|
|
|
|
},{
|
|
|
|
first_name: 'Test',
|
|
|
|
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
|
|
|
logins: 2,
|
|
|
|
created_at: new Date(),
|
|
|
|
updated_at: new Date(),
|
|
|
|
last_name: 'User',
|
|
|
|
email:'test5@example.com'
|
|
|
|
}], 'id');
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-11-25 12:26:28 -05:00
|
|
|
it('will fail when multple inserts are made into a unique column', function() {
|
2013-09-11 23:36:55 -04:00
|
|
|
|
2013-11-25 12:26:28 -05:00
|
|
|
return knex('accounts')
|
2013-09-12 13:30:47 -04:00
|
|
|
.logMe()
|
2013-09-11 23:36:55 -04:00
|
|
|
.where('id', '>', 1)
|
|
|
|
.orWhere('x', 2)
|
|
|
|
.insert({
|
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User',
|
|
|
|
email:'test5@example.com',
|
|
|
|
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
|
|
|
logins: 2,
|
|
|
|
created_at: new Date(),
|
|
|
|
updated_at: new Date()
|
2013-11-25 12:26:28 -05:00
|
|
|
}, 'id').then(function() {
|
|
|
|
throw new Error('There should be a fail when multi-insert are made in unique col.');
|
|
|
|
}, function() {});
|
2013-09-11 23:36:55 -04:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should drop any where clause bindings', function() {
|
|
|
|
|
|
|
|
return knex('accounts')
|
2013-09-12 13:30:47 -04:00
|
|
|
.logMe()
|
2013-09-11 23:36:55 -04:00
|
|
|
.where('id', '>', 1)
|
|
|
|
.orWhere('x', 2)
|
|
|
|
.insert({
|
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User',
|
|
|
|
email:'test6@example.com',
|
|
|
|
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
|
|
|
logins: 2,
|
|
|
|
created_at: new Date(),
|
|
|
|
updated_at: new Date()
|
|
|
|
}, 'id');
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-11-25 12:26:28 -05:00
|
|
|
it('should not allow inserting invalid values into enum fields', function() {
|
2013-09-11 23:36:55 -04:00
|
|
|
|
2013-11-25 12:26:28 -05:00
|
|
|
return knex('datatype_test')
|
2013-09-12 13:30:47 -04:00
|
|
|
.logMe()
|
2013-09-11 23:36:55 -04:00
|
|
|
.insert({'enum_value': 'd'})
|
|
|
|
.then(function() {
|
|
|
|
// No errors happen in sqlite3, which doesn't have native support
|
|
|
|
// for the enum type.
|
2013-11-25 12:26:28 -05:00
|
|
|
if (knex.client.dialect !== 'sqlite3') {
|
|
|
|
throw new Error('There should be an error for invalid enum inserts');
|
|
|
|
}
|
|
|
|
}, function() {});
|
2013-09-11 23:36:55 -04:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-11-25 12:26:28 -05:00
|
|
|
it('should not allow invalid uuids in postgresql', function() {
|
2013-09-11 23:36:55 -04:00
|
|
|
|
2013-11-25 12:26:28 -05:00
|
|
|
return knex('datatype_test')
|
2013-09-11 23:36:55 -04:00
|
|
|
.insert({
|
|
|
|
enum_value: 'c',
|
2013-11-25 12:26:28 -05:00
|
|
|
uuid: uuid.v4()
|
|
|
|
}).then(function() {
|
2013-09-11 23:36:55 -04:00
|
|
|
return knex('datatype_test').insert({enum_value: 'c', uuid: 'test'});
|
|
|
|
}).then(function() {
|
|
|
|
// No errors happen in sqlite3 or mysql, which dont have native support
|
|
|
|
// for the uuid type.
|
2013-11-25 12:26:28 -05:00
|
|
|
if (knex.client.dialect === 'postgresql') {
|
|
|
|
throw new Error('There should be an error in postgresql for uuids');
|
|
|
|
}
|
|
|
|
}, function() {});
|
2013-09-11 23:36:55 -04:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not mutate the array passed in', function() {
|
|
|
|
|
|
|
|
var a = {enum_value: 'a', uuid: uuid.v4()};
|
|
|
|
var b = {enum_value: 'c', uuid: uuid.v4()};
|
|
|
|
var x = [a, b];
|
|
|
|
|
|
|
|
return knex('datatype_test')
|
|
|
|
.insert(x)
|
|
|
|
.then(function() {
|
|
|
|
expect(x).to.eql([a, b]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle empty inserts', function() {
|
2013-09-12 13:30:47 -04:00
|
|
|
|
|
|
|
return knex.schema.createTable('test_default_table', function(qb) {
|
|
|
|
qb.increments().primary();
|
|
|
|
qb.string('string').defaultTo('hello');
|
|
|
|
qb.tinyint('tinyint').defaultTo(0);
|
|
|
|
qb.text('text').nullable();
|
|
|
|
}).then(function() {
|
|
|
|
return knex('test_default_table').logMe().insert({}, 'id');
|
|
|
|
});
|
|
|
|
|
2013-09-11 23:36:55 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-09-08 15:57:32 -04:00
|
|
|
};
|