auth cleanups
[urlittler] / test / src / db / index.js
1 'use strict';
2
3 const assert = require('node:assert');
4 const sinon = require('sinon');
5 const DatabaseFactory = require('../../../src/db');
6 const DBErrors = require('../../../src/db/errors');
7
8 const noExpectedException = 'did not get expected exception';
9
10 describe('DatabaseFactory', function () {
11 let db, logger, options;
12
13 beforeEach(function () {
14 logger = {
15 debug: () => {},
16 error: () => {},
17 info: () => {},
18 };
19 options = {};
20 });
21
22 afterEach(function () {
23 sinon.restore();
24 });
25
26 it('fails on invalid engine', function () {
27 try {
28 new DatabaseFactory(logger, options);
29 assert.fail(noExpectedException);
30 } catch (e) {
31 assert(e instanceof DBErrors.UnsupportedEngine);
32 }
33 });
34
35 it('creates sqlite engine', function () {
36 this.slow(500);
37 options.connectionString = 'sqlite://';
38 db = new DatabaseFactory(logger, options);
39 assert.strictEqual(db.constructor.name, 'SQLiteDatabase');
40 });
41
42 it('creates postgres engine', function () {
43 this.slow(500);
44 const stubPgp = sinon.stub();
45 stubPgp.utils = {
46 enumSql: sinon.stub().returns({}),
47 };
48 stubPgp.QueryFile = sinon.stub().returns({});
49 options.connectionString = 'postgresql://';
50 db = new DatabaseFactory(logger, options, stubPgp);
51 assert.strictEqual(db.constructor.name, 'PostgresDatabase');
52 });
53
54 });