Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmaLRussell committed Sep 12, 2023
1 parent 1e0e97d commit 79d9fe0
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 74 deletions.
32 changes: 16 additions & 16 deletions app/static/src/app/serialise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<SerialisedSensitivityResult>;
Object.keys(sensitivity.parameterSetResults).forEach((name) => {
Expand All @@ -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
};
}

Expand Down
111 changes: 101 additions & 10 deletions app/static/tests/unit/serialiser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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"],
Expand Down Expand Up @@ -274,7 +342,7 @@ describe("serialise", () => {
model: modelState,
run: runState,
sensitivity: sensitivityState,
multiSensitivity: mockMultiSensitivityState(),
multiSensitivity: multiSensitivityState,
versions: { versions: null },
graphSettings: {
logScaleYAxis: true,
Expand All @@ -300,7 +368,7 @@ describe("serialise", () => {
model: modelState,
run: runState,
sensitivity: sensitivityState,
multiSensitivity: mockMultiSensitivityState(),
multiSensitivity: multiSensitivityState,
fitData: fitDataState,
modelFit: modelFitState,
versions: { versions: null },
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -431,6 +503,7 @@ describe("serialise", () => {
model: expectedModel,
run: expectedRun,
sensitivity: expectedSensitivity,
multiSensitivity: expectedMultiSensitivity,
graphSettings: expectedGraphSettings
};
expect(JSON.parse(serialised)).toStrictEqual(expected);
Expand All @@ -444,6 +517,7 @@ describe("serialise", () => {
model: expectedModel,
run: expectedRun,
sensitivity: expectedSensitivity,
multiSensitivity: expectedMultiSensitivity,
fitData: expectedFitData,
modelFit: expectedModelFit,
graphSettings: expectedGraphSettings
Expand Down Expand Up @@ -472,6 +546,13 @@ describe("serialise", () => {
batch: null
}
},
multiSensitivity: {
...multiSensitivityState,
result: {
...multiSensitivityState.result,
batch: null
}
},
modelFit: {
...modelFitState,
result: {
Expand All @@ -495,6 +576,10 @@ describe("serialise", () => {
...expectedSensitivity,
result: { ...expectedSensitivity.result, hasResult: false }
},
multiSensitivity: {
...expectedMultiSensitivity,
result: { ...expectedMultiSensitivity.result, hasResult: false }
},
fitData: expectedFitData,
modelFit: {
...expectedModelFit,
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -535,6 +622,7 @@ describe("serialise", () => {
model: mockModelState(),
run: { ...mockRunState(), advancedSettings: expectedRun.advancedSettings },
sensitivity: mockSensitivityState(),
multiSensitivity: mockMultiSensitivityState(),
fitData: mockFitDataState(),
modelFit: mockModelFitState(),
versions: mockVersionsState(),
Expand Down Expand Up @@ -566,6 +654,7 @@ describe("serialise", () => {
model: mockModelState(),
run: mockRunState(),
sensitivity: mockSensitivityState(),
multiSensitivity: mockMultiSensitivityState(),
fitData: mockFitDataState(),
modelFit: mockModelFitState(),
versions: mockVersionsState(),
Expand All @@ -588,6 +677,7 @@ describe("serialise", () => {
} as any),
run: { ...mockRunState(), advancedSettings: expectedRun.advancedSettings },
sensitivity: mockSensitivityState(),
multiSensitivity: mockMultiSensitivityState(),
fitData: mockFitDataState(),
modelFit: mockModelFitState(),
versions: mockVersionsState()
Expand Down Expand Up @@ -619,6 +709,7 @@ describe("serialise", () => {
model: mockModelState(),
run: { ...mockRunState(), advancedSettings: expectedRun.advancedSettings },
sensitivity: mockSensitivityState(),
multiSensitivity: mockMultiSensitivityState(),
fitData: mockFitDataState(),
modelFit: mockModelFitState(),
versions: mockVersionsState()
Expand Down
8 changes: 7 additions & 1 deletion app/static/tests/unit/store/multiSensitivity/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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);
});
});
Loading

0 comments on commit 79d9fe0

Please sign in to comment.