2013-09-11 23:36:55 -04:00
var uuid = require ( 'node-uuid' ) ;
2013-12-10 13:33:49 -05:00
var _ = require ( 'lodash' ) ;
2013-09-11 23:36:55 -04:00
2013-12-27 14:44:21 -05:00
function stackSql ( stack , sql ) {
expect ( stack . sql ) . to . equal ( sql ) ;
}
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-12-27 14:44:21 -05:00
return knex ( 'accounts' ) . 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.' ,
2013-12-27 14:44:21 -05:00
created _at : d ,
updated _at : d
} , 'id' ) . testSql ( function ( tester ) {
tester (
'mysql' ,
'insert into `accounts` (`about`, `created_at`, `email`, `first_name`, `last_name`, `logins`, `updated_at`) values (?, ?, ?, ?, ?, ?, ?)' ,
[ 'Lorem ipsum Dolore labore incididunt enim.' , d , 'test@example.com' , 'Test' , 'User' , 1 , d ] ,
[ 1 ]
) ;
tester (
'postgresql' ,
'insert into "accounts" ("about", "created_at", "email", "first_name", "last_name", "logins", "updated_at") values (?, ?, ?, ?, ?, ?, ?) returning "id"' ,
[ 'Lorem ipsum Dolore labore incididunt enim.' , d , 'test@example.com' , 'Test' , 'User' , 1 , d ] ,
[ '1' ]
) ;
tester (
'sqlite3' ,
'insert into "accounts" ("about", "created_at", "email", "first_name", "last_name", "logins", "updated_at") values (?, ?, ?, ?, ?, ?, ?)' ,
[ 'Lorem ipsum Dolore labore incididunt enim.' , d , 'test@example.com' , 'Test' , 'User' , 1 , d ] ,
[ 1 ]
) ;
2014-08-11 12:25:39 +02:00
tester (
'oracle' ,
'insert into "accounts" ("about", "created_at", "email", "first_name", "last_name", "logins", "updated_at") values (?, ?, ?, ?, ?, ?, ?) returning "id" into ?' ,
[ 'Lorem ipsum Dolore labore incididunt enim.' , d , 'test@example.com' , 'Test' , 'User' , 1 , d , function ( v ) { return v . toString ( ) === '[object ReturningHelper:id]' ; } ] ,
[ 1 ]
) ;
2013-12-27 14:44:21 -05:00
} ) ;
2013-09-11 23:36:55 -04:00
} ) ;
it ( 'should handle multi inserts' , function ( ) {
return knex ( 'accounts' )
. insert ( [ {
first _name : 'Test' ,
last _name : 'User' ,
email : 'test2@example.com' ,
logins : 1 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
2013-12-27 14:44:21 -05:00
created _at : d ,
updated _at : d
2013-09-11 23:36:55 -04:00
} , {
first _name : 'Test' ,
last _name : 'User' ,
email : 'test3@example.com' ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
logins : 2 ,
2013-12-27 14:44:21 -05:00
created _at : d ,
updated _at : d
} ] , 'id' ) . testSql ( function ( tester ) {
tester (
'mysql' ,
'insert into `accounts` (`about`, `created_at`, `email`, `first_name`, `last_name`, `logins`, `updated_at`) values (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?)' ,
[ 'Lorem ipsum Dolore labore incididunt enim.' , d , 'test2@example.com' , 'Test' , 'User' , 1 , d , 'Lorem ipsum Dolore labore incididunt enim.' , d , 'test3@example.com' , 'Test' , 'User' , 2 , d ] ,
[ 2 ]
) ;
tester (
'postgresql' ,
'insert into "accounts" ("about", "created_at", "email", "first_name", "last_name", "logins", "updated_at") values (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?) returning "id"' ,
[ 'Lorem ipsum Dolore labore incididunt enim.' , d , 'test2@example.com' , 'Test' , 'User' , 1 , d , 'Lorem ipsum Dolore labore incididunt enim.' , d , 'test3@example.com' , 'Test' , 'User' , 2 , d ] ,
[ '2' , '3' ]
) ;
tester (
'sqlite3' ,
'insert into "accounts" ("about", "created_at", "email", "first_name", "last_name", "logins", "updated_at") select ? as "about", ? as "created_at", ? as "email", ? as "first_name", ? as "last_name", ? as "logins", ? as "updated_at" union all select ? as "about", ? as "created_at", ? as "email", ? as "first_name", ? as "last_name", ? as "logins", ? as "updated_at"' ,
[ 'Lorem ipsum Dolore labore incididunt enim.' , d , 'test2@example.com' , 'Test' , 'User' , 1 , d , 'Lorem ipsum Dolore labore incididunt enim.' , d , 'test3@example.com' , 'Test' , 'User' , 2 , d ] ,
[ 3 ]
) ;
2014-08-11 12:25:39 +02:00
tester (
'oracle' ,
"insert all into \"accounts\" (\"about\", \"created_at\", \"email\", \"first_name\", \"last_name\", \"logins\", \"updated_at\") values (?, ?, ?, ?, ?, ?, ?) into \"accounts\" (\"about\", \"created_at\", \"email\", \"first_name\", \"last_name\", \"logins\", \"updated_at\") values (?, ?, ?, ?, ?, ?, ?) select 1 from dual" ,
[ 'Lorem ipsum Dolore labore incididunt enim.' , d , 'test2@example.com' , 'Test' , 'User' , 1 , d , 'Lorem ipsum Dolore labore incididunt enim.' , d , 'test3@example.com' , 'Test' , 'User' , 2 , d ]
) ;
2013-12-27 14:44:21 -05:00
} ) ;
2013-09-11 23:36:55 -04:00
} ) ;
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-12-27 14:44:21 -05:00
return knex ( 'accounts' ) . 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 ,
2013-12-27 14:44:21 -05:00
created _at : d ,
updated _at : d
2013-09-11 23:36:55 -04:00
} , {
first _name : 'Test' ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
logins : 2 ,
2013-12-27 14:44:21 -05:00
created _at : d ,
updated _at : d ,
2013-09-11 23:36:55 -04:00
last _name : 'User' ,
email : 'test5@example.com'
2013-12-27 14:44:21 -05:00
} ] , 'id' ) . testSql ( function ( tester ) {
tester (
'mysql' ,
'insert into `accounts` (`about`, `created_at`, `email`, `first_name`, `last_name`, `logins`, `updated_at`) values (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?)' ,
[ 'Lorem ipsum Dolore labore incididunt enim.' , d , 'test4@example.com' , 'Test' , 'User' , 2 , d , 'Lorem ipsum Dolore labore incididunt enim.' , d , 'test5@example.com' , 'Test' , 'User' , 2 , d ] ,
[ 4 ]
) ;
tester (
'postgresql' ,
'insert into "accounts" ("about", "created_at", "email", "first_name", "last_name", "logins", "updated_at") values (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?) returning "id"' ,
[ 'Lorem ipsum Dolore labore incididunt enim.' , d , 'test4@example.com' , 'Test' , 'User' , 2 , d , 'Lorem ipsum Dolore labore incididunt enim.' , d , 'test5@example.com' , 'Test' , 'User' , 2 , d ] ,
[ '4' , '5' ]
) ;
tester (
'sqlite3' ,
'insert into "accounts" ("about", "created_at", "email", "first_name", "last_name", "logins", "updated_at") select ? as "about", ? as "created_at", ? as "email", ? as "first_name", ? as "last_name", ? as "logins", ? as "updated_at" union all select ? as "about", ? as "created_at", ? as "email", ? as "first_name", ? as "last_name", ? as "logins", ? as "updated_at"' ,
[ 'Lorem ipsum Dolore labore incididunt enim.' , d , 'test4@example.com' , 'Test' , 'User' , 2 , d , 'Lorem ipsum Dolore labore incididunt enim.' , d , 'test5@example.com' , 'Test' , 'User' , 2 , d ] ,
[ 5 ]
) ;
} ) ;
2013-09-11 23:36:55 -04:00
} ) ;
2013-12-27 14:44:21 -05:00
it ( 'will fail when multiple 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-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 ,
2013-12-27 14:44:21 -05:00
created _at : d ,
updated _at : d
} , 'id' )
. testSql ( function ( tester ) {
tester (
'mysql' ,
'insert into `accounts` (`about`, `created_at`, `email`, `first_name`, `last_name`, `logins`, `updated_at`) values (?, ?, ?, ?, ?, ?, ?)' ,
[ 'Lorem ipsum Dolore labore incididunt enim.' , d , 'test5@example.com' , 'Test' , 'User' , 2 , d ]
) ;
tester (
'postgresql' ,
'insert into "accounts" ("about", "created_at", "email", "first_name", "last_name", "logins", "updated_at") values (?, ?, ?, ?, ?, ?, ?) returning "id"' ,
[ 'Lorem ipsum Dolore labore incididunt enim.' , d , 'test5@example.com' , 'Test' , 'User' , 2 , d ]
) ;
tester (
'sqlite3' ,
'insert into "accounts" ("about", "created_at", "email", "first_name", "last_name", "logins", "updated_at") values (?, ?, ?, ?, ?, ?, ?)' ,
[ 'Lorem ipsum Dolore labore incididunt enim.' , d , 'test5@example.com' , 'Test' , 'User' , 2 , d ]
) ;
} )
. then ( function ( ) {
2013-11-25 12:26:28 -05:00
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' )
. 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 ,
2013-12-27 14:44:21 -05:00
created _at : d ,
updated _at : d
} , 'id' ) . testSql ( function ( tester ) {
tester (
'mysql' ,
'insert into `accounts` (`about`, `created_at`, `email`, `first_name`, `last_name`, `logins`, `updated_at`) values (?, ?, ?, ?, ?, ?, ?)' ,
[ 'Lorem ipsum Dolore labore incididunt enim.' , d , 'test6@example.com' , 'Test' , 'User' , 2 , d ] ,
[ 7 ]
) ;
tester (
'postgresql' ,
'insert into "accounts" ("about", "created_at", "email", "first_name", "last_name", "logins", "updated_at") values (?, ?, ?, ?, ?, ?, ?) returning "id"' ,
[ 'Lorem ipsum Dolore labore incididunt enim.' , d , 'test6@example.com' , 'Test' , 'User' , 2 , d ] ,
[ '7' ]
) ;
tester (
'sqlite3' ,
'insert into "accounts" ("about", "created_at", "email", "first_name", "last_name", "logins", "updated_at") values (?, ?, ?, ?, ?, ?, ?)' ,
[ 'Lorem ipsum Dolore labore incididunt enim.' , d , 'test6@example.com' , 'Test' , 'User' , 2 , d ] ,
[ 6 ]
) ;
} ) ;
2013-09-11 23:36:55 -04:00
} ) ;
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-11 23:36:55 -04:00
. insert ( { 'enum_value' : 'd' } )
2013-12-27 14:44:21 -05:00
. testSql ( function ( tester ) {
tester (
'mysql' ,
'insert into `datatype_test` (`enum_value`) values (?)' ,
[ 'd' ]
) ;
tester (
'postgresql' ,
'insert into "datatype_test" ("enum_value") values (?)' ,
[ 'd' ]
) ;
tester (
'sqlite3' ,
'insert into "datatype_test" ("enum_value") values (?)' ,
[ 'd' ] ,
[ 1 ]
) ;
} )
2013-09-11 23:36:55 -04:00
. 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
2013-12-27 14:44:21 -05: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' ) . insert ( { } , 'id' ) . testSql ( function ( tester ) {
tester (
'mysql' ,
'insert into `test_default_table` () values ()' ,
[ ] ,
[ 1 ]
) ;
tester (
'postgresql' ,
'insert into "test_default_table" default values returning "id"' ,
[ ] ,
[ 1 ]
) ;
tester (
'sqlite3' ,
'insert into "test_default_table" default values' ,
[ ] ,
[ 1 ]
) ;
2014-08-11 12:25:39 +02:00
tester (
'oracle' ,
'insert into "test_default_table" ("id") values (default) returning "id" into ?' ,
[ function ( v ) { return v . toString ( ) === '[object ReturningHelper:id]' ; } ] ,
[ 1 ]
) ;
2013-12-27 14:44:21 -05:00
} ) ;
} ) ;
2013-12-10 13:33:49 -05:00
} ) ;
2013-09-12 13:30:47 -04:00
2013-12-10 13:33:49 -05:00
it ( 'should take an array of columns to return in postgres' , function ( ) {
var insertData = {
account _id : 10 ,
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
} ;
var returning = [ 'account_id' , 'details' ] ;
2014-08-11 12:25:39 +02:00
if ( knex . client . dialect === 'oracle' ) {
returning = 'account_id' ;
}
2013-12-27 14:44:21 -05:00
return knex ( 'test_table_two' ) . insert ( insertData , returning ) . testSql ( function ( tester ) {
tester (
'mysql' ,
'insert into `test_table_two` (`account_id`, `details`, `status`) values (?, ?, ?)' ,
[ 10 , 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.' , 0 ] ,
[ 4 ]
) ;
tester (
'postgresql' ,
'insert into "test_table_two" ("account_id", "details", "status") values (?, ?, ?) returning "account_id", "details"' ,
[ 10 , 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.' , 0 ] ,
[ {
account _id : 10 ,
details : 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.'
} ]
) ;
tester (
'sqlite3' ,
'insert into "test_table_two" ("account_id", "details", "status") values (?, ?, ?)' ,
[ 10 , 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.' , 0 ] ,
[ 4 ]
) ;
2014-08-11 12:25:39 +02:00
tester (
'oracle' ,
'insert into "test_table_two" ("account_id", "details", "status") values (?, ?, ?) returning "account_id" into ?' ,
[
10 ,
'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.' ,
0 ,
function ( v ) { return v . toString ( ) === '[object ReturningHelper:account_id]' ; }
// function (v) {return v.toString() === '[object ReturningHelper:details]';},
] ,
[ 10 ]
) ;
2013-12-27 14:44:21 -05:00
} ) . then ( function ( rows ) {
2013-12-10 13:33:49 -05:00
expect ( rows . length ) . to . equal ( 1 ) ;
if ( knex . client . dialect === 'postgresql' ) {
expect ( _ . keys ( rows [ 0 ] ) . length ) . to . equal ( 2 ) ;
expect ( rows [ 0 ] . account _id ) . to . equal ( insertData . account _id ) ;
expect ( rows [ 0 ] . details ) . to . equal ( insertData . details ) ;
}
} ) ;
} ) ;
it ( 'should allow a * for returning in postgres' , function ( ) {
var insertData = {
account _id : 10 ,
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
} ;
2013-12-27 14:44:21 -05:00
return knex ( 'test_table_two' ) . insert ( insertData , '*' ) . testSql ( function ( tester ) {
tester (
'postgresql' ,
'insert into "test_table_two" ("account_id", "details", "status") values (?, ?, ?) returning *' ,
[ 10 , 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.' , 0 ] ,
[ {
id : 5 ,
account _id : 10 ,
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 ,
json _data : null
} ]
) ;
} ) . then ( function ( rows ) {
2013-12-10 13:33:49 -05:00
expect ( rows . length ) . to . equal ( 1 ) ;
if ( knex . client . dialect === 'postgresql' ) {
expect ( _ . keys ( rows [ 0 ] ) . length ) . to . equal ( 5 ) ;
expect ( rows [ 0 ] . account _id ) . to . equal ( insertData . account _id ) ;
expect ( rows [ 0 ] . details ) . to . equal ( insertData . details ) ;
expect ( rows [ 0 ] . status ) . to . equal ( insertData . status ) ;
expect ( rows [ 0 ] . json _data ) . to . equal ( null ) ;
}
} ) ;
2013-09-11 23:36:55 -04:00
} ) ;
} ) ;
2014-04-16 02:50:19 -04:00
} ;