UNPKG

7.33 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('the column "from" does not overwrite the from method', function() {
171 var table = Table.define({ name: 'foo', columns: [] });
172 table.addColumn('from');
173 assert.equal(typeof table.from, 'function');
174});
175
176test('getColumn returns the from column', function() {
177 var table = Table.define({ name: 'foo', columns: [] });
178 table.addColumn('from');
179 assert(table.getColumn('from') instanceof Column);
180 assert(table.get('from') instanceof Column);
181});
182
183test('set and get schema', function () {
184 var table = Table.define({ name: 'foo', schema: 'bar', columns: [] });
185 assert.equal(table.getSchema(), 'bar');
186 table.setSchema('barbarz');
187 assert.equal(table.getSchema(), 'barbarz');
188});
189
190suite('table.clone', function() {
191 test('check if it is a copy, not just a reference', function() {
192 var table = Table.define({ name: 'foo', columns: [] });
193 var copy = table.clone();
194 assert.notEqual(table, copy);
195 });
196
197 test('copy columns', function() {
198 var table = Table.define({ name: 'foo', columns: ['bar'] });
199 var copy = table.clone();
200 assert(copy.get('bar') instanceof Column);
201 });
202
203 test('overwrite config while copying', function() {
204 var table = Table.define({
205 name: 'foo',
206 schema: 'foobar',
207 columns: ['bar'],
208 snakeToCamel: true,
209 columnWhiteList: true
210 });
211
212 var copy = table.clone({
213 schema: 'test',
214 snakeToCamel: false,
215 columnWhiteList: false
216 });
217
218 assert.equal(copy.getSchema(), 'test');
219 assert.equal(copy.snakeToCamel, false);
220 assert.equal(copy.columnWhiteList, false);
221 });
222});
223
224test('dialects', function () {
225 var sql = new Sql.Sql('mysql');
226 var foo = sql.define({ name: 'foo', columns: [ 'id' ] }),
227 bar = sql.define({ name: 'bar', columns: [ 'id' ] });
228
229 var actual = foo.join(bar).on(bar.id.equals(1)).toString();
230 assert.equal(actual, '`foo` INNER JOIN `bar` ON (`bar`.`id` = 1)');
231
232 sql = new Sql.Sql('postgres');
233 foo = sql.define({ name: 'foo', columns: [ 'id' ] });
234 bar = sql.define({ name: 'bar', columns: [ 'id' ] });
235 actual = foo.join(bar).on(bar.id.equals(1)).toString();
236 assert.equal(actual, '"foo" INNER JOIN "bar" ON ("bar"."id" = 1)');
237});
238
239test('limit', function () {
240 var user = Table.define({name: 'user', columns: ['id', 'name']});
241 var query = user.limit(3);
242 assert.equal(query.nodes.length, 1);
243 assert.equal(query.nodes[0].type, 'LIMIT');
244 assert.equal(query.nodes[0].count, 3);
245});
246
247test('offset', function () {
248 var user = Table.define({name: 'user', columns: ['id', 'name']});
249 var query = user.offset(20);
250 assert.equal(query.nodes.length, 1);
251 assert.equal(query.nodes[0].type, 'OFFSET');
252 assert.equal(query.nodes[0].count, 20);
253});
254
255test('order', function () {
256 var user = Table.define({name: 'user', columns: ['id', 'name']});
257 var query = user.order(user.name);
258 assert.equal(query.nodes.length, 1);
259 assert.equal(query.nodes[0].type, 'ORDER BY');
260});