minor updates
[urlittler] / test / src / db / postgres / index.js
1 /* eslint-disable capitalized-comments */
2 /* eslint-env mocha */
3
4 'use strict';
5
6 const assert = require('assert');
7 const sinon = require('sinon'); // eslint-disable-line node/no-unpublished-require
8
9 const noExpectedException = 'did not get expected exception';
10
11 describe('PostgresDatabase', function () {
12 const PostgresDatabase = require('../../../../src/db/postgres');
13 let pgpStub, db, logger, options, dbCtx;
14
15 beforeEach(function () {
16
17 pgpStub = () => {
18 const stub = {
19 result: sinon.stub(),
20 all: sinon.stub(),
21 get: sinon.stub(),
22 run: sinon.stub(),
23 one: sinon.stub(),
24 manyOrNone: sinon.stub(),
25 oneOrNone: sinon.stub(),
26 query: sinon.stub(),
27 batch: sinon.stub(),
28 };
29 stub.tx = async (fn) => await fn(stub);
30 stub.txIf = async (fn) => await fn(stub);
31 stub.task = async (fn) => await fn(stub);
32 return stub;
33 };
34 pgpStub.utils = {
35 enumSql: () => ({}),
36 };
37 pgpStub.QueryFile = class {};
38 pgpStub.end = () => {},
39
40 logger = {
41 debug: sinon.stub(),
42 error: sinon.stub(),
43 };
44 options = {};
45 db = new PostgresDatabase(logger, options, pgpStub);
46 db.statement = {};
47 dbCtx = undefined;
48 });
49
50 describe('context', function () {
51 it('covers', async function () {
52 const fn = sinon.stub();
53 await db.context(fn);
54 assert(fn.called);
55 });
56 }); // context
57
58 describe('transaction', function () {
59 it('covers', async function () {
60 const fn = sinon.stub();
61 await db.transaction(undefined, fn);
62 assert(fn.called);
63 });
64 }); // transaction
65
66 describe('getAuthById', function () {
67 let id;
68 it('stubbed success', async function () {
69 const expected = {
70 id: 'id',
71 secret: 'secret',
72 password: 'password',
73 };
74 id = 'id';
75 db.db.oneOrNone.resolves(expected);
76 const result = await db.getAuthById(dbCtx, id);
77 assert.deepStrictEqual(result, expected);
78 });
79 it('stubbed failure', async function () {
80 const expectedExeption = new Error('blah');
81 id = 'id';
82 db.db.oneOrNone.rejects(expectedExeption);
83 try {
84 await db.getAuthById(dbCtx, id);
85 assert.fail(noExpectedException);
86 } catch (e) {
87 assert.deepStrictEqual(e, expectedExeption, noExpectedException);
88 }
89 });
90 }); // getAuthById
91
92 describe('_epochFix', function () {
93 it('clamps infinity', function () {
94 const epoch = Infinity;
95 const expected = Number.MAX_SAFE_INTEGER;
96 const result = PostgresDatabase._epochFix(epoch);
97 assert.strictEqual(result, expected);
98 });
99 it('clamps negative infinity', function () {
100 const epoch = -Infinity;
101 const expected = 0;
102 const result = PostgresDatabase._epochFix(epoch);
103 assert.strictEqual(result, expected);
104 });
105 it('returns number', function () {
106 const epoch = 123;
107 const expected = 123;
108 const result = PostgresDatabase._epochFix(epoch);
109 assert.strictEqual(result, expected);
110 });
111 }); // _epochFix
112
113 describe('_linkToNative', function () {
114 it('handles missing link', function () {
115 const link = undefined;
116 const expected = undefined;
117 const result = PostgresDatabase._linkToNative(link);
118 assert.deepStrictEqual(result, expected);
119 });
120 it('converts epochs', function () {
121 const link = {
122 id: 'id',
123 url: 'url',
124 expires: null,
125 lastAccess: -Infinity,
126 created: 123,
127 };
128 const expected = {
129 id: 'id',
130 url: 'url',
131 expires: null,
132 lastAccess: 0,
133 created: 123,
134 };
135 const result = PostgresDatabase._linkToNative(link);
136 assert.deepStrictEqual(result, expected);
137 });
138 }); // _linkToNative
139
140 describe('insertLink', function () {
141 let id, url, authToken;
142 it('stubbed success', async function () {
143 const returns = {
144 rowCount: 0,
145 rows: [],
146 duration: 0,
147 };
148 id = 'id';
149 db.db.result.resolves(returns);
150 const expected = {
151 changes: 0,
152 duration: 0,
153 lastInsertRowid: undefined,
154 };
155 const result = await db.insertLink(dbCtx, id, url, authToken);
156 assert.deepStrictEqual(result, expected);
157 });
158 it('stubbed failure', async function () {
159 const expectedExeption = new Error('blah');
160 id = 'id';
161 db.db.result.rejects(expectedExeption);
162 try {
163 await db.insertLink(dbCtx, id, url, authToken);
164 assert.fail(noExpectedException);
165 } catch (e) {
166 assert.deepStrictEqual(e, expectedExeption, noExpectedException);
167 }
168 });
169 }); // insertLink
170
171 describe('getLinkById', function () {
172 let id;
173 it('stubbed success', async function () {
174 const expected = {
175 id: 'id',
176 url: 'url',
177 created: 0,
178 expires: 0,
179 lastAccess: 0,
180 };
181 id = 'id';
182 db.db.oneOrNone.resolves(expected);
183 const result = await db.getLinkById(dbCtx, id);
184 assert.deepStrictEqual(result, expected);
185 });
186 it('stubbed failure', async function () {
187 const expectedExeption = new Error('blah');
188 id = 'id';
189 db.db.oneOrNone.rejects(expectedExeption);
190 try {
191 await db.getLinkById(dbCtx, id);
192 assert.fail(noExpectedException);
193 } catch (e) {
194 assert.deepStrictEqual(e, expectedExeption, noExpectedException);
195 }
196 });
197 }); // getLinkById
198
199 describe('getLinkByUrl', function () {
200 let url;
201 it('stubbed success', async function () {
202 const expected = {
203 id: 'id',
204 url: 'url',
205 created: 0,
206 expires: 0,
207 lastAccess: 0,
208 };
209 url = 'url';
210 db.db.oneOrNone.resolves(expected);
211 const result = await db.getLinkByUrl(dbCtx, url);
212 assert.deepStrictEqual(result, expected);
213 });
214 it('stubbed failure', async function () {
215 const expectedExeption = new Error('blah');
216 url = 'url';
217 db.db.oneOrNone.rejects(expectedExeption);
218 try {
219 await db.getLinkByUrl(dbCtx, url);
220 assert.fail(noExpectedException);
221 } catch (e) {
222 assert.deepStrictEqual(e, expectedExeption, noExpectedException);
223 }
224 });
225 }); // getLinkByUrl
226
227 describe('accessLink', function () {
228 let id;
229 it('stubbed success', async function () {
230 const expected = {
231 id: 'id',
232 url: 'url',
233 created: 0,
234 expires: 0,
235 lastAccess: 0,
236 };
237 id = 'id';
238 db.db.oneOrNone.resolves(expected);
239 const result = await db.accessLink(dbCtx, id);
240 assert.deepStrictEqual(result, expected);
241 });
242 it('stubbed failure', async function () {
243 const expectedExeption = new Error('blah');
244 id = 'id';
245 db.db.oneOrNone.rejects(expectedExeption);
246 try {
247 await db.accessLink(dbCtx, id);
248 assert.fail(noExpectedException);
249 } catch (e) {
250 assert.deepStrictEqual(e, expectedExeption, noExpectedException);
251 }
252 });
253 }); // accessLink
254
255 describe('expireLink', function () {
256 let id, expires;
257 it('stubbed success', async function () {
258 const returns = {
259 rowCount: 1,
260 rows: [ { id: 1 } ],
261 duration: 10,
262 };
263 const expected = {
264 changes: 1,
265 duration: 10,
266 lastInsertRowid: 1,
267 };
268 id = 'id';
269 expires = null;
270 db.db.result.resolves(returns);
271 const result = await db.expireLink(dbCtx, id, expires);
272 assert.deepStrictEqual(result, expected);
273 });
274 it('stubbed failure', async function () {
275 const expectedExeption = new Error('blah');
276 id = 'id';
277 expires = null;
278 db.db.result.rejects(expectedExeption);
279 try {
280 await db.expireLink(dbCtx, id, expires);
281 assert.fail(noExpectedException);
282 } catch (e) {
283 assert.deepStrictEqual(e, expectedExeption, noExpectedException);
284 }
285 });
286 }); // expireLink
287
288 describe('updateLink', function () {
289 let id, expires;
290 it('stubbed success', async function () {
291 const returns = {
292 rowCount: 1,
293 rows: [ { id: 1 } ],
294 duration: 10,
295 };
296 const expected = {
297 changes: 1,
298 duration: 10,
299 lastInsertRowid: 1,
300 };
301 id = 'id';
302 expires = null;
303 db.db.result.resolves(returns);
304 const result = await db.updateLink(dbCtx, id, expires);
305 assert.deepStrictEqual(result, expected);
306 });
307 it('stubbed failure', async function () {
308 const expectedExeption = new Error('blah');
309 id = 'id';
310 expires = null;
311 db.db.result.rejects(expectedExeption);
312 try {
313 await db.updateLink(dbCtx, id, expires);
314 assert.fail(noExpectedException);
315 } catch (e) {
316 assert.deepStrictEqual(e, expectedExeption, noExpectedException);
317 }
318 });
319 }); // updateLink
320
321 describe('getAllLinks', function () {
322 it('stubbed success', async function () {
323 const expected = [
324 {
325 id: 'id',
326 url: 'url',
327 created: 0,
328 expires: 0,
329 lastAccess: 0,
330 },
331 ];
332 db.db.manyOrNone.resolves(expected);
333 const result = await db.getAllLinks(dbCtx);
334 assert.deepStrictEqual(result, expected);
335 });
336 it('stubbed failure', async function () {
337 const expectedExeption = new Error('blah');
338 db.db.manyOrNone.rejects(expectedExeption);
339 try {
340 await db.getAllLinks(dbCtx);
341 assert.fail(noExpectedException);
342 } catch (e) {
343 assert.deepStrictEqual(e, expectedExeption, noExpectedException);
344 }
345 });
346 }); // getAllLinks
347
348 });