Skip to content

Commit

Permalink
#102: add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mohit-s96 committed Sep 26, 2024
1 parent 143d723 commit bc79461
Show file tree
Hide file tree
Showing 3 changed files with 760 additions and 2 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require('dotenv').config();

const { schema, combineErrors, generateJsonSchema, validate, VALIDATION_ERROR_MESSAGES } = require('./lib/schema');
const { schema, combineErrors, generateJsonSchema, validate, generateReports, VALIDATION_ERROR_MESSAGES } = require('./lib/schema');
const { restore } = require('./lib/restore');
const { runDDTests, DEFAULT_LIMIT } = require('./lib/certification/data-dictionary');
const { runUpiTests, parseUpi } = require('./lib/certification/upi');
Expand Down Expand Up @@ -186,5 +186,6 @@ module.exports = {
convertMetadata,
combineErrors,
generateJsonSchema,
validate
validate,
generateReports
};
141 changes: 141 additions & 0 deletions test/create-reports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
const { generateReports } = require('..');
const assert = require('assert');
const {
expectDAReportSimple,
expectDDReportSimple,
expectDAReportMulti,
expectedMultiResourceDAReport,
expectedMultiResourceDDReport
} = require('./schema/report-samples');
const { createReplicationStateServiceInstance } = require('../common');

const resetReplicationStateForTests = () => {
const replicationInstance = createReplicationStateServiceInstance();
[
replicationInstance.getMetadataMap(),
replicationInstance.getRecordCountHashMap(),
replicationInstance.getResourceAvailabilityMap(),
replicationInstance.getResponses(),
replicationInstance.getTopLevelResourceCounts()
].forEach(obj => {
if (Array.isArray(obj)) {
obj.length = 0;
} else {
Object.keys(obj).forEach(k => {
if (obj.hasOwnProperty(k)) {
delete obj[k];
}
});
}
});
};

describe('Schema validation tests', async () => {
beforeEach(resetReplicationStateForTests);

it('Should generate valid reports for a simple payload', async () => {
const payload = {
'@reso.context': 'urn:reso:metadata:1.7:resource:property',
Country: 'CA',
StateOrProvince: 'ON',
City: 'SampleCityEnumValue',
PostalCode: 'K2G 1Y9',
StreetName: 'Starwood Rd',
StreetNumber: '39',
Foo: 2,
OriginatingSystem: null
};
const { daReport, ddReport } = await generateReports([payload]);
delete daReport.generatedOn;
delete ddReport.generatedOn;
assert.deepEqual(daReport, expectDAReportSimple);
assert.deepEqual(ddReport, expectDDReportSimple);
});

it('Should generate valid reports for multiple payloads', async () => {
const payload1 = {
'@reso.context': 'urn:reso:metadata:1.7:resource:property',
Country: 'CA',
StateOrProvince: 'ON',
City: 'SampleCityEnumValue',
PostalCode: 'K2G 1Y9',
StreetName: 'Starwood Rd',
StreetNumber: '39',
Foo: 2,
OriginatingSystem: null
};
const payload2 = {
'@reso.context': 'urn:reso:metadata:1.7:resource:property',
Country: 'CA',
StateOrProvince: 'ON',
City: 'SampleCityEnumValue',
PostalCode: 'K2G 1Y9',
StreetName: 'Starwood Rd',
StreetNumber: '38',
Foo: 2,
OriginatingSystem: null
};
const { daReport, ddReport } = await generateReports([payload1, payload2]);
delete daReport.generatedOn;
delete ddReport.generatedOn;
assert.deepEqual(daReport, expectDAReportMulti);
assert.deepEqual(ddReport, expectDDReportSimple);
});

it('Should generate valid reports 2 payloads with different resources', async () => {
const propertyPayload = {
'@reso.context': 'urn:reso:metadata:1.7:resource:property',
Country: 'CA',
StateOrProvince: 'ON'
};
const lookupPayload = {
'@reso.context': 'urn:reso:metadata:1.7:resource:lookup',
LookupName: 'Cooling',
LookupValue: 'Central Air'
};
const { daReport, ddReport } = await generateReports([propertyPayload, lookupPayload]);
delete daReport.generatedOn;
delete ddReport.generatedOn;
assert.deepEqual(daReport, expectedMultiResourceDAReport);
assert.deepEqual(ddReport, expectedMultiResourceDDReport);
});

it('Should correctly infer string data type and max length', async () => {
const payload = {
'@reso.context': 'urn:reso:metadata:1.7:resource:property',
value: [
{
Country: 'CA',
LocalStringValue: 'Foo'
},
{
Country: 'US',
LocalStringValue: 'Fooo'
}
]
};
const { ddReport } = await generateReports([payload]);
const field = ddReport.fields.find(f => f.fieldName === 'LocalStringValue');
assert.equal(field.type, 'Edm.String');
assert.equal(field.maxLength, 4);
});

it('Should correctly infer int data type', async () => {
const payload = {
'@reso.context': 'urn:reso:metadata:1.7:resource:property',
value: [
{
Country: 'CA',
LocalIntValue: 10
},
{
Country: 'US',
LocalIntValue: 68000
}
]
};
const { ddReport } = await generateReports([payload]);
const field = ddReport.fields.find(f => f.fieldName === 'LocalIntValue');
assert.equal(field.type, 'Edm.Int32');
});
});
Loading

0 comments on commit bc79461

Please sign in to comment.