UNPKG

7.63 kBJavaScriptView Raw
1'use strict';
2var assert = require('assert');
3
4var Table = require(__dirname + '/../lib/table');
5var Column = require(__dirname + '/../lib/column');
6var Sql = require('../');
7
8suite('table', function() {
9 var table = new Table({
10 name: 'bang'
11 });
12
13 test('has name', function() {
14 assert.equal(table.getName(), 'bang');
15 });
16
17 test('has no columns', function() {
18 assert.equal(table.columns.length, 0);
19 });
20
21 test('can add column', function() {
22 var col = new Column({
23 table: table,
24 name: 'boom'
25 });
26
27 assert.equal(col.name, 'boom');
28 assert.equal(col.table.getName(), 'bang');
29
30 table.addColumn(col);
31 assert.equal(table.columns.length, 1);
32 assert.equal(table.boom, col);
33 });
34
35 test('creates query node', function() {
36 var sel = table.select(table.boom);
37 assert.equal(sel.type, 'QUERY');
38 });
39
40 test('creates *-query if no args is provided to select()', function() {
41 var sel = table.select();
42 assert.ok(sel.nodes[0].nodes[0].star);
43 });
44
45 test('can be defined', function() {
46 var user = Table.define({
47 name: 'user',
48 columns: ['id', 'name']
49 });
50 assert.equal(user.getName(), 'user');
51 assert.equal(user.columns.length, 2);
52 assert.equal(user.columns[0].name, 'id');
53 assert.equal(user.columns[1].name, 'name');
54 assert.equal(user.columns[0].name, user.id.name);
55 assert.equal(user.id.table, user);
56 assert.equal(user.name.table, user);
57 });
58});
59
60test('table with user-defined column property names', function () {
61 var table = Table.define({
62 name: 'blah',
63 columns: [{
64 name: 'id',
65 property: 'theId'
66 }, {
67 name: 'email',
68 property: 'uniqueEmail'
69 }]
70 });
71 var cols = table.columns;
72 assert.equal(cols.length, 2);
73 assert.equal(cols[0].name, 'id');
74 assert(cols[0] === table.theId, 'Expected table.theId to be the first column');
75 assert(table.id === undefined, 'Expected table.id to not exist');
76 assert.equal(cols[1].name, 'email');
77 assert(cols[1] === table.uniqueEmail, 'Expected table.uniqueEmail to be the second column');
78 assert(table.email === undefined, 'Expected table.email to not exist');
79});
80
81test('table with fancier column definitions', function() {
82 var table = Table.define({
83 name: 'blah',
84 columns: [{
85 name: 'id',
86 type: 'serial',
87 notNull: true,
88 primaryKey: true
89 }, {
90 name: 'email',
91 type: 'text',
92 notNull: true,
93 unique: true,
94 anythingYouWant: 'awesome'
95 }]
96 });
97 var cols = table.columns;
98 assert.equal(cols.length, 2);
99 var id = cols[0];
100 assert.equal(id.name, 'id');
101 assert.equal(id.type, 'serial');
102 assert.equal(id.notNull, true);
103 assert.equal(id.primaryKey, true);
104 var email = cols[1];
105 assert.equal(email.name, 'email');
106 assert.equal(email.type, 'text');
107 assert.equal(email.notNull, true);
108 assert.equal(email.unique, true);
109 assert.equal(email.anythingYouWant, 'awesome');
110});
111
112test('table with object structured column definitions', function() {
113 var table = Table.define({
114 name: 'blah',
115 columns: {
116 id: {
117 type: 'serial',
118 notNull: true,
119 primaryKey: true
120 },
121 email: {
122 type: 'text',
123 notNull: true,
124 unique: true,
125 anythingYouWant: 'awesome'
126 }
127 }
128 });
129 var cols = table.columns;
130 assert.equal(cols.length, 2);
131 var id = cols[0];
132 assert.equal(id.name, 'id');
133 assert.equal(id.type, 'serial');
134 assert.equal(id.notNull, true);
135 assert.equal(id.primaryKey, true);
136 var email = cols[1];
137 assert.equal(email.name, 'email');
138 assert.equal(email.type, 'text');
139 assert.equal(email.notNull, true);
140 assert.equal(email.unique, true);
141 assert.equal(email.anythingYouWant, 'awesome');
142});
143
144test('table with dynamic column definition', function() {
145 var table = Table.define({ name: 'foo', columns: [] });
146 assert.equal(table.columns.length, 0);
147
148 table.addColumn('foo');
149 assert.equal(table.columns.length, 1);
150
151 assert.throws(function() {
152 table.addColumn('foo');
153 });
154
155 assert.doesNotThrow(function() {
156 table.addColumn('foo', { noisy: false });
157 });
158
159 assert.equal(table.columns.length, 1);
160});
161
162test('hasColumn', function() {
163 var table = Table.define({ name: 'foo', columns: [] });
164
165 assert.equal(table.hasColumn('baz'), false);
166 table.addColumn('baz');
167 assert.equal(table.hasColumn('baz'), true);
168});
169
170test('hasColumn with user-defined column property', function() {
171 var table = Table.define({
172 name: 'blah',
173 columns: [{
174 name: 'id',
175 property: 'theId'
176 }, {name: 'foo'}]
177 });
178
179 assert.equal(table.hasColumn('id'), true);
180 assert.equal(table.hasColumn('theId'), true);
181});
182
183test('the column "from" does not overwrite the from method', function() {
184 var table = Table.define({ name: 'foo', columns: [] });
185 table.addColumn('from');
186 assert.equal(typeof table.from, 'function');
187});
188
189test('getColumn returns the from column', function() {
190 var table = Table.define({ name: 'foo', columns: [] });
191 table.addColumn('from');
192 assert(table.getColumn('from') instanceof Column);
193 assert(table.get('from') instanceof Column);
194});
195
196test('set and get schema', function () {
197 var table = Table.define({ name: 'foo', schema: 'bar', columns: [] });
198 assert.equal(table.getSchema(), 'bar');
199 table.setSchema('barbarz');
200 assert.equal(table.getSchema(), 'barbarz');
201});
202
203suite('table.clone', function() {
204 test('check if it is a copy, not just a reference', function() {
205 var table = Table.define({ name: 'foo', columns: [] });
206 var copy = table.clone();
207 assert.notEqual(table, copy);
208 });
209
210 test('copy columns', function() {
211 var table = Table.define({ name: 'foo', columns: ['bar'] });
212 var copy = table.clone();
213 assert(copy.get('bar') instanceof Column);
214 });
215
216 test('overwrite config while copying', function() {
217 var table = Table.define({
218 name: 'foo',
219 schema: 'foobar',
220 columns: ['bar'],
221 snakeToCamel: true,
222 columnWhiteList: true
223 });
224
225 var copy = table.clone({
226 schema: 'test',
227 snakeToCamel: false,
228 columnWhiteList: false
229 });
230
231 assert.equal(copy.getSchema(), 'test');
232 assert.equal(copy.snakeToCamel, false);
233 assert.equal(copy.columnWhiteList, false);
234 });
235});
236
237test('dialects', function () {
238 var sql = new Sql.Sql('mysql');
239 var foo = sql.define({ name: 'foo', columns: [ 'id' ] }),
240 bar = sql.define({ name: 'bar', columns: [ 'id' ] });
241
242 var actual = foo.join(bar).on(bar.id.equals(1)).toString();
243 assert.equal(actual, '`foo` INNER JOIN `bar` ON (`bar`.`id` = 1)');
244
245 sql = new Sql.Sql('postgres');
246 foo = sql.define({ name: 'foo', columns: [ 'id' ] });
247 bar = sql.define({ name: 'bar', columns: [ 'id' ] });
248 actual = foo.join(bar).on(bar.id.equals(1)).toString();
249 assert.equal(actual, '"foo" INNER JOIN "bar" ON ("bar"."id" = 1)');
250});
251
252test('limit', function () {
253 var user = Table.define({name: 'user', columns: ['id', 'name']});
254 var query = user.limit(3);
255 assert.equal(query.nodes.length, 1);
256 assert.equal(query.nodes[0].type, 'LIMIT');
257 assert.equal(query.nodes[0].count, 3);
258});
259
260test('offset', function () {
261 var user = Table.define({name: 'user', columns: ['id', 'name']});
262 var query = user.offset(20);
263 assert.equal(query.nodes.length, 1);
264 assert.equal(query.nodes[0].type, 'OFFSET');
265 assert.equal(query.nodes[0].count, 20);
266});
267
268test('order', function () {
269 var user = Table.define({name: 'user', columns: ['id', 'name']});
270 var query = user.order(user.name);
271 assert.equal(query.nodes.length, 1);
272 assert.equal(query.nodes[0].type, 'ORDER BY');
273});