Skip to content

Commit 5d80fb0

Browse files
committed
Test that demonstrates issue with caching mechanism
1 parent 04c8fa8 commit 5d80fb0

File tree

2 files changed

+68
-20
lines changed

2 files changed

+68
-20
lines changed

tests/read-models/test-framework.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ import {Dependencies} from '../../src/dependencies';
1919
import {applyToResource} from '../../src/commands/apply-command-to-resource';
2020
import {initSharedReadModel} from '../../src/read-models/shared-state';
2121
import {localGoogleHelpers} from '../init-dependencies/pull-local-google';
22-
import {cacheSheetData} from '../../src/init-dependencies/google/get-cached-sheet-data';
22+
import {
23+
cacheSheetData,
24+
getCachedSheetData,
25+
} from '../../src/init-dependencies/google/get-cached-sheet-data';
2326
import {ensureCachedSheetDataTableExists} from '../../src/init-dependencies/google/ensure-cached-sheet-data-table-exists';
2427

2528
type ToFrameworkCommands<T> = {
@@ -47,6 +50,7 @@ export type TestFramework = {
4750
getResourceEvents: Dependencies['getResourceEvents'];
4851
};
4952
eventStoreDb: libsqlClient.Client;
53+
getCachedSheetData: Dependencies['getCachedSheetData'];
5054
};
5155

5256
export const initTestFramework = async (
@@ -98,6 +102,7 @@ export const initTestFramework = async (
98102
commitEvent: frameworkCommitEvent,
99103
getResourceEvents: getResourceEvents(dbClient),
100104
},
105+
getCachedSheetData: getCachedSheetData(dbClient),
101106
commands: {
102107
area: {
103108
create: frameworkify(commands.area.create),

tests/training-sheets/async-apply-external.test.ts

Lines changed: 62 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import {faker} from '@faker-js/faker';
44
import * as gsheetData from '../data/google_sheet_data';
55
import {initTestFramework, TestFramework} from '../read-models/test-framework';
66
import {EmailAddress} from '../../src/types';
7-
import {getSomeOrFail} from '../helpers';
7+
import {getRightOrFail, getSomeOrFail} from '../helpers';
88
import {
99
EpochTimestampMilliseconds,
1010
Equipment,
1111
} from '../../src/read-models/shared-state/return-types';
12+
import {EventOfType} from '../../src/types/domain-event';
1213

1314
describe('Integration asyncApplyExternalEventSources', () => {
1415
const addArea = async (framework: TestFramework) => {
@@ -145,25 +146,67 @@ describe('Integration asyncApplyExternalEventSources', () => {
145146
results2.equipmentAfter.get(bambu.id)!.lastQuizSync
146147
);
147148
});
148-
it('Repeat equipment pull no rate limit', async () => {
149-
const rateLimitMs = 100;
150-
const framework = await initTestFramework(rateLimitMs);
151-
const areaId = await addArea(framework);
152-
const bambu = await addWithSheet(
153-
framework,
154-
'bambu',
155-
areaId,
156-
O.some(gsheetData.BAMBU.apiResp.spreadsheetId!)
157-
);
158-
const results1 = await runAsyncApplyExternalEventSources(framework);
159-
checkLastQuizSyncUpdated(results1);
149+
describe('Repeat equipment pull no rate limit', () => {
150+
let framework: TestFramework;
151+
let results1: ApplyExternalEventsResults;
152+
let cachedData1: ReadonlyArray<
153+
| EventOfType<'EquipmentTrainingQuizResult'>
154+
| EventOfType<'EquipmentTrainingQuizSync'>
155+
>;
156+
let results2: ApplyExternalEventsResults;
157+
let cachedData2: ReadonlyArray<
158+
| EventOfType<'EquipmentTrainingQuizResult'>
159+
| EventOfType<'EquipmentTrainingQuizSync'>
160+
>;
161+
let equipment: {id: UUID; trainingSheetId: O.Option<string>};
160162

161-
await new Promise(res => setTimeout(res, rateLimitMs));
162-
const results2 = await runAsyncApplyExternalEventSources(framework);
163-
checkLastQuizSyncUpdated(results2);
164-
expect(results1.equipmentAfter.get(bambu.id)!.lastQuizSync).not.toEqual(
165-
results2.equipmentAfter.get(bambu.id)!.lastQuizSync
166-
);
163+
beforeEach(async () => {
164+
const rateLimitMs = 100;
165+
framework = await initTestFramework(rateLimitMs);
166+
const areaId = await addArea(framework);
167+
equipment = await addWithSheet(
168+
framework,
169+
'bambu',
170+
areaId,
171+
O.some(gsheetData.BAMBU.apiResp.spreadsheetId!)
172+
);
173+
results1 = await runAsyncApplyExternalEventSources(framework);
174+
cachedData1 = getRightOrFail(
175+
getSomeOrFail(
176+
getRightOrFail(
177+
await framework.getCachedSheetData(
178+
getSomeOrFail(equipment.trainingSheetId)
179+
)()
180+
)
181+
).cached_data
182+
);
183+
await new Promise(res => setTimeout(res, rateLimitMs));
184+
results2 = await runAsyncApplyExternalEventSources(framework);
185+
cachedData2 = getRightOrFail(
186+
getSomeOrFail(
187+
getRightOrFail(
188+
await framework.getCachedSheetData(
189+
getSomeOrFail(equipment.trainingSheetId)
190+
)()
191+
)
192+
).cached_data
193+
);
194+
});
195+
196+
it('updates the last quiz sync both times indicating a sync both times', () => {
197+
checkLastQuizSyncUpdated(results1);
198+
checkLastQuizSyncUpdated(results2);
199+
expect(
200+
results1.equipmentAfter.get(equipment.id)!.lastQuizSync
201+
).not.toEqual(results2.equipmentAfter.get(equipment.id)!.lastQuizSync);
202+
});
203+
204+
it('Cached data is the same after both runs', () => {
205+
// The source (our simulated google endpoint using local data) doesn't change so we should cache the same
206+
// number of events both times.
207+
expect(cachedData1.length).toBeGreaterThan(1); // At least 1 event is the sync event then x more events from the data source.
208+
expect(cachedData2.length).toStrictEqual(cachedData1.length);
209+
});
167210
});
168211
});
169212

0 commit comments

Comments
 (0)