-'use strict';
-
-const assert = require('node:assert');
-const sinon = require('sinon');
-const DBErrors = require('../../../../src/db/errors');
-
-const noExpectedException = 'did not get expected exception';
-
-describe('SQLiteDatabase', function () {
- const SQLiteDatabase = require('../../../../src/db/sqlite-old');
- let db, logger, options, dbCtx;
-
- beforeEach(function () {
- logger = {
- debug: sinon.stub(),
- info: sinon.stub(),
- error: sinon.stub(),
- };
- options = {};
- db = new SQLiteDatabase(logger, options);
- dbCtx = undefined;
- });
-
- describe('context', function () {
- it('covers', async function () {
- const fn = sinon.stub();
- await db.context(fn);
- assert(fn.called);
- });
- }); // context
-
- describe('transaction', function () {
- it('covers', async function () {
- const fn = sinon.stub();
- await db.transaction(dbCtx, fn);
- assert(fn.called);
- });
- it('covers rollback', async function () {
- const fn = sinon.stub();
- fn.throws(new Error('rollback'));
- try {
- await db.transaction(dbCtx, fn);
- assert.fail(noExpectedException);
- } catch (e) {
- assert.strictEqual(e.message, 'rollback', noExpectedException);
- }
- });
- }); // transaction
-
- describe('getAuthById', function () {
- let id;
- beforeEach(function () {
- sinon.stub(db.statement.getAuthById, 'get');
- });
-
- it('stubbed success', async function () {
- const expected = {
- id: 'id',
- secret: 'secret',
- password: 'password',
- };
- id = 'id';
- db.statement.getAuthById.get.returns(expected);
- const result = await db.getAuthById(dbCtx, id);
- assert.deepStrictEqual(result, expected);
- });
- it('stubbed failure', async function () {
- const expectedExeption = new Error('blah');
- id = 'id';
- db.statement.getAuthById.get.throws(expectedExeption);
- try {
- await db.getAuthById(dbCtx, id);
- assert.fail(noExpectedException);
- } catch (e) {
- assert.deepStrictEqual(e, expectedExeption, noExpectedException);
- }
- });
- }); // getAuthById
-
- describe('upsertAuth', function () {
- let id, secret, credential;
- beforeEach(function () {
- sinon.stub(db.statement.insertAuth, 'run').returns({ changes: 1n, lastInsertRowid: 123n });
- sinon.stub(db.statement.updateAuth, 'run').returns({ changes: 1n, lastInsertRowid: 123n });
- });
- it('stubbed insert success', async function () {
- await db.upsertAuth(dbCtx, id, secret, credential);
- });
- it('stubbed update success', async function () {
- db.statement.insertAuth.run.throws({ code: 'SQLITE_CONSTRAINT_UNIQUE' });
- await db.upsertAuth(dbCtx, id, secret, credential);
- });
- it('covers error', async function () {
- const expectedException = new Error('blah');
- db.statement.insertAuth.run.throws(expectedException);
- try {
- await db.upsertAuth(dbCtx, id, secret, credential);
- assert.fail(noExpectedException);
- } catch (e) {
- assert.deepStrictEqual(e, expectedException, noExpectedException);
- }
- });
- it('covers unexpected error', async function () {
- const expectedException = DBErrors.UnexpectedResult;
- const returns = {
- changes: 0n,
- lastInsertRowid: undefined,
- };
- db.statement.insertAuth.run.returns(returns);
- try {
- await db.upsertAuth(dbCtx, id, secret, credential);
- assert.fail(noExpectedException);
- } catch (e) {
- assert(e instanceof expectedException, noExpectedException);
- }
- });
- }); // upsertAuth
-
- describe('upsertLink', function () {
- let id, url, authToken;
- beforeEach(function () {
- sinon.stub(db.statement.upsertLink, 'run');
- sinon.stub(db.statement.updateLink, 'run');
- });
-
- it('stubbed insert success', async function () {
- const info = {
- changes: BigInt(1),
- lastInsertRowid: BigInt(123),
- };
- id = 'id';
- db.statement.upsertLink.run.returns(info);
- const expected = {
- changes: 1,
- lastInsertRowid: 123,
- };
- const result = await db.upsertLink(dbCtx, id, url, authToken);
- assert.deepStrictEqual(result, expected);
- });
- it('stubbed update success', async function () {
- const info = {
- changes: BigInt(1),
- lastInsertRowid: BigInt(123),
- };
- id = 'id';
- db.statement.upsertLink.run.throws({ code: 'SQLITE_CONSTRAINT_UNIQUE' });
- db.statement.updateLink.run.returns(info);
- const expected = {
- changes: 1,
- lastInsertRowid: 123,
- };
- const result = await db.upsertLink(dbCtx, id, url, authToken);
- assert.deepStrictEqual(result, expected);
- });
- it('stubbed failure', async function () {
- const expectedExeption = new Error('blah');
- id = 'id';
- db.statement.upsertLink.run.throws(expectedExeption);
- try {
- await db.upsertLink(dbCtx, id, url, authToken);
- assert.fail(noExpectedException);
- } catch (e) {
- assert.deepStrictEqual(e, expectedExeption, noExpectedException);
- }
- });
- it('stubbed unexpected failure', async function () {
- const expectedException = DBErrors.UnexpectedResult;
- const returns = {
- changes: 0,
- lastInsertRowid: undefined,
- };
- id = 'id';
- db.statement.upsertLink.run.returns(returns);
- try {
- await db.upsertLink(dbCtx, id, url, authToken);
- assert.fail(noExpectedException);
- } catch (e) {
- assert(e instanceof expectedException);
- }
- });
- }); // upsertLink
-
- describe('getLinkById', function () {
- let id;
-
- beforeEach(function () {
- sinon.stub(db.statement.getLinkById, 'get');
- });
-
- it('stubbed success', async function () {
- const returns = {
- id: 'id',
- isSpecial: false,
- url: 'url',
- created: 0,
- expires: 0,
- 'auth_token': 'abc',
- 'last_access': 0,
- accesses: 0,
- };
- const expected = {
- id: 'id',
- isSpecial: false,
- url: 'url',
- created: 0,
- expires: 0,
- authToken: 'abc',
- lastAccess: 0,
- accesses: 0,
- };
- id = 'id';
- db.statement.getLinkById.get.returns(returns);
- const result = await db.getLinkById(dbCtx, id);
- assert.deepStrictEqual(result, expected);
- });
- it('stubbed failure', async function () {
- const expectedExeption = new Error('blah');
- id = 'id';
- db.statement.getLinkById.get.throws(expectedExeption);
- try {
- await db.getLinkById(dbCtx, id);
- assert.fail(noExpectedException);
- } catch (e) {
- assert.deepStrictEqual(e, expectedExeption, noExpectedException);
- }
- });
- }); // getLinkById
-
- describe('getLinkByUrl', function () {
- let url;
-
- beforeEach(function () {
- sinon.stub(db.statement.getLinkByUrl, 'get');
- });
-
- it('stubbed success', async function () {
- const returns = {
- id: 'id',
- isSpecial: false,
- url: 'url',
- created: 0,
- expires: 123,
- 'auth_token': 'abc',
- 'last_access': 0,
- accesses: 0,
- };
- const expected = {
- id: 'id',
- isSpecial: false,
- url: 'url',
- created: 0,
- expires: 123,
- authToken: 'abc',
- lastAccess: 0,
- accesses: 0,
- };
- url = 'url';
- db.statement.getLinkByUrl.get.returns(returns);
- const result = await db.getLinkByUrl(dbCtx, url);
- assert.deepStrictEqual(result, expected);
- });
- it('stubbed failure', async function () {
- const expectedExeption = new Error('blah');
- url = 'url';
- db.statement.getLinkByUrl.get.throws(expectedExeption);
- try {
- await db.getLinkByUrl(dbCtx, url);
- assert.fail(noExpectedException);
- } catch (e) {
- assert.deepStrictEqual(e, expectedExeption, noExpectedException);
- }
- });
- }); // getLinkByUrl
-
- describe('accessLink', function () {
- let id;
-
- beforeEach(function () {
- sinon.stub(db.statement.getLinkById, 'get');
- sinon.stub(db.statement.incrementLinkAccess, 'run');
- });
-
- it('stubbed exists success', async function () {
- const returns = {
- id: 'id',
- isSpecial: false,
- url: 'url',
- created: 0,
- expires: 0,
- 'auth_token': 'abc',
- 'last_access': 0,
- accesses: 0,
- };
- const expected = {
- id: 'id',
- isSpecial: false,
- url: 'url',
- created: 0,
- expires: 0,
- authToken: 'abc',
- lastAccess: 0,
- accesses: 0,
- };
- id = 'id';
- db.statement.getLinkById.get.returns(returns);
- db.statement.incrementLinkAccess.run.returns({ changes: 1 });
- const result = await db.accessLink(dbCtx, id);
- assert.deepStrictEqual(result, expected);
- });
- it('stubbed missing success', async function () {
- const returns = undefined;
- const expected = undefined;
- id = 'id';
- db.statement.getLinkById.get.returns(returns);
- db.statement.incrementLinkAccess.run.returns({ changes: 0 });
- const result = await db.accessLink(dbCtx, id);
- assert.deepStrictEqual(result, expected);
- });
- it('stubbed increment failure', async function () {
- const expectedExeption = DBErrors.UnexpectedResult;
- const returns = {
- id: 'id',
- url: 'url',
- created: 0,
- expires: 0,
- 'auth_token': 'abc',
- 'last_access': 0,
- accesses: 0,
- };
- id = 'id';
- db.statement.getLinkById.get.returns(returns);
- db.statement.incrementLinkAccess.run.returns({ changes: 0 });
- try {
- await db.accessLink(dbCtx, id);
- assert.fail(noExpectedException);
- } catch (e) {
- assert(e instanceof expectedExeption, noExpectedException);
- }
- });
- it('stubbed failure', async function () {
- const expectedExeption = new Error('blah');
- id = 'id';
- db.statement.getLinkById.get.throws(expectedExeption);
- try {
- await db.accessLink(dbCtx, id);
- assert.fail(noExpectedException);
- } catch (e) {
- assert.deepStrictEqual(e, expectedExeption, noExpectedException);
- }
- });
- }); // accessLink
-
- describe('expireLink', function () {
- let id, expires;
-
- beforeEach(function () {
- sinon.stub(db.statement.expireLink, 'run');
- });
-
- it('stubbed success', async function () {
- const returns = {
- changes: 1,
- lastInsertRowid: 123,
- };
- const expected = {
- changes: 1,
- lastInsertRowid: 123,
- };
- id = 'id';
- expires = null;
- db.statement.expireLink.run.returns(returns);
- const result = await db.expireLink(dbCtx, id, expires);
- assert.deepStrictEqual(result, expected);
- });
- it('stubbed change failure', async function () {
- const expectedExeption = DBErrors.UnexpectedResult;
- const returns = {
- changes: 0,
- lastInsertRowid: undefined,
- };
- id = 'id';
- db.statement.expireLink.run.returns(returns);
- try {
- await db.expireLink(dbCtx, id);
- assert.fail(noExpectedException);
- } catch (e) {
- assert(e instanceof expectedExeption, noExpectedException);
- }
- });
- it('stubbed failure', async function () {
- const expectedExeption = new Error('blah');
- id = 'id';
- expires = null;
- db.statement.expireLink.run.throws(expectedExeption);
- try {
- await db.expireLink(dbCtx, id, expires);
- assert.fail(noExpectedException);
- } catch (e) {
- assert.deepStrictEqual(e, expectedExeption, noExpectedException);
- }
- });
- }); // expireLink
-
- describe('getAllLinks', function () {
- beforeEach(function () {
- sinon.stub(db.statement.linkGetAll, 'all');
- });
-
- it('stubbed success', async function () {
- const returns = [
- {
- id: 'id',
- isSpecial: false,
- url: 'url',
- created: 0,
- expires: 0,
- 'auth_token': 'abc',
- 'last_access': 0,
- accesses: 0,
- },
- ];
- const expected = [
- {
- id: 'id',
- isSpecial: false,
- url: 'url',
- created: 0,
- expires: 0,
- authToken: 'abc',
- lastAccess: 0,
- accesses: 0,
- },
- ];
- db.statement.linkGetAll.all.returns(returns);
- const result = await db.getAllLinks(dbCtx);
- assert.deepStrictEqual(result, expected);
- });
- it('stubbed failure', async function () {
- const expectedExeption = new Error('blah');
- db.statement.linkGetAll.all.throws(expectedExeption);
- try {
- await db.getAllLinks(dbCtx);
- assert.fail(noExpectedException);
- } catch (e) {
- assert.deepStrictEqual(e, expectedExeption, noExpectedException);
- }
- });
- }); // getAllLinks
-
- describe('_optimize', function () {
- let cslo, oac;
- beforeEach(function () {
- cslo = db.changesSinceLastOptimize;
- oac = db.optimizeAfterChanges;
- sinon.stub(db.db, 'prepare').returns({
- all: sinon.stub(),
- });
- sinon.stub(db.db, 'pragma');
- });
- afterEach(function () {
- db.changesSinceLastOptimize = cslo;
- db.optimizeAfterChanges = oac;
- });
- it('covers', function () {
- db._optimize();
- assert(db.db.pragma.called);
- });
- it('_maybeOptimize', function () {
- db.changesSinceLastOptimize = BigInt(1000);
- db.optimizeAfterChanges = BigInt(10);
- sinon.stub(db, '_optimize');
- db._maybeOptimize();
- assert(db._optimize.called);
- });
- });
-
-});
\ No newline at end of file