From 80e176e35b4fd319e5a07ba4c6e48c0358dcb9a9 Mon Sep 17 00:00:00 2001 From: Stefanni Brasil Date: Thu, 19 Mar 2026 11:04:53 -0600 Subject: [PATCH 1/5] reuse organization to create fewer factories --- spec/models/organization_spec.rb | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/spec/models/organization_spec.rb b/spec/models/organization_spec.rb index ddbd10442a..b9eb013ccf 100644 --- a/spec/models/organization_spec.rb +++ b/spec/models/organization_spec.rb @@ -107,7 +107,6 @@ describe 'users' do subject { organization.users } - let(:organization) { create(:organization) } context 'when a organizaton has a user that has two roles' do let(:user) { create(:user) } @@ -361,7 +360,6 @@ end context 'with invisible items' do - let!(:organization) { create(:organization) } let!(:item1) { create(:item, organization: organization, active: true, visible_to_partners: true) } let!(:item2) { create(:item, organization: organization, active: true, visible_to_partners: false) } let!(:item3) { create(:item, organization: organization, active: false, visible_to_partners: true) } @@ -401,21 +399,20 @@ describe 'earliest reporting year' do # re 2813 update annual report -- allowing an earliest reporting year will let us do system testing and staging for annual reports it 'is the organization created year if no associated data' do - org = create(:organization) - expect(org.earliest_reporting_year).to eq(org.created_at.year) + expect(organization.earliest_reporting_year).to eq(organization.created_at.year) end + it 'is the year of the earliest of donation, purchase, or distribution if they are earlier ' do - org = create(:organization) - create(:donation, organization: org, issued_at: 364.days.from_now) - create(:purchase, organization: org, issued_at: 364.days.from_now) - create(:distribution, organization: org, issued_at: 364.days.from_now) - expect(org.earliest_reporting_year).to eq(org.created_at.year) - create(:donation, organization: org, issued_at: 5.years.ago) - expect(org.earliest_reporting_year).to eq(5.years.ago.year) - create(:purchase, organization: org, issued_at: 6.years.ago) - expect(org.earliest_reporting_year).to eq(6.years.ago.year) - create(:purchase, organization: org, issued_at: 7.years.ago) - expect(org.earliest_reporting_year).to eq(7.years.ago.year) + create(:donation, organization: organization, issued_at: 364.days.from_now) + create(:purchase, organization: organization, issued_at: 364.days.from_now) + create(:distribution, organization: organization, issued_at: 364.days.from_now) + expect(organization.earliest_reporting_year).to eq(organization.created_at.year) + create(:donation, organization: organization, issued_at: 5.years.ago) + expect(organization.earliest_reporting_year).to eq(5.years.ago.year) + create(:purchase, organization: organization, issued_at: 6.years.ago) + expect(organization.earliest_reporting_year).to eq(6.years.ago.year) + create(:purchase, organization: organization, issued_at: 7.years.ago) + expect(organization.earliest_reporting_year).to eq(7.years.ago.year) end end From 179c75c7824ac65750ea41e0abc4615d44f15717 Mon Sep 17 00:00:00 2001 From: Stefanni Brasil Date: Fri, 20 Mar 2026 15:42:11 -0600 Subject: [PATCH 2/5] Use DateTime.current instead of DateTime.now Rails extends the Time and DateTime objects, and includes the `current`` property for retrieving the time the Rails environment is set to (default = UTC), as opposed to the server time (could be anything). https://stackoverflow.com/a/18811305 --- app/models/concerns/issued_at.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/concerns/issued_at.rb b/app/models/concerns/issued_at.rb index 93d1c3e2ef..e7b1ad9fc5 100644 --- a/app/models/concerns/issued_at.rb +++ b/app/models/concerns/issued_at.rb @@ -21,7 +21,7 @@ def issued_at_cannot_be_before_2000 end def issued_at_cannot_be_further_than_1_year - if issued_at.present? && issued_at > DateTime.now.next_year + if issued_at.present? && issued_at > DateTime.current.next_year errors.add(:issued_at, "cannot be more than 1 year in the future") end end From ea7c734ca9983d9890c7a27be4ed30979b13f7a6 Mon Sep 17 00:00:00 2001 From: Stefanni Brasil Date: Fri, 20 Mar 2026 15:43:19 -0600 Subject: [PATCH 3/5] Use same helper as the model for consistency --- spec/factories/donations.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/factories/donations.rb b/spec/factories/donations.rb index 70e2bef821..ef5bb6c1a0 100644 --- a/spec/factories/donations.rb +++ b/spec/factories/donations.rb @@ -22,7 +22,7 @@ source { Donation::SOURCES[:misc] } storage_location organization { Organization.try(:first) || create(:organization) } - issued_at { Time.current } + issued_at { DateTime.current } factory :manufacturer_donation do manufacturer From e1aa163dcf96fffb0cf47579e1d32f8e6fa7c2db Mon Sep 17 00:00:00 2001 From: Stefanni Brasil Date: Fri, 20 Mar 2026 15:43:25 -0600 Subject: [PATCH 4/5] Time updates to fix flakiness Freezing the time and checking for the same time period should fix the flakiness. --- spec/models/organization_spec.rb | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/spec/models/organization_spec.rb b/spec/models/organization_spec.rb index b9eb013ccf..ad40e1b7d6 100644 --- a/spec/models/organization_spec.rb +++ b/spec/models/organization_spec.rb @@ -403,16 +403,20 @@ end it 'is the year of the earliest of donation, purchase, or distribution if they are earlier ' do - create(:donation, organization: organization, issued_at: 364.days.from_now) - create(:purchase, organization: organization, issued_at: 364.days.from_now) - create(:distribution, organization: organization, issued_at: 364.days.from_now) - expect(organization.earliest_reporting_year).to eq(organization.created_at.year) - create(:donation, organization: organization, issued_at: 5.years.ago) - expect(organization.earliest_reporting_year).to eq(5.years.ago.year) - create(:purchase, organization: organization, issued_at: 6.years.ago) - expect(organization.earliest_reporting_year).to eq(6.years.ago.year) - create(:purchase, organization: organization, issued_at: 7.years.ago) - expect(organization.earliest_reporting_year).to eq(7.years.ago.year) + freeze_time do + create(:donation, organization: organization, issued_at: DateTime.current.next_year - 1.day) + create(:purchase, organization: organization, issued_at: DateTime.current.next_year - 1.day) + create(:distribution, organization: organization, issued_at: DateTime.current.next_year - 1.day) + expect(organization.earliest_reporting_year).to eq(organization.created_at.year) + create(:donation, organization: organization, issued_at: 5.years.ago) + expect(organization.earliest_reporting_year).to eq(5.years.ago.year) + create(:purchase, organization: organization, issued_at: 6.years.ago) + expect(organization.earliest_reporting_year).to eq(6.years.ago.year) + create(:purchase, organization: organization, issued_at: 7.years.ago) + expect(organization.earliest_reporting_year).to eq(7.years.ago.year) + ensure + travel_back + end end end From 0cc84c5e48584cb6d8fcb134b0b13eff9e5be697 Mon Sep 17 00:00:00 2001 From: Stefanni Brasil Date: Fri, 20 Mar 2026 15:55:01 -0600 Subject: [PATCH 5/5] Freeze entire block to prevent flakiness Time was being frozen after creating the factory. Move the test setup inside a freeze_time block to ensure created_at is created 1.day.ago. Plus, using the same date/time helper as the IssuedAt concern helps with keeping the test consistent with the code. --- spec/requests/reports/donations_summary_spec.rb | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/spec/requests/reports/donations_summary_spec.rb b/spec/requests/reports/donations_summary_spec.rb index f8291020c7..9598dc2f9c 100644 --- a/spec/requests/reports/donations_summary_spec.rb +++ b/spec/requests/reports/donations_summary_spec.rb @@ -8,15 +8,16 @@ end describe "time display" do - let!(:donation) { create(:donation, :with_items, issued_at: 1.day.ago) } + it "uses issued_at for the relative time display, not created_at" do + freeze_time do + create(:donation, :with_items, issued_at: DateTime.current - 1.day) - before do - freeze_time - get reports_donations_summary_path - end + get reports_donations_summary_path - it "uses issued_at for the relative time display, not created_at" do - expect(response.body).to include("1 day ago") + expect(response.body).to include("1 day ago") + ensure + travel_back + end end end