Skip to content

Commit e5f21c9

Browse files
Merge pull request #340 from code-dot-org/more-tests
More tests
2 parents 3c05b9b + b6e9a29 commit e5f21c9

File tree

2 files changed

+57
-40
lines changed

2 files changed

+57
-40
lines changed

src/oceans/models/predict.js

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,30 @@ export const init = () => {
2929
// seconds, and if it happens immediately it will prevent React from rendering
3030
// the loading UI first. If we are going to show the loading spinner, then also
3131
// delay the beginning of our training.
32-
setTimeout(() => {
33-
// Manually send a GA event for Modes.Predicting.
34-
reportPageView('predicting');
32+
return new Promise(resolve => setTimeout(resolve, trainingDelayTime)).then(
33+
() => {
34+
// Manually send a GA event for Modes.Predicting.
35+
reportPageView('predicting');
3536

36-
state.trainer.train();
37+
state.trainer.train();
3738

38-
let fishData = [];
39-
if (state.appMode === AppMode.CreaturesVTrashDemo) {
40-
fishData = fishData.concat(generateOcean(4, 0, true, true, false));
41-
fishData = fishData.concat(generateOcean(3, 4, false, false, true));
42-
} else if (state.appMode === AppMode.FishLong) {
43-
fishData = generateOcean(500);
44-
} else {
45-
fishData = generateOcean(100);
46-
}
39+
let fishData = [];
40+
if (state.appMode === AppMode.CreaturesVTrashDemo) {
41+
fishData = fishData.concat(generateOcean(4, 0, true, true, false));
42+
fishData = fishData.concat(generateOcean(3, 4, false, false, true));
43+
} else if (state.appMode === AppMode.FishLong) {
44+
fishData = generateOcean(500);
45+
} else {
46+
fishData = generateOcean(100);
47+
}
4748

48-
if (setLoadingSpinner) {
49-
finishLoading(startTime, () => onLoadComplete(fishData));
50-
} else {
51-
onLoadComplete(fishData);
49+
if (setLoadingSpinner) {
50+
finishLoading(startTime, () => onLoadComplete(fishData));
51+
} else {
52+
onLoadComplete(fishData);
53+
}
5254
}
53-
}, trainingDelayTime);
55+
);
5456
};
5557

5658
const onLoadComplete = fishData => {
@@ -63,8 +65,7 @@ const onLoadComplete = fishData => {
6365
export const predictFish = (state, idx) => {
6466
return new Promise(resolve => {
6567
const fish = state.fishData[idx];
66-
67-
state.trainer.predict(fish).then(prediction => {
68+
return state.trainer.predict(fish).then(prediction => {
6869
fish.setResult(prediction);
6970
resolve(prediction);
7071
});
Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,61 @@
11
const {initFishData} = require('@ml/utils/fishData');
22
import {setState, getState, resetState} from '@ml/oceans/state';
3-
import {ClassType, Modes} from '@ml/oceans/constants';
3+
import {ClassType, Modes, AppMode} from '@ml/oceans/constants';
44
import {init, predictFish} from '@ml/oceans/models/predict';
55
import SimpleTrainer from '@ml/utils/SimpleTrainer';
6+
import {TrashOceanObject} from '@ml/oceans/OceanObject';
67

7-
describe('Model quality test', () => {
8+
describe('Predict test', () => {
89
beforeAll(() => {
910
initFishData();
11+
jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000;
1012
});
1113

1214
beforeEach(() => {
15+
const trainer = new SimpleTrainer();
16+
trainer.predict = jest.fn(async example => {
17+
if (example instanceof TrashOceanObject) {
18+
return {predictedClassId: 1, confidenceByClassId: {0: 0, 1: 1}};
19+
} else {
20+
return {predictedClassId: 0, confidenceByClassId: {0: 1, 1: 0}};
21+
}
22+
});
23+
1324
resetState();
1425
setState({
15-
trainer: new SimpleTrainer()
26+
trainer
1627
});
1728
});
1829

19-
jest.useFakeTimers();
20-
2130
test('init state without intermediate loading', () => {
22-
init();
23-
// There's a setTimeout in the predict init. Ideally this would be
24-
// refactored to use promises if possible but until then, wait 100 ms.
25-
setTimeout(async () => {
31+
return init().then(() => {
2632
const state = getState();
2733
expect(state).toBeTruthy();
28-
expect(state.mode).toBe(Modes.Predicting);
34+
expect(state.currentMode).toBe(Modes.Predicting);
2935
expect(state.fishData).toBeTruthy();
3036
expect(state.fishData.length).toBeGreaterThan(0);
31-
}, 100);
37+
});
3238
});
3339

34-
test('fish gets prediction on predict', async () => {
40+
test('init state to intermediate loading', async () => {
41+
setState({appMode: AppMode.FishShort});
3542
init();
36-
// There's a setTimeout in the predict init. Ideally this would be
37-
// refactored to use promises if possible but until then, wait 100 ms.
38-
setTimeout(async () => {
39-
const state = getState();
40-
await predictFish(state, 0);
41-
expect(state.trainer.predict).toBeCalled();
42-
expect(state.fishData[0].result).toBeInstanceOf(Number);
43-
}, 100);
43+
44+
const state = getState();
45+
expect(state).toBeTruthy();
46+
expect(state.currentMode).toBe(Modes.IntermediateLoading);
47+
});
48+
49+
it('fish gets prediction on predict', async () => {
50+
setState({appMode: AppMode.FishVTrash});
51+
let state = getState();
52+
return init().then(() => {
53+
state = getState();
54+
return predictFish(state, 0).then(() => {
55+
expect(
56+
state.fishData[0].result.predictedClassId
57+
).toBeGreaterThanOrEqual(0);
58+
});
59+
});
4460
});
4561
});

0 commit comments

Comments
 (0)