Skip to content

Commit

Permalink
Merge pull request #356 from Chia-Network/tests/coverage2
Browse files Browse the repository at this point in the history
Updated tests
  • Loading branch information
MichaelTaylor3D authored Mar 17, 2022
2 parents 56acc11 + d5067e8 commit ad6293d
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/controllers/units.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export const findAll = async (req, res) => {
where = {};
}

where.warehouseProjectId = {
where.warehouseUnitId = {
[Sequelize.Op.in]: mappedResults,
};
}
Expand Down
2 changes: 0 additions & 2 deletions src/utils/data-assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ export const assertNoPendingCommits = async () => {
raw: true,
});

console.log(pendingCommits);

if (pendingCommits.length > 0) {
throw new Error(
'You currently have changes pending on the blockchain. Please wait for them to propagate before making more changes',
Expand Down
108 changes: 89 additions & 19 deletions tests/resources/units.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import app from '../../src/server';
import supertest from 'supertest';
import newUnit from '../test-data/new-unit.json';
import { pullPickListValues } from '../../src/utils/data-loaders';
import { expect } from 'chai';
import sinon from 'sinon';

import { Organization } from '../../src/models';
import app from '../../src/server';
import * as testFixtures from '../test-fixtures';
import datalayer from '../../src/datalayer';
import { prepareDb } from '../../src/database';
import newUnit from '../test-data/new-unit.json';
import { pullPickListValues } from '../../src/utils/data-loaders';

describe('Units Resource CRUD', function () {
before(async function () {
Expand All @@ -17,16 +22,22 @@ describe('Units Resource CRUD', function () {
});

describe('success states', function () {
// eslint-disable-next-line no-unused-vars
let warehouseUnitId;
let response;
beforeEach(async function () {
await supertest(app).post('/v1/units').send(newUnit);
await testFixtures.createTestHomeOrg();
await testFixtures.getHomeOrgId();
await testFixtures.createNewUnit();
await testFixtures.commitStagingRecords();
await testFixtures.waitForDataLayerSync();
const result = await supertest(app).get('/v1/units');
warehouseUnitId = result.body[0].warehouseUnitId;
response = result.body[0];
});

afterEach(async function () {
await supertest(app).delete('/v1/units').send(newUnit);
await supertest(app).delete('/v1/units').send({
warehouseUnitId: newUnit.warehouseUnitId,
});
await supertest(app).post('/v1/staging/commit');
});

it('gets all the units available', async function () {
Expand All @@ -35,34 +46,93 @@ describe('Units Resource CRUD', function () {

expect(result.body.length).to.not.equal(0);
});
it('gets all the units filtered by orgUid', function () {
it('gets all the units filtered by orgUid', async function () {
const result = await supertest(app)
.get('/v1/units')
.query({ orgUid: response.orgUid });

expect(result.body.length).to.not.equal(1);
// ?orgUid=XXXX
});
it('gets all the units for a search term', function () {
it('gets all the units for a search term', async function () {
// ?search=XXXX
const result = await supertest(app)
.get('/v1/units')
.query({ search: 'Certification' });

expect(result.body.length).to.not.equal(1);
});
it('gets all the units for a search term filtered by orgUid', function () {
it('gets all the units for a search term filtered by orgUid', async function () {
// ?orgUid=XXXX&search=XXXX
const result = await supertest(app)
.get('/v1/units')
.query({ orgUid: response.orgUid, search: 'Certification' });

expect(result.body.length).to.not.equal(1);
});
it('gets optional paginated results', function () {
it('gets optional paginated results', async function () {
// ?page=X&limit=10
const result = await supertest(app)
.get('/v1/units')
.query({ page: 1, limit: 1 });

expect(result.body.length).to.not.equal(1);
});
it('finds a single result by warehouseUnitId', function () {
it('finds a single result by warehouseUnitId', async function () {
// ?warehouseUnitId=XXXX
const result = await supertest(app)
.get('/v1/units')
.query({ warehouseUnitId: response.warehouseUnitId, limit: 1 });

expect(result.body.length).to.not.equal(1);
});
});
});

describe('POST Units - Create', function () {
describe('error states', function () {
it('errors if no home organization exists', function () {});
it('errors if there is a current set of pending commits', function () {});
it('errors if there if there is no connection to the datalayer', function () {});
it('errors if no home organization exists', async function () {
await Organization.destroy({
where: {},
truncate: true,
});

const responsePost = await supertest(app)
.post('/v1/units')
.send(newUnit);
expect(responsePost.statusCode).to.equal(400);
expect(responsePost.body.error).to.equal(
'No Home organization found, please create an organization to write data',
);
});
it('errors if there is a current set of pending commits', async function () {});
it('errors if there if there is no connection to the datalayer', async function () {
await testFixtures.createTestHomeOrg();
sinon.stub(datalayer, 'dataLayerAvailable').resolves(false);
const responsePost = await supertest(app)
.post('/v1/units')
.send(newUnit);
expect(responsePost.statusCode).to.equal(400);
expect(responsePost.body.error).to.equal(
'Can not establish connection to Chia Datalayer',
);
});
});

describe('success states', function () {
it('creates a new unit with no child tables', function () {});
it('creates a new unit with all child tables', function () {});
describe.skip('success states', function () {
it('creates a new unit with no child tables', async function () {
await testFixtures.createTestHomeOrg();
await testFixtures.createNewUnit(newUnit);
});

it('creates a new unit with all child tables', async function () {
await testFixtures.createTestHomeOrg();
const payload = newUnit;
delete payload.labels;
delete payload.issuances;

await testFixtures.createNewUnit(payload);
});
});
});

Expand Down

0 comments on commit ad6293d

Please sign in to comment.