Skip to content

Commit

Permalink
tests cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nicedexter committed Sep 13, 2019
1 parent 9e4e4fd commit 12e2beb
Show file tree
Hide file tree
Showing 66 changed files with 1,000 additions and 13,964 deletions.
2 changes: 1 addition & 1 deletion .env.development
Original file line number Diff line number Diff line change
@@ -1 +1 @@
REACT_APP_BACKEND_URL=http://localhost:8080
REACT_APP_BACKEND_URL=http://localhost:8080
1 change: 1 addition & 0 deletions src/components/API/APIAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ class APIAdapter {

const p =
resultParsed && resultParsed.length > 0 && resultParsed[0].result;

if (p) {
const isExareme = p.every((r: any) => r.data && r.type);

Expand Down
28 changes: 19 additions & 9 deletions src/components/API/Core.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ export interface AlgorithmConstraint {

export interface AlgorithmParameter {
code: string;
constraints: any;
default_value: any;
constraints?: any;
default_value?: any;
value: any;
values?: any;
description: string;
label: string;
type: string;
visible: boolean;
description?: string;
label?: string;
type?: string;
visible?: boolean;
}

export interface Parameter {
Expand Down Expand Up @@ -161,6 +161,7 @@ class Core extends Container<State> {

const pathology = json.length > 0 ? json[0] : undefined;
let variables: any = [];

// TODO: fanciest function
const dummyAccumulator = (node: any) => {
if (node.variables) {
Expand Down Expand Up @@ -188,7 +189,11 @@ class Core extends Container<State> {
}
};

public setPathology = (code: string): Promise<void> => {
public setPathology = async (code: string): Promise<void> => {
if (!this.state.datasets) {
await this.pathologies();
}

const pathology =
pathologiesCached && pathologiesCached.filter(g => g.code === code).pop();

Expand All @@ -199,8 +204,13 @@ class Core extends Container<State> {
});
};

public datasets = async (): Promise<VariableEntity[] | undefined> =>
Promise.resolve(this.state.datasets);
public datasets = async (): Promise<VariableEntity[] | undefined> => {
if (!this.state.datasets) {
await this.pathologies();
}

return Promise.resolve(this.state.datasets);
};

public stats = async (): Promise<void> => {
try {
Expand Down
2 changes: 1 addition & 1 deletion src/components/API/ExaremeAPIAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ const buildExaremeAlgorithmRequest = (
model: ModelResponse,
selectedMethod: Algorithm,
newParams: AlgorithmParameter[]
) => {
): AlgorithmParameter[] => {
const params = [];
let variableString;
let covariablesArray: string[] = [];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import APICore from '../API/Core';
import APICore, { AlgorithmParameter, VariableEntity } from './Core';
import { buildExaremeAlgorithmRequest } from './ExaremeAPIAdapter';
import APIExperiment, {
ExperimentPayload,
State as ExperimentState
} from '../API/Experiment';
import APIModel, { ModelState } from '../API/Model';
import config from '../API/RequestHeaders';
import { State as CoreState } from '../API/Core';
} from './Experiment';
import APIModel, { ModelState, ModelResponse } from './Model';
import config from './RequestHeaders';
import { Engine } from './Experiment';

const apiModel = new APIModel(config);
const apiExperiment = new APIExperiment(config);
const apiCore = new APICore(config);

const TIMEOUT_DURATION = 60 * 2;

const datasets = async (): Promise<CoreState> => {
// Tests utils

const getDatasets = async (): Promise<VariableEntity[] | undefined> => {
await apiCore.datasets();
return apiCore.state;

return apiCore.state.datasets;
};

const createModel = async ({
Expand All @@ -29,6 +33,37 @@ const createModel = async ({
return apiModel.state;
};

const createExaremePayload = (
model: (datasets: VariableEntity[]) => ModelResponse,
datasets: VariableEntity[],
experimentCode: string,
parameters: AlgorithmParameter[],
modelSlug: string
): ExperimentPayload => {
const requestParameters = buildExaremeAlgorithmRequest(
model(datasets),
{ code: experimentCode, name: experimentCode, validation: false },
parameters
);

const payload: ExperimentPayload = {
algorithms: [
{
code: experimentCode,
name: experimentCode,
parameters: requestParameters,
validation: false
}
],
model: modelSlug,
name: experimentCode,
validations: [],
engine: Engine.Exareme
};

return payload;
};

const createExperiment = async ({
experiment
}: {
Expand Down Expand Up @@ -70,4 +105,11 @@ const uid = (): string =>
return v.toString(16);
});

export { createExperiment, createModel, datasets, uid, waitForResult };
export {
createExperiment,
createModel,
createExaremePayload,
getDatasets,
uid,
waitForResult
};
8 changes: 8 additions & 0 deletions src/components/API/__tests__/Integration/Core.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,12 @@ describe('Integration Test Core API', () => {
expect(error).toBeFalsy();
expect(result).toBeTruthy();
});

it('get pathologies', async () => {
await apiCore.pathologies();
const result = apiCore.state.pathologies;
const error = apiCore.state.error;
expect(error).toBeFalsy();
expect(result).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { mount } from 'enzyme';
import * as React from 'react';
import { buildExaremeAlgorithmRequest } from '../../../ExaremeAPIAdapter';
import Result from '../../../../Result/Result';

import Result from '../../../Result/Result';
import { VariableEntity } from '../../Core';
import { ExperimentPayload } from '../../Experiment';
import {
createExaremePayload,
createExperiment,
createModel,
getDatasets,
waitForResult
} from '../../../../utils/TestUtils';
import { VariableEntity } from '../../../Core';
import { Engine } from '../../../Experiment';
} from '../../Utils';

// config

Expand All @@ -20,7 +22,7 @@ const parameters = [
{ code: 'sstype', value: 2 },
{ code: 'outputformat', value: 'pfa' }
];
const datasets = [{ code: 'adni' }];

const model: any = (datasets: VariableEntity[]) => ({
query: {
coVariables: [],
Expand All @@ -42,42 +44,38 @@ const model: any = (datasets: VariableEntity[]) => ({
// Test

describe('Integration Test for experiment API', () => {
let datasets: VariableEntity[] | undefined;

beforeAll(async () => {
datasets = await getDatasets();
expect(datasets).toBeTruthy();

const mstate = await createModel({
model: model(datasets),
modelSlug
});

expect(mstate.error).toBeFalsy();
expect(mstate.model).toBeTruthy();

return true;
return datasets !== undefined && mstate.model !== undefined;
});

it(`create ${experimentCode}`, async () => {
const requestParameters = buildExaremeAlgorithmRequest(
model(datasets),
{ code: experimentCode },
parameters
if (!datasets) {
throw new Error('datasets not defined');
}
const payload: ExperimentPayload = createExaremePayload(
model,
datasets,
experimentCode,
parameters,
modelSlug
);

const payload: ExperimentPayload = {
algorithms: [
{
code: experimentCode,
name: experimentCode, // FIXME: name is used to parse response which is bad !!!
parameters: requestParameters,
validation: false
}
],
model: modelSlug,
name: `${experimentCode}-${modelSlug}`,
validations: [],
engine: Engine.Exareme
};

const { error, experiment } = await createExperiment({
experiment: payload
});

expect(error).toBeFalsy();
expect(experiment).toBeTruthy();

Expand All @@ -95,6 +93,6 @@ describe('Integration Test for experiment API', () => {
const wrapper = mount(<Result {...props} />);
expect(wrapper.find('.error')).toHaveLength(0);
expect(wrapper.find('.loading')).toHaveLength(0);
expect(wrapper.find('div#tabs-methods')).toHaveLength(1);
expect(wrapper.find('.result')).toHaveLength(1);
});
});
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { mount } from 'enzyme';
import * as React from 'react';

import Result from '../../../../Result/Result';
import Result from '../../../Result/Result';
import { VariableEntity } from '../../Core';
import { ExperimentPayload } from '../../Experiment';
import {
createExaremePayload,
createExperiment,
createModel,
getDatasets,
waitForResult
} from '../../../../utils/TestUtils';
import { VariableEntity } from '../../../Core';
import { buildExaremeAlgorithmRequest } from '../../../ExaremeAPIAdapter';
import { Engine } from '../../../Experiment';
} from '../../Utils';

// config

const modelSlug = `model-${Math.round(Math.random() * 10000)}`;
const experimentCode = 'HISTOGRAMS';
const parameters = [{ code: 'bins', value: '40' }];
const datasets = [{ code: 'adni' }];

const model: any = (datasets: VariableEntity[]) => ({
query: {
coVariables: [{ code: 'lefthippocampus' }],
Expand All @@ -38,44 +39,38 @@ const model: any = (datasets: VariableEntity[]) => ({
// Test

describe('Integration Test for experiment API', () => {
let datasets: VariableEntity[] | undefined;

beforeAll(async () => {
datasets = await getDatasets();
expect(datasets).toBeTruthy();

const mstate = await createModel({
model: model(datasets),
modelSlug
});

expect(mstate.error).toBeFalsy();
expect(mstate.model).toBeTruthy();

return true;
return datasets !== undefined && mstate.model !== undefined;
});

it(`create ${experimentCode}`, async () => {
const requestParameters = buildExaremeAlgorithmRequest(
model(datasets),
{ code: experimentCode },
parameters
if (!datasets) {
throw new Error('datasets not defined');
}
const payload: ExperimentPayload = createExaremePayload(
model,
datasets,
experimentCode,
parameters,
modelSlug
);

const payload: ExperimentPayload = {
algorithms: [
{
code: experimentCode,
name: experimentCode, // FIXME: name is used to parse response which is bad !!!
parameters: requestParameters,
validation: false
}
],
model: modelSlug,
name: `${experimentCode}-${modelSlug}`,
validations: [],
engine: Engine.Exareme
};

console.log(payload);

const { error, experiment } = await createExperiment({
experiment: payload
});

expect(error).toBeFalsy();
expect(experiment).toBeTruthy();

Expand All @@ -93,6 +88,6 @@ describe('Integration Test for experiment API', () => {
const wrapper = mount(<Result {...props} />);
expect(wrapper.find('.error')).toHaveLength(0);
expect(wrapper.find('.loading')).toHaveLength(0);
expect(wrapper.find('div#tabs-methods')).toHaveLength(1);
expect(wrapper.find('.result')).toHaveLength(1);
});
});
Loading

0 comments on commit 12e2beb

Please sign in to comment.