UNPKG

5.28 kBJavaScriptView Raw
1'use strict';
2
3var assert = require('assert');
4var sql = require(__dirname + '/../lib');
5
6describe('column', function() {
7 var table = sql.define({
8 name: 'user',
9 columns: ['id', 'created', 'alias']
10 });
11
12 it('can be accessed by property and array', function() {
13 assert.equal(table.created, table.columns[1], 'should be able to access created both by array and property');
14 });
15
16 describe('toQuery()', function() {
17 it('works', function() {
18 assert.equal(table.id.toQuery().text, '"user"."id"');
19 });
20
21 it('works with a column name of "alias"', function() {
22 assert.equal(table.alias.toQuery().text, '"user"."alias"');
23 });
24
25 it('respects AS rename', function() {
26 assert.equal(table.id.as('userId').toQuery().text, '"user"."id" AS "userId"');
27 });
28
29 it('respects count and distinct', function() {
30 assert.equal(table.id.count().distinct().as("userIdCount").toQuery().text, 'COUNT(DISTINCT("user"."id")) AS "userIdCount"');
31 });
32
33 describe('in subquery with min', function() {
34 var subquery = table.subQuery('subTable').select(table.id.min().as('subId'));
35 var col = subquery.subId.toQuery().text;
36 assert.equal(col, '"subTable"."subId"');
37 });
38
39 describe('property', function() {
40 var table = sql.define({
41 name: 'roundtrip',
42 columns: {
43 column_name: { property: 'propertyName' }
44 }
45 });
46 it('used as alias when !== column name', function() {
47 assert.equal(table.propertyName.toQuery().text, '"roundtrip"."column_name" AS "propertyName"');
48 });
49 it('uses explicit alias when !== column name', function() {
50 assert.equal(table.propertyName.as('alias').toQuery().text, '"roundtrip"."column_name" AS "alias"');
51 });
52 it('maps to column name in insert', function() {
53 assert.equal(table.insert({propertyName:'propVal'}).toQuery().text, 'INSERT INTO "roundtrip" ("column_name") VALUES ($1)');
54 });
55 it('maps to column name in update', function() {
56 assert.equal(table.update({propertyName:'propVal'}).toQuery().text, 'UPDATE "roundtrip" SET "column_name" = $1');
57 });
58 it('explicitly selected by *', function() {
59 assert.equal(table.select(table.star()).from(table).toQuery().text, 'SELECT "roundtrip"."column_name" AS "propertyName" FROM "roundtrip"');
60 });
61 });
62
63 describe('autoGenerate', function() {
64 var table = sql.define({
65 name: 'ag',
66 columns: {
67 id: {autoGenerated: true},
68 name: {}
69 }
70 });
71 it('does not include auto generated columns in insert', function() {
72 assert.equal(table.insert({id:0, name:'name'}).toQuery().text,'INSERT INTO "ag" ("name") VALUES ($1)');
73 });
74 it('does not include auto generated columns in update', function() {
75 assert.equal(table.update({id:0, name:'name'}).toQuery().text,'UPDATE "ag" SET "name" = $1');
76 });
77 });
78
79 describe('white listed', function() {
80 var table = sql.define({
81 name: 'wl',
82 columnWhiteList: true,
83 columns: ['id', 'name']
84 });
85 it('excludes insert properties that are not a column', function() {
86 assert.equal(table.insert({id:0, _private:'_private', name:'name'}).toQuery().text, 'INSERT INTO "wl" ("id", "name") VALUES ($1, $2)');
87 });
88 it('excludes update properties that are not a column', function() {
89 assert.equal(table.update({id:0, _private:'_private', name:'name'}).toQuery().text, 'UPDATE "wl" SET "id" = $1, "name" = $2');
90 });
91 });
92
93 describe('not white listed', function() {
94 var table = sql.define({
95 name: 'wl',
96 columns: ['id', 'name']
97 });
98 it('throws for insert properties that are not a column', function() {
99 assert.throws(function() { table.insert({id:0, _private:'_private', name:'name'}); }, Error);
100 });
101 it('throws for update properties that are not a column', function() {
102 assert.throws(function() { table.update({id:0, _private:'_private', name:'name'}); }, Error);
103 });
104 });
105
106 describe('snake to camel', function() {
107 var table = sql.define({
108 name: 'sc',
109 snakeToCamel: true,
110 columns: {
111 make_me_camel: {},
112 not_to_camel: {property: 'not2Cam'}
113 }
114 });
115 it('for snake column names with no explicit property name', function(){
116 assert.equal(table.makeMeCamel.toQuery().text, '"sc"."make_me_camel" AS "makeMeCamel"');
117 });
118 it('but not when with explicit property name', function() {
119 assert.equal(table.not2Cam.toQuery().text, '"sc"."not_to_camel" AS "not2Cam"');
120 });
121 it('does not use property alias within CASE ... END', function() {
122 assert.equal(table.makeMeCamel.case([table.makeMeCamel.equals(0)],[table.makeMeCamel]).as('rename').toQuery().text,
123 '(CASE WHEN ("sc"."make_me_camel" = $1) THEN "sc"."make_me_camel" END) AS "rename"');
124 });
125 it('respects AS rename in RETURNING clause', function() {
126 assert.equal(table.update({makeMeCamel:0}).returning(table.makeMeCamel.as('rename')).toQuery().text,
127 'UPDATE "sc" SET "make_me_camel" = $1 RETURNING "make_me_camel" AS "rename"');
128 });
129 });
130 });
131});