mirror: add internal method `_createUpdate` (#868)

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
This commit is contained in:
William Chargin 2018-09-20 11:12:21 -07:00 committed by GitHub
parent cdceedef8d
commit e572551cd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 0 deletions

View File

@ -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; return result;
} }
type UpdateId = number;
/** /**
* Execute a function inside a database transaction. * Execute a function inside a database transaction.
* *

View File

@ -173,6 +173,46 @@ describe("graphql/mirror", () => {
expect(() => new Mirror(db, buildGithubSchema())).not.toThrow(); 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", () => { describe("_buildSchemaInfo", () => {