Skip to content

Commit e900583

Browse files
Fix 3-month lookback period key mismatch in reports (#5781)
Story card: [SIMPLEBACK-87](https://rtsl.atlassian.net/browse/SIMPLEBACK-87) ### Because Reporting logic uses a 3-month internal lookback buffer to calculate correct patient denominators while displaying only an 18-month UI window. ### This address Ensures the first visible month in reports correctly includes patients registered 3 months earlier, preventing incorrect denominator counts. ### Test instructions Run repository specs and verify the 3-month lookback reports return the correct denominator values.
1 parent 042f49a commit e900583

1 file changed

Lines changed: 40 additions & 63 deletions

File tree

spec/models/reports/repository_spec.rb

Lines changed: 40 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -938,11 +938,11 @@
938938
context "period extension for 3-month lookback (18 month UI window + 3 month buffer)" do
939939
let(:facility) { create(:facility, facility_group: facility_group_1) }
940940

941-
let(:end_period) { Period.month("February 2026") }
941+
let(:end_period) { Period.current }
942942
let(:start_period) { end_period.advance(months: -(Reports::MAX_MONTHS_OF_DATA - 1)) }
943943
let(:original_range) { (start_period..end_period) }
944944

945-
let(:repo) { Reports::Repository.new(facility.region, periods: original_range) }
945+
let(:single_period) { end_period }
946946

947947
before do
948948
allow(Reports::PatientState).to receive(:get_refresh_months).and_return(
@@ -951,16 +951,46 @@
951951
end_period.to_date
952952
)
953953
)
954+
955+
diabetes_patients = create_list(:patient, 3, :diabetes,
956+
recorded_at: start_period.advance(months: -Reports::REGISTRATION_BUFFER_IN_MONTHS).to_date,
957+
assigned_facility: facility,
958+
registration_user: user)
959+
960+
diabetes_patients.each do |p|
961+
create(:blood_sugar, :with_encounter,
962+
patient: p,
963+
facility: facility,
964+
recorded_at: start_period.to_date,
965+
user: user)
966+
end
967+
968+
hypertension_patients = create_list(:patient, 3, :hypertension,
969+
recorded_at: start_period.advance(months: -Reports::REGISTRATION_BUFFER_IN_MONTHS).to_date,
970+
assigned_facility: facility,
971+
registration_user: user)
972+
973+
hypertension_patients.each do |p|
974+
create(:bp_with_encounter,
975+
patient: p,
976+
facility: facility,
977+
recorded_at: start_period.to_date,
978+
user: user)
979+
end
980+
981+
refresh_views
954982
end
955983

956984
it "keeps original UI period window as 18 months" do
985+
repo = Reports::Repository.new(facility.region, periods: original_range)
986+
957987
expect(repo.periods).to eq(original_range)
958-
expect(repo.periods.count).to eq(18)
988+
expect(repo.periods.count).to eq(Reports::MAX_MONTHS_OF_DATA)
959989
end
960990

961991
[:adjusted_diabetes_patients_with_ltfu, :adjusted_patients_with_ltfu].each do |method|
962992
it "does not expose buffer months for #{method}" do
963-
refresh_views
993+
repo = Reports::Repository.new(facility.region, periods: original_range)
964994
result = repo.send(method)[facility.region.slug]
965995

966996
expect(result.keys.min).to eq(start_period)
@@ -972,90 +1002,37 @@
9721002
end
9731003

9741004
it "ensures first visible month has access to 3-month back denominator data" do
975-
patients = create_list(:patient, 3, :diabetes,
976-
recorded_at: start_period.advance(months: -3).to_date,
977-
assigned_facility: facility,
978-
registration_user: user)
1005+
repo = Reports::Repository.new(facility.region, periods: original_range)
9791006

980-
patients.each do |patient|
981-
create(:blood_sugar, :with_encounter, patient: patient, facility: facility, recorded_at: start_period.to_date, user: user)
982-
end
983-
984-
refresh_views
9851007
result = repo.adjusted_diabetes_patients_with_ltfu[facility.region.slug]
9861008
expect(result[start_period]).to eq(3)
9871009
end
9881010

9891011
it "works correctly for single period input with 3-month lookback data" do
990-
single_period = Period.month("February 2026")
991-
992-
patients = create_list(:patient, 2, :diabetes,
993-
recorded_at: single_period.advance(months: -3).to_date,
994-
assigned_facility: facility,
995-
registration_user: user)
996-
997-
patients.each do |patient|
998-
create(:blood_sugar, :with_encounter,
999-
patient: patient,
1000-
facility: facility,
1001-
recorded_at: single_period.to_date,
1002-
user: user)
1003-
end
1004-
10051012
repo = Reports::Repository.new(facility.region, periods: single_period)
10061013

1007-
refresh_views
10081014
result = repo.adjusted_diabetes_patients_with_ltfu[facility.region.slug]
1015+
10091016
expect(repo.periods).to eq(single_period..single_period)
10101017
expect(result.keys).to eq([single_period])
1011-
expect(result[single_period]).to eq(2)
1018+
expect(result[single_period]).to eq(3)
10121019
end
10131020

10141021
it "ensures first visible month has access to 3-month back denominator data for hypertension" do
1015-
patients = create_list(:patient, 3, :hypertension,
1016-
recorded_at: start_period.advance(months: -3).to_date,
1017-
assigned_facility: facility,
1018-
registration_user: user)
1019-
1020-
patients.each do |patient|
1021-
create(:bp_with_encounter, patient: patient, facility: facility, recorded_at: start_period.to_date, user: user)
1022-
end
1022+
repo = Reports::Repository.new(facility.region, periods: original_range)
10231023

1024-
refresh_views
10251024
result = repo.adjusted_patients_with_ltfu[facility.region.slug]
10261025
expect(result[start_period]).to eq(3)
10271026
end
10281027

10291028
it "works correctly for single period input with 3-month lookback data for hypertension" do
1030-
single_period = Period.month("February 2026")
1031-
1032-
allow(Reports::PatientState).to receive(:get_refresh_months).and_return(
1033-
ReportingHelpers.get_refresh_months_between_dates(
1034-
single_period.advance(months: -Reports::REGISTRATION_BUFFER_IN_MONTHS).to_date,
1035-
single_period.to_date
1036-
)
1037-
)
1038-
1039-
patients = create_list(:patient, 2, :hypertension,
1040-
recorded_at: single_period.advance(months: -3).to_date,
1041-
assigned_facility: facility,
1042-
registration_user: user)
1043-
1044-
patients.each do |patient|
1045-
create(:bp_with_encounter,
1046-
patient: patient,
1047-
facility: facility,
1048-
recorded_at: single_period.to_date,
1049-
user: user)
1050-
end
1051-
10521029
repo = Reports::Repository.new(facility.region, periods: single_period)
10531030

1054-
refresh_views
10551031
result = repo.adjusted_patients_with_ltfu[facility.region.slug]
1032+
10561033
expect(repo.periods).to eq(single_period..single_period)
10571034
expect(result.keys).to eq([single_period])
1058-
expect(result[single_period]).to eq(2)
1035+
expect(result[single_period]).to eq(3)
10591036
end
10601037
end
10611038
end

0 commit comments

Comments
 (0)