Skip to content

Commit

Permalink
feat: use fake timers in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
frantzarty committed Jan 13, 2022
1 parent ea69454 commit 8bfbc22
Show file tree
Hide file tree
Showing 4 changed files with 235 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/fullnode/simulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,26 @@ export const updateUnitRecord = async (
encodedRecord,
stagingRecordId,
) => {
await deleteUnitRecord(uuid);
await createUnitRecord(uuid, encodedRecord);
setTimeout(async () => {
let record = JSON.parse(atob(encodedRecord));
await Unit.create({
...record,
});

if (stagingRecordId) {
await Staging.destroy({
await Unit.destroy({
where: {
id: stagingRecordId,
warehouseUnitId: uuid,
},
});
}

if (stagingRecordId) {
await Staging.destroy({
where: {
id: stagingRecordId,
},
});
}
}, WAIT_TIME);
};

export const createUnitRecord = (uuid, encodedRecord, stagingRecordId) => {
Expand Down
2 changes: 2 additions & 0 deletions src/models/units/units.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class Unit extends Model {
UnitMirror.create(values, options);
}

console.log(values);

const createResult = await super.create(values, options);
const { orgUid } = createResult;

Expand Down
10 changes: 10 additions & 0 deletions tests/integration/unit.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import chai from 'chai';
import supertest from 'supertest';
import sinon from 'sinon';
import app from '../../src/server';
import _ from 'lodash';

Expand All @@ -8,10 +9,19 @@ 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
207 changes: 207 additions & 0 deletions tests/integration/unit.spec.js.rej
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
diff a/tests/integration/unit.spec.js b/tests/integration/unit.spec.js (rejected hunks)
@@ -123,79 +133,64 @@ 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.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);
-
- 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;
+ console.log(newRecord1, splitRecord1.warehouseUnitId);
+
+ 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 () => {
@@ -264,42 +259,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.countryJuridictionOfOwner).to.equal(
- payload.countryJuridictionOfOwner,
- );
- expect(newRecord.inCountryJuridictionOfOwner).to.equal(
- payload.inCountryJuridictionOfOwner,
- );
- 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 8bfbc22

Please sign in to comment.