|
938 | 938 | context "period extension for 3-month lookback (18 month UI window + 3 month buffer)" do |
939 | 939 | let(:facility) { create(:facility, facility_group: facility_group_1) } |
940 | 940 |
|
941 | | - let(:end_period) { Period.month("February 2026") } |
| 941 | + let(:end_period) { Period.current } |
942 | 942 | let(:start_period) { end_period.advance(months: -(Reports::MAX_MONTHS_OF_DATA - 1)) } |
943 | 943 | let(:original_range) { (start_period..end_period) } |
944 | 944 |
|
945 | | - let(:repo) { Reports::Repository.new(facility.region, periods: original_range) } |
| 945 | + let(:single_period) { end_period } |
946 | 946 |
|
947 | 947 | before do |
948 | 948 | allow(Reports::PatientState).to receive(:get_refresh_months).and_return( |
|
951 | 951 | end_period.to_date |
952 | 952 | ) |
953 | 953 | ) |
| 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 |
954 | 982 | end |
955 | 983 |
|
956 | 984 | it "keeps original UI period window as 18 months" do |
| 985 | + repo = Reports::Repository.new(facility.region, periods: original_range) |
| 986 | + |
957 | 987 | 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) |
959 | 989 | end |
960 | 990 |
|
961 | 991 | [:adjusted_diabetes_patients_with_ltfu, :adjusted_patients_with_ltfu].each do |method| |
962 | 992 | it "does not expose buffer months for #{method}" do |
963 | | - refresh_views |
| 993 | + repo = Reports::Repository.new(facility.region, periods: original_range) |
964 | 994 | result = repo.send(method)[facility.region.slug] |
965 | 995 |
|
966 | 996 | expect(result.keys.min).to eq(start_period) |
|
972 | 1002 | end |
973 | 1003 |
|
974 | 1004 | 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) |
979 | 1006 |
|
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 |
985 | 1007 | result = repo.adjusted_diabetes_patients_with_ltfu[facility.region.slug] |
986 | 1008 | expect(result[start_period]).to eq(3) |
987 | 1009 | end |
988 | 1010 |
|
989 | 1011 | 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 | | - |
1005 | 1012 | repo = Reports::Repository.new(facility.region, periods: single_period) |
1006 | 1013 |
|
1007 | | - refresh_views |
1008 | 1014 | result = repo.adjusted_diabetes_patients_with_ltfu[facility.region.slug] |
| 1015 | + |
1009 | 1016 | expect(repo.periods).to eq(single_period..single_period) |
1010 | 1017 | expect(result.keys).to eq([single_period]) |
1011 | | - expect(result[single_period]).to eq(2) |
| 1018 | + expect(result[single_period]).to eq(3) |
1012 | 1019 | end |
1013 | 1020 |
|
1014 | 1021 | 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) |
1023 | 1023 |
|
1024 | | - refresh_views |
1025 | 1024 | result = repo.adjusted_patients_with_ltfu[facility.region.slug] |
1026 | 1025 | expect(result[start_period]).to eq(3) |
1027 | 1026 | end |
1028 | 1027 |
|
1029 | 1028 | 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 | | - |
1052 | 1029 | repo = Reports::Repository.new(facility.region, periods: single_period) |
1053 | 1030 |
|
1054 | | - refresh_views |
1055 | 1031 | result = repo.adjusted_patients_with_ltfu[facility.region.slug] |
| 1032 | + |
1056 | 1033 | expect(repo.periods).to eq(single_period..single_period) |
1057 | 1034 | expect(result.keys).to eq([single_period]) |
1058 | | - expect(result[single_period]).to eq(2) |
| 1035 | + expect(result[single_period]).to eq(3) |
1059 | 1036 | end |
1060 | 1037 | end |
1061 | 1038 | end |
0 commit comments