Skip to content

Commit

Permalink
add tests for Time
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickdemers6 committed Oct 27, 2023
1 parent 2bc2780 commit 8d0d89c
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"main": "src/index.ts",
"scripts": {
"build": "tsc",
"test": "cross-env NODE_ENV=test ts-mocha 'src/spec/**/*.spec.ts'",
"test:coverage": "nyc --reporter=html ts-mocha 'src/test/**/*.ts'",
"test": "cross-env NODE_ENV=test TZ=UTC ts-mocha 'src/spec/**/*.spec.ts'",
"test:coverage": "cross-env NODE_ENV=test TZ=UTC nyc --reporter=html ts-mocha 'src/spec/**/*.ts'",
"lint": "eslint .",
"start": "node build/index.js",
"dev": "npm run build && node build/index.js"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,12 @@ class GTFSRealtimeDataAdapter
const stopSequence = stopTimeUpdate.stopSequence;
const stop = stopTimeUpdate.stopId;
const update: RealtimeUpdate = {
arrival: Time.fromLong(stopTimeUpdate.arrival.time as Long),
departure: Time.fromLong(stopTimeUpdate.departure.time as Long),
arrival: Time.fromSeconds(
this.#toNumber(stopTimeUpdate.arrival.time)
),
departure: Time.fromSeconds(
this.#toNumber(stopTimeUpdate.departure.time)
),
trip: tripId,
route: trip.tripUpdate.trip.routeId,
stopSequence: stopSequence,
Expand All @@ -87,6 +91,11 @@ class GTFSRealtimeDataAdapter
return updates;
}

#toNumber(n: number | Long) {
if (typeof n === "number") return n;
return n.toInt();
}

#archiveData(data: ArrayBuffer) {
this.#options.archiveManager.archive(data, "trip updates");
}
Expand Down
12 changes: 10 additions & 2 deletions src/providers/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ class Time {
minutes: number;

constructor(hours: number, minutes: number) {
if (minutes < 0) throw "minutes cannot be negative";
if (hours < 0) throw "hours cannot be negative";

this.hours = hours;
this.minutes = minutes;
}
Expand All @@ -22,8 +25,8 @@ class Time {
return Time.fromDate(date);
}

static fromLong(long: Long) {
return Time.fromDate(fromUnixTime(long.toInt()));
static fromSeconds(s: number) {
return Time.fromDate(new Date(s * 1000));
}

static copy(time: Time) {
Expand All @@ -42,9 +45,14 @@ class Time {
}

setHours(hours: number) {
if (hours < 0) throw "hours cannot be negative";

this.hours = hours;
}

setMinutes(minutes: number) {
if (minutes < 0) throw "minutes cannot be negative";

this.minutes = minutes;
}

Expand Down
158 changes: 158 additions & 0 deletions src/spec/providers/time.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import { expect } from "chai";
import Time from "../../providers/time";
import Sinon from "sinon";
import Long from "long";

const systemDate = new Date(2030, 5, 3, 4, 19, 30);

describe("Time", () => {
beforeEach(() => {
Sinon.useFakeTimers(systemDate);
});

afterEach(() => {
Sinon.restore();
});

describe("#copy", () => {
it("creates an identical clone", () => {
const time = new Time(4, 19);
const clone = Time.copy(time);
expect(clone.hours).to.equal(4);
expect(clone.minutes).to.equal(19);
});
});

describe("#fromDate", () => {
it("gets minutes and hours", () => {
const d = new Date();
const time = Time.fromDate(d);
expect(time.hours).to.equal(d.getHours());
expect(time.minutes).to.equal(d.getMinutes());
});
});

describe("#now", () => {
it("gets data from current time", () => {
const time = Time.now();
expect(time.hours).to.equal(systemDate.getHours());
expect(time.minutes).to.equal(systemDate.getMinutes());
});
});

describe("#addMinutes", () => {
it("adds positive minutes in same hour", () => {
const time = new Time(4, 0);
time.addMinutes(10);
expect(time.hours).to.equal(4);
expect(time.minutes).to.equal(10);
});

it("adds positive minutes to new hour", () => {
const time = new Time(4, 0);
time.addMinutes(75);
expect(time.hours).to.equal(5);
expect(time.minutes).to.equal(15);
});

it("adds positive minutes in same hour", () => {
const time = new Time(4, 20);
time.addMinutes(-10);
expect(time.hours).to.equal(4);
expect(time.minutes).to.equal(10);
});

it("adds negative minutes to new hour", () => {
const time = new Time(4, 0);
time.addMinutes(-75);
expect(time.hours).to.equal(2);
expect(time.minutes).to.equal(45);
});
});

describe("#toInteger", () => {
it("returns minutes into day", () => {
const time = new Time(1, 10);
expect(time.toInteger()).to.equal(70);
});
});

describe("constructor", () => {
it("creates with proper time", () => {
const time = new Time(1, 11);
expect(time.hours).to.equal(1);
expect(time.minutes).to.equal(11);
});
it("rejects negative minutes", () => {
expect(() => new Time(1, -11)).to.throw("minutes cannot be negative");
});
it("rejects negative hours", () => {
expect(() => new Time(-1, 11)).to.throw("hours cannot be negative");
});
});

describe("#fromShortTimeString", () => {
it("parses hours and minutes", () => {
const time = Time.fromShortTimeString("02:03:00");
expect(time.hours).to.equal(2);
expect(time.minutes).to.equal(3);
});
});

describe("#fromDateString", () => {
it("parses iso date string", () => {
const time = Time.fromDateString("2023-10-27T03:58:26.833");
expect(time.hours).to.equal(3);
expect(time.minutes).to.equal(58);
});
it("parses date string", () => {
const time = Time.fromDateString(
"Thu Oct 26 2023 23:01:48 GMT-0500 (Central Daylight Time)"
);
// TZ=UTC, thus time reported in UTC (5 hours added) not Central Daylight Time
expect(time.hours).to.equal(4);
expect(time.minutes).to.equal(1);
});
});

describe("#fromLong", () => {
it("reads long", () => {
const ms = systemDate.getTime();
const time = Time.fromSeconds(ms / 1000);
expect(time.hours).to.equal(4);
expect(time.minutes).to.equal(19);
});
});

describe("#setHours", () => {
it("sets hours", () => {
const time = new Time(0, 0);
time.setHours(1);
expect(time.hours).to.equal(1);
time.setHours(23);
expect(time.hours).to.equal(23);
});

it("rejects negative hours", () => {
expect(() => new Time(0, 0).setHours(-1)).to.throw(
"hours cannot be negative"
);
});
});

describe("#setMinutes", () => {
it("sets minutes", () => {
const time = new Time(0, 0);
time.setMinutes(1);
expect(time.minutes).to.equal(1);
time.setMinutes(23);
expect(time.minutes).to.equal(23);
});

it("rejects negative minutes", () => {
expect(() => new Time(1, 11).setMinutes(-1)).to.throw(
"minutes cannot be negative"
);
});
});
});

0 comments on commit 8d0d89c

Please sign in to comment.