UNPKG

5.48 kBtext/coffeescriptView Raw
1mesa = require '../src/postgres'
2
3module.exports =
4
5 'escape table names':
6
7 'should escape the table names without a schema': (test) ->
8 connection =
9 query: (sql, params, cb) ->
10 test.equal sql, 'INSERT INTO "schema"("name", "email") VALUES ($1, $2) RETURNING id'
11 cb null, {rows: [{id: 3}]}
12
13 userTable = mesa
14 .connection(connection)
15 .table('schema')
16 .attributes(['name', 'email'])
17 .insert {name: 'foo', email: 'foo'}, (err, id) ->
18 throw err if err?
19 test.done()
20
21 'should escape the table names with a schema': (test) ->
22 connection =
23 query: (sql, params, cb) ->
24 test.equal sql, 'INSERT INTO "schema"."user"("name", "email") VALUES ($1, $2) RETURNING id'
25 cb null, {rows: [{id: 3}]}
26
27 userTable = mesa
28 .connection(connection)
29 .table('schema.user')
30 .attributes(['name', 'email'])
31 .insert {name: 'foo', email: 'foo'}, (err, id) ->
32 throw err if err?
33 test.done()
34
35 'mesa controlled connection':
36
37 'done is called on insert': (test) ->
38 test.expect 1
39
40 getConnection = (cb) ->
41 process.nextTick ->
42 done = -> test.ok true
43 connection =
44 query: (sql, params, cb) ->
45 cb null, {rows: [{id: 3}]}
46 cb null, connection, done
47
48 userTable = mesa
49 .connection(getConnection)
50 .table('user')
51 .attributes(['name'])
52
53 userTable.insert {name: 'foo'}, (err, id) ->
54 throw err if err?
55 test.done()
56
57 'done is called on update': (test) ->
58 test.expect 1
59
60 getConnection = (cb) ->
61 process.nextTick ->
62 done = -> test.ok true
63 connection =
64 query: (sql, params, cb) ->
65 cb()
66 cb null, connection, done
67
68 userTable = mesa
69 .connection(getConnection)
70 .table('user')
71 .attributes(['name'])
72
73 userTable.update {name: 'foo'}, (err) ->
74 throw err if err?
75 test.done()
76
77 'done is called on delete': (test) ->
78 test.expect 1
79
80 getConnection = (cb) ->
81 process.nextTick ->
82 done = -> test.ok true
83 connection =
84 query: (sql, params, cb) ->
85 cb()
86 cb null, connection, done
87
88 userTable = mesa
89 .connection(getConnection)
90 .table('user')
91
92 userTable.delete (err) ->
93 throw err if err?
94 test.done()
95
96 'done is called on first when a record is returned': (test) ->
97 test.expect 1
98
99 getConnection = (cb) ->
100 process.nextTick ->
101 done = -> test.ok true
102 connection =
103 query: (sql, params, cb) ->
104 cb null, {rows: [{x: 1}]}
105 cb null, connection, done
106
107 userTable = mesa
108 .connection(getConnection)
109 .table('user')
110
111 userTable.first (err) ->
112 throw err if err?
113 test.done()
114
115 'done is called on first when no record is returned': (test) ->
116 test.expect 1
117
118 getConnection = (cb) ->
119 process.nextTick ->
120 done = -> test.ok true
121 connection =
122 query: (sql, params, cb) ->
123 cb null, {rows: []}
124 cb null, connection, done
125
126 userTable = mesa
127 .connection(getConnection)
128 .table('user')
129
130 userTable.first (err) ->
131 throw err if err?
132 test.done()
133
134 'done is called on find when a record is returned': (test) ->
135 test.expect 1
136
137 getConnection = (cb) ->
138 process.nextTick ->
139 done = -> test.ok true
140 connection =
141 query: (sql, params, cb) ->
142 cb null, {rows: [{x: 1}]}
143 cb null, connection, done
144
145 userTable = mesa
146 .connection(getConnection)
147 .table('user')
148
149 userTable.find (err) ->
150 throw err if err?
151 test.done()
152
153 'done is called on find when no records are returned': (test) ->
154 test.expect 1
155
156 getConnection = (cb) ->
157 process.nextTick ->
158 done = -> test.ok true
159 connection =
160 query: (sql, params, cb) ->
161 cb null, {rows: []}
162 cb null, connection, done
163
164 userTable = mesa
165 .connection(getConnection)
166 .table('user')
167
168 userTable.find (err) ->
169 throw err if err?
170 test.done()