Skip to content

Commit

Permalink
- DEVSU-2521
Browse files Browse the repository at this point in the history
- Add POST endpoint to genes and unit test
  • Loading branch information
bnguyen-bcgsc committed Nov 25, 2024
1 parent f0d452f commit 9573f1d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
29 changes: 28 additions & 1 deletion app/routes/report/gene.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ const router = express.Router({mergeParams: true});

const schemaGenerator = require('../../schemas/schemaGenerator');
const validateAgainstSchema = require('../../libs/validateAgainstSchema');
const {REPORT_UPDATE_BASE_URI} = require('../../constants');
const {REPORT_CREATE_BASE_URI, REPORT_UPDATE_BASE_URI} = require('../../constants');

// Generate schemas
const createSchema = schemaGenerator(db.models.genes, {
baseUri: REPORT_CREATE_BASE_URI,
});
const updateSchema = schemaGenerator(db.models.genes, {
baseUri: REPORT_UPDATE_BASE_URI, nothingRequired: true,
});
Expand Down Expand Up @@ -138,6 +141,30 @@ router.route('/')
error: {message: 'Unable to retrieve genes'},
});
}
})
.post(async (req, res) => {
// Validate request against schema
try {
validateAgainstSchema(createSchema, req.body);
} catch (error) {
const message = `Error while validating gene create request ${error}`;
logger.error(message);
return res.status(HTTP_STATUS.BAD_REQUEST).json({error: {message}});
}

// Create new entry in db
try {
const result = await db.models.genes.create({
...req.body,
reportId: req.report.id,
});
return res.status(HTTP_STATUS.CREATED).json(result.view('public'));
} catch (error) {
logger.error(`Unable to create gene ${error}`);
return res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).json({
error: {message: 'Unable to create gene'},
});
}
});

module.exports = router;
2 changes: 1 addition & 1 deletion app/routes/report/tmburMutationBurden.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ router.route('/')
.post(async (req, res) => {
// Validate request against schema
try {
await validateAgainstSchema(createSchema, req.body);
validateAgainstSchema(createSchema, req.body);
} catch (error) {
const message = `Error while validating tmbur mutation burden create request ${error}`;
logger.error(message);
Expand Down
46 changes: 45 additions & 1 deletion test/routes/report/gene.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('/reports/{report}/genes', () => {
afterEach(async () => {
return db.models.genes.destroy({
where: {ident: getGene.ident},
force: true,
// force: true,
});
});

Expand Down Expand Up @@ -162,6 +162,50 @@ describe('/reports/{report}/genes', () => {
});
});

describe('POST', () => {
test('/ - 201 Created', async () => {
const res = await request
.post(`/api/reports/${report.ident}/genes`)
.send(GENE_DATA)
.auth(username, password)
.type('json')
.expect(HTTP_STATUS.CREATED);

checkGene(res.body);

Check failure on line 174 in test/routes/report/gene.test.js

View workflow job for this annotation

GitHub Actions / eslint

Expected indentation of 6 spaces but found 8
expect(res.body).toEqual(expect.objectContaining(GENE_DATA));

// Check that record was created in the db
const result = await db.models.genes.findOne({
where: {ident: res.body.ident},
});
expect(result).not.toBeNull();

// Delete entry
await result.destroy({force: true});
});

test('/ - 400 Bad Request - Additional Property', async () => {
await request
.post(`/api/reports/${report.ident}/genes`)
.send({
...GENE_UPDATE_DATA,
additionalProperty: 'ADDITIONAL_PROPERTY',
})
.auth(username, password)
.type('json')
.expect(HTTP_STATUS.BAD_REQUEST);
});

test('/ - 400 Bad Request - Gene name is required', async () => {
await request
.post(`/api/reports/${report.ident}/genes`)
.send({})
.auth(username, password)
.type('json')
.expect(HTTP_STATUS.BAD_REQUEST);
});
});

describe('PUT', () => {
let putGene;

Expand Down

0 comments on commit 9573f1d

Please sign in to comment.