Skip to content

Commit

Permalink
Merge pull request #797 from CMSgov/QPPA-8974
Browse files Browse the repository at this point in the history
QPPA-8974: Update Measures Unit Test Coverage
  • Loading branch information
ckawell-sb authored May 9, 2024
2 parents b50e584 + c0e3006 commit 5535944
Show file tree
Hide file tree
Showing 129 changed files with 2,376 additions and 11,642 deletions.
57 changes: 57 additions & 0 deletions scripts/measures/2022/ingest-strata.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

import appRoot from 'app-root-path';
import { parse } from 'csv-parse/sync';
import fs from 'fs';
import _ from 'lodash';
import path from 'path';

import * as Util from './update-measures-util';
import { ingestStrata } from './ingest-strata';
import { DataValidationError } from '../../errors';

const performanceYear = 2022
const testDataPath = `test/measures/${performanceYear}`;
const measuresPath = `${testDataPath}/test-measures.json`;

const measuresJson = JSON.parse(
fs.readFileSync(path.join(appRoot + "", measuresPath), "utf8")
);

describe('ingest-strata', () => {
it('successfully ingests strata changes', () => {
const strataPath = `${testDataPath}/quality-strata.csv`;
const measuresResultPath = `${testDataPath}/test-measures-result.json`;
const measuresResultJson = JSON.parse(
fs.readFileSync(path.join(appRoot + "", measuresResultPath), "utf8")
);
jest.spyOn(JSON, 'parse').mockReturnValueOnce(measuresJson);
const writeSpy = jest.spyOn(Util, 'writeToFile');
writeSpy.mockImplementation();

ingestStrata(performanceYear, strataPath);

expect(writeSpy).toBeCalledWith(measuresResultJson, `measures/${performanceYear}/measures-data.json`);
});

it('fails to ingest strata if the measureId is blank in the csv.', () => {
const strataPath = `${testDataPath}/blank-measureid-quality-strata.csv`;
jest.spyOn(JSON, 'parse').mockReturnValueOnce(measuresJson);
const writeSpy = jest.spyOn(Util, 'writeToFile');
writeSpy.mockImplementation();

expect(() => {
ingestStrata(performanceYear, strataPath);
}).toThrowError(new DataValidationError(strataPath, 'MeasureId is required.'));
});

it('fails to ingest strata if the name or description is blank in the csv.', () => {
const strataPath = `${testDataPath}/bad-quality-strata.csv`;
jest.spyOn(JSON, 'parse').mockReturnValueOnce(measuresJson);
const writeSpy = jest.spyOn(Util, 'writeToFile');
writeSpy.mockImplementation();

expect(() => {
ingestStrata(performanceYear, strataPath);
}).toThrowError(new DataValidationError(strataPath, 'Name and description are required.'));
});
});
55 changes: 29 additions & 26 deletions scripts/measures/2022/ingest-strata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,36 @@ import _ from 'lodash';
import path from 'path';

import { DataValidationError } from '../../errors';
import { writeToFile } from './update-measures-util';
import { csvStratum, jsonStratum } from '../lib/measures.types';

const performanceYear = process.argv[2];
const strataPath = process.argv[3];
export function ingestStrata(performanceYear: number, strataPath: string) {
const measuresPath = `measures/${performanceYear}/measures-data.json`;

const measuresPath = `measures/${performanceYear}/measures-data.json`;

const measuresJson = JSON.parse(
fs.readFileSync(path.join(appRoot + "", measuresPath), "utf8")
);
const strata = parse(
fs.readFileSync(path.join(appRoot + "", strataPath), "utf8"),
{ columns: true, skip_empty_lines: true, bom: true }
);
const measuresJson = JSON.parse(
fs.readFileSync(path.join(appRoot + "", measuresPath), "utf8")
);
const strata: csvStratum[] = parse(
fs.readFileSync(path.join(appRoot + "", strataPath), "utf8"), {
columns: true,
relax_column_count: true,
bom: true,
skip_records_with_empty_values: true,
});

export function ingestStrata() {
const uniqueMeasureIds = [
...new Set(strata.map((stratum) => stratum.measureId)),
...new Set(strata.map((stratum) => {
if (!stratum.measureId) {
throw new DataValidationError(
strataPath,
"MeasureId is required."
);
}
return stratum.measureId;
})),
];
for (let i = 0; i < uniqueMeasureIds.length; i++) {
const currentStrata = measuresJson.find(
const currentStrata: jsonStratum[] = measuresJson.find(
(measure: any) => measure.measureId === uniqueMeasureIds[i]
).strata;

Expand All @@ -44,7 +54,7 @@ export function ingestStrata() {
);
}
return {
...currentStrata.find((currentStratum: any) => currentStratum.name === stratum.stratumName),
...currentStrata.find((currentStratum) => currentStratum.name === stratum.stratumName),
name: stratum.stratumName,
description: stratum.description,
};
Expand All @@ -56,14 +66,7 @@ export function ingestStrata() {
writeToFile(measuresJson, measuresPath);
}

function writeToFile(file: any, filePath: string) {
fs.writeFile(
path.join(appRoot + "", filePath),
JSON.stringify(file, null, 2),
function writeJSON(err) {
if (err) return console.log(err);
}
);
}

ingestStrata();
/* c8 ignore next */
if (process.argv[2] && process.argv[2] !== '--coverage')
/* c8 ignore next */
ingestStrata(parseInt(process.argv[2]), process.argv[3]);
57 changes: 57 additions & 0 deletions scripts/measures/2023/ingest-strata.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

import appRoot from 'app-root-path';
import { parse } from 'csv-parse/sync';
import fs from 'fs';
import _ from 'lodash';
import path from 'path';

import * as Lib from '../lib/measures-lib';
import { ingestStrata } from './ingest-strata';
import { DataValidationError } from '../../errors';

const performanceYear = 2024
const testDataPath = `test/measures/${performanceYear}`;
const measuresPath = `${testDataPath}/test-measures.json`;

const measuresJson = JSON.parse(
fs.readFileSync(path.join(appRoot + "", measuresPath), "utf8")
);

describe('ingest-strata', () => {
it('successfully ingests strata changes', () => {
const strataPath = `${testDataPath}/test-strata.csv`;
const measuresResultPath = `${testDataPath}/test-measures-result.json`;
const measuresResultJson = JSON.parse(
fs.readFileSync(path.join(appRoot + "", measuresResultPath), "utf8")
);
jest.spyOn(JSON, 'parse').mockReturnValueOnce(measuresJson);
const writeSpy = jest.spyOn(Lib, 'writeToFile');
writeSpy.mockImplementation();

ingestStrata(performanceYear, strataPath);

expect(writeSpy).toBeCalledWith(measuresResultJson, `measures/${performanceYear}/measures-data.json`);
});

it('fails to ingest strata if the measureId is blank in the csv.', () => {
const strataPath = `${testDataPath}/blank-measureid-test-strata.csv`;
jest.spyOn(JSON, 'parse').mockReturnValueOnce(measuresJson);
const writeSpy = jest.spyOn(Lib, 'writeToFile');
writeSpy.mockImplementation();

expect(() => {
ingestStrata(performanceYear, strataPath);
}).toThrowError(new DataValidationError(strataPath, 'MeasureId is required.'));
});

it('fails to ingest strata if the name or description is blank in the csv.', () => {
const strataPath = `${testDataPath}/bad-test-strata.csv`;
jest.spyOn(JSON, 'parse').mockReturnValueOnce(measuresJson);
const writeSpy = jest.spyOn(Lib, 'writeToFile');
writeSpy.mockImplementation();

expect(() => {
ingestStrata(performanceYear, strataPath);
}).toThrowError(new DataValidationError(strataPath, 'Name and description are required.'));
});
});
57 changes: 30 additions & 27 deletions scripts/measures/2023/ingest-strata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,36 @@ import _ from 'lodash';
import path from 'path';

import { DataValidationError } from '../../errors';
import { writeToFile } from '../lib/measures-lib';
import { csvStratum, jsonStratum } from '../lib/measures.types';

const performanceYear = process.argv[2];
const strataPath = process.argv[3];
export function ingestStrata(performanceYear: number, strataPath: string) {
const measuresPath = `measures/${performanceYear}/measures-data.json`;

const measuresPath = `measures/${performanceYear}/measures-data.json`;

const measuresJson = JSON.parse(
fs.readFileSync(path.join(appRoot + "", measuresPath), "utf8")
);
const strata = parse(
fs.readFileSync(path.join(appRoot + "", strataPath), "utf8"),
{ columns: true, skip_empty_lines: true, bom: true }
);
const measuresJson = JSON.parse(
fs.readFileSync(path.join(appRoot + "", measuresPath), "utf8")
);
const strata: csvStratum[] = parse(
fs.readFileSync(path.join(appRoot + "", strataPath), "utf8"), {
columns: true,
relax_column_count: true,
bom: true,
skip_records_with_empty_values: true,
});

export function ingestStrata() {
const uniqueMeasureIds = [
...new Set(strata.map((stratum) => stratum.measureId)),
...new Set(strata.map((stratum) => {
if (!stratum.measureId) {
throw new DataValidationError(
strataPath,
"MeasureId is required."
);
}
return stratum.measureId;
})),
];
for (let i = 0; i < uniqueMeasureIds.length; i++) {
const currentStrata = measuresJson.find(
const currentStrata: jsonStratum[] = measuresJson.find(
(measure: any) => measure.measureId === uniqueMeasureIds[i]
).strata;

Expand All @@ -43,10 +53,10 @@ export function ingestStrata() {
"Name and description are required."
);
}

//remove 'Rate x: ' from the start of the stratum descriptions, if present.
stratum.description = stratum.description.replace(/^[Rr]ate \d+: /, "");

return {
...currentStrata.find((currentStratum: any) => currentStratum.name === stratum.stratumName),
name: stratum.stratumName,
Expand All @@ -60,14 +70,7 @@ export function ingestStrata() {
writeToFile(measuresJson, measuresPath);
}

function writeToFile(file: any, filePath: string) {
fs.writeFile(
path.join(appRoot + "", filePath),
JSON.stringify(file, null, 2),
function writeJSON(err) {
if (err) return console.log(err);
}
);
}

ingestStrata();
/* c8 ignore next */
if (process.argv[2] && process.argv[2] !== '--coverage')
/* c8 ignore next */
ingestStrata(parseInt(process.argv[2]), process.argv[3]);
57 changes: 57 additions & 0 deletions scripts/measures/2024/ingest-strata.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

import appRoot from 'app-root-path';
import { parse } from 'csv-parse/sync';
import fs from 'fs';
import _ from 'lodash';
import path from 'path';

import * as Lib from '../lib/measures-lib';
import { ingestStrata } from './ingest-strata';
import { DataValidationError } from '../../errors';

const performanceYear = 2023
const testDataPath = `test/measures/${performanceYear}`;
const measuresPath = `${testDataPath}/test-measures.json`;

const measuresJson = JSON.parse(
fs.readFileSync(path.join(appRoot + "", measuresPath), "utf8")
);

describe('ingest-strata', () => {
it('successfully ingests strata changes', () => {
const strataPath = `${testDataPath}/test-strata.csv`;
const measuresResultPath = `${testDataPath}/test-measures-result.json`;
const measuresResultJson = JSON.parse(
fs.readFileSync(path.join(appRoot + "", measuresResultPath), "utf8")
);
jest.spyOn(JSON, 'parse').mockReturnValueOnce(measuresJson);
const writeSpy = jest.spyOn(Lib, 'writeToFile');
writeSpy.mockImplementation();

ingestStrata(performanceYear, strataPath);

expect(writeSpy).toBeCalledWith(measuresResultJson, `measures/${performanceYear}/measures-data.json`);
});

it('fails to ingest strata if the measureId is blank in the csv.', () => {
const strataPath = `${testDataPath}/blank-measureid-test-strata.csv`;
jest.spyOn(JSON, 'parse').mockReturnValueOnce(measuresJson);
const writeSpy = jest.spyOn(Lib, 'writeToFile');
writeSpy.mockImplementation();

expect(() => {
ingestStrata(performanceYear, strataPath);
}).toThrowError(new DataValidationError(strataPath, 'MeasureId is required.'));
});

it('fails to ingest strata if the name or description is blank in the csv.', () => {
const strataPath = `${testDataPath}/bad-test-strata.csv`;
jest.spyOn(JSON, 'parse').mockReturnValueOnce(measuresJson);
const writeSpy = jest.spyOn(Lib, 'writeToFile');
writeSpy.mockImplementation();

expect(() => {
ingestStrata(performanceYear, strataPath);
}).toThrowError(new DataValidationError(strataPath, 'Name and description are required.'));
});
});
Loading

0 comments on commit 5535944

Please sign in to comment.