KPIs page drawing CaCu's measures from the WebApi's FHIR MeasureReport bundles
This commit is contained in:
@@ -0,0 +1,145 @@
|
||||
// What this covers: reading the WebApi's FHIR `MeasureReport` bundles (`/<domain>/kpis/<id>`) into a series, drawing
|
||||
// it here, the service that asks for it and the component that shows it.
|
||||
// Fixtures: `cacu_ind1_measure_report_bundle.json` (IND1, 2025-S1 and 2025-S2) and
|
||||
// `cacu_eu13_measure_report_bundle.json` (EU13, 2025-S1, with the `grupo_edad` stratifier) and
|
||||
// `cacu_eu7_measure_report_bundle.json` (EU7, a composition, 2025-S1 and 2025-S2): written by the WebApi's
|
||||
// route tests over the synthetic fixture database, counts only.
|
||||
import { Component, input } from '@angular/core';
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { provideHttpClient } from '@angular/common/http';
|
||||
import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing';
|
||||
import { firstValueFrom } from 'rxjs';
|
||||
import { PlotlyChart } from '../../shared/plotly/plotly-chart';
|
||||
import ind1 from './fixtures/cacu_ind1_measure_report_bundle.json';
|
||||
import eu13 from './fixtures/cacu_eu13_measure_report_bundle.json';
|
||||
import eu7 from './fixtures/cacu_eu7_measure_report_bundle.json';
|
||||
import { IndicadorMeasureReport } from './indicador-measure-report';
|
||||
import { compositionFigure, compositionFrom, InvalidMeasureReport, MeasureReportBundle, periodLabel, proportionFigure, seriesFrom } from './measure-report';
|
||||
import { KpisPage } from './kpis-page';
|
||||
import { MeasureReportService } from './measure-report.service';
|
||||
|
||||
@Component({ selector: 'app-plotly-chart', template: '' })
|
||||
class ChartStub { figure = input<unknown>(); label = input<string>(); }
|
||||
|
||||
const IND1 = ind1 as unknown as MeasureReportBundle;
|
||||
const EU13 = eu13 as unknown as MeasureReportBundle;
|
||||
const EU7 = eu7 as unknown as MeasureReportBundle;
|
||||
|
||||
describe('measure reports', () => {
|
||||
it('names a period from its FHIR dates', () => {
|
||||
// Pins down: semesters and months read as the WebApp names them. Fails if FHIR's inclusive end is misread.
|
||||
expect(periodLabel({ start: '2025-01-01', end: '2025-06-30' })).toBe('2025-S1');
|
||||
expect(periodLabel({ start: '2025-07-01', end: '2025-12-31' })).toBe('2025-S2');
|
||||
expect(periodLabel({ start: '2025-03-01', end: '2025-03-31' })).toBe('2025-03');
|
||||
expect(periodLabel({ start: '2025-01-01', end: '2025-03-31' })).toBe('2025-01-01 a 2025-03-31');
|
||||
});
|
||||
|
||||
it('reads one point per period from a bundle', () => {
|
||||
// Pins down: the counts come from the populations, the order from the bundle. Fails if the reading of FHIR's
|
||||
// population codes or of `improvementNotation` changes.
|
||||
const series = seriesFrom(IND1);
|
||||
expect(series.measure).toBe('cacu/IND1|0.3.2');
|
||||
expect(series.lowerIsBetter).toBe(true);
|
||||
expect(series.total).toEqual([
|
||||
{ period: '2025-S1', numerator: 1, denominator: 4, value: 0.25 },
|
||||
{ period: '2025-S2', numerator: 1, denominator: 4, value: 0.25 },
|
||||
]);
|
||||
});
|
||||
|
||||
it('reads the strata of a stratifier', () => {
|
||||
// Pins down: each stratum's value is a series of its own. Fails if `stratifier` stops being read.
|
||||
expect(seriesFrom(EU13).strata).toEqual({
|
||||
grupo_edad: { '45-54': [{ period: '2025-S1', numerator: 0, denominator: 4, value: 0 }] },
|
||||
});
|
||||
});
|
||||
|
||||
it('rejects what is not a bundle of reports of one measure', () => {
|
||||
// Pins down: a malformed answer fails loudly instead of drawing zeros. Fails if validation is dropped.
|
||||
expect(() => seriesFrom({ resourceType: 'Bundle', entry: [] } as MeasureReportBundle)).toThrow(InvalidMeasureReport);
|
||||
const mixed = { ...IND1, entry: [IND1.entry[0], { resource: { ...IND1.entry[1].resource, measure: 'otro' } }] };
|
||||
expect(() => seriesFrom(mixed)).toThrow(InvalidMeasureReport);
|
||||
});
|
||||
|
||||
it('draws the figure here: percentages per period, one line per stratum value', () => {
|
||||
// Pins down: the WebApp decides the figure from the data. Fails if values stop being percentages, or a
|
||||
// stratum stops giving one trace per value.
|
||||
const total = proportionFigure(seriesFrom(IND1), 'Pacientes con citología anómala');
|
||||
expect(total.data).toHaveLength(1);
|
||||
expect(total.data[0]['x']).toEqual(['2025-S1', '2025-S2']);
|
||||
expect(total.data[0]['y']).toEqual([25, 25]);
|
||||
const byAge = proportionFigure(seriesFrom(EU13), 'Tasa de detección', 'grupo_edad');
|
||||
expect(byAge.data.map(trace => trace['name'])).toEqual(['45-54']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('MeasureReportService and IndicadorMeasureReport', () => {
|
||||
beforeEach(() => TestBed.configureTestingModule({ providers: [provideHttpClient(), provideHttpClientTesting()] }));
|
||||
|
||||
it('asks the WebApi for the periods and cut-offs and reads the bundle', async () => {
|
||||
// Pins down the request: the route, `periodos` comma-separated and the cut-offs only when given. Fails if
|
||||
// the URL or a parameter's name drifts from the WebApi's.
|
||||
const answer = firstValueFrom(TestBed.inject(MeasureReportService)
|
||||
.series('cacu', 'IND1', ['2025-S1', '2025-S2'], { evento: '2025-12-31' }));
|
||||
const request = TestBed.inject(HttpTestingController).expectOne(r => r.url === '/cacu/kpis/IND1');
|
||||
expect(request.request.params.get('periodos')).toBe('2025-S1,2025-S2');
|
||||
expect(request.request.params.get('corte_evento')).toBe('2025-12-31');
|
||||
expect(request.request.params.has('corte_conocimiento')).toBe(false);
|
||||
request.flush(IND1);
|
||||
expect((await answer).total.map(p => p.value)).toEqual([0.25, 0.25]);
|
||||
});
|
||||
|
||||
it('shows the figure it drew from the answer', () => {
|
||||
// Pins down: the component hands the chart the figure built from the bundle. Fails if it stops drawing.
|
||||
TestBed.overrideComponent(IndicadorMeasureReport, { remove: { imports: [PlotlyChart] }, add: { imports: [ChartStub] } });
|
||||
const fixture = TestBed.createComponent(IndicadorMeasureReport);
|
||||
Object.assign(fixture.componentInstance, { dominio: 'cacu', indicador: 'IND1', titulo: 'IND1', periodos: ['2025-S1', '2025-S2'] });
|
||||
fixture.componentInstance.ngOnChanges();
|
||||
TestBed.inject(HttpTestingController).expectOne(r => r.url === '/cacu/kpis/IND1').flush(IND1);
|
||||
fixture.detectChanges();
|
||||
expect(fixture.componentInstance.status()).toBe('ready');
|
||||
expect(fixture.componentInstance.figure()?.data[0]['y']).toEqual([25, 25]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('compositions', () => {
|
||||
it('reads the categories of each period from the `resultado` stratifier', () => {
|
||||
// Pins down: a composition is a count per category and period, zero where a period lacks one. Fails if the
|
||||
// stratifier stops being read or a missing category stops being zero.
|
||||
const series = compositionFrom(EU7);
|
||||
expect(series.periods).toEqual(['2025-S1', '2025-S2']);
|
||||
expect(series.categories).toEqual({ LIEBG: [1, 0], NEGATIVO: [2, 1], SIN_DATO: [1, 2], LIEAG: [0, 1] });
|
||||
});
|
||||
|
||||
it('draws stacked bars, one segment per category', () => {
|
||||
// Pins down: the WebApp draws compositions from the same contract. Fails if a category stops being a trace.
|
||||
const figure = compositionFigure(compositionFrom(EU7), 'EU7');
|
||||
expect(figure.layout['barmode']).toBe('stack');
|
||||
expect(figure.data.map(trace => trace['name']).sort()).toEqual(['LIEAG', 'LIEBG', 'NEGATIVO', 'SIN_DATO']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('KpisPage', () => {
|
||||
beforeEach(() => TestBed.configureTestingModule({ providers: [provideHttpClient(), provideHttpClientTesting()] }));
|
||||
|
||||
it('draws every measure the WebApi lists, each from its reports', () => {
|
||||
// Pins down: the page needs nothing per measure: it lists `/cacu/kpis` and draws proportions and compositions
|
||||
// from the one contract. Fails if a scoring stops being drawn or the page starts naming measures.
|
||||
TestBed.overrideComponent(KpisPage, { remove: { imports: [PlotlyChart] }, add: { imports: [ChartStub] } });
|
||||
const fixture = TestBed.createComponent(KpisPage);
|
||||
fixture.componentInstance.ngOnInit();
|
||||
const http = TestBed.inject(HttpTestingController);
|
||||
http.expectOne('/cacu/kpis').flush([
|
||||
{ id: 'IND1', url: 'cacu/IND1', version: '0.3.2', scoring: 'proportion' },
|
||||
{ id: 'CACU_EU13', url: 'cacu/CACU_EU13', version: '0.3.2', scoring: 'proportion' },
|
||||
{ id: 'CACU_EU7', url: 'cacu/CACU_EU7', version: '0.3.3', scoring: 'composition' },
|
||||
]);
|
||||
http.expectOne(r => r.url === '/cacu/kpis/IND1').flush(IND1);
|
||||
http.expectOne(r => r.url === '/cacu/kpis/CACU_EU13').flush(EU13);
|
||||
http.expectOne(r => r.url === '/cacu/kpis/CACU_EU7').flush(EU7);
|
||||
fixture.detectChanges();
|
||||
expect(fixture.componentInstance.status()).toBe('ready');
|
||||
expect(fixture.componentInstance.figures().map(f => f.id)).toEqual(['IND1', 'CACU_EU13', 'CACU_EU7']);
|
||||
expect(fixture.componentInstance.figures()[2].figure.layout['barmode']).toBe('stack');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user