2016-03-02 17:07:05 +01:00
|
|
|
'use strict';
|
|
|
|
|
2018-10-15 22:29:53 -04:00
|
|
|
const tape = require('tape');
|
|
|
|
const omit = require('lodash/omit');
|
2021-01-09 17:59:53 +02:00
|
|
|
const QueryBuilder = require('../../lib/query/querybuilder');
|
2019-07-10 22:48:43 +01:00
|
|
|
const Client = require('../../lib/client');
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
tape('accumulates multiple update calls #647', function (t) {
|
2018-07-09 08:10:34 -04:00
|
|
|
t.plan(1);
|
2018-10-15 22:29:53 -04:00
|
|
|
const qb = new QueryBuilder({});
|
2018-07-09 08:10:34 -04:00
|
|
|
qb.update('a', 1).update('b', 2);
|
|
|
|
t.deepEqual(qb._single.update, { a: 1, b: 2 });
|
|
|
|
});
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
tape('allows for object syntax in join', function (t) {
|
2018-07-09 08:10:34 -04:00
|
|
|
t.plan(1);
|
2018-10-15 22:29:53 -04:00
|
|
|
const qb = new QueryBuilder(new Client({ client: 'mysql' }));
|
|
|
|
const sql = qb
|
2018-07-09 08:10:34 -04:00
|
|
|
.table('users')
|
|
|
|
.innerJoin('accounts', {
|
|
|
|
'accounts.id': 'users.account_id',
|
|
|
|
'accounts.owner_id': 'users.id',
|
|
|
|
})
|
|
|
|
.toSQL('join');
|
|
|
|
t.equal(
|
|
|
|
sql.sql,
|
|
|
|
'inner join "accounts" on "accounts"."id" = "users"."account_id" and "accounts"."owner_id" = "users"."id"'
|
|
|
|
);
|
|
|
|
});
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
tape('clones correctly', function (t) {
|
2018-10-15 22:29:53 -04:00
|
|
|
const qb = new QueryBuilder(new Client({ client: 'mysql' }));
|
2020-04-19 00:40:23 +02:00
|
|
|
const original = qb.table('users').debug().innerJoin('accounts', {
|
|
|
|
'accounts.id': 'users.account_id',
|
|
|
|
'accounts.owner_id': 'users.id',
|
|
|
|
});
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2018-10-15 22:29:53 -04:00
|
|
|
const cloned = original.clone();
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
t.notEqual(original, cloned);
|
|
|
|
|
|
|
|
// `deepEqual` freezes when it encounters circular references,
|
|
|
|
// so they must be omitted.
|
2018-07-09 08:10:34 -04:00
|
|
|
t.deepEqual(omit(cloned, 'client', 'and'), omit(original, 'client', 'and'));
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
t.equal(cloned.client, original.client, 'clone references same client');
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
t.equal(cloned.and, cloned, 'cloned `and` references self');
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|