forked from 2do2go/json-sql
-
Notifications
You must be signed in to change notification settings - Fork 3
/
2_insert.js
73 lines (65 loc) · 1.6 KB
/
2_insert.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'use strict';
var jsonSql = require('../lib')();
var expect = require('chai').expect;
describe('Insert', function() {
it('should throw error without `values` property', function() {
expect(function() {
jsonSql.build({
type: 'insert',
table: 'users'
});
}).to.throw('`values` property is not set in `insert` clause');
});
it('should be ok with `values` property', function() {
var result = jsonSql.build({
type: 'insert',
table: 'users',
values: {
name: 'Max'
}
});
expect(result.query).to.be.equal('insert into "users" ("name") values ($p1);');
expect(result.values).to.be.eql({p1: 'Max'});
});
it('should be ok with `with` property', function() {
var result = jsonSql.build({
'with': [{
name: 't_1',
select: {
table: 't_1'
}
}],
type: 'insert',
table: 'users',
values: {
name: 'Max',
age: 17,
lastVisit: null,
active: true
}
});
expect(result.query).to.be.equal(
'with "t_1" as (select * from "t_1") insert into "users" ' +
'("name", "age", "lastVisit", "active") values ($p1, 17, null, true);'
);
expect(result.values).to.be.eql({p1: 'Max'});
});
it('should be ok with `returning` property', function() {
var result = jsonSql.build({
type: 'insert',
table: 'users',
values: {
name: 'Max',
age: 17,
lastVisit: null,
active: true
},
returning: ['users.*']
});
expect(result.query).to.be.equal(
'insert into "users" ("name", "age", "lastVisit", "active") ' +
'values ($p1, 17, null, true) returning "users".*;'
);
expect(result.values).to.be.eql({p1: 'Max'});
});
});