forked from brianc/node-sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable-tests.js
More file actions
237 lines (206 loc) · 6.43 KB
/
table-tests.js
File metadata and controls
237 lines (206 loc) · 6.43 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
'use strict';
var assert = require('assert');
var Table = require(__dirname + '/../lib/table');
var Column = require(__dirname + '/../lib/column');
var Sql = require('../');
suite('table', function() {
var table = new Table({
name: 'bang'
});
test('has name', function() {
assert.equal(table.getName(), 'bang');
});
test('has no columns', function() {
assert.equal(table.columns.length, 0);
});
test('can add column', function() {
var col = new Column({
table: table,
name: 'boom'
});
assert.equal(col.name, 'boom');
assert.equal(col.table.getName(), 'bang');
table.addColumn(col);
assert.equal(table.columns.length, 1);
assert.equal(table.boom, col);
});
test('creates query node', function() {
var sel = table.select(table.boom);
assert.equal(sel.type, 'QUERY');
});
test('creates *-query if no args is provided to select()', function() {
var sel = table.select();
assert.ok(sel.nodes[0].nodes[0].star);
});
test('can be defined', function() {
var user = Table.define({
name: 'user',
columns: ['id', 'name']
});
assert.equal(user.getName(), 'user');
assert.equal(user.columns.length, 2);
assert.equal(user.columns[0].name, 'id');
assert.equal(user.columns[1].name, 'name');
assert.equal(user.columns[0].name, user.id.name);
assert.equal(user.id.table, user);
assert.equal(user.name.table, user);
});
});
test('table with user-defined column property names', function () {
var table = Table.define({
name: 'blah',
columns: [{
name: 'id',
property: 'theId'
}, {
name: 'email',
property: 'uniqueEmail'
}]
});
var cols = table.columns;
assert.equal(cols.length, 2);
assert.equal(cols[0].name, 'id');
assert(cols[0] === table.theId, 'Expected table.theId to be the first column');
assert(table.id === undefined, 'Expected table.id to not exist');
assert.equal(cols[1].name, 'email');
assert(cols[1] === table.uniqueEmail, 'Expected table.uniqueEmail to be the second column');
assert(table.email === undefined, 'Expected table.email to not exist');
});
test('table with fancier column definitions', function() {
var table = Table.define({
name: 'blah',
columns: [{
name: 'id',
type: 'serial',
notNull: true,
primaryKey: true
}, {
name: 'email',
type: 'text',
notNull: true,
unique: true,
anythingYouWant: 'awesome'
}]
});
var cols = table.columns;
assert.equal(cols.length, 2);
var id = cols[0];
assert.equal(id.name, 'id');
assert.equal(id.type, 'serial');
assert.equal(id.notNull, true);
assert.equal(id.primaryKey, true);
var email = cols[1];
assert.equal(email.name, 'email');
assert.equal(email.type, 'text');
assert.equal(email.notNull, true);
assert.equal(email.unique, true);
assert.equal(email.anythingYouWant, 'awesome');
});
test('table with object structured column definitions', function() {
var table = Table.define({
name: 'blah',
columns: {
id: {
type: 'serial',
notNull: true,
primaryKey: true
},
email: {
type: 'text',
notNull: true,
unique: true,
anythingYouWant: 'awesome'
}
}
});
var cols = table.columns;
assert.equal(cols.length, 2);
var id = cols[0];
assert.equal(id.name, 'id');
assert.equal(id.type, 'serial');
assert.equal(id.notNull, true);
assert.equal(id.primaryKey, true);
var email = cols[1];
assert.equal(email.name, 'email');
assert.equal(email.type, 'text');
assert.equal(email.notNull, true);
assert.equal(email.unique, true);
assert.equal(email.anythingYouWant, 'awesome');
});
test('table with dynamic column definition', function() {
var table = Table.define({ name: 'foo', columns: [] });
assert.equal(table.columns.length, 0);
table.addColumn('foo');
assert.equal(table.columns.length, 1);
assert.throws(function() {
table.addColumn('foo');
});
assert.doesNotThrow(function() {
table.addColumn('foo', { noisy: false });
});
assert.equal(table.columns.length, 1);
});
test('hasColumn', function() {
var table = Table.define({ name: 'foo', columns: [] });
assert.equal(table.hasColumn('baz'), false);
table.addColumn('baz');
assert.equal(table.hasColumn('baz'), true);
});
test('the column "from" does not overwrite the from method', function() {
var table = Table.define({ name: 'foo', columns: [] });
table.addColumn('from');
assert.equal(typeof table.from, 'function');
});
test('getColumn returns the from column', function() {
var table = Table.define({ name: 'foo', columns: [] });
table.addColumn('from');
assert(table.getColumn('from') instanceof Column);
assert(table.get('from') instanceof Column);
});
test('set and get schema', function () {
var table = Table.define({ name: 'foo', schema: 'bar', columns: [] });
assert.equal(table.getSchema(), 'bar');
table.setSchema('barbarz');
assert.equal(table.getSchema(), 'barbarz');
});
suite('table.clone', function() {
test('check if it is a copy, not just a reference', function() {
var table = Table.define({ name: 'foo', columns: [] });
var copy = table.clone();
assert.notEqual(table, copy);
});
test('copy columns', function() {
var table = Table.define({ name: 'foo', columns: ['bar'] });
var copy = table.clone();
assert(copy.get('bar') instanceof Column);
});
test('overwrite config while copying', function() {
var table = Table.define({
name: 'foo',
schema: 'foobar',
columns: ['bar'],
snakeToCamel: true,
columnWhiteList: true
});
var copy = table.clone({
schema: 'test',
snakeToCamel: false,
columnWhiteList: false
});
assert.equal(copy.getSchema(), 'test');
assert.equal(copy.snakeToCamel, false);
assert.equal(copy.columnWhiteList, false);
});
});
test('dialects', function () {
var sql = new Sql.Sql('mysql');
var foo = sql.define({ name: 'foo', columns: [ 'id' ] }),
bar = sql.define({ name: 'bar', columns: [ 'id' ] });
var actual = foo.join(bar).on(bar.id.equals(1)).toString();
assert.equal(actual, '`foo` INNER JOIN `bar` ON (`bar`.`id` = 1)');
sql = new Sql.Sql('postgres');
foo = sql.define({ name: 'foo', columns: [ 'id' ] });
bar = sql.define({ name: 'bar', columns: [ 'id' ] });
actual = foo.join(bar).on(bar.id.equals(1)).toString();
assert.equal(actual, '"foo" INNER JOIN "bar" ON ("bar"."id" = 1)');
});