Skip to content

Commit

Permalink
Fix flaky test in overdue patient specs (#5383)
Browse files Browse the repository at this point in the history
**Story card:**
[sc-10942](https://app.shortcut.com/simpledotorg/story/10942/investigate-flaky-test-in-overdue-patients-in-region-summary-spec)

## Because

The test for `return the count of contactable patients returned to care
who were removed from overdue list` is flaky.
 
Current test arrangement:
- [One of the
patients](https://github.com/simpledotorg/simple-server/blob/0e548dcafba79c72357a5367b3182496725c4ff0/spec/models/reports/region_summary_spec.rb#L1469)
has 2 call results, one in the previous month and one in the current
month.
 - The patient returns to care on `the 2nd of the current month`.  
- The test will pass if the visit date of the patient fall within 15
days of the call .i.e (visit date - call date <=15).
 - First call was made on `1.month.ago`. 
- `1.month.ago` is relative to the current day and the date changes
daily.
 
For eg, assume today is 31-Jan-2024
 - date of first call: `1.month.ago` = 31-Dec-2023 (changes daily)
 - visit date: `one_month_ago + 2.days` = 2-Jan-2023 (fixed date)
- The test pass only when `(visit date - date of first call) <= 15`.
Since this difference (visit date - date of first call) changes daily,
the test turned to be flaky.

## This addresses

- Use a fixed date for `date of first call`
- date of first call: `one_month_ago + 20.days` = 20-Dec-2023 (A random
date which falls in the 15 day window)
- [`one_month_ago +
20.days`](https://github.com/simpledotorg/simple-server/blob/0e548dcafba79c72357a5367b3182496725c4ff0/spec/models/reports/region_summary_spec.rb#L734)
is always the 20th of the previous month.
  • Loading branch information
PriyangaPKini committed Feb 5, 2024
1 parent f4146ba commit 67ef945
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 5 additions & 1 deletion spec/factories/patients.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,14 @@
trait(:removed_from_overdue_list) do
transient do
user { create(:user, registration_facility: registration_facility) }
call_date { 1.month.ago }
end
recorded_at { 2.years.ago }
with_overdue_appointments
call_results { build_list(:call_result, 1, user_id: user.id, appointment_id: appointments.first.id, result_type: :removed_from_overdue_list, remove_reason: CallResult.remove_reasons.keys.sample, device_created_at: 1.month.ago) }
call_results {
build_list(:call_result, 1, user_id: user.id, appointment_id: appointments.first.id, result_type: :removed_from_overdue_list,
remove_reason: CallResult.remove_reasons.keys.sample, device_created_at: call_date)
}
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/models/reports/region_summary_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1461,12 +1461,12 @@
end
end

xit "return the count of contactable patients returned to care who were removed from overdue list" do
it "return the count of contactable patients returned to care who were removed from overdue list" do
facility_1_contactable_patients = create_list(:patient, 3, :with_sanitized_phone_number, :hypertension, assigned_facility: facility_1, recorded_at: five_months_ago)
facility_1_contactable_patients.each { |patient| create(:appointment, patient: patient, device_created_at: three_months_ago, scheduled_date: three_months_ago + 14.days) }
facility_1_patients_without_phone = create(:patient, :without_phone_number, :hypertension, assigned_facility: facility_1, recorded_at: five_months_ago)
create(:appointment, patient: facility_1_patients_without_phone, device_created_at: three_months_ago, scheduled_date: three_months_ago + 14.days)
facility_1_patients_removed_from_list = create(:patient, :removed_from_overdue_list, :hypertension, assigned_facility: facility_1, recorded_at: five_months_ago)
facility_1_patients_removed_from_list = create(:patient, :hypertension, :removed_from_overdue_list, call_date: one_month_ago + 20.days, assigned_facility: facility_1, recorded_at: five_months_ago)
create(:appointment, patient: facility_1_patients_removed_from_list, device_created_at: three_months_ago, scheduled_date: three_months_ago + 14.days)

create(:call_result, patient: facility_1_patients_without_phone, result_type: "removed_from_overdue_list", remove_reason: "other", device_created_at: this_month)
Expand Down

0 comments on commit 67ef945

Please sign in to comment.