209 lines
7.4 KiB
JavaScript
Raw Normal View History

/*global describe, expect, it, d*/
'use strict';
module.exports = function(knex) {
2013-09-12 13:30:47 -04:00
describe('Updates', function () {
2013-09-13 10:24:39 -04:00
it('should handle updates', function() {
return knex('accounts')
.where('id', 1)
.update({
first_name: 'User',
last_name: 'Test',
email:'test100@example.com'
2013-12-27 14:44:21 -05:00
}).testSql(function(tester) {
tester(
'mysql',
'update `accounts` set `first_name` = ?, `last_name` = ?, `email` = ? where `id` = ?',
['User','Test','test100@example.com',1],
2013-12-27 14:44:21 -05:00
1
);
tester(
'postgresql',
'update "accounts" set "first_name" = ?, "last_name" = ?, "email" = ? where "id" = ?',
['User','Test','test100@example.com',1],
2013-12-27 14:44:21 -05:00
1
);
Add redshift support without changing cli or package.json (#2233) * Add a Redshift dialect that inherits from Postgres. * Turn .index() and .dropIndex() into no-ops with warnings in the Redshift dialect. * Update the Redshift dialect to be compatible with master. * Update package.json * Disable liftoff cli * Remove the CLI * Add lib to the repo * Allow the escaping of named bindings. * Update dist * Update the Redshift dialect’s instantiation of the query and column compilers. * Update the distribution * Fix a merge conflict * Take lib back out * Trying to bring back in line with tgreisser/knex * Add npm 5 package-lock * Bring cli.js back in line * Bring cli.js back in line * Progress commit on redshift integration tests * Revert "Progress commit on redshift integration tests" This reverts commit 207e31635c638853dec54ce0580d34559ba5a54c. * Progress commit * Working not null on primary columns in createTable * Working redshift unit tests * Working unit and integration tests, still need to fix migration tests * Brought datatypes more in line with what redshift actually supports * Added query compiler unit tests * Add a hacky returning clause for redshift ugh * Working migration integration tests * Working insert integration tests * Allow multiple insert returning values * Working select integration tests * Working join integration tests * Working aggregate integration tests * All integration suite tests working * Put docker index for reconnect tests back * Redshift does not support insert...returning, there does not seem to be a way around that, therefore accept it and test accordingly * Leave redshift integration tests in place, but do not run them by default * Fix mysql order by test * Fix more tests * Change test db name to knex_test for consistency * Address PR comments
2018-02-03 08:33:02 -05:00
tester(
'pg-redshift',
'update "accounts" set "first_name" = ?, "last_name" = ?, "email" = ? where "id" = ?',
['User','Test','test100@example.com',1],
1
);
2013-12-27 14:44:21 -05:00
tester(
'sqlite3',
'update `accounts` set `first_name` = ?, `last_name` = ?, `email` = ? where `id` = ?',
['User','Test','test100@example.com',1],
2013-12-27 14:44:21 -05:00
1
);
tester(
'mssql',
'update [accounts] set [first_name] = ?, [last_name] = ?, [email] = ? where [id] = ?;select @@rowcount',
['User','Test','test100@example.com',1],
1
);
});
});
it('should allow for null updates', function() {
return knex('accounts')
.where('id', 1000)
.update({
email:'test100@example.com',
first_name: null,
last_name: 'Test'
}).testSql(function(tester) {
tester(
'mysql',
'update `accounts` set `email` = ?, `first_name` = ?, `last_name` = ? where `id` = ?',
['test100@example.com', null, 'Test', 1000],
0
);
tester(
'mssql',
'update [accounts] set [email] = ?, [first_name] = ?, [last_name] = ? where [id] = ?;select @@rowcount',
['test100@example.com', null, 'Test', 1000],
0
);
});
});
2013-11-20 09:17:08 -05:00
it('should increment a value', function() {
return knex('accounts').select('logins').where('id', 1).then(function(accounts) {
return knex('accounts').where('id', 1).increment('logins').then(function(rowsAffected) {
expect(rowsAffected).to.equal(1);
return knex('accounts').select('logins').where('id', 1);
}).then(function(accounts2) {
expect(accounts[0].logins + 1).to.equal(accounts2[0].logins);
2013-11-20 09:17:08 -05:00
});
});
});
it('should increment a negative value', function() {
return knex('accounts').select('logins').where('id', 1).then(function(accounts) {
return knex('accounts').where('id', 1).increment('logins', -2).then(function(rowsAffected) {
expect(rowsAffected).to.equal(1);
return knex('accounts').select('logins').where('id', 1);
}).then(function(accounts2) {
expect(accounts[0].logins - 2).to.equal(accounts2[0].logins);
});
});
});
2013-11-20 09:17:08 -05:00
it('should increment a float value', function() {
return knex('accounts').select('balance').where('id', 1).then(function(accounts) {
return knex('accounts').where('id', 1).increment('balance', 22.53).then(function(rowsAffected) {
expect(rowsAffected).to.equal(1);
return knex('accounts').select('balance').where('id', 1);
}).then(function(accounts2) {
expect(accounts[0].balance + 22.53).to.be.closeTo(accounts2[0].balance, 0.001);
});
});
});
2013-11-20 09:17:08 -05:00
it('should decrement a value', function() {
return knex('accounts').select('logins').where('id', 1).then(function(accounts) {
return knex('accounts').where('id', 1).decrement('logins').then(function(rowsAffected) {
expect(rowsAffected).to.equal(1);
return knex('accounts').select('logins').where('id', 1);
}).then(function(accounts2) {
expect(accounts[0].logins - 1).to.equal(accounts2[0].logins);
2013-11-20 09:17:08 -05:00
});
});
});
2013-11-20 09:17:08 -05:00
it('should decrement a negative value', function() {
return knex('accounts').select('logins').where('id', 1).then(function(accounts) {
return knex('accounts').where('id', 1).decrement('logins', -2).then(function(rowsAffected) {
expect(rowsAffected).to.equal(1);
return knex('accounts').select('logins').where('id', 1);
}).then(function(accounts2) {
expect(accounts[0].logins + 2).to.equal(accounts2[0].logins);
});
});
2013-11-20 09:17:08 -05:00
});
it('should decrement a float value', function() {
return knex('accounts').select('balance').where('id', 1).then(function(accounts) {
return knex('accounts').where('id', 1).decrement('balance', 10.29).then(function(rowsAffected) {
expect(rowsAffected).to.equal(1);
return knex('accounts').select('balance').where('id', 1);
}).then(function(accounts2) {
expect(accounts[0].balance - 10.29).to.be.closeTo(accounts2[0].balance, 0.001);
});
});
});
it('should allow returning for updates in postgresql', function() {
2013-12-27 14:44:21 -05:00
return knex('accounts').where('id', 1).update({
email:'test100@example.com',
first_name: 'UpdatedUser',
last_name: 'UpdatedTest'
2013-12-27 14:44:21 -05:00
}, '*').testSql(function(tester) {
tester(
'mysql',
'update `accounts` set `email` = ?, `first_name` = ?, `last_name` = ? where `id` = ?',
['test100@example.com','UpdatedUser','UpdatedTest',1],
1
);
tester(
'postgresql',
'update "accounts" set "email" = ?, "first_name" = ?, "last_name" = ? where "id" = ? returning *',
['test100@example.com','UpdatedUser','UpdatedTest',1],
[{
id: '1',
first_name: 'UpdatedUser',
last_name: 'UpdatedTest',
email: 'test100@example.com',
logins: 1,
about: 'Lorem ipsum Dolore labore incididunt enim.',
created_at: d,
updated_at: d,
phone: null
}]
);
tester(
Add redshift support without changing cli or package.json (#2233) * Add a Redshift dialect that inherits from Postgres. * Turn .index() and .dropIndex() into no-ops with warnings in the Redshift dialect. * Update the Redshift dialect to be compatible with master. * Update package.json * Disable liftoff cli * Remove the CLI * Add lib to the repo * Allow the escaping of named bindings. * Update dist * Update the Redshift dialect’s instantiation of the query and column compilers. * Update the distribution * Fix a merge conflict * Take lib back out * Trying to bring back in line with tgreisser/knex * Add npm 5 package-lock * Bring cli.js back in line * Bring cli.js back in line * Progress commit on redshift integration tests * Revert "Progress commit on redshift integration tests" This reverts commit 207e31635c638853dec54ce0580d34559ba5a54c. * Progress commit * Working not null on primary columns in createTable * Working redshift unit tests * Working unit and integration tests, still need to fix migration tests * Brought datatypes more in line with what redshift actually supports * Added query compiler unit tests * Add a hacky returning clause for redshift ugh * Working migration integration tests * Working insert integration tests * Allow multiple insert returning values * Working select integration tests * Working join integration tests * Working aggregate integration tests * All integration suite tests working * Put docker index for reconnect tests back * Redshift does not support insert...returning, there does not seem to be a way around that, therefore accept it and test accordingly * Leave redshift integration tests in place, but do not run them by default * Fix mysql order by test * Fix more tests * Change test db name to knex_test for consistency * Address PR comments
2018-02-03 08:33:02 -05:00
'pg-redshift',
'update "accounts" set "email" = ?, "first_name" = ?, "last_name" = ? where "id" = ?',
['test100@example.com','UpdatedUser','UpdatedTest',1],
1
);
tester(
2013-12-27 14:44:21 -05:00
'sqlite3',
'update `accounts` set `email` = ?, `first_name` = ?, `last_name` = ? where `id` = ?',
2013-12-27 14:44:21 -05:00
['test100@example.com','UpdatedUser','UpdatedTest',1],
1
);
2014-08-11 12:25:39 +02:00
tester(
'oracle',
'update "accounts" set "email" = ?, "first_name" = ?, "last_name" = ? where "id" = ?',
['test100@example.com','UpdatedUser','UpdatedTest',1],
1
);
tester(
'mssql',
'update [accounts] set [email] = ?, [first_name] = ?, [last_name] = ? output inserted.* where [id] = ?',
['test100@example.com','UpdatedUser','UpdatedTest',1],
[{
id: '1',
first_name: 'UpdatedUser',
last_name: 'UpdatedTest',
email: 'test100@example.com',
logins: 1,
about: 'Lorem ipsum Dolore labore incididunt enim.',
created_at: d,
updated_at: d,
phone: null
}]
);
2013-12-27 14:44:21 -05:00
});
});
});
};