Skip to content

Commit

Permalink
test(onConflict): saveQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
afontainec committed Jun 10, 2021
1 parent 207f0da commit 9f23bef
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions models/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ class Table {
options = options || {};
const query = this.table().insert(entry).returning('*');
const { onConflict } = options;
if (onConflict && !onConflict.columns) throw new ChinchayError(new Error('onConflict requires columns attribute'), 'onConflictColumns', 'options.onConflict must have a "columns" property.');
if (onConflict && onConflict.merge) {
query.onConflict(onConflict.columns).merge(onConflict.merge);
} else if (onConflict && onConflict.ignore) {
Expand Down
82 changes: 82 additions & 0 deletions test/models/table/save/save-query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/* global describe, it, before, after */
process.env.NODE_ENV = 'test';

// Require the dev-dependencies
const { assert } = require('chai');
const knex = require('../../../../knex');
const Coffee = require('../../../../models/coffee-example');


// Our parent block
describe('TABLE GATEWAY: saveQuery', () => { // eslint-disable-line max-lines-per-function

before(async () => {
await knex.seed.run();
await Coffee.delete({});
await knex.raw('alter table coffee add constraint unique_name unique (name);');

});

after(async () => {
await knex.raw('alter table coffee drop constraint unique_name;');
});

it('onConflict and merge', async () => {
const entry = { name: 'Test', price: 100 };
const onConflict = { columns: ['name'], merge: { price: knex.raw('coffee.price + EXCLUDED.price') } };
const options = { onConflict };
const query = Coffee.saveQuery(entry, options);
const expected = 'insert into "coffee" ("name", "price") values (\'Test\', 100) on conflict ("name") do update set "price" = coffee.price + EXCLUDED.price returning *';
assert.equal(query.toString(), expected);
});

it('onConflict and ignore', async () => {
const entry = { name: 'Test', price: 100 };
const onConflict = { columns: ['name'], ignore: true };
const options = { onConflict };
const query = Coffee.saveQuery(entry, options);
const expected = 'insert into "coffee" ("name", "price") values (\'Test\', 100) on conflict ("name") do nothing returning *';
assert.equal(query.toString(), expected);
});

it('onConflict but columns not defined', async () => {
const entry = { name: 'Test', price: 100 };
const onConflict = { ignore: true };
const options = { onConflict };
try {
Coffee.saveQuery(entry, options);
throw new Error();
} catch (err) {
assert.equal(err.chinchayCode, 'onConflictColumns');

}
});

it('no onConflict', async () => {
const entry = { name: 'Test', price: 100 };
const options = { };
const query = Coffee.saveQuery(entry, options);
const expected = 'insert into "coffee" ("name", "price") values (\'Test\', 100) returning *';
assert.equal(query.toString(), expected);
});

it('no options', async () => {
const entry = { name: 'Test', price: 100 };
const query = Coffee.saveQuery(entry);
const expected = 'insert into "coffee" ("name", "price") values (\'Test\', 100) returning *';
assert.equal(query.toString(), expected);
});

it('functional test', async () => {
const entry = { name: 'Test', price: 100 };
const onConflict = { columns: ['name'], merge: { price: knex.raw('coffee.price + EXCLUDED.price') } };
const options = { onConflict };
const [first] = await Coffee.saveQuery(entry, options);
const [second] = await Coffee.saveQuery(entry, options);
assert.equal(first.id, second.id);
assert.equal(first.price, 100);
assert.equal(second.price, 200);
});


});

0 comments on commit 9f23bef

Please sign in to comment.