2014-09-01 17:18:45 +02:00
/*global describe, expect, it, d*/
'use strict' ;
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-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' ,
2014-08-20 09:34:32 +02:00
"insert into \"accounts\" (\"about\", \"created_at\", \"email\", \"first_name\", \"last_name\", \"logins\", \"updated_at\") values (?, ?, ?, ?, ?, ?, ?) returning ROWID into ?" ,
2014-08-11 12:25:39 +02:00
[ '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' ,
2014-08-20 09:34:32 +02:00
"begin execute immediate 'insert into \"accounts\" (\"about\", \"created_at\", \"email\", \"first_name\", \"last_name\", \"logins\", \"updated_at\") values (:1, :2, :3, :4, :5, :6, :7) returning ROWID into :8' using ?, ?, ?, ?, ?, ?, ?, out ?; execute immediate 'insert into \"accounts\" (\"about\", \"created_at\", \"email\", \"first_name\", \"last_name\", \"logins\", \"updated_at\") values (:1, :2, :3, :4, :5, :6, :7) returning ROWID into :8' using ?, ?, ?, ?, ?, ?, ?, out ?;end;" ,
2014-08-13 21:37:22 +02:00
[ 'Lorem ipsum Dolore labore incididunt enim.' , d , 'test2@example.com' , 'Test' , 'User' , 1 , d , function ( v ) { return v . toString ( ) === '[object ReturningHelper:id]' ; } ,
'Lorem ipsum Dolore labore incididunt enim.' , d , 'test3@example.com' , 'Test' , 'User' , 2 , d , function ( v ) { return v . toString ( ) === '[object ReturningHelper:id]' ; } ] ,
[ 2 , 3 ]
2014-08-11 12:25:39 +02:00
) ;
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
2014-08-21 23:39:12 +02:00
} ] , 'id' )
. testSql ( function ( tester ) {
tester (
'oracle' ,
"begin execute immediate 'insert into \"test_table_two\" (\"account_id\", \"details\", \"status\") values (:1, :2, :3) returning ROWID into :4' using ?, ?, ?, out ?; execute immediate 'insert into \"test_table_two\" (\"account_id\", \"details\", \"status\") values (:1, :2, :3) returning ROWID into :4' using ?, ?, ?, out ?; execute immediate 'insert into \"test_table_two\" (\"account_id\", \"details\", \"status\") values (:1, :2, :3) returning ROWID into :4' using ?, ?, ?, out ?;end;" ,
[
1 ,
'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:id]' ; } ,
2 ,
'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.' ,
1 ,
function ( v ) { return v . toString ( ) === '[object ReturningHelper:id]' ; } ,
3 ,
'' ,
1 ,
function ( v ) { return v . toString ( ) === '[object ReturningHelper:id]' ; }
] ,
[ 1 , 2 , 3 ]
) ;
2014-09-02 22:56:51 +02:00
} ) . exec ( function ( err ) {
2013-09-11 23:36:55 -04:00
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 ]
) ;
2014-08-21 23:39:12 +02:00
tester (
'oracle' ,
"begin execute immediate 'insert into \"accounts\" (\"about\", \"created_at\", \"email\", \"first_name\", \"last_name\", \"logins\", \"updated_at\") values (:1, :2, :3, :4, :5, :6, :7) returning ROWID into :8' using ?, ?, ?, ?, ?, ?, ?, out ?; execute immediate 'insert into \"accounts\" (\"about\", \"created_at\", \"email\", \"first_name\", \"last_name\", \"logins\", \"updated_at\") values (:1, :2, :3, :4, :5, :6, :7) returning ROWID into :8' using ?, ?, ?, ?, ?, ?, ?, out ?;end;" ,
[
'Lorem ipsum Dolore labore incididunt enim.' ,
d ,
'test4@example.com' ,
'Test' ,
'User' ,
2 ,
d ,
function ( v ) { return v . toString ( ) === '[object ReturningHelper:id]' ; } ,
'Lorem ipsum Dolore labore incididunt enim.' ,
d ,
'test5@example.com' ,
'Test' ,
'User' ,
2 ,
d ,
function ( v ) { return v . toString ( ) === '[object ReturningHelper:id]' ; }
] ,
[ 4 , 5 ]
) ;
2013-12-27 14:44:21 -05:00
} ) ;
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 ]
) ;
2014-08-21 23:39:12 +02:00
tester (
'oracle' ,
"insert into \"accounts\" (\"about\", \"created_at\", \"email\", \"first_name\", \"last_name\", \"logins\", \"updated_at\") values (?, ?, ?, ?, ?, ?, ?) returning ROWID into ?" ,
[ 'Lorem ipsum Dolore labore incididunt enim.' , d , 'test5@example.com' , 'Test' , 'User' , 2 , d , function ( v ) { return v . toString ( ) === '[object ReturningHelper:id]' ; } ]
) ;
2013-12-27 14:44:21 -05:00
} )
. 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 ]
) ;
2014-08-21 23:39:12 +02:00
tester (
'oracle' ,
"insert into \"accounts\" (\"about\", \"created_at\", \"email\", \"first_name\", \"last_name\", \"logins\", \"updated_at\") values (?, ?, ?, ?, ?, ?, ?) returning ROWID into ?" ,
[ 'Lorem ipsum Dolore labore incididunt enim.' , d , 'test6@example.com' , 'Test' , 'User' , 2 , d , function ( v ) { return v . toString ( ) === '[object ReturningHelper:id]' ; } ] ,
[ 7 ]
) ;
2013-12-27 14:44:21 -05:00
} ) ;
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 ]
) ;
2014-08-21 23:39:12 +02:00
tester (
'oracle' ,
'insert into "datatype_test" ("enum_value") values (?)' ,
[ 'd' ]
) ;
2013-12-27 14:44:21 -05:00
} )
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' ,
2014-08-20 09:34:32 +02:00
"insert into \"test_default_table\" (\"id\") values (default) returning ROWID into ?" ,
2014-08-11 12:25:39 +02:00
[ 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
2014-09-08 16:02:46 +02:00
it ( 'should handle empty arrays inserts' , function ( ) {
return knex . schema
. createTable ( 'test_default_table2' , 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_table2' ) . insert ( [ { } ] , 'id' ) . testSql ( function ( tester ) {
tester (
'mysql' ,
'insert into `test_default_table2` () values ()' ,
[ ] ,
[ 1 ]
) ;
tester (
'postgresql' ,
'insert into "test_default_table2" default values returning "id"' ,
[ ] ,
[ 1 ]
) ;
tester (
'sqlite3' ,
'insert into "test_default_table2" default values' ,
[ ] ,
[ 1 ]
) ;
tester (
'oracle' ,
"insert into \"test_default_table2\" (\"id\") values (default) returning ROWID into ?" ,
[ function ( v ) { return v . toString ( ) === '[object ReturningHelper:id]' ; } ] ,
[ 1 ]
) ;
} ) ;
} ) ;
} ) ;
it ( 'should handle multiple default inserts with returning only' , function ( ) {
if ( knex . client . dialect === 'sqlite3' ) {
console . log ( 'not tested for sqlite3' ) ;
return ;
}
return knex . schema
. createTable ( 'test_default_table3' , 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_table3' ) . insert ( [ { } , { } ] , 'id' ) . testSql ( function ( tester ) {
tester (
'mysql' ,
'insert into `test_default_table3` () values (), ()' ,
[ ] ,
[ 1 ]
) ;
tester (
'postgresql' ,
'insert into "test_default_table3" ("id") values (default), (default) returning "id"' ,
[ ] ,
[ 1 , 2 ]
) ;
tester (
'oracle' ,
"begin execute immediate 'insert into \"test_default_table3\" (\"id\") values (default) returning ROWID into :1' using out ?; execute immediate 'insert into \"test_default_table3\" (\"id\") values (default) returning ROWID into :1' using out ?;end;" ,
[ function ( v ) { return v . toString ( ) === '[object ReturningHelper:id]' ; } , function ( v ) { return v . toString ( ) === '[object ReturningHelper:id]' ; } ] ,
[ 1 , 2 ]
) ;
} ) ;
} ) . then ( function ( ) {
return knex ( 'test_default_table3' ) . then ( function ( rows ) {
expect ( rows . length ) . to . equal ( 2 ) ;
} ) ;
} ) ;
} ) ;
2014-08-13 21:37:22 +02:00
it ( 'should take an array of columns to return in oracle or postgres' , function ( ) {
2013-12-10 13:33:49 -05:00
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
} ;
2014-08-20 09:34:32 +02:00
return knex ( 'test_table_two' ) . insert ( insertData , [ 'account_id' , 'details' ] ) . testSql ( function ( tester ) {
2013-12-27 14:44:21 -05:00
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' ,
2014-08-20 09:34:32 +02:00
"insert into \"test_table_two\" (\"account_id\", \"details\", \"status\") values (?, ?, ?) returning ROWID into ?" ,
2014-08-11 12:25:39 +02:00
[
10 ,
'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.' ,
0 ,
2014-08-20 09:34:32 +02:00
function ( v ) { return v . toString ( ) === '[object ReturningHelper:account_id:details]' ; }
2014-08-11 12:25:39 +02:00
] ,
2014-08-13 21:37:22 +02:00
[ {
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.'
} ]
2014-08-11 12:25:39 +02:00
) ;
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 ) ;
}
} ) ;
} ) ;
2014-08-20 09:34:32 +02:00
it ( 'should allow a * for returning in postgres and oracle' , function ( ) {
2013-12-10 13:33:49 -05:00
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
} ;
2014-08-13 21:37:22 +02:00
var returningColumn = '*' ;
return knex ( 'test_table_two' ) . insert ( insertData , returningColumn ) . testSql ( function ( tester ) {
2013-12-27 14:44:21 -05:00
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
} ]
) ;
2014-08-20 09:34:32 +02:00
tester (
'oracle' ,
"insert into \"test_table_two\" (\"account_id\", \"details\", \"status\") values (?, ?, ?) returning ROWID 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:*]' ; }
] ,
[ {
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
} ]
) ;
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 ( 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
} ;