From 7a6131e5bcde51b796c0b91e48e95a46eeb34800 Mon Sep 17 00:00:00 2001 From: Samuel Date: Mon, 19 May 2025 15:01:33 +0100 Subject: [PATCH] Make XML tests check unordered file contents stops tests flakily failing on the order of cols which is not important in xml --- .../lettings_log_export_service_spec.rb | 20 ++++++++--------- .../organisation_export_service_spec.rb | 10 ++++----- .../exports/sales_log_export_service_spec.rb | 16 +++++++------- .../exports/user_export_service_spec.rb | 10 ++++----- spec/spec_helper.rb | 22 +++++++++++++++++++ 5 files changed, 50 insertions(+), 28 deletions(-) diff --git a/spec/services/exports/lettings_log_export_service_spec.rb b/spec/services/exports/lettings_log_export_service_spec.rb index 600590e71..c2197c027 100644 --- a/spec/services/exports/lettings_log_export_service_spec.rb +++ b/spec/services/exports/lettings_log_export_service_spec.rb @@ -102,7 +102,7 @@ RSpec.describe Exports::LettingsLogExportService do expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content| entry = Zip::File.open_buffer(content).find_entry(expected_manifest_filename) expect(entry).not_to be_nil - expect(entry.get_input_stream.read).to eq(expected_content) + expect(entry.get_input_stream.read).to have_same_xml_contents_as(expected_content) end export_service.export_xml_lettings_logs @@ -113,7 +113,7 @@ RSpec.describe Exports::LettingsLogExportService do expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content| entry = Zip::File.open_buffer(content).find_entry(expected_data_filename) expect(entry).not_to be_nil - expect(entry.get_input_stream.read).to eq(expected_content) + expect(entry.get_input_stream.read).to have_same_xml_contents_as(expected_content) end export_service.export_xml_lettings_logs @@ -143,7 +143,7 @@ RSpec.describe Exports::LettingsLogExportService do expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content| entry = Zip::File.open_buffer(content).find_entry(expected_data_filename) expect(entry).not_to be_nil - expect(entry.get_input_stream.read).to eq(expected_content) + expect(entry.get_input_stream.read).to have_same_xml_contents_as(expected_content) end export_service.export_xml_lettings_logs @@ -188,7 +188,7 @@ RSpec.describe Exports::LettingsLogExportService do expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content| entry = Zip::File.open_buffer(content).find_entry(expected_data_filename) expect(entry).not_to be_nil - expect(entry.get_input_stream.read).to eq(expected_content) + expect(entry.get_input_stream.read).to have_same_xml_contents_as(expected_content) end export_service.export_xml_lettings_logs @@ -271,7 +271,7 @@ RSpec.describe Exports::LettingsLogExportService do expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content| entry = Zip::File.open_buffer(content).find_entry(expected_manifest_filename) expect(entry).not_to be_nil - expect(entry.get_input_stream.read).to eq(expected_content) + expect(entry.get_input_stream.read).to have_same_xml_contents_as(expected_content) end export_service.export_xml_lettings_logs @@ -390,7 +390,7 @@ RSpec.describe Exports::LettingsLogExportService do expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content| entry = Zip::File.open_buffer(content).find_entry(expected_manifest_filename) expect(entry).not_to be_nil - expect(entry.get_input_stream.read).to eq(expected_content) + expect(entry.get_input_stream.read).to have_same_xml_contents_as(expected_content) end expect(export_service.export_xml_lettings_logs).to eq({ expected_zip_filename.gsub(".zip", "") => start_time }) @@ -410,7 +410,7 @@ RSpec.describe Exports::LettingsLogExportService do expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content| entry = Zip::File.open_buffer(content).find_entry(expected_data_filename) expect(entry).not_to be_nil - expect(entry.get_input_stream.read).to eq(expected_content) + expect(entry.get_input_stream.read).to have_same_xml_contents_as(expected_content) end export_service.export_xml_lettings_logs @@ -441,7 +441,7 @@ RSpec.describe Exports::LettingsLogExportService do expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content| entry = Zip::File.open_buffer(content).find_entry(expected_data_filename) expect(entry).not_to be_nil - expect(entry.get_input_stream.read).to eq(expected_content) + expect(entry.get_input_stream.read).to have_same_xml_contents_as(expected_content) end export_service.export_xml_lettings_logs @@ -473,7 +473,7 @@ RSpec.describe Exports::LettingsLogExportService do expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content| entry = Zip::File.open_buffer(content).find_entry(expected_data_filename) expect(entry).not_to be_nil - expect(entry.get_input_stream.read).to eq(expected_content) + expect(entry.get_input_stream.read).to have_same_xml_contents_as(expected_content) end export_service.export_xml_lettings_logs @@ -504,7 +504,7 @@ RSpec.describe Exports::LettingsLogExportService do expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content| entry = Zip::File.open_buffer(content).find_entry(expected_data_filename) expect(entry).not_to be_nil - expect(entry.get_input_stream.read).to eq(expected_content) + expect(entry.get_input_stream.read).to have_same_xml_contents_as(expected_content) end export_service.export_xml_lettings_logs(full_update: true) end diff --git a/spec/services/exports/organisation_export_service_spec.rb b/spec/services/exports/organisation_export_service_spec.rb index 6ef66161a..03474de78 100644 --- a/spec/services/exports/organisation_export_service_spec.rb +++ b/spec/services/exports/organisation_export_service_spec.rb @@ -64,7 +64,7 @@ RSpec.describe Exports::OrganisationExportService do expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content| entry = Zip::File.open_buffer(content).find_entry(expected_manifest_filename) expect(entry).not_to be_nil - expect(entry.get_input_stream.read).to eq(expected_content) + expect(entry.get_input_stream.read).to have_same_xml_contents_as(expected_content) end export_service.export_xml_organisations @@ -75,7 +75,7 @@ RSpec.describe Exports::OrganisationExportService do expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content| entry = Zip::File.open_buffer(content).find_entry(expected_data_filename) expect(entry).not_to be_nil - expect(entry.get_input_stream.read).to eq(expected_content) + expect(entry.get_input_stream.read).to have_same_xml_contents_as(expected_content) end export_service.export_xml_organisations @@ -99,7 +99,7 @@ RSpec.describe Exports::OrganisationExportService do expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content| entry = Zip::File.open_buffer(content).find_entry(expected_data_filename) expect(entry).not_to be_nil - expect(entry.get_input_stream.read).to eq(expected_content) + expect(entry.get_input_stream.read).to have_same_xml_contents_as(expected_content) end export_service.export_xml_organisations @@ -118,7 +118,7 @@ RSpec.describe Exports::OrganisationExportService do expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content| entry = Zip::File.open_buffer(content).find_entry(expected_manifest_filename) expect(entry).not_to be_nil - expect(entry.get_input_stream.read).to eq(expected_content) + expect(entry.get_input_stream.read).to have_same_xml_contents_as(expected_content) end export_service.export_xml_organisations @@ -231,7 +231,7 @@ RSpec.describe Exports::OrganisationExportService do expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content| entry = Zip::File.open_buffer(content).find_entry(expected_manifest_filename) expect(entry).not_to be_nil - expect(entry.get_input_stream.read).to eq(expected_content) + expect(entry.get_input_stream.read).to have_same_xml_contents_as(expected_content) end expect(export_service.export_xml_organisations).to eq({ expected_zip_filename.gsub(".zip", "") => start_time }) diff --git a/spec/services/exports/sales_log_export_service_spec.rb b/spec/services/exports/sales_log_export_service_spec.rb index c11c13712..15894ae3d 100644 --- a/spec/services/exports/sales_log_export_service_spec.rb +++ b/spec/services/exports/sales_log_export_service_spec.rb @@ -87,7 +87,7 @@ RSpec.describe Exports::SalesLogExportService do expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content| entry = Zip::File.open_buffer(content).find_entry(expected_manifest_filename) expect(entry).not_to be_nil - expect(entry.get_input_stream.read).to eq(expected_content) + expect(entry.get_input_stream.read).to have_same_xml_contents_as(expected_content) end export_service.export_xml_sales_logs @@ -98,7 +98,7 @@ RSpec.describe Exports::SalesLogExportService do expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content| entry = Zip::File.open_buffer(content).find_entry(expected_data_filename) expect(entry).not_to be_nil - expect(entry.get_input_stream.read).to eq(expected_content) + expect(entry.get_input_stream.read).to have_same_xml_contents_as(expected_content) end export_service.export_xml_sales_logs @@ -183,7 +183,7 @@ RSpec.describe Exports::SalesLogExportService do expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content| entry = Zip::File.open_buffer(content).find_entry(expected_manifest_filename) expect(entry).not_to be_nil - expect(entry.get_input_stream.read).to eq(expected_content) + expect(entry.get_input_stream.read).to have_same_xml_contents_as(expected_content) end export_service.export_xml_sales_logs @@ -304,7 +304,7 @@ RSpec.describe Exports::SalesLogExportService do expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content| entry = Zip::File.open_buffer(content).find_entry(expected_manifest_filename) expect(entry).not_to be_nil - expect(entry.get_input_stream.read).to eq(expected_content) + expect(entry.get_input_stream.read).to have_same_xml_contents_as(expected_content) end expect(export_service.export_xml_sales_logs).to eq({ expected_zip_filename.gsub(".zip", "") => start_time }) @@ -324,7 +324,7 @@ RSpec.describe Exports::SalesLogExportService do expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content| entry = Zip::File.open_buffer(content).find_entry(expected_data_filename) expect(entry).not_to be_nil - expect(entry.get_input_stream.read).to eq(expected_content) + expect(entry.get_input_stream.read).to have_same_xml_contents_as(expected_content) end export_service.export_xml_sales_logs @@ -355,7 +355,7 @@ RSpec.describe Exports::SalesLogExportService do expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content| entry = Zip::File.open_buffer(content).find_entry(expected_data_filename) expect(entry).not_to be_nil - expect(entry.get_input_stream.read).to eq(expected_content) + expect(entry.get_input_stream.read).to have_same_xml_contents_as(expected_content) end export_service.export_xml_sales_logs(full_update: true, collection_year: 2024) @@ -377,7 +377,7 @@ RSpec.describe Exports::SalesLogExportService do expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content| entry = Zip::File.open_buffer(content).find_entry(expected_data_filename) expect(entry).not_to be_nil - expect(entry.get_input_stream.read).to eq(expected_content) + expect(entry.get_input_stream.read).to have_same_xml_contents_as(expected_content) end export_service.export_xml_sales_logs @@ -414,7 +414,7 @@ RSpec.describe Exports::SalesLogExportService do expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content| entry = Zip::File.open_buffer(content).find_entry(expected_data_filename) expect(entry).not_to be_nil - expect(entry.get_input_stream.read).to eq(expected_content) + expect(entry.get_input_stream.read).to have_same_xml_contents_as(expected_content) end export_service.export_xml_sales_logs diff --git a/spec/services/exports/user_export_service_spec.rb b/spec/services/exports/user_export_service_spec.rb index 93ba124d9..bafbc7f77 100644 --- a/spec/services/exports/user_export_service_spec.rb +++ b/spec/services/exports/user_export_service_spec.rb @@ -64,7 +64,7 @@ RSpec.describe Exports::UserExportService do expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content| entry = Zip::File.open_buffer(content).find_entry(expected_manifest_filename) expect(entry).not_to be_nil - expect(entry.get_input_stream.read).to eq(expected_content) + expect(entry.get_input_stream.read).to have_same_xml_contents_as(expected_content) end export_service.export_xml_users @@ -75,7 +75,7 @@ RSpec.describe Exports::UserExportService do expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content| entry = Zip::File.open_buffer(content).find_entry(expected_data_filename) expect(entry).not_to be_nil - expect(entry.get_input_stream.read).to eq(expected_content) + expect(entry.get_input_stream.read).to have_same_xml_contents_as(expected_content) end export_service.export_xml_users @@ -97,7 +97,7 @@ RSpec.describe Exports::UserExportService do expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content| entry = Zip::File.open_buffer(content).find_entry(expected_manifest_filename) expect(entry).not_to be_nil - expect(entry.get_input_stream.read).to eq(expected_content) + expect(entry.get_input_stream.read).to have_same_xml_contents_as(expected_content) end export_service.export_xml_users @@ -210,7 +210,7 @@ RSpec.describe Exports::UserExportService do expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content| entry = Zip::File.open_buffer(content).find_entry(expected_manifest_filename) expect(entry).not_to be_nil - expect(entry.get_input_stream.read).to eq(expected_content) + expect(entry.get_input_stream.read).to have_same_xml_contents_as(expected_content) end expect(export_service.export_xml_users).to eq({ expected_zip_filename.gsub(".zip", "") => start_time }) @@ -229,7 +229,7 @@ RSpec.describe Exports::UserExportService do expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content| entry = Zip::File.open_buffer(content).find_entry(expected_manifest_filename) expect(entry).not_to be_nil - expect(entry.get_input_stream.read).to eq(expected_content) + expect(entry.get_input_stream.read).to have_same_xml_contents_as(expected_content) end expect(export_service.export_xml_users).to eq({ expected_zip_filename.gsub(".zip", "") => start_time }) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 9677a128c..3a4a7daed 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -126,3 +126,25 @@ RSpec.configure do |config| end RSpec::Matchers.define_negated_matcher :not_change, :change + +def fix_xml_content_order(xml_file_content) + # split the content up my lines, sort them, then join together again + # this ensures the order of the output elements doesn't matter for the tests + # as the tests care about the content of the XML, not the order + xml_file_content.split("\n").sort.join("\n") +end + +# write a matcher that checks if two strings are equal after apply fix_xml_content_order to both +RSpec::Matchers.define :have_same_xml_contents_as do |expected| + match do |actual| + fix_xml_content_order(actual) == fix_xml_content_order(expected) + end + + failure_message do |actual| + "expected that unordered #{fix_xml_content_order(actual)} would be equal to unordered #{fix_xml_content_order(expected)}" + end + + failure_message_when_negated do |actual| + "expected that unordered #{fix_xml_content_order(actual)} would not be equal to unordered #{fix_xml_content_order(expected)}" + end +end