This repository has been archived by the owner on Nov 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 231
/
insert.test.coffee
153 lines (114 loc) · 5 KB
/
insert.test.coffee
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
###
Copyright (c) 2012 Ramesh Nair (hiddentao.com)
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
###
{test, assert, expect, should} = require './testbase'
squel = require "../src/squel"
test['INSERT builder'] =
beforeEach: ->
@inst = squel.insert()
'instanceof QueryBuilder': ->
assert.instanceOf @inst, squel.QueryBuilder
'default field values': ->
assert.same null, @inst.table
assert.same {}, @inst.fields
assert.same { usingValuePlaceholders: false }, @inst.options
'constructor':
'override options': ->
@inst = squel.insert
usingValuePlaceholders: true
dummy: true
assert.same null, @inst.table
assert.same {}, @inst.fields
assert.same { usingValuePlaceholders: true, dummy: true }, @inst.options
'>> into()':
beforeEach: ->
test.mocker.spy(@inst, '_sanitizeTable')
'args: ()': ->
assert.throws (=> @inst.into()), 'table name must be a string'
assert.ok @inst._sanitizeTable.calledWithExactly(undefined)
'args: (table)':
beforeEach: ->
@ret = @inst.into('table')
'update internal state': ->
assert.same @ret, @inst
assert.same @inst.table, 'table'
assert.ok @inst._sanitizeTable.calledWithExactly('table')
'>> args(table2)': ->
assert.same @inst.into('table2'), @inst
assert.same @inst.table, 'table2'
'>> set()':
beforeEach: ->
test.mocker.spy(@inst, '_sanitizeField')
test.mocker.spy(@inst, '_sanitizeValue')
'args: ()': ->
assert.throws (=> @inst.set()), 'field name must be a string'
assert.ok @inst._sanitizeField.calledWithExactly(undefined)
'args: (field)': ->
assert.throws (=> @inst.set('field')), 'field value must be a string, number, boolean or null'
assert.ok @inst._sanitizeField.calledWithExactly('field')
assert.ok @inst._sanitizeValue.calledWithExactly(undefined)
'args: (field, null)':
beforeEach: ->
@ret = @inst.set('field', null)
'update internal state': ->
assert.same @ret, @inst
assert.same @inst.fields, { 'field': null }
assert.ok @inst._sanitizeField.calledWithExactly('field')
assert.ok @inst._sanitizeValue.calledWithExactly(null)
'>> args: (field, 1)':
beforeEach: ->
@ret = @inst.set('field', 1)
'update internal state': ->
assert.same @ret, @inst
assert.same @inst.fields, { 'field': 1 }
assert.ok @inst._sanitizeField.calledWithExactly('field')
assert.ok @inst._sanitizeValue.calledWithExactly(1)
'build query':
'need to call into() first': ->
assert.throws (=> @inst.toString()), 'into() needs to be called'
'need to call set() first': ->
@inst.into('table')
assert.throws (=> @inst.toString()), 'set() needs to be called'
'>> into(table).set(field, 1)':
beforeEach: -> @inst.into('table').set('field', 1)
toString: ->
assert.same @inst.toString(), 'INSERT INTO table (field) VALUES (1)'
'>> set(field2, 1.2)':
beforeEach: -> @inst.set('field2', 1.2)
toString: ->
assert.same @inst.toString(), 'INSERT INTO table (field, field2) VALUES (1, 1.2)'
'>> set(field2, "str")':
beforeEach: -> @inst.set('field2', 'str')
toString: ->
assert.same @inst.toString(), 'INSERT INTO table (field, field2) VALUES (1, \'str\')'
'and when using value placeholders': ->
@inst.options.usingValuePlaceholders = true
@inst.set('field2', 'str')
assert.same @inst.toString(), 'INSERT INTO table (field, field2) VALUES (1, str)'
'>> set(field2, true)':
beforeEach: -> @inst.set('field2', true)
toString: ->
assert.same @inst.toString(), 'INSERT INTO table (field, field2) VALUES (1, TRUE)'
'>> set(field2, null)':
beforeEach: -> @inst.set('field2', null)
toString: ->
assert.same @inst.toString(), 'INSERT INTO table (field, field2) VALUES (1, NULL)'
module?.exports[require('path').basename(__filename)] = test