1 /* eslint-disable capitalized-comments */
6 const assert
= require('assert');
7 const sinon
= require('sinon'); // eslint-disable-line node/no-unpublished-require
8 const DBErrors
= require('../../../../src/db/errors');
10 const noExpectedException
= 'did not get expected exception';
12 describe('SQLiteDatabase', function () {
13 const SQLiteDatabase
= require('../../../../src/db/sqlite');
14 let db
, logger
, options
, dbCtx
;
16 beforeEach(function () {
23 db
= new SQLiteDatabase(logger
, options
);
27 describe('context', function () {
28 it('covers', async
function () {
29 const fn
= sinon
.stub();
35 describe('transaction', function () {
36 it('covers', async
function () {
37 const fn
= sinon
.stub();
38 await db
.transaction(dbCtx
, fn
);
43 describe('getAuthById', function () {
45 beforeEach(function () {
46 sinon
.stub(db
.statement
.getAuthById
, 'get');
49 it('stubbed success', async
function () {
56 db
.statement
.getAuthById
.get.returns(expected
);
57 const result
= await db
.getAuthById(dbCtx
, id
);
58 assert
.deepStrictEqual(result
, expected
);
60 it('stubbed failure', async
function () {
61 const expectedExeption
= new Error('blah');
63 db
.statement
.getAuthById
.get.throws(expectedExeption
);
65 await db
.getAuthById(dbCtx
, id
);
66 assert
.fail(noExpectedException
);
68 assert
.deepStrictEqual(e
, expectedExeption
, noExpectedException
);
73 describe('insertLink', function () {
74 let id
, url
, authToken
;
75 beforeEach(function () {
76 sinon
.stub(db
.statement
.insertLink
, 'run');
77 sinon
.stub(db
.statement
.updateLink
, 'run');
80 it('stubbed insert success', async
function () {
83 lastInsertRowid: BigInt(123),
86 db
.statement
.insertLink
.run
.returns(info
);
91 const result
= await db
.insertLink(dbCtx
, id
, url
, authToken
);
92 assert
.deepStrictEqual(result
, expected
);
94 it('stubbed update success', async
function () {
97 lastInsertRowid: BigInt(123),
100 db
.statement
.insertLink
.run
.throws({ code: 'SQLITE_CONSTRAINT_UNIQUE' });
101 db
.statement
.updateLink
.run
.returns(info
);
104 lastInsertRowid: 123,
106 const result
= await db
.insertLink(dbCtx
, id
, url
, authToken
);
107 assert
.deepStrictEqual(result
, expected
);
109 it('stubbed failure', async
function () {
110 const expectedExeption
= new Error('blah');
112 db
.statement
.insertLink
.run
.throws(expectedExeption
);
114 await db
.insertLink(dbCtx
, id
, url
, authToken
);
115 assert
.fail(noExpectedException
);
117 assert
.deepStrictEqual(e
, expectedExeption
, noExpectedException
);
120 it('stubbed unexpected failure', async
function () {
121 const expectedException
= DBErrors
.UnexpectedResult
;
124 lastInsertRowid: undefined,
127 db
.statement
.insertLink
.run
.returns(returns
);
129 await db
.insertLink(dbCtx
, id
, url
, authToken
);
130 assert
.fail(noExpectedException
);
132 assert(e
instanceof expectedException
);
137 describe('getLinkById', function () {
140 beforeEach(function () {
141 sinon
.stub(db
.statement
.getLinkById
, 'get');
144 it('stubbed success', async
function () {
164 db
.statement
.getLinkById
.get.returns(returns
);
165 const result
= await db
.getLinkById(dbCtx
, id
);
166 assert
.deepStrictEqual(result
, expected
);
168 it('stubbed failure', async
function () {
169 const expectedExeption
= new Error('blah');
171 db
.statement
.getLinkById
.get.throws(expectedExeption
);
173 await db
.getLinkById(dbCtx
, id
);
174 assert
.fail(noExpectedException
);
176 assert
.deepStrictEqual(e
, expectedExeption
, noExpectedException
);
181 describe('getLinkByUrl', function () {
184 beforeEach(function () {
185 sinon
.stub(db
.statement
.getLinkByUrl
, 'get');
188 it('stubbed success', async
function () {
208 db
.statement
.getLinkByUrl
.get.returns(returns
);
209 const result
= await db
.getLinkByUrl(dbCtx
, url
);
210 assert
.deepStrictEqual(result
, expected
);
212 it('stubbed failure', async
function () {
213 const expectedExeption
= new Error('blah');
215 db
.statement
.getLinkByUrl
.get.throws(expectedExeption
);
217 await db
.getLinkByUrl(dbCtx
, url
);
218 assert
.fail(noExpectedException
);
220 assert
.deepStrictEqual(e
, expectedExeption
, noExpectedException
);
225 describe('accessLink', function () {
228 beforeEach(function () {
229 sinon
.stub(db
.statement
.getLinkById
, 'get');
230 sinon
.stub(db
.statement
.incrementLinkAccess
, 'run');
233 it('stubbed exists success', async
function () {
253 db
.statement
.getLinkById
.get.returns(returns
);
254 db
.statement
.incrementLinkAccess
.run
.returns({ changes: 1 });
255 const result
= await db
.accessLink(dbCtx
, id
);
256 assert
.deepStrictEqual(result
, expected
);
258 it('stubbed missing success', async
function () {
259 const returns
= undefined;
260 const expected
= undefined;
262 db
.statement
.getLinkById
.get.returns(returns
);
263 db
.statement
.incrementLinkAccess
.run
.returns({ changes: 0 });
264 const result
= await db
.accessLink(dbCtx
, id
);
265 assert
.deepStrictEqual(result
, expected
);
267 it('stubbed increment failure', async
function () {
268 const expectedExeption
= DBErrors
.UnexpectedResult
;
279 db
.statement
.getLinkById
.get.returns(returns
);
280 db
.statement
.incrementLinkAccess
.run
.returns({ changes: 0 });
282 await db
.accessLink(dbCtx
, id
);
283 assert
.fail(noExpectedException
);
285 assert(e
instanceof expectedExeption
, noExpectedException
);
288 it('stubbed failure', async
function () {
289 const expectedExeption
= new Error('blah');
291 db
.statement
.getLinkById
.get.throws(expectedExeption
);
293 await db
.accessLink(dbCtx
, id
);
294 assert
.fail(noExpectedException
);
296 assert
.deepStrictEqual(e
, expectedExeption
, noExpectedException
);
301 describe('expireLink', function () {
304 beforeEach(function () {
305 sinon
.stub(db
.statement
.expireLink
, 'run');
308 it('stubbed success', async
function () {
311 lastInsertRowid: 123,
315 lastInsertRowid: 123,
319 db
.statement
.expireLink
.run
.returns(returns
);
320 const result
= await db
.expireLink(dbCtx
, id
, expires
);
321 assert
.deepStrictEqual(result
, expected
);
323 it('stubbed change failure', async
function () {
324 const expectedExeption
= DBErrors
.UnexpectedResult
;
327 lastInsertRowid: undefined,
330 db
.statement
.expireLink
.run
.returns(returns
);
332 await db
.expireLink(dbCtx
, id
);
333 assert
.fail(noExpectedException
);
335 assert(e
instanceof expectedExeption
, noExpectedException
);
338 it('stubbed failure', async
function () {
339 const expectedExeption
= new Error('blah');
342 db
.statement
.expireLink
.run
.throws(expectedExeption
);
344 await db
.expireLink(dbCtx
, id
, expires
);
345 assert
.fail(noExpectedException
);
347 assert
.deepStrictEqual(e
, expectedExeption
, noExpectedException
);
352 describe('updateLink', function () {
355 beforeEach(function () {
356 sinon
.stub(db
.statement
.updateLink
, 'run');
359 it('stubbed success', async
function () {
370 db
.statement
.updateLink
.run
.returns(returns
);
371 const result
= await db
.updateLink(dbCtx
, id
, expires
);
372 assert
.deepStrictEqual(result
, expected
);
374 it('stubbed change failure', async
function () {
375 const expectedExeption
= DBErrors
.UnexpectedResult
;
378 lastInsertRowid: undefined,
381 db
.statement
.updateLink
.run
.returns(returns
);
383 await db
.updateLink(dbCtx
, id
);
384 assert
.fail(noExpectedException
);
386 assert(e
instanceof expectedExeption
, noExpectedException
);
389 it('stubbed failure', async
function () {
390 const expectedExeption
= new Error('blah');
393 db
.statement
.updateLink
.run
.throws(expectedExeption
);
395 await db
.updateLink(dbCtx
, id
, expires
);
396 assert
.fail(noExpectedException
);
398 assert
.deepStrictEqual(e
, expectedExeption
, noExpectedException
);
403 describe('getAllLinks', function () {
404 beforeEach(function () {
405 sinon
.stub(db
.statement
.linkGetAll
, 'all');
408 it('stubbed success', async
function () {
431 db
.statement
.linkGetAll
.all
.returns(returns
);
432 const result
= await db
.getAllLinks(dbCtx
);
433 assert
.deepStrictEqual(result
, expected
);
435 it('stubbed failure', async
function () {
436 const expectedExeption
= new Error('blah');
437 db
.statement
.linkGetAll
.all
.throws(expectedExeption
);
439 await db
.getAllLinks(dbCtx
);
440 assert
.fail(noExpectedException
);
442 assert
.deepStrictEqual(e
, expectedExeption
, noExpectedException
);
447 describe('_optimize', function () {
449 beforeEach(function () {
450 cslo
= db
.changesSinceLastOptimize
;
451 oac
= db
.optimizeAfterChanges
;
452 sinon
.stub(db
.db
, 'prepare').returns({
455 sinon
.stub(db
.db
, 'pragma');
457 afterEach(function () {
458 db
.changesSinceLastOptimize
= cslo
;
459 db
.optimizeAfterChanges
= oac
;
461 it('covers', function () {
463 assert(db
.db
.pragma
.called
);
465 it('_maybeOptimize', function () {
466 db
.changesSinceLastOptimize
= BigInt(1000);
467 db
.optimizeAfterChanges
= BigInt(10);
468 sinon
.stub(db
, '_optimize');
470 assert(db
._optimize
.called
);