X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=test%2Fsrc%2Fdb%2Fpostgres.js;h=626b0c2573a145f1727e6cc5e099e06e16d1cdc1;hb=737fbd003d5c4dfea81b667ef906f1c106a60612;hp=ef4790542cd147a076a0fdc11985da6641f10b64;hpb=3fae9b64c5c70ad52376558caa71db99778541b0;p=websub-hub diff --git a/test/src/db/postgres.js b/test/src/db/postgres.js index ef47905..626b0c2 100644 --- a/test/src/db/postgres.js +++ b/test/src/db/postgres.js @@ -227,11 +227,24 @@ describe('DatabasePostgres', function () { }); it('covers migration', async function() { sinon.stub(db.db, 'oneOrNone').resolves({}); - sinon.stub(db.db, 'multiResult'); - sinon.stub(db, '_currentSchema').resolves(db.schemaVersionsSupported.max); + sinon.stub(db.db, 'multiResult').resolves({}); + sinon.stub(db, '_currentSchema').resolves(db.schemaVersionsSupported.min); sinon.stub(db.db, 'one').resolves(db.schemaVersionsSupported.max); await db.initialize(); }); + it('covers migration failure', async function() { + const expected = new Error('oh no'); + sinon.stub(db.db, 'oneOrNone').resolves({}); + sinon.stub(db.db, 'multiResult').rejects(expected); + sinon.stub(db, '_currentSchema').resolves(db.schemaVersionsSupported.min); + sinon.stub(db.db, 'one').resolves(db.schemaVersionsSupported.max); + try { + await db.initialize(); + assert.fail(noExpectedException); + } catch (e) { + assert.deepStrictEqual(e, expected); + } + }); it('covers listener', async function() { db.listener = { start: sinon.stub(), @@ -557,7 +570,7 @@ describe('DatabasePostgres', function () { changes: 1, lastInsertRowid: undefined, duration: 10, - } + }; sinon.stub(db.db, 'result').resolves(dbResult); const result = await db.subscriptionDelete(dbCtx, callback, topicId); assert.deepStrictEqual(result, expected); @@ -574,6 +587,34 @@ describe('DatabasePostgres', function () { }); }); // subscriptionDelete + describe('subscriptionDeleteExpired', function () { + it('success', async function () { + const dbResult = { + rowCount: 1, + rows: [], + duration: 10, + }; + const expected = { + changes: 1, + lastInsertRowid: undefined, + duration: 10, + }; + sinon.stub(db.db, 'result').resolves(dbResult); + const result = await db.subscriptionDeleteExpired(dbCtx, topicId); + assert.deepStrictEqual(result, expected); + }); + it('failure', async function() { + const expected = new Error(); + sinon.stub(db.db, 'result').rejects(expected); + try { + await db.subscriptionDeleteExpired(dbCtx, topicId); + assert.fail(noExpectedException); + } catch (e) { + assert.deepStrictEqual(e, expected); + } + }); + }); + describe('subscriptionDeliveryClaim', function () { it('success', async function() { const dbResult = [ @@ -631,12 +672,16 @@ describe('DatabasePostgres', function () { }); // subscriptionDeliveryClaimById describe('subscriptionDeliveryComplete', function () { + let topicContentUpdated; + before(function () { + topicContentUpdated = new Date(); + }); it('success', async function() { const dbResult = { rowCount: 1, }; sinon.stub(db.db, 'result').resolves(dbResult); - await db.subscriptionDeliveryComplete(dbCtx, callback, topicId); + await db.subscriptionDeliveryComplete(dbCtx, callback, topicId, topicContentUpdated); }); it('failure', async function () { const dbResult = { @@ -644,7 +689,7 @@ describe('DatabasePostgres', function () { }; sinon.stub(db.db, 'result').onCall(0).resolves(dbResult); try { - await db.subscriptionDeliveryComplete(dbCtx, callback, topicId); + await db.subscriptionDeliveryComplete(dbCtx, callback, topicId, topicContentUpdated); assert.fail(noExpectedException); } catch (e) { assert(e instanceof DBErrors.UnexpectedResult); @@ -659,7 +704,7 @@ describe('DatabasePostgres', function () { }; sinon.stub(db.db, 'result').onCall(0).resolves(dbResult0).onCall(1).resolves(dbResult1); try { - await db.subscriptionDeliveryComplete(dbCtx, callback, topicId); + await db.subscriptionDeliveryComplete(dbCtx, callback, topicId, topicContentUpdated); assert.fail(noExpectedException); } catch (e) { assert(e instanceof DBErrors.UnexpectedResult); @@ -1214,6 +1259,84 @@ describe('DatabasePostgres', function () { }); }); // topicGetContentById + describe('topicPendingDelete', function () { + beforeEach(function () { + sinon.stub(db.db, 'one'); + sinon.stub(db.db, 'result'); + }); + it('success', async function () { + db.db.one.onCall(0).resolves({ + id: topicId, + isDeleted: true, + }).onCall(1).resolves({ + count: 0, + }); + const dbResult = { + rowCount: 1, + rows: [], + duration: 10, + }; + db.db.result.resolves(dbResult); + await db.topicPendingDelete(dbCtx, topicId); + assert(db.db.result.called); + }); + it('does not delete non-deleted topic', async function () { + db.db.one.onCall(0).resolves({ + id: topicId, + isDeleted: false, + }).onCall(1).resolves({ + count: 0, + }); + await db.topicPendingDelete(dbCtx, topicId); + assert(!db.db.result.called); + }); + it('does not delete topic with active subscriptions', async function () { + db.db.one.onCall(0).resolves({ + id: topicId, + isDeleted: true, + }).onCall(1).resolves({ + count: 10, + }); + await db.topicPendingDelete(dbCtx, topicId); + assert(!db.db.result.called); + }); + it('covers no deletion', async function () { + db.db.one.onCall(0).resolves({ + id: topicId, + isDeleted: true, + }).onCall(1).resolves({ + count: 0, + }); + const dbResult = { + rowCount: 0, + rows: [], + duration: 10, + }; + db.db.result.resolves(dbResult); + try { + await db.topicPendingDelete(dbCtx, topicId); + assert.fail(noExpectedException); + } catch (e) { + assert(e instanceof DBErrors.UnexpectedResult); + } + }); + }); + + describe('topicPublishHistory', function () { + beforeEach(function () { + sinon.stub(db.db, 'manyOrNone'); + }); + it('success', async function () { + db.db.manyOrNone.returns([ + { daysAgo: 1, contentUpdates: 1 }, + { daysAgo: 3, contentUpdates: 2 }, + ]); + const result = await db.topicPublishHistory(dbCtx, topicId, 7); + const expected = [0, 1, 0, 2, 0, 0, 0]; + assert.deepStrictEqual(result, expected); + }); + }); // topicPublishHistory + describe('topicSet', function () { let data; beforeEach(function () { @@ -1284,6 +1407,7 @@ describe('DatabasePostgres', function () { contentType: 'text/plain', contentHash: 'abc123', }; + sinon.stub(db.db, 'result'); }); it('success', async function() { const dbResult = { @@ -1296,7 +1420,7 @@ describe('DatabasePostgres', function () { lastInsertRowid: undefined, duration: 10, }; - sinon.stub(db.db, 'result').resolves(dbResult); + db.db.result.resolves(dbResult); const result = await db.topicSetContent(dbCtx, data); assert.deepStrictEqual(result, expected); }); @@ -1306,7 +1430,28 @@ describe('DatabasePostgres', function () { rows: [], duration: 10, }; - sinon.stub(db.db, 'result').resolves(dbResult); + db.db.result.resolves(dbResult); + try { + await db.topicSetContent(dbCtx, data); + assert.fail(noExpectedException); + } catch (e) { + assert(e instanceof DBErrors.UnexpectedResult); + } + }); + it('failure 2', async function () { + const dbResultSuccess = { + rowCount: 1, + rows: [], + duration: 10, + }; + const dbResultFail = { + rowCount: 0, + rows: [], + duration: 10, + }; + db.db.result + .onCall(0).resolves(dbResultSuccess) + .onCall(1).resolves(dbResultFail); try { await db.topicSetContent(dbCtx, data); assert.fail(noExpectedException);