Browse Source

Fix flaky tests (#2019)

* feat: set encoding by bom for file IO before parsing

* feat: set bom encoding for illness task as well

* feat: use StringIOs in testing and fix row count bug found

* feat: revert toheaders: false

* feat: timecop around tests

* feat: timecop around tests

* feat: fix flaky merge test
pull/2020/head v0.3.73
natdeanlewissoftwire 1 year ago committed by GitHub
parent
commit
afa5ae7fdb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      spec/lib/tasks/send_missing_addresses_csv_spec.rb
  2. 12
      spec/services/merge/merge_organisations_service_spec.rb

13
spec/lib/tasks/send_missing_addresses_csv_spec.rb

@ -5,9 +5,14 @@ RSpec.describe "correct_addresses" do
describe ":send_missing_addresses_lettings_csv", type: :task do describe ":send_missing_addresses_lettings_csv", type: :task do
subject(:task) { Rake::Task["correct_addresses:send_missing_addresses_lettings_csv"] } subject(:task) { Rake::Task["correct_addresses:send_missing_addresses_lettings_csv"] }
before do around do |example|
Timecop.travel(Time.zone.local(2023, 10, 10)) Timecop.freeze(Time.zone.local(2023, 10, 10)) do
Singleton.__init__(FormHandler) Singleton.__init__(FormHandler)
example.run
end
end
before do
organisation.users.destroy_all organisation.users.destroy_all
Rake.application.rake_require("tasks/send_missing_addresses_csv") Rake.application.rake_require("tasks/send_missing_addresses_csv")
Rake::Task.define_task(:environment) Rake::Task.define_task(:environment)
@ -44,10 +49,6 @@ RSpec.describe "correct_addresses" do
.to_return(status: 200, body: body_2, headers: {}) .to_return(status: 200, body: body_2, headers: {})
end end
after do
Timecop.return
end
context "when the rake task is run" do context "when the rake task is run" do
let(:organisation) { create(:organisation, name: "test organisation") } let(:organisation) { create(:organisation, name: "test organisation") }

12
spec/services/merge/merge_organisations_service_spec.rb

@ -141,7 +141,6 @@ RSpec.describe Merge::MergeOrganisationsService do
let!(:owned_lettings_log_no_location) { create(:lettings_log, :sh, scheme:, startdate: Time.zone.tomorrow, owning_organisation: merging_organisation) } let!(:owned_lettings_log_no_location) { create(:lettings_log, :sh, scheme:, startdate: Time.zone.tomorrow, owning_organisation: merging_organisation) }
before do before do
create(:location, scheme:, name: "fake location", postcode: "A1 1AA")
create(:location, scheme: deactivated_scheme) create(:location, scheme: deactivated_scheme)
create(:scheme_deactivation_period, scheme: deactivated_scheme, deactivation_date: Time.zone.today - 1.month) create(:scheme_deactivation_period, scheme: deactivated_scheme, deactivation_date: Time.zone.today - 1.month)
create(:location_deactivation_period, location: deactivated_location, deactivation_date: Time.zone.today - 1.month) create(:location_deactivation_period, location: deactivated_location, deactivation_date: Time.zone.today - 1.month)
@ -162,7 +161,7 @@ RSpec.describe Merge::MergeOrganisationsService do
expect(absorbing_organisation.owned_schemes.first.service_name).to eq(scheme.service_name) expect(absorbing_organisation.owned_schemes.first.service_name).to eq(scheme.service_name)
expect(absorbing_organisation.owned_schemes.first.old_id).to be_nil expect(absorbing_organisation.owned_schemes.first.old_id).to be_nil
expect(absorbing_organisation.owned_schemes.first.old_visible_id).to be_nil expect(absorbing_organisation.owned_schemes.first.old_visible_id).to be_nil
expect(absorbing_organisation.owned_schemes.first.locations.count).to eq(2) expect(absorbing_organisation.owned_schemes.first.locations.count).to eq(1)
expect(absorbing_organisation.owned_schemes.first.locations.first.postcode).to eq(location.postcode) expect(absorbing_organisation.owned_schemes.first.locations.first.postcode).to eq(location.postcode)
expect(absorbing_organisation.owned_schemes.first.locations.first.old_id).to be_nil expect(absorbing_organisation.owned_schemes.first.locations.first.old_id).to be_nil
expect(absorbing_organisation.owned_schemes.first.locations.first.old_visible_id).to be_nil expect(absorbing_organisation.owned_schemes.first.locations.first.old_visible_id).to be_nil
@ -312,7 +311,6 @@ RSpec.describe Merge::MergeOrganisationsService do
let!(:owned_lettings_log_no_location) { create(:lettings_log, :sh, scheme:, startdate: Time.zone.tomorrow, owning_organisation: merging_organisation) } let!(:owned_lettings_log_no_location) { create(:lettings_log, :sh, scheme:, startdate: Time.zone.tomorrow, owning_organisation: merging_organisation) }
before do before do
create(:location, scheme:, name: "fake location", postcode: "A1 1AA")
create(:location, scheme: deactivated_scheme) create(:location, scheme: deactivated_scheme)
create(:scheme_deactivation_period, scheme: deactivated_scheme, deactivation_date: Time.zone.today - 1.month) create(:scheme_deactivation_period, scheme: deactivated_scheme, deactivation_date: Time.zone.today - 1.month)
create(:location_deactivation_period, location: deactivated_location, deactivation_date: Time.zone.today - 1.month) create(:location_deactivation_period, location: deactivated_location, deactivation_date: Time.zone.today - 1.month)
@ -331,7 +329,7 @@ RSpec.describe Merge::MergeOrganisationsService do
absorbing_organisation.reload absorbing_organisation.reload
expect(absorbing_organisation.owned_schemes.count).to eq(1) expect(absorbing_organisation.owned_schemes.count).to eq(1)
expect(absorbing_organisation.owned_schemes.first.service_name).to eq(scheme.service_name) expect(absorbing_organisation.owned_schemes.first.service_name).to eq(scheme.service_name)
expect(absorbing_organisation.owned_schemes.first.locations.count).to eq(2) expect(absorbing_organisation.owned_schemes.first.locations.count).to eq(1)
expect(absorbing_organisation.owned_schemes.first.locations.first.postcode).to eq(location.postcode) expect(absorbing_organisation.owned_schemes.first.locations.first.postcode).to eq(location.postcode)
expect(scheme.scheme_deactivation_periods.count).to eq(1) expect(scheme.scheme_deactivation_periods.count).to eq(1)
expect(scheme.scheme_deactivation_periods.first.deactivation_date.to_date).to eq(Time.zone.yesterday) expect(scheme.scheme_deactivation_periods.first.deactivation_date.to_date).to eq(Time.zone.yesterday)
@ -652,7 +650,6 @@ RSpec.describe Merge::MergeOrganisationsService do
let!(:owned_lettings_log_no_location) { create(:lettings_log, :sh, scheme:, startdate: Time.zone.tomorrow, owning_organisation: merging_organisation) } let!(:owned_lettings_log_no_location) { create(:lettings_log, :sh, scheme:, startdate: Time.zone.tomorrow, owning_organisation: merging_organisation) }
before do before do
create(:location, scheme:, name: "fake location", postcode: "A1 1AA")
create(:location, scheme: deactivated_scheme) create(:location, scheme: deactivated_scheme)
create(:scheme_deactivation_period, scheme: deactivated_scheme, deactivation_date: Time.zone.today - 1.month) create(:scheme_deactivation_period, scheme: deactivated_scheme, deactivation_date: Time.zone.today - 1.month)
create(:location_deactivation_period, location: deactivated_location, deactivation_date: Time.zone.today - 1.month) create(:location_deactivation_period, location: deactivated_location, deactivation_date: Time.zone.today - 1.month)
@ -673,7 +670,7 @@ RSpec.describe Merge::MergeOrganisationsService do
expect(new_absorbing_organisation.owned_schemes.first.service_name).to eq(scheme.service_name) expect(new_absorbing_organisation.owned_schemes.first.service_name).to eq(scheme.service_name)
expect(new_absorbing_organisation.owned_schemes.first.old_id).to be_nil expect(new_absorbing_organisation.owned_schemes.first.old_id).to be_nil
expect(new_absorbing_organisation.owned_schemes.first.old_visible_id).to be_nil expect(new_absorbing_organisation.owned_schemes.first.old_visible_id).to be_nil
expect(new_absorbing_organisation.owned_schemes.first.locations.count).to eq(2) expect(new_absorbing_organisation.owned_schemes.first.locations.count).to eq(1)
expect(new_absorbing_organisation.owned_schemes.first.locations.first.postcode).to eq(location.postcode) expect(new_absorbing_organisation.owned_schemes.first.locations.first.postcode).to eq(location.postcode)
expect(new_absorbing_organisation.owned_schemes.first.locations.first.old_id).to be_nil expect(new_absorbing_organisation.owned_schemes.first.locations.first.old_id).to be_nil
expect(new_absorbing_organisation.owned_schemes.first.locations.first.old_visible_id).to be_nil expect(new_absorbing_organisation.owned_schemes.first.locations.first.old_visible_id).to be_nil
@ -823,7 +820,6 @@ RSpec.describe Merge::MergeOrganisationsService do
let!(:owned_lettings_log_no_location) { create(:lettings_log, :sh, scheme:, startdate: Time.zone.tomorrow, owning_organisation: merging_organisation) } let!(:owned_lettings_log_no_location) { create(:lettings_log, :sh, scheme:, startdate: Time.zone.tomorrow, owning_organisation: merging_organisation) }
before do before do
create(:location, scheme:, name: "fake location", postcode: "A1 1AA")
create(:location, scheme: deactivated_scheme) create(:location, scheme: deactivated_scheme)
create(:scheme_deactivation_period, scheme: deactivated_scheme, deactivation_date: Time.zone.today - 1.month) create(:scheme_deactivation_period, scheme: deactivated_scheme, deactivation_date: Time.zone.today - 1.month)
create(:location_deactivation_period, location: deactivated_location, deactivation_date: Time.zone.today - 1.month) create(:location_deactivation_period, location: deactivated_location, deactivation_date: Time.zone.today - 1.month)
@ -842,7 +838,7 @@ RSpec.describe Merge::MergeOrganisationsService do
new_absorbing_organisation.reload new_absorbing_organisation.reload
expect(new_absorbing_organisation.owned_schemes.count).to eq(1) expect(new_absorbing_organisation.owned_schemes.count).to eq(1)
expect(new_absorbing_organisation.owned_schemes.first.service_name).to eq(scheme.service_name) expect(new_absorbing_organisation.owned_schemes.first.service_name).to eq(scheme.service_name)
expect(new_absorbing_organisation.owned_schemes.first.locations.count).to eq(2) expect(new_absorbing_organisation.owned_schemes.first.locations.count).to eq(1)
expect(new_absorbing_organisation.owned_schemes.first.locations.first.postcode).to eq(location.postcode) expect(new_absorbing_organisation.owned_schemes.first.locations.first.postcode).to eq(location.postcode)
expect(scheme.scheme_deactivation_periods.count).to eq(1) expect(scheme.scheme_deactivation_periods.count).to eq(1)
expect(scheme.scheme_deactivation_periods.first.deactivation_date.to_date).to eq(Time.zone.yesterday) expect(scheme.scheme_deactivation_periods.first.deactivation_date.to_date).to eq(Time.zone.yesterday)

Loading…
Cancel
Save