From 79d9fe04cf2fe196839b07d93535612b945932a7 Mon Sep 17 00:00:00 2001 From: EmmaLRussell Date: Tue, 12 Sep 2023 17:52:02 +0100 Subject: [PATCH] unit tests --- app/static/src/app/serialise.ts | 32 ++--- app/static/tests/unit/serialiser.test.ts | 111 ++++++++++++++++-- .../store/multiSensitivity/actions.test.ts | 8 +- .../unit/store/sensitivity/actions.test.ts | 99 ++++++++-------- 4 files changed, 176 insertions(+), 74 deletions(-) diff --git a/app/static/src/app/serialise.ts b/app/static/src/app/serialise.ts index 2961f2c9c..7a26c8ffe 100644 --- a/app/static/src/app/serialise.ts +++ b/app/static/src/app/serialise.ts @@ -3,7 +3,7 @@ import { FitState } from "./store/fit/state"; import { CodeState } from "./store/code/state"; import { ModelState } from "./store/model/state"; import { RunState } from "./store/run/state"; -import { SensitivityState } from "./store/sensitivity/state"; +import {BaseSensitivityState, SensitivityState} from "./store/sensitivity/state"; import { FitDataState } from "./store/fitData/state"; import { ModelFitState } from "./store/modelFit/state"; import { OdinFitResult, OdinRunResultDiscrete, OdinRunResultOde } from "./types/wrapperTypes"; @@ -71,6 +71,18 @@ function serialiseRun(run: RunState): SerialisedRunState { }; } +function serialiseBaseSensitivity(sensitivity: BaseSensitivityState) { + return { + running: false, + sensitivityUpdateRequired: sensitivity.sensitivityUpdateRequired, + result: sensitivity.result ? { + inputs: sensitivity.result.inputs, + hasResult: !!sensitivity.result.batch, + error: sensitivity.result.error + } : null + } +} + function serialiseSensitivity(sensitivity: SensitivityState): SerialisedSensitivityState { const serialisedParameterSetResults = {} as Dict; Object.keys(sensitivity.parameterSetResults).forEach((name) => { @@ -83,29 +95,17 @@ function serialiseSensitivity(sensitivity: SensitivityState): SerialisedSensitiv }); return { - running: false, + ...serialiseBaseSensitivity(sensitivity), paramSettings: sensitivity.paramSettings, - sensitivityUpdateRequired: sensitivity.sensitivityUpdateRequired, plotSettings: sensitivity.plotSettings, - result: sensitivity.result ? { - inputs: sensitivity.result.inputs, - hasResult: !!sensitivity.result.batch, - error: sensitivity.result.error - } : null, parameterSetResults: serialisedParameterSetResults }; } function serialiseMultiSensitivity(multiSensitivity: MultiSensitivityState): SerialisedMultiSensitivityState { return { - running: false, - paramSettings: multiSensitivity.paramSettings, - sensitivityUpdateRequired: multiSensitivity.sensitivityUpdateRequired, - result: multiSensitivity.result ? { - inputs: multiSensitivity.result.inputs, - hasResult: !!multiSensitivity.result.batch, - error: multiSensitivity.result.error - } : null + ...serialiseBaseSensitivity(multiSensitivity), + paramSettings: multiSensitivity.paramSettings }; } diff --git a/app/static/tests/unit/serialiser.test.ts b/app/static/tests/unit/serialiser.test.ts index f6da475c4..b2e2b3291 100644 --- a/app/static/tests/unit/serialiser.test.ts +++ b/app/static/tests/unit/serialiser.test.ts @@ -20,6 +20,7 @@ import { defaultState as defaultGraphSettingsState } from "../../src/app/store/g import { Language } from "../../src/app/types/languageTypes"; import { AdvancedOptions } from "../../src/app/types/responseTypes"; import { AdvancedComponentType } from "../../src/app/store/run/state"; +import { noSensitivityUpdateRequired } from "../../src/app/store/sensitivity/sensitivity"; describe("serialise", () => { const codeState = { @@ -212,6 +213,73 @@ describe("serialise", () => { } }; + const multiSensitivityBatchPars = { + base: { alpha: 1, beta: 1.1 }, + varying: [ + { + name: "alpha", + values: [0.5, 0.75, 1, 1.25, 1.5] + }, + { + name: "beta", + values: [1, 1.1, 2] + } + ] + }; + + const multiSensitivityState = { + running: true, + loading: false, + downloading: false, + userSummaryDownloadFileName: "", + paramSettings: [ + { + parameterToVary: "alpha", + scaleType: SensitivityScaleType.Arithmetic, + variationType: SensitivityVariationType.Percentage, + variationPercentage: 50, + rangeFrom: 0, + rangeTo: 1, + numberOfRuns: 5, + customValues: [] + }, + { + parameterToVary: "beta", + scaleType: SensitivityScaleType.Arithmetic, + variationType: SensitivityVariationType.Percentage, + variationPercentage: 10, + rangeFrom: 0, + rangeTo: 1, + numberOfRuns: 3, + customValues: [] + } + ], + sensitivityUpdateRequired: { + modelChanged: false, + parameterValueChanged: false, + endTimeChanged: false, + sensitivityOptionsChanged: false, + numberOfReplicatesChanged: false, + advancedSettingsChanged: false + }, + result: { + inputs: { + endTime: 10, + pars: multiSensitivityBatchPars + }, + batch: { + pars: multiSensitivityBatchPars, + solutions: [jest.fn(), jest.fn()], + errors: [], + successfulVaryingParams: [], + valueAtTime: jest.fn(), + extreme: jest.fn(), + compute: jest.fn() + }, + error: { error: "multiSensitivity error", detail: "multiSensitivity error detail" } + } + }; + const fitDataState = { data: [{ time: 0, cases: 0 }, { time: 1, cases: 2 }], columns: ["time", "cases"], @@ -274,7 +342,7 @@ describe("serialise", () => { model: modelState, run: runState, sensitivity: sensitivityState, - multiSensitivity: mockMultiSensitivityState(), + multiSensitivity: multiSensitivityState, versions: { versions: null }, graphSettings: { logScaleYAxis: true, @@ -300,7 +368,7 @@ describe("serialise", () => { model: modelState, run: runState, sensitivity: sensitivityState, - multiSensitivity: mockMultiSensitivityState(), + multiSensitivity: multiSensitivityState, fitData: fitDataState, modelFit: modelFitState, versions: { versions: null }, @@ -364,14 +432,7 @@ describe("serialise", () => { const expectedSensitivity = { running: false, paramSettings: sensitivityState.paramSettings, - sensitivityUpdateRequired: { - modelChanged: false, - parameterValueChanged: false, - endTimeChanged: false, - sensitivityOptionsChanged: false, - numberOfReplicatesChanged: false, - advancedSettingsChanged: false - }, + sensitivityUpdateRequired: noSensitivityUpdateRequired(), plotSettings: sensitivityState.plotSettings, result: { inputs: sensitivityState.result.inputs, @@ -387,6 +448,17 @@ describe("serialise", () => { } }; + const expectedMultiSensitivity = { + running: false, + paramSettings: multiSensitivityState.paramSettings, + sensitivityUpdateRequired: noSensitivityUpdateRequired(), + result: { + inputs: multiSensitivityState.result.inputs, + hasResult: true, + error: multiSensitivityState.result.error + } + }; + const expectedGraphSettings = { logScaleYAxis: true, lockYAxis: true, @@ -431,6 +503,7 @@ describe("serialise", () => { model: expectedModel, run: expectedRun, sensitivity: expectedSensitivity, + multiSensitivity: expectedMultiSensitivity, graphSettings: expectedGraphSettings }; expect(JSON.parse(serialised)).toStrictEqual(expected); @@ -444,6 +517,7 @@ describe("serialise", () => { model: expectedModel, run: expectedRun, sensitivity: expectedSensitivity, + multiSensitivity: expectedMultiSensitivity, fitData: expectedFitData, modelFit: expectedModelFit, graphSettings: expectedGraphSettings @@ -472,6 +546,13 @@ describe("serialise", () => { batch: null } }, + multiSensitivity: { + ...multiSensitivityState, + result: { + ...multiSensitivityState.result, + batch: null + } + }, modelFit: { ...modelFitState, result: { @@ -495,6 +576,10 @@ describe("serialise", () => { ...expectedSensitivity, result: { ...expectedSensitivity.result, hasResult: false } }, + multiSensitivity: { + ...expectedMultiSensitivity, + result: { ...expectedMultiSensitivity.result, hasResult: false } + }, fitData: expectedFitData, modelFit: { ...expectedModelFit, @@ -511,6 +596,7 @@ describe("serialise", () => { model: { ...modelState, odin: null }, run: { ...runState, resultOde: null, resultDiscrete: null }, sensitivity: { ...sensitivityState, result: null }, + multiSensitivity: { ...multiSensitivityState, result: null }, modelFit: { ...modelFitState, result: null } }; const serialised = serialiseState(state); @@ -521,6 +607,7 @@ describe("serialise", () => { model: { ...expectedModel, hasOdin: false }, run: { ...expectedRun, resultOde: null, resultDiscrete: null }, sensitivity: { ...expectedSensitivity, result: null }, + multiSensitivity: { ...expectedMultiSensitivity, result: null }, fitData: expectedFitData, modelFit: { ...expectedModelFit, result: null }, graphSettings: expectedGraphSettings @@ -535,6 +622,7 @@ describe("serialise", () => { model: mockModelState(), run: { ...mockRunState(), advancedSettings: expectedRun.advancedSettings }, sensitivity: mockSensitivityState(), + multiSensitivity: mockMultiSensitivityState(), fitData: mockFitDataState(), modelFit: mockModelFitState(), versions: mockVersionsState(), @@ -566,6 +654,7 @@ describe("serialise", () => { model: mockModelState(), run: mockRunState(), sensitivity: mockSensitivityState(), + multiSensitivity: mockMultiSensitivityState(), fitData: mockFitDataState(), modelFit: mockModelFitState(), versions: mockVersionsState(), @@ -588,6 +677,7 @@ describe("serialise", () => { } as any), run: { ...mockRunState(), advancedSettings: expectedRun.advancedSettings }, sensitivity: mockSensitivityState(), + multiSensitivity: mockMultiSensitivityState(), fitData: mockFitDataState(), modelFit: mockModelFitState(), versions: mockVersionsState() @@ -619,6 +709,7 @@ describe("serialise", () => { model: mockModelState(), run: { ...mockRunState(), advancedSettings: expectedRun.advancedSettings }, sensitivity: mockSensitivityState(), + multiSensitivity: mockMultiSensitivityState(), fitData: mockFitDataState(), modelFit: mockModelFitState(), versions: mockVersionsState() diff --git a/app/static/tests/unit/store/multiSensitivity/actions.test.ts b/app/static/tests/unit/store/multiSensitivity/actions.test.ts index 70f5414cc..e8402e110 100644 --- a/app/static/tests/unit/store/multiSensitivity/actions.test.ts +++ b/app/static/tests/unit/store/multiSensitivity/actions.test.ts @@ -6,10 +6,11 @@ import { mockRunState, mockRunnerOde, rootGetters, - testCommonRunSensitivity + testCommonRunSensitivity, expectRunOnRehydrateToUseParametersFromResult } from "../sensitivity/actions.test"; import { AppType } from "../../../../src/app/store/appState/state"; import { BaseSensitivityMutation } from "../../../../src/app/store/sensitivity/mutations"; +import {SensitivityAction} from "../../../../src/app/store/sensitivity/actions"; jest.mock("../../../../src/app/excel/wodinSensitivitySummaryDownload"); @@ -66,4 +67,9 @@ describe("multiSensitivity actions", () => { ); expect(dispatch).not.toHaveBeenCalled(); }); + + it("run multiSensitivity on rehydrate uses parameters from result", () => { + expectRunOnRehydrateToUseParametersFromResult( + actions[MultiSensitivityAction.RunMultiSensitivityOnRehydrate] as any); + }); }); diff --git a/app/static/tests/unit/store/sensitivity/actions.test.ts b/app/static/tests/unit/store/sensitivity/actions.test.ts index 4a4e801d5..e7a7f7882 100644 --- a/app/static/tests/unit/store/sensitivity/actions.test.ts +++ b/app/static/tests/unit/store/sensitivity/actions.test.ts @@ -1,4 +1,4 @@ -import { Action } from "vuex"; +import {Action, ActionTree} from "vuex"; import { actions, BaseSensitivityAction, SensitivityAction } from "../../../../src/app/store/sensitivity/actions"; import { BaseSensitivityMutation, SensitivityMutation } from "../../../../src/app/store/sensitivity/mutations"; import { ModelGetter } from "../../../../src/app/store/model/getters"; @@ -8,6 +8,7 @@ import { AdvancedOptions } from "../../../../src/app/types/responseTypes"; import { AdvancedComponentType } from "../../../../src/app/store/run/state"; import { WodinSensitivitySummaryDownload } from "../../../../src/app/excel/wodinSensitivitySummaryDownload"; import Mock = jest.Mock; +import {BaseSensitivityState} from "../../../../src/app/store/sensitivity/state"; jest.mock("../../../../src/app/excel/wodinSensitivitySummaryDownload"); @@ -63,6 +64,55 @@ export const rootGetters = { [`model/${ModelGetter.hasRunner}`]: true }; +export const expectRunOnRehydrateToUseParametersFromResult = (action: Action) => { + const mockResultBatchPars = {}; + const rootState = { + model: mockModelState, + run: mockRunState + }; + const state = { + result: { + inputs: { + pars: mockResultBatchPars + } + } + }; + + const commit = jest.fn(); + const dispatch = jest.fn(); + + (action as any)({ + rootState, getters, commit, dispatch, state, rootGetters + }); + + expect(commit).toHaveBeenCalledTimes(2); + expect(commit.mock.calls[0][0]).toBe(BaseSensitivityMutation.SetResult); + expect(commit.mock.calls[0][1]).toStrictEqual({ + inputs: { endTime: 99, pars: mockResultBatchPars }, + batch: mockBatch, + error: null + }); + expect(commit.mock.calls[1][0]).toBe(BaseSensitivityMutation.SetUpdateRequired); + expect(commit.mock.calls[1][1]).toStrictEqual({ + endTimeChanged: false, + modelChanged: false, + parameterValueChanged: false, + sensitivityOptionsChanged: false, + numberOfReplicatesChanged: false, + advancedSettingsChanged: false + }); + + expect(mockRunnerOde.batchRun).toHaveBeenCalledWith( + rootState.model.odin, + mockBatchPars, + 0, + 99, + defaultAdvanced + ); + + expect(dispatch).not.toHaveBeenCalled(); +}; + describe("BaseSensitivity actions", () => { afterEach(() => { jest.clearAllMocks(); @@ -510,51 +560,6 @@ describe("Sensitivity actions", () => { }); it("run sensitivity on rehydrate uses parameters from result", () => { - const mockResultBatchPars = {}; - const rootState = { - model: mockModelState, - run: mockRunState - }; - const state = { - result: { - inputs: { - pars: mockResultBatchPars - } - } - }; - - const commit = jest.fn(); - const dispatch = jest.fn(); - - (actions[SensitivityAction.RunSensitivityOnRehydrate] as any)({ - rootState, getters, commit, dispatch, state, rootGetters - }); - - expect(commit).toHaveBeenCalledTimes(2); - expect(commit.mock.calls[0][0]).toBe(BaseSensitivityMutation.SetResult); - expect(commit.mock.calls[0][1]).toStrictEqual({ - inputs: { endTime: 99, pars: mockResultBatchPars }, - batch: mockBatch, - error: null - }); - expect(commit.mock.calls[1][0]).toBe(BaseSensitivityMutation.SetUpdateRequired); - expect(commit.mock.calls[1][1]).toStrictEqual({ - endTimeChanged: false, - modelChanged: false, - parameterValueChanged: false, - sensitivityOptionsChanged: false, - numberOfReplicatesChanged: false, - advancedSettingsChanged: false - }); - - expect(mockRunnerOde.batchRun).toHaveBeenCalledWith( - rootState.model.odin, - mockBatchPars, - 0, - 99, - defaultAdvanced - ); - - expect(dispatch).not.toHaveBeenCalled(); + expectRunOnRehydrateToUseParametersFromResult(actions[SensitivityAction.RunSensitivityOnRehydrate] as any); }); });