lint cleanup
[websub-hub] / test / src / db / postgres.js
index 0f037d6fae91b6dd4eed3098626317c9797f9d79..c7621354de979ce9a78039813cfb20bf78b76570 100644 (file)
@@ -65,7 +65,7 @@ describe('DatabasePostgres', function () {
     httpRemoteAddr = '127.0.0.1';
     httpFrom = 'user@example.com';
     wanted = 5;
-});
+  });
   afterEach(function () {
     sinon.restore();
   });
@@ -141,7 +141,7 @@ describe('DatabasePostgres', function () {
             columnTwo: 4,
           },
         ];
-        db.pgpInitOptions.receive(data, result, event)
+        db.pgpInitOptions.receive({ data, result, ctx: event });
         assert(db.logger.debug.called);
         assert.deepStrictEqual(data, expectedData);
       });
@@ -170,7 +170,7 @@ describe('DatabasePostgres', function () {
             columnTwo: 4,
           },
         ];
-        db.pgpInitOptions.receive(data, result, event)
+        db.pgpInitOptions.receive({ data, result, ctx: event });
         assert(!db.logger.debug.called);
         assert.deepStrictEqual(data, expectedData);
       });
@@ -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(),
@@ -618,7 +631,7 @@ describe('DatabasePostgres', function () {
       const expected = new Error();
       sinon.stub(db.db, 'manyOrNone').throws(expected);
       try {
-        await db.subscriptionDeliveryClaim(dbCtx, wanted, claimTimeoutSeconds, claimant );
+        await db.subscriptionDeliveryClaim(dbCtx, wanted, claimTimeoutSeconds, claimant);
         assert.fail(noExpectedException);
       } catch (e) {
         assert.deepStrictEqual(e, expected);
@@ -637,7 +650,7 @@ describe('DatabasePostgres', function () {
         changes: 1,
         lastInsertRowid: 'c2e254c5-aa6e-4a8f-b1a1-e474b07392bb',
         duration: 11,
-      }
+      };
       sinon.stub(db.db, 'result').resolves(dbResult);
       const result = await db.subscriptionDeliveryClaimById(dbCtx, subscriptionId, claimTimeoutSeconds, claimant);
       assert.deepStrictEqual(result, expected);
@@ -659,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 = {
@@ -672,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);
@@ -687,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);
@@ -1009,7 +1026,7 @@ describe('DatabasePostgres', function () {
         rowCount: 1,
         rows: [],
         duration: 10,
-      }
+      };
       const expected = {
         changes: 1,
         lastInsertRowid: undefined,
@@ -1031,7 +1048,7 @@ describe('DatabasePostgres', function () {
         rowCount: 1,
         rows: [],
         duration: 10,
-      }
+      };
       const expected = {
         changes: 1,
         lastInsertRowid: undefined,
@@ -1053,7 +1070,7 @@ describe('DatabasePostgres', function () {
         rowCount: 0,
         rows: [],
         duration: 10,
-      }
+      };
       sinon.stub(db.db, 'one').resolves(dbOne);
       sinon.stub(db.db, 'result').onCall(0).resolves(dbResult0).onCall(1).resolves(dbResult1);
       try {
@@ -1074,7 +1091,7 @@ describe('DatabasePostgres', function () {
         rowCount: 0,
         rows: [],
         duration: 10,
-      }
+      };
       sinon.stub(db.db, 'one').resolves(dbOne);
       sinon.stub(db.db, 'result').onCall(0).resolves(dbResult0).onCall(1).resolves(dbResult1);
       try {
@@ -1305,6 +1322,21 @@ describe('DatabasePostgres', function () {
     });
   });
 
+  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 () {
@@ -1375,6 +1407,7 @@ describe('DatabasePostgres', function () {
         contentType: 'text/plain',
         contentHash: 'abc123',
       };
+      sinon.stub(db.db, 'result');
     });
     it('success', async function() {
       const dbResult = {
@@ -1387,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);
     });
@@ -1397,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);
@@ -1710,7 +1764,7 @@ describe('DatabasePostgres', function () {
         rowCount: 0,
         rows: [],
         duration: 10,
-      }
+      };
       sinon.stub(db.db, 'result').resolves(dbResult);
       try {
         await db.verificationUpdate(dbCtx, verificationId, data);
@@ -1736,7 +1790,7 @@ describe('DatabasePostgres', function () {
         rowCount: 1,
         rows: [],
         duration: 10,
-      }
+      };
       sinon.stub(db.db, 'result').resolves(dbResult);
       await db.verificationValidated(dbCtx, verificationId);
     });
@@ -1745,7 +1799,7 @@ describe('DatabasePostgres', function () {
         rowCount: 0,
         rows: [],
         duration: 10,
-      }
+      };
       sinon.stub(db.db, 'result').resolves(dbResult);
       try {
         await db.verificationValidated(dbCtx, verificationId);