-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmi-repository.test.ts
More file actions
117 lines (101 loc) · 3.12 KB
/
mi-repository.test.ts
File metadata and controls
117 lines (101 loc) · 3.12 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
import { Logger } from "pino";
import {
DBContext,
createTables,
deleteTables,
setupDynamoDBContainer,
} from "./db";
import { createTestLogger } from "./logs";
import { MIRepository } from "../mi-repository";
// Database tests can take longer, especially with setup and teardown
jest.setTimeout(30_000);
describe("MiRepository", () => {
let db: DBContext;
let miRepository: MIRepository;
let logger: Logger;
beforeAll(async () => {
db = await setupDynamoDBContainer();
});
beforeEach(async () => {
await createTables(db);
({ logger } = createTestLogger());
miRepository = new MIRepository(db.docClient, logger, db.config);
});
afterEach(async () => {
await deleteTables(db);
jest.useRealTimers();
});
afterAll(async () => {
await db.container.stop();
});
describe("putMi", () => {
it("creates a letter with id and timestamps", async () => {
jest.useFakeTimers();
// Month is zero-indexed in JS Date
jest.setSystemTime(new Date(2020, 1, 1));
const mi = {
specificationId: "spec1",
supplierId: "supplier1",
groupId: "group1",
lineItem: "item1",
quantity: 12,
timestamp: new Date().toISOString(),
stockRemaining: 0,
};
const persistedMi = await miRepository.putMI(mi);
expect(persistedMi).toEqual(
expect.objectContaining({
id: expect.any(String),
createdAt: "2020-02-01T00:00:00.000Z",
updatedAt: "2020-02-01T00:00:00.000Z",
ttl: 1_580_518_800, // 2020-02-01T00:01:00.000Z, seconds since epoch
...mi,
}),
);
});
});
describe("getMi", () => {
it("throws an error when fetching MI information that does not exist", async () => {
await expect(miRepository.getMI("XXX", "supplier1")).rejects.toThrow(
"Management information not found: supplierId=supplier1, miId=XXX",
);
});
it("creates MI with id and timestamps", async () => {
jest.useFakeTimers();
// Month is zero-indexed in JS Date
jest.setSystemTime(new Date(2020, 1, 1));
const mi = {
specificationId: "spec1",
supplierId: "supplier1",
groupId: "group1",
lineItem: "item1",
quantity: 12,
timestamp: new Date().toISOString(),
stockRemaining: 0,
};
const persistedMi = await miRepository.putMI(mi);
expect(persistedMi).toEqual(
expect.objectContaining({
id: expect.any(String),
createdAt: "2020-02-01T00:00:00.000Z",
updatedAt: "2020-02-01T00:00:00.000Z",
ttl: 1_580_518_800, // 2020-02-01T00:01:00.000Z, seconds since epoch
...mi,
}),
);
const fetchedMi = await miRepository.getMI(
persistedMi.id,
persistedMi.supplierId,
);
expect(fetchedMi).toEqual(
expect.objectContaining({
id: expect.any(String),
createdAt: "2020-02-01T00:00:00.000Z",
updatedAt: "2020-02-01T00:00:00.000Z",
ttl: 1_580_518_800, // 2020-02-01T00:01:00.000Z, seconds since epoch
...mi,
}),
);
});
});
});