From e572551cd82dfab737772f603ab84fec8c879c88 Mon Sep 17 00:00:00 2001 From: William Chargin Date: Thu, 20 Sep 2018 11:12:21 -0700 Subject: [PATCH] mirror: add internal method `_createUpdate` (#868) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: It’s useful to add this simple function now because the rest of the commits required to implement #622 will want to use it extensively in test code. Actual clients of the API will not need to use it, because the concept of “updates” is an implementation detail: clients will always provide simple timestamps. Test Plan: Unit tests included, with full coverage; run `yarn unit`. wchargin-branch: mirror-createupdate --- src/graphql/mirror.js | 12 ++++++++++++ src/graphql/mirror.test.js | 40 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/graphql/mirror.js b/src/graphql/mirror.js index d378d51..4ff10d4 100644 --- a/src/graphql/mirror.js +++ b/src/graphql/mirror.js @@ -247,6 +247,16 @@ export class Mirror { } }); } + + /** + * Register a new update, representing one communication with the + * remote server. A unique ID will be created and returned. + */ + _createUpdate(updateTimestamp: Date): UpdateId { + return this._db + .prepare("INSERT INTO updates (time_epoch_millis) VALUES (?)") + .run(+updateTimestamp).lastInsertROWID; + } } /** @@ -341,6 +351,8 @@ export function _buildSchemaInfo(schema: Schema.Schema): SchemaInfo { return result; } +type UpdateId = number; + /** * Execute a function inside a database transaction. * diff --git a/src/graphql/mirror.test.js b/src/graphql/mirror.test.js index 22f1142..fbfedf4 100644 --- a/src/graphql/mirror.test.js +++ b/src/graphql/mirror.test.js @@ -173,6 +173,46 @@ describe("graphql/mirror", () => { expect(() => new Mirror(db, buildGithubSchema())).not.toThrow(); }); }); + + describe("_createUpdate", () => { + it("creates an update with the proper timestamp", () => { + const db = new Database(":memory:"); + const mirror = new Mirror(db, buildGithubSchema()); + + const date = new Date(0); + // This is equivalent to `new Date(12345)`, just more explicit + // about the units---we should be explicit at least once in + // update-related test code. + date.setUTCMilliseconds(12345); + + mirror._createUpdate(date); + expect(+date).toBe(12345); // please don't mutate the date... + expect( + db + .prepare("SELECT time_epoch_millis FROM updates") + .pluck() + .all() + ).toEqual([12345]); + }); + it("returns distinct results regardless of timestamps", () => { + const db = new Database(":memory:"); + const mirror = new Mirror(db, buildGithubSchema()); + const date0 = new Date(0); + const date1 = new Date(1); + const uid1 = mirror._createUpdate(date0); + const uid2 = mirror._createUpdate(date0); + const uid3 = mirror._createUpdate(date1); + expect(uid1).not.toEqual(uid2); + expect(uid2).not.toEqual(uid3); + expect(uid3).not.toEqual(uid1); + expect( + db + .prepare("SELECT COUNT(1) FROM updates") + .pluck() + .get() + ).toEqual(3); + }); + }); }); describe("_buildSchemaInfo", () => {