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