-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.js
104 lines (83 loc) · 3.86 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const log = require("lambda-log");
const athena = require("./lib/athena");
const bigquery = require("./lib/bigquery");
const index = require("./index");
describe("index", () => {
describe("handler", () => {
it("validates dates", async () => {
jest.spyOn(log, "error").mockReturnValue();
expect(index.handler({ day: "n/a" })).rejects.toThrow(/invalid date/i);
expect(log.error).toHaveBeenCalledTimes(1);
expect(log.error.mock.calls[0][0]).toMatch(/error running rollups/);
});
it("inserts a day of data", async () => {
const day = new Date("2024-05-07");
const rows = [["1234/abcd", 5678]];
jest.spyOn(log, "info").mockReturnValue();
jest.spyOn(athena, "queryUsage").mockReturnValue(rows);
jest.spyOn(bigquery, "totalBytes").mockReturnValue(0);
jest.spyOn(bigquery, "deleteDay").mockReturnValue();
jest.spyOn(bigquery, "loadDay").mockReturnValue();
await index.handler({ day: "2024-05-07" });
expect(athena.queryUsage).toHaveBeenCalledTimes(1);
expect(athena.queryUsage.mock.calls[0][0]).toEqual(day);
expect(bigquery.totalBytes).toHaveBeenCalledTimes(1);
expect(bigquery.totalBytes.mock.calls[0][0]).toEqual(day);
expect(bigquery.deleteDay).toHaveBeenCalledTimes(1);
expect(bigquery.deleteDay.mock.calls[0][0]).toEqual(day);
expect(bigquery.loadDay).toHaveBeenCalledTimes(1);
expect(bigquery.loadDay.mock.calls[0][0]).toEqual(day);
expect(bigquery.loadDay.mock.calls[0][1]).toEqual([
{
feeder_podcast: 1234,
feeder_episode: "abcd",
bytes: 5678,
day: "2024-05-07",
},
]);
});
it("rolls up the latest day forward", async () => {
const today = new Date("2024-05-07");
const latest = new Date("2024-05-05");
jest.useFakeTimers().setSystemTime(today);
jest.spyOn(bigquery, "latestRollupDay").mockReturnValue(latest);
jest.spyOn(index, "rollup").mockReturnValue(true);
process.env.MAX_DAYS_TO_ROLLUP = 4;
await index.handler();
expect(index.rollup).toHaveBeenCalledTimes(3);
expect(index.rollup.mock.calls[0][0]).toEqual(latest);
expect(index.rollup.mock.calls[1][0]).toEqual(new Date("2024-05-06"));
expect(index.rollup.mock.calls[2][0]).toEqual(today);
});
it("logs an error if we're falling behind", async () => {
jest.spyOn(log, "info").mockReturnValue();
jest.spyOn(log, "error").mockReturnValue();
const today = new Date("2024-05-07");
const latest = new Date("2024-04-24");
jest.useFakeTimers().setSystemTime(today);
jest.spyOn(bigquery, "latestRollupDay").mockReturnValue(latest);
jest.spyOn(index, "rollup").mockReturnValue(true);
process.env.MAX_DAYS_TO_ROLLUP = 4;
await index.handler();
expect(index.rollup).toHaveBeenCalledTimes(4);
expect(log.error).toHaveBeenCalledTimes(1);
expect(log.error.mock.calls[0][0]).toEqual("rollups behind");
expect(log.error.mock.calls[0][1]).toEqual({ latest: "2024-04-27" });
});
it("logs an error if the day totalBytes is larger than what we queried", async () => {
const day = new Date("2024-05-07");
const rows = [["1234/abcd", 5678]];
jest.spyOn(log, "info").mockReturnValue();
jest.spyOn(log, "error").mockReturnValue();
jest.spyOn(athena, "queryUsage").mockReturnValue(rows);
jest.spyOn(bigquery, "totalBytes").mockReturnValue(9999);
await index.handler({ day: "2024-05-07" });
expect(athena.queryUsage).toHaveBeenCalledTimes(1);
expect(athena.queryUsage.mock.calls[0][0]).toEqual(day);
expect(bigquery.totalBytes).toHaveBeenCalledTimes(1);
expect(bigquery.totalBytes.mock.calls[0][0]).toEqual(day);
expect(log.error).toHaveBeenCalledTimes(1);
expect(log.error.mock.calls[0][0]).toMatch(/bigquery total mismatch/);
});
});
});