Skip to content

Commit b07374d

Browse files
committed
Avoid unstable SQL sort in ProFormA service specs
The existing test relied on the order given by convert_to_task_service.test to match the order given through the tests variable (filled with ProformaXML::Test). However, the ordering is not guaranteed through PostgreSQL and could change, e.g. through an update operation. Hence, it is safer to order the elements first and then perform the expectations.
1 parent 1538d84 commit b07374d

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

spec/services/proforma_service/convert_proforma_task_to_task_spec.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,15 +236,22 @@
236236
]
237237
end
238238

239+
let(:converted_tests) do
240+
# Preload tests and files, then sort files by title to ensure consistent order (for expectations below)
241+
ActiveRecord::Associations::Preloader.new(records: [convert_to_task_service], associations: {tests: :files}).call
242+
# The converted tests should be in the same order as the original tests. Hence, we sort the converted tests by the original test id.
243+
convert_to_task_service.tests.sort_by {|test| tests.index {|t| t.id.to_s == test.xml_id } }
244+
end
245+
239246
it 'creates files with correct attributes' do
240247
attribute_exceptions = %w[id created_at fileable_id updated_at]
241-
expect(convert_to_task_service.tests[1].files[0]).to have_attributes(convert_to_task_service.tests[0].files[0].attributes.except(*attribute_exceptions).merge('xml_id' => 'id-2'))
242-
expect(convert_to_task_service.tests[2].files[0]).to have_attributes(convert_to_task_service.tests[0].files[0].attributes.except(*attribute_exceptions).merge('xml_id' => 'id-3'))
248+
expect(converted_tests[1].files[0]).to have_attributes(converted_tests[0].files[0].attributes.except(*attribute_exceptions).merge('xml_id' => 'id-2'))
249+
expect(converted_tests[2].files[0]).to have_attributes(converted_tests[0].files[0].attributes.except(*attribute_exceptions).merge('xml_id' => 'id-3'))
243250
end
244251

245252
it 'creates separate copies of referenced file' do
246-
expect(convert_to_task_service.tests[1].files[0]).to not_eql convert_to_task_service.tests[0].files[0]
247-
expect(convert_to_task_service.tests[2].files[0]).to not_eql convert_to_task_service.tests[0].files[0]
253+
expect(converted_tests[1].files[0]).to not_eql converted_tests[0].files[0]
254+
expect(converted_tests[2].files[0]).to not_eql converted_tests[0].files[0]
248255
end
249256

250257
it 'is valid' do

0 commit comments

Comments
 (0)