1 /* eslint-disable capitalized-comments */
6 const assert
= require('assert');
7 const sinon
= require('sinon'); // eslint-disable-line node/no-unpublished-require
9 const noExpectedException
= 'did not get expected exception';
11 describe('PostgresDatabase', function () {
12 const PostgresDatabase
= require('../../../../src/db/postgres');
13 let pgpStub
, db
, logger
, options
, dbCtx
;
15 beforeEach(function () {
24 manyOrNone: sinon
.stub(),
25 oneOrNone: sinon
.stub(),
29 stub
.tx
= async (fn
) => await
fn(stub
);
30 stub
.txIf
= async (fn
) => await
fn(stub
);
31 stub
.task
= async (fn
) => await
fn(stub
);
37 pgpStub
.QueryFile
= class {};
38 pgpStub
.end
= () => {},
45 db
= new PostgresDatabase(logger
, options
, pgpStub
);
50 describe('context', function () {
51 it('covers', async
function () {
52 const fn
= sinon
.stub();
58 describe('transaction', function () {
59 it('covers', async
function () {
60 const fn
= sinon
.stub();
61 await db
.transaction(undefined, fn
);
66 describe('getAuthById', function () {
68 it('stubbed success', async
function () {
75 db
.db
.oneOrNone
.resolves(expected
);
76 const result
= await db
.getAuthById(dbCtx
, id
);
77 assert
.deepStrictEqual(result
, expected
);
79 it('stubbed failure', async
function () {
80 const expectedExeption
= new Error('blah');
82 db
.db
.oneOrNone
.rejects(expectedExeption
);
84 await db
.getAuthById(dbCtx
, id
);
85 assert
.fail(noExpectedException
);
87 assert
.deepStrictEqual(e
, expectedExeption
, noExpectedException
);
92 describe('_epochFix', function () {
93 it('clamps infinity', function () {
94 const epoch
= Infinity
;
95 const expected
= Number
.MAX_SAFE_INTEGER
;
96 const result
= PostgresDatabase
._epochFix(epoch
);
97 assert
.strictEqual(result
, expected
);
99 it('clamps negative infinity', function () {
100 const epoch
= -Infinity
;
102 const result
= PostgresDatabase
._epochFix(epoch
);
103 assert
.strictEqual(result
, expected
);
105 it('returns number', function () {
107 const expected
= 123;
108 const result
= PostgresDatabase
._epochFix(epoch
);
109 assert
.strictEqual(result
, expected
);
113 describe('_linkToNative', function () {
114 it('handles missing link', function () {
115 const link
= undefined;
116 const expected
= undefined;
117 const result
= PostgresDatabase
._linkToNative(link
);
118 assert
.deepStrictEqual(result
, expected
);
120 it('converts epochs', function () {
125 lastAccess: -Infinity
,
135 const result
= PostgresDatabase
._linkToNative(link
);
136 assert
.deepStrictEqual(result
, expected
);
140 describe('insertLink', function () {
141 let id
, url
, authToken
;
142 it('stubbed success', async
function () {
149 db
.db
.result
.resolves(returns
);
153 lastInsertRowid: undefined,
155 const result
= await db
.insertLink(dbCtx
, id
, url
, authToken
);
156 assert
.deepStrictEqual(result
, expected
);
158 it('stubbed failure', async
function () {
159 const expectedExeption
= new Error('blah');
161 db
.db
.result
.rejects(expectedExeption
);
163 await db
.insertLink(dbCtx
, id
, url
, authToken
);
164 assert
.fail(noExpectedException
);
166 assert
.deepStrictEqual(e
, expectedExeption
, noExpectedException
);
171 describe('getLinkById', function () {
173 it('stubbed success', async
function () {
182 db
.db
.oneOrNone
.resolves(expected
);
183 const result
= await db
.getLinkById(dbCtx
, id
);
184 assert
.deepStrictEqual(result
, expected
);
186 it('stubbed failure', async
function () {
187 const expectedExeption
= new Error('blah');
189 db
.db
.oneOrNone
.rejects(expectedExeption
);
191 await db
.getLinkById(dbCtx
, id
);
192 assert
.fail(noExpectedException
);
194 assert
.deepStrictEqual(e
, expectedExeption
, noExpectedException
);
199 describe('getLinkByUrl', function () {
201 it('stubbed success', async
function () {
210 db
.db
.oneOrNone
.resolves(expected
);
211 const result
= await db
.getLinkByUrl(dbCtx
, url
);
212 assert
.deepStrictEqual(result
, expected
);
214 it('stubbed failure', async
function () {
215 const expectedExeption
= new Error('blah');
217 db
.db
.oneOrNone
.rejects(expectedExeption
);
219 await db
.getLinkByUrl(dbCtx
, url
);
220 assert
.fail(noExpectedException
);
222 assert
.deepStrictEqual(e
, expectedExeption
, noExpectedException
);
227 describe('accessLink', function () {
229 it('stubbed success', async
function () {
238 db
.db
.oneOrNone
.resolves(expected
);
239 const result
= await db
.accessLink(dbCtx
, id
);
240 assert
.deepStrictEqual(result
, expected
);
242 it('stubbed failure', async
function () {
243 const expectedExeption
= new Error('blah');
245 db
.db
.oneOrNone
.rejects(expectedExeption
);
247 await db
.accessLink(dbCtx
, id
);
248 assert
.fail(noExpectedException
);
250 assert
.deepStrictEqual(e
, expectedExeption
, noExpectedException
);
255 describe('expireLink', function () {
257 it('stubbed success', async
function () {
270 db
.db
.result
.resolves(returns
);
271 const result
= await db
.expireLink(dbCtx
, id
, expires
);
272 assert
.deepStrictEqual(result
, expected
);
274 it('stubbed failure', async
function () {
275 const expectedExeption
= new Error('blah');
278 db
.db
.result
.rejects(expectedExeption
);
280 await db
.expireLink(dbCtx
, id
, expires
);
281 assert
.fail(noExpectedException
);
283 assert
.deepStrictEqual(e
, expectedExeption
, noExpectedException
);
288 describe('updateLink', function () {
290 it('stubbed success', async
function () {
303 db
.db
.result
.resolves(returns
);
304 const result
= await db
.updateLink(dbCtx
, id
, expires
);
305 assert
.deepStrictEqual(result
, expected
);
307 it('stubbed failure', async
function () {
308 const expectedExeption
= new Error('blah');
311 db
.db
.result
.rejects(expectedExeption
);
313 await db
.updateLink(dbCtx
, id
, expires
);
314 assert
.fail(noExpectedException
);
316 assert
.deepStrictEqual(e
, expectedExeption
, noExpectedException
);
321 describe('getAllLinks', function () {
322 it('stubbed success', async
function () {
332 db
.db
.manyOrNone
.resolves(expected
);
333 const result
= await db
.getAllLinks(dbCtx
);
334 assert
.deepStrictEqual(result
, expected
);
336 it('stubbed failure', async
function () {
337 const expectedExeption
= new Error('blah');
338 db
.db
.manyOrNone
.rejects(expectedExeption
);
340 await db
.getAllLinks(dbCtx
);
341 assert
.fail(noExpectedException
);
343 assert
.deepStrictEqual(e
, expectedExeption
, noExpectedException
);