3 const assert
= require('node:assert');
4 const sinon
= require('sinon');
5 const DBErrors
= require('../../../../src/db/errors');
7 const noExpectedException
= 'did not get expected exception';
9 describe('SQLiteDatabase', function () {
10 const SQLiteDatabase
= require('../../../../src/db/sqlite');
11 let db
, logger
, options
, dbCtx
;
13 beforeEach(function () {
20 db
= new SQLiteDatabase(logger
, options
);
24 describe('context', function () {
25 it('covers', async
function () {
26 const fn
= sinon
.stub();
32 describe('transaction', function () {
33 it('covers', async
function () {
34 const fn
= sinon
.stub();
35 await db
.transaction(dbCtx
, fn
);
38 it('covers rollback', async
function () {
39 const fn
= sinon
.stub();
40 fn
.throws(new Error('rollback'));
42 await db
.transaction(dbCtx
, fn
);
43 assert
.fail(noExpectedException
);
45 assert
.strictEqual(e
.message
, 'rollback', noExpectedException
);
50 describe('getAuthById', function () {
52 beforeEach(function () {
53 sinon
.stub(db
.statement
.getAuthById
, 'get');
56 it('stubbed success', async
function () {
63 db
.statement
.getAuthById
.get.returns(expected
);
64 const result
= await db
.getAuthById(dbCtx
, id
);
65 assert
.deepStrictEqual(result
, expected
);
67 it('stubbed failure', async
function () {
68 const expectedExeption
= new Error('blah');
70 db
.statement
.getAuthById
.get.throws(expectedExeption
);
72 await db
.getAuthById(dbCtx
, id
);
73 assert
.fail(noExpectedException
);
75 assert
.deepStrictEqual(e
, expectedExeption
, noExpectedException
);
80 describe('upsertAuth', function () {
81 let id
, secret
, credential
;
82 beforeEach(function () {
83 sinon
.stub(db
.statement
.insertAuth
, 'run').returns({ changes: 1n
, lastInsertRowid: 123n
});
84 sinon
.stub(db
.statement
.updateAuth
, 'run').returns({ changes: 1n
, lastInsertRowid: 123n
});
86 it('stubbed insert success', async
function () {
87 await db
.upsertAuth(dbCtx
, id
, secret
, credential
);
89 it('stubbed update success', async
function () {
90 db
.statement
.insertAuth
.run
.throws({ code: 'SQLITE_CONSTRAINT_UNIQUE' });
91 await db
.upsertAuth(dbCtx
, id
, secret
, credential
);
93 it('covers error', async
function () {
94 const expectedException
= new Error('blah');
95 db
.statement
.insertAuth
.run
.throws(expectedException
);
97 await db
.upsertAuth(dbCtx
, id
, secret
, credential
);
98 assert
.fail(noExpectedException
);
100 assert
.deepStrictEqual(e
, expectedException
, noExpectedException
);
103 it('covers unexpected error', async
function () {
104 const expectedException
= DBErrors
.UnexpectedResult
;
107 lastInsertRowid: undefined,
109 db
.statement
.insertAuth
.run
.returns(returns
);
111 await db
.upsertAuth(dbCtx
, id
, secret
, credential
);
112 assert
.fail(noExpectedException
);
114 assert(e
instanceof expectedException
, noExpectedException
);
119 describe('insertLink', function () {
120 let id
, url
, authToken
;
121 beforeEach(function () {
122 sinon
.stub(db
.statement
.insertLink
, 'run');
123 sinon
.stub(db
.statement
.updateLink
, 'run');
126 it('stubbed insert success', async
function () {
129 lastInsertRowid: BigInt(123),
132 db
.statement
.insertLink
.run
.returns(info
);
135 lastInsertRowid: 123,
137 const result
= await db
.insertLink(dbCtx
, id
, url
, authToken
);
138 assert
.deepStrictEqual(result
, expected
);
140 it('stubbed update success', async
function () {
143 lastInsertRowid: BigInt(123),
146 db
.statement
.insertLink
.run
.throws({ code: 'SQLITE_CONSTRAINT_UNIQUE' });
147 db
.statement
.updateLink
.run
.returns(info
);
150 lastInsertRowid: 123,
152 const result
= await db
.insertLink(dbCtx
, id
, url
, authToken
);
153 assert
.deepStrictEqual(result
, expected
);
155 it('stubbed failure', async
function () {
156 const expectedExeption
= new Error('blah');
158 db
.statement
.insertLink
.run
.throws(expectedExeption
);
160 await db
.insertLink(dbCtx
, id
, url
, authToken
);
161 assert
.fail(noExpectedException
);
163 assert
.deepStrictEqual(e
, expectedExeption
, noExpectedException
);
166 it('stubbed unexpected failure', async
function () {
167 const expectedException
= DBErrors
.UnexpectedResult
;
170 lastInsertRowid: undefined,
173 db
.statement
.insertLink
.run
.returns(returns
);
175 await db
.insertLink(dbCtx
, id
, url
, authToken
);
176 assert
.fail(noExpectedException
);
178 assert(e
instanceof expectedException
);
183 describe('getLinkById', function () {
186 beforeEach(function () {
187 sinon
.stub(db
.statement
.getLinkById
, 'get');
190 it('stubbed success', async
function () {
212 db
.statement
.getLinkById
.get.returns(returns
);
213 const result
= await db
.getLinkById(dbCtx
, id
);
214 assert
.deepStrictEqual(result
, expected
);
216 it('stubbed failure', async
function () {
217 const expectedExeption
= new Error('blah');
219 db
.statement
.getLinkById
.get.throws(expectedExeption
);
221 await db
.getLinkById(dbCtx
, id
);
222 assert
.fail(noExpectedException
);
224 assert
.deepStrictEqual(e
, expectedExeption
, noExpectedException
);
229 describe('getLinkByUrl', function () {
232 beforeEach(function () {
233 sinon
.stub(db
.statement
.getLinkByUrl
, 'get');
236 it('stubbed success', async
function () {
258 db
.statement
.getLinkByUrl
.get.returns(returns
);
259 const result
= await db
.getLinkByUrl(dbCtx
, url
);
260 assert
.deepStrictEqual(result
, expected
);
262 it('stubbed failure', async
function () {
263 const expectedExeption
= new Error('blah');
265 db
.statement
.getLinkByUrl
.get.throws(expectedExeption
);
267 await db
.getLinkByUrl(dbCtx
, url
);
268 assert
.fail(noExpectedException
);
270 assert
.deepStrictEqual(e
, expectedExeption
, noExpectedException
);
275 describe('accessLink', function () {
278 beforeEach(function () {
279 sinon
.stub(db
.statement
.getLinkById
, 'get');
280 sinon
.stub(db
.statement
.incrementLinkAccess
, 'run');
283 it('stubbed exists success', async
function () {
305 db
.statement
.getLinkById
.get.returns(returns
);
306 db
.statement
.incrementLinkAccess
.run
.returns({ changes: 1 });
307 const result
= await db
.accessLink(dbCtx
, id
);
308 assert
.deepStrictEqual(result
, expected
);
310 it('stubbed missing success', async
function () {
311 const returns
= undefined;
312 const expected
= undefined;
314 db
.statement
.getLinkById
.get.returns(returns
);
315 db
.statement
.incrementLinkAccess
.run
.returns({ changes: 0 });
316 const result
= await db
.accessLink(dbCtx
, id
);
317 assert
.deepStrictEqual(result
, expected
);
319 it('stubbed increment failure', async
function () {
320 const expectedExeption
= DBErrors
.UnexpectedResult
;
331 db
.statement
.getLinkById
.get.returns(returns
);
332 db
.statement
.incrementLinkAccess
.run
.returns({ changes: 0 });
334 await db
.accessLink(dbCtx
, id
);
335 assert
.fail(noExpectedException
);
337 assert(e
instanceof expectedExeption
, noExpectedException
);
340 it('stubbed failure', async
function () {
341 const expectedExeption
= new Error('blah');
343 db
.statement
.getLinkById
.get.throws(expectedExeption
);
345 await db
.accessLink(dbCtx
, id
);
346 assert
.fail(noExpectedException
);
348 assert
.deepStrictEqual(e
, expectedExeption
, noExpectedException
);
353 describe('expireLink', function () {
356 beforeEach(function () {
357 sinon
.stub(db
.statement
.expireLink
, 'run');
360 it('stubbed success', async
function () {
363 lastInsertRowid: 123,
367 lastInsertRowid: 123,
371 db
.statement
.expireLink
.run
.returns(returns
);
372 const result
= await db
.expireLink(dbCtx
, id
, expires
);
373 assert
.deepStrictEqual(result
, expected
);
375 it('stubbed change failure', async
function () {
376 const expectedExeption
= DBErrors
.UnexpectedResult
;
379 lastInsertRowid: undefined,
382 db
.statement
.expireLink
.run
.returns(returns
);
384 await db
.expireLink(dbCtx
, id
);
385 assert
.fail(noExpectedException
);
387 assert(e
instanceof expectedExeption
, noExpectedException
);
390 it('stubbed failure', async
function () {
391 const expectedExeption
= new Error('blah');
394 db
.statement
.expireLink
.run
.throws(expectedExeption
);
396 await db
.expireLink(dbCtx
, id
, expires
);
397 assert
.fail(noExpectedException
);
399 assert
.deepStrictEqual(e
, expectedExeption
, noExpectedException
);
404 describe('updateLink', function () {
407 beforeEach(function () {
408 sinon
.stub(db
.statement
.updateLink
, 'run');
411 it('stubbed success', async
function () {
422 db
.statement
.updateLink
.run
.returns(returns
);
423 const result
= await db
.updateLink(dbCtx
, id
, expires
);
424 assert
.deepStrictEqual(result
, expected
);
426 it('stubbed change failure', async
function () {
427 const expectedExeption
= DBErrors
.UnexpectedResult
;
430 lastInsertRowid: undefined,
433 db
.statement
.updateLink
.run
.returns(returns
);
435 await db
.updateLink(dbCtx
, id
);
436 assert
.fail(noExpectedException
);
438 assert(e
instanceof expectedExeption
, noExpectedException
);
441 it('stubbed failure', async
function () {
442 const expectedExeption
= new Error('blah');
445 db
.statement
.updateLink
.run
.throws(expectedExeption
);
447 await db
.updateLink(dbCtx
, id
, expires
);
448 assert
.fail(noExpectedException
);
450 assert
.deepStrictEqual(e
, expectedExeption
, noExpectedException
);
455 describe('getAllLinks', function () {
456 beforeEach(function () {
457 sinon
.stub(db
.statement
.linkGetAll
, 'all');
460 it('stubbed success', async
function () {
485 db
.statement
.linkGetAll
.all
.returns(returns
);
486 const result
= await db
.getAllLinks(dbCtx
);
487 assert
.deepStrictEqual(result
, expected
);
489 it('stubbed failure', async
function () {
490 const expectedExeption
= new Error('blah');
491 db
.statement
.linkGetAll
.all
.throws(expectedExeption
);
493 await db
.getAllLinks(dbCtx
);
494 assert
.fail(noExpectedException
);
496 assert
.deepStrictEqual(e
, expectedExeption
, noExpectedException
);
501 describe('_optimize', function () {
503 beforeEach(function () {
504 cslo
= db
.changesSinceLastOptimize
;
505 oac
= db
.optimizeAfterChanges
;
506 sinon
.stub(db
.db
, 'prepare').returns({
509 sinon
.stub(db
.db
, 'pragma');
511 afterEach(function () {
512 db
.changesSinceLastOptimize
= cslo
;
513 db
.optimizeAfterChanges
= oac
;
515 it('covers', function () {
517 assert(db
.db
.pragma
.called
);
519 it('_maybeOptimize', function () {
520 db
.changesSinceLastOptimize
= BigInt(1000);
521 db
.optimizeAfterChanges
= BigInt(10);
522 sinon
.stub(db
, '_optimize');
524 assert(db
._optimize
.called
);