Skip to content

Commit

Permalink
Address comments pr 126 (#128)
Browse files Browse the repository at this point in the history
* add route test
* add test for getListOfExperiments
  • Loading branch information
Anugerah Erlaut authored May 25, 2021
1 parent 44cd2ee commit ba97ffc
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 29 deletions.
7 changes: 7 additions & 0 deletions src/api/route-services/__mocks__/experiment.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ const mockExperimentData = jest.fn((experimentId) => new Promise((resolve) => {
});
}));

const mockGetListOfExperiments = jest.fn((experimentIds) => new Promise((resolve) => {
resolve(experimentIds.map((experimentId) => ({
experimentId,
})));
}));

const mockGetCellSets = jest.fn(() => new Promise((resolve) => {
resolve({
cellSets: [
Expand Down Expand Up @@ -102,6 +108,7 @@ const mockUpdateProcessingConfig = jest.fn(

const mock = jest.fn().mockImplementation(() => ({
getExperimentData: mockExperimentData,
getListOfExperiments: mockGetListOfExperiments,
getCellSets: mockGetCellSets,
updateCellSets: mockUpdateCellSets,
getProcessingConfig: mockGetProcessingConfig,
Expand Down
14 changes: 14 additions & 0 deletions src/api/route-services/__mocks__/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ const mockGetProject = jest.fn((projectUuid) => new Promise((resolve) => {
resolve(OK());
}));

const mockGetExperiments = jest.fn((projectUuid) => new Promise((resolve) => {
if (projectUuid === 'unknown-project') {
throw new NotFoundError('Project not found');
}

resolve([
{
experimentId: 'mock-experiment',
name: 'someExperiments',
},
]);
}));

const mockUpdateProject = jest.fn((projectUuid) => new Promise((resolve) => {
if (projectUuid === 'unknown-project') {
throw new NotFoundError('Project not found');
Expand All @@ -25,6 +38,7 @@ const mockDeleteProject = jest.fn((projectUuid) => new Promise((resolve) => {
}));

const mock = jest.fn().mockImplementation(() => ({
getExperiments: mockGetExperiments,
getProject: mockGetProject,
updateProject: mockUpdateProject,
deleteProject: mockDeleteProject,
Expand Down
2 changes: 1 addition & 1 deletion src/api/route-services/experiment.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ExperimentService {
const params = {
RequestItems: {
[this.experimentsTableName]: {
Keys: [...experimentIds].map((experimentId) => convertToDynamoDbRecord({ experimentId })),
Keys: experimentIds.map((experimentId) => convertToDynamoDbRecord({ experimentId })),
},
},
};
Expand Down
2 changes: 1 addition & 1 deletion src/specs/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ paths:
items:
$ref: ./models/api-body-schemas/Experiment.v1.yaml
'401':
description: Not Found
description: Unauthorized
content:
application/json:
schema:
Expand Down
30 changes: 30 additions & 0 deletions tests/api/route-services/experiment.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
mockDynamoUpdateItem,
mockS3GetObject,
mockS3PutObject,
mockDynamoBatchGetItem,
} = require('../../test-utils/mockAWSServices');

jest.setTimeout(30000);
Expand Down Expand Up @@ -38,6 +39,35 @@ describe('tests for the experiment service', () => {
.then(() => done());
});

it('Get list of experiments work', async (done) => {
const experimentIds = ['experiment-1', 'experiment-2', 'experiment-3'];

const response = {
Responses: {
'experiments-test': experimentIds.map((experimentId) => AWS.DynamoDB.Converter.marshall({ experimentId })),
},
};

const fnSpy = mockDynamoBatchGetItem(response);

(new ExperimentService()).getListOfExperiments(experimentIds)
.then((data) => {
expect(data).toEqual(experimentIds.map((experimentId) => ({ experimentId })));
expect(fnSpy).toHaveBeenCalledWith(
{
RequestItems: {
'experiments-test': {
Keys: experimentIds.map(
(experimentId) => AWS.DynamoDB.Converter.marshall({ experimentId }),
),
},
},
},
);
})
.then(() => done());
});

it('Get cell sets works', async (done) => {
const jsData = {
cellSets: [
Expand Down
69 changes: 42 additions & 27 deletions tests/api/route-services/projects.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,38 @@ const AWSMock = require('aws-sdk-mock');
const AWS = require('../../../src/utils/requireAWS');

const ProjectsService = require('../../../src/api/route-services/projects');
const ExperimentsService = require('../../../src/api/route-services/experiment');
const { mockDynamoUpdateItem, mockDynamoDeleteItem, mockDynamoGetItem } = require('../../test-utils/mockAWSServices');
const { OK } = require('../../../src/utils/responses');

jest.mock('../../../src/api/route-services/experiment');

describe('tests for the projects service', () => {
const mockProject = {
name: 'Test project',
description: '',
createdDate: '',
lastModified: '',
uuid: 'project-1',
experiments: ['experiment-1'],
lastAnalyzed: null,
samples: [],
};

afterEach(() => {
AWSMock.restore('DynamoDB');
});

it('GetProject gets project and samples properly', async (done) => {
const project = {
name: 'Test project',
description: '',
createdDate: '',
lastModified: '',
uuid: 'project-1',
experiments: [],
lastAnalyzed: null,
samples: [],
};

const marshalledKey = AWS.DynamoDB.Converter.marshall({
projectUuid: 'project-1',
});

const getFnSpy = mockDynamoGetItem({ projects: project });
const getFnSpy = mockDynamoGetItem({ projects: mockProject });

(new ProjectsService()).getProject('project-1')
.then((res) => {
expect(res).toEqual(project);
expect(res).toEqual(mockProject);
expect(getFnSpy).toHaveBeenCalledWith({
TableName: 'projects-test',
Key: marshalledKey,
Expand All @@ -39,29 +42,41 @@ describe('tests for the projects service', () => {
.then(() => done());
});

it('UpdateProject updates project properly', async (done) => {
const jsData = {
name: 'Test project',
description: '',
createdDate: '',
lastModified: '',
uuid: 'project-1',
experiments: [],
lastAnalyzed: null,
samples: [],
};
it('GetExperiments gets projects', async (done) => {
const fnSpy = mockDynamoGetItem({ projects: mockProject });

const experimentsService = new ExperimentsService();

const marshalledKey = AWS.DynamoDB.Converter.marshall({
projectUuid: mockProject.projectUuid,
});

(new ProjectsService()).getExperiments()
.then((res) => {
expect(res).toEqual([{ experimentId: mockProject.experiments[0] }]);
expect(fnSpy).toHaveBeenCalledWith({
TableName: 'projects-test',
Key: marshalledKey,
});
expect(
experimentsService.getListOfExperiments,
).toHaveBeenCalledWith(mockProject.experiments);
})
.then(() => done());
});

it('UpdateProject updates project properly', async (done) => {
const marshalledKey = AWS.DynamoDB.Converter.marshall({
projectUuid: 'project-1',
});

const marshalledData = AWS.DynamoDB.Converter.marshall({
':project': jsData,
':project': mockProject,
});

const getItemSpy = mockDynamoUpdateItem({ projects: jsData });
const getItemSpy = mockDynamoUpdateItem({ projects: mockProject });

(new ProjectsService()).updateProject('project-1', jsData)
(new ProjectsService()).updateProject('project-1', mockProject)
.then((res) => {
expect(res).toEqual(OK());
expect(getItemSpy).toHaveBeenCalledWith({
Expand Down
24 changes: 24 additions & 0 deletions tests/api/routes/projects.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,30 @@ describe('tests for projects route', () => {
jest.restoreAllMocks();
});

it('Getting project experiments send 200', async (done) => {
request(app)
.get('/v1/projects/someId/experiments')
.expect(200)
.end((err) => {
if (err) {
return done(err);
}
return done();
});
});

it('Getting project experiments send 404 if id is not found', async (done) => {
request(app)
.get('/v1/projects/unknown-project/experiments')
.expect(404)
.end((err) => {
if (err) {
return done(err);
}
return done();
});
});

it('Updating project send 200', async (done) => {
const payload = {
name: 'Test project',
Expand Down
11 changes: 11 additions & 0 deletions tests/test-utils/mockAWSServices.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ const mockDynamoGetItem = (payload = {}, error = null) => {
return fnSpy;
};

const mockDynamoBatchGetItem = (response = {}, error = null) => {
const fnSpy = jest.fn((x) => x);
AWSMock.setSDKInstance(AWS);
AWSMock.mock('DynamoDB', 'batchGetItem', (params, callback) => {
fnSpy(params);
callback(error, response);
});
return fnSpy;
};

const mockDynamoDeleteItem = (payload = {}, error = null) => {
const fnSpy = jest.fn((x) => x);
AWSMock.setSDKInstance(AWS);
Expand Down Expand Up @@ -76,6 +86,7 @@ const mockS3PutObject = (payload = {}, error = null) => {

module.exports = {
mockDynamoGetItem,
mockDynamoBatchGetItem,
mockDynamoQuery,
mockDynamoUpdateItem,
mockDynamoDeleteItem,
Expand Down

0 comments on commit ba97ffc

Please sign in to comment.