Skip to content

Commit

Permalink
feat: clock stubs in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
frantzarty committed Jan 14, 2022
1 parent d4388d5 commit 32fabfd
Showing 1 changed file with 92 additions and 111 deletions.
203 changes: 92 additions & 111 deletions tests/integration/unit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@ import chai from 'chai';
import supertest from 'supertest';
import app from '../../src/server';
import _ from 'lodash';
import sinon from 'sinon';

import { WAIT_TIME } from '../../src/fullnode/simulator';

const { expect } = chai;

describe('Create Unit Integration', () => {
let clock;
beforeEach(async () => {
clock = sinon.useFakeTimers({
toFake: ['setTimeout'],
});
await supertest(app).get(`/v1/staging/clean`);
});

afterEach(() => {
clock.restore();
});

it('splits an existing unit end-to-end', async () => {
// Get a unit to split
const allUnitsResult = await supertest(app).get('/v1/units');
Expand Down Expand Up @@ -76,7 +85,7 @@ describe('Create Unit Integration', () => {
expect(splitRecord2.unitCount).to.equal(1);

// Expect the split unitscounts to add up to the original unit count
expect(splitRecord1.unitCount + splitRecord2.unitCount).to.equal(
expect(splitRecord1.unitCount splitRecord2.unitCount).to.equal(
originalRecord.unitCount,
);

Expand Down Expand Up @@ -123,79 +132,63 @@ describe('Create Unit Integration', () => {
// The node simulator runs on an async process, we are importing
// the WAIT_TIME constant from the simulator, padding it and waiting for the
// appropriate amount of time for the simulator to finish its operations
await new Promise((resolve, reject) => {
setTimeout(async () => {
try {
const warehouseRes = await supertest(app)
.get(`/v1/units`)
.query({ warehouseUnitId: splitRecord1.warehouseUnitId });

const newRecord1 = warehouseRes.body;

expect(newRecord1.warehouseUnitId).to.equal(
splitRecord1.warehouseUnitId,
);
expect(newRecord1.orgUid).to.equal(splitRecord1.orgUid);
expect(newRecord1.unitOwnerOrgUid).to.equal(
splitRecord1.unitOwnerOrgUid,
);
expect(newRecord1.serialNumberBlock).to.equal(
splitRecord1.serialNumberBlock,
);
expect(newRecord1.countryJurisdictionOfOwner).to.equal(
splitRecord1.countryJurisdictionOfOwner,
);
expect(newRecord1.inCountryJurisdictionOfOwner).to.equal(
splitRecord1.inCountryJurisdictionOfOwner,
);
expect(newRecord1.tokenIssuanceHash).to.equal(
splitRecord1.tokenIssuanceHash,
);

const warehouse2Res = await supertest(app)
.get(`/v1/units`)
.query({ warehouseUnitId: splitRecord2.warehouseUnitId });

const newRecord2 = warehouse2Res.body;

expect(newRecord2.warehouseUnitId).to.equal(
splitRecord2.warehouseUnitId,
);
expect(newRecord2.orgUid).to.equal(splitRecord2.orgUid);
expect(newRecord2.unitOwnerOrgUid).to.equal(
splitRecord2.unitOwnerOrgUid,
);
expect(newRecord1.serialNumberBlock).to.equal(
splitRecord1.serialNumberBlock,
);
expect(newRecord2.countryJurisdictionOfOwner).to.equal(
splitRecord2.countryJurisdictionOfOwner,
);
expect(newRecord2.inCountryJurisdictionOfOwner).to.equal(
splitRecord2.inCountryJurisdictionOfOwner,
);
expect(newRecord2.tokenIssuanceHash).to.equal(
splitRecord2.tokenIssuanceHash,
);

// make sure the original record was deleted
const warehouse3Res = await supertest(app)
.get(`/v1/units`)
.query({ warehouseUnitId: warehouseUnitIdToSplit });
expect(warehouse3Res.body).to.equal(null);

// Make sure the staging table is cleaned up
const stagingRes3 = await supertest(app).get('/v1/staging');

// There should be no staging records left
expect(stagingRes3.body.length).to.equal(0);

resolve();
} catch (err) {
reject(err);
}
}, WAIT_TIME * 3);
});
clock.tick(WAIT_TIME * 4);

const warehouseRes = await supertest(app)
.get(`/v1/units`)
.query({ warehouseUnitId: splitRecord1.warehouseUnitId });

const newRecord1 = warehouseRes.body;

expect(newRecord1.warehouseUnitId).to.equal(splitRecord1.warehouseUnitId);
expect(newRecord1.orgUid).to.equal(splitRecord1.orgUid);
expect(newRecord1.unitOwnerOrgUid).to.equal(splitRecord1.unitOwnerOrgUid);
expect(newRecord1.serialNumberBlock).to.equal(
splitRecord1.serialNumberBlock,
);
expect(newRecord1.countryJuridictionOfOwner).to.equal(
splitRecord1.countryJuridictionOfOwner,
);
expect(newRecord1.inCountryJuridictionOfOwner).to.equal(
splitRecord1.inCountryJuridictionOfOwner,
);
expect(newRecord1.tokenIssuanceHash).to.equal(
splitRecord1.tokenIssuanceHash,
);

const warehouse2Res = await supertest(app)
.get(`/v1/units`)
.query({ warehouseUnitId: splitRecord2.warehouseUnitId });

const newRecord2 = warehouse2Res.body;

expect(newRecord2.warehouseUnitId).to.equal(splitRecord2.warehouseUnitId);
expect(newRecord2.orgUid).to.equal(splitRecord2.orgUid);
expect(newRecord2.unitOwnerOrgUid).to.equal(splitRecord2.unitOwnerOrgUid);
expect(newRecord1.serialNumberBlock).to.equal(
splitRecord1.serialNumberBlock,
);
expect(newRecord2.countryJuridictionOfOwner).to.equal(
splitRecord2.countryJuridictionOfOwner,
);
expect(newRecord2.inCountryJuridictionOfOwner).to.equal(
splitRecord2.inCountryJuridictionOfOwner,
);
expect(newRecord2.tokenIssuanceHash).to.equal(
splitRecord2.tokenIssuanceHash,
);

// make sure the original record was deleted
const warehouse3Res = await supertest(app)
.get(`/v1/units`)
.query({ warehouseUnitId: warehouseUnitIdToSplit });
expect(warehouse3Res.body).to.equal(null);

// Make sure the staging table is cleaned up
const stagingRes3 = await supertest(app).get('/v1/staging');

// There should be no staging records left
expect(stagingRes3.body.length).to.equal(0);
});

it('creates a new unit end-to-end', async () => {
Expand Down Expand Up @@ -264,42 +257,30 @@ describe('Create Unit Integration', () => {
// The node simulator runs on an async process, we are importing
// the WAIT_TIME constant from the simulator, padding it and waiting for the
// appropriate amount of time for the simulator to finish its operations
await new Promise((resolve, reject) => {
setTimeout(async () => {
try {
// Make sure the staging table is cleaned up
const stagingRes3 = await supertest(app).get('/v1/staging');

// There should be no staging records left
expect(stagingRes3.body.length).to.equal(0);

const warehouseRes = await supertest(app)
.get(`/v1/units`)
.query({ warehouseUnitId });

const newRecord = warehouseRes.body;

expect(newRecord.warehouseUnitId).to.equal(warehouseUnitId);
expect(newRecord.orgUid).to.equal(orgUid);
expect(newRecord.unitOwnerOrgUid).to.equal(orgUid);
expect(newRecord.serialNumberBlock).to.equal(
payload.serialNumberBlock,
);
expect(newRecord.countryJurisdictionOfOwner).to.equal(
payload.countryJurisdictionOfOwner,
);
expect(newRecord.inCountryJurisdictionOfOwner).to.equal(
payload.inCountryJurisdictionOfOwner,
);
expect(newRecord.tokenIssuanceHash).to.equal(
payload.tokenIssuanceHash,
);

resolve();
} catch (err) {
reject(err);
}
}, WAIT_TIME * 2);
});
clock.tick(WAIT_TIME * 2);

// Make sure the staging table is cleaned up
const stagingRes3 = await supertest(app).get('/v1/staging');

// There should be no staging records left
expect(stagingRes3.body.length).to.equal(0);

const warehouseRes = await supertest(app)
.get(`/v1/units`)
.query({ warehouseUnitId });

const newRecord = warehouseRes.body;

expect(newRecord.warehouseUnitId).to.equal(warehouseUnitId);
expect(newRecord.orgUid).to.equal(orgUid);
expect(newRecord.unitOwnerOrgUid).to.equal(orgUid);
expect(newRecord.serialNumberBlock).to.equal(payload.serialNumberBlock);
expect(newRecord.countryJuridictionOfOwner).to.equal(
payload.countryJuridictionOfOwner,
);
expect(newRecord.inCountryJuridictionOfOwner).to.equal(
payload.inCountryJuridictionOfOwner,
);
expect(newRecord.tokenIssuanceHash).to.equal(payload.tokenIssuanceHash);
});
});

0 comments on commit 32fabfd

Please sign in to comment.