Skip to content

Unexpected record order returned when using use_multiple_files and set_filenames #219

@paveg

Description

@paveg

Add a test case to make it happen.
( The location where the test cases were added was small, so I'm just using aliases_spec.rb . )

diff --git a/spec/active_yaml/aliases_spec.rb b/spec/active_yaml/aliases_spec.rb
index d51d182..515d19f 100644
--- a/spec/active_yaml/aliases_spec.rb
+++ b/spec/active_yaml/aliases_spec.rb
@@ -68,21 +68,43 @@ describe ActiveYaml::Aliases do
     let(:coke) { model.where(:name => 'Coke').first }
     let(:schweppes) { model.where(:name => 'Schweppes').first }

-    before do
-      class MultipleFiles < ActiveYaml::Base
-        include ActiveYaml::Aliases
-        use_multiple_files
-        set_filenames 'array_products', 'array_products_2'
+    after do
+      Object.send :remove_const, :MultipleFiles
+    end
+
+    shared_examples 'returns correct data from both files' do
+      it do
+        expect(coke.flavor).to eq 'sweet'
+        expect(schweppes.flavor).to eq 'bitter'
+        binding.pry
+        expect(model.all.map(&:id)).to eq((1..5).to_a)
       end
     end

-    after do
-      Object.send :remove_const, :MultipleFiles
+    context 'when set_filenames correctly order' do
+      before do
+        class MultipleFiles < ActiveYaml::Base
+          include ActiveYaml::Aliases
+          use_multiple_files
+          files = %w[array_products array_products_2]
+          set_filenames *files
+        end
+      end
+
+      it_behaves_like 'returns correct data from both files'
     end

-    it 'returns correct data from both files' do
-      expect(coke.flavor).to eq 'sweet'
-      expect(schweppes.flavor).to eq 'bitter'
+    context 'when set_filenames incorrectly order' do
+      before do
+        class MultipleFiles < ActiveYaml::Base
+          include ActiveYaml::Aliases
+          use_multiple_files
+          files = %w[array_products_2 array_products]
+          set_filenames *files
+        end
+      end
+
+      it_behaves_like 'returns correct data from both files'
     end
   end
 end

And this test returns the following results.

Failures:

  1) ActiveYaml::Aliases Loading multiple files when set_filenames incorrectly order behaves like returns correct data from both files is expected to eq [1, 2, 3, 4, 5]
     Failure/Error: expect(model.all.map(&:id)).to eq((1..5).to_a)

       expected: [1, 2, 3, 4, 5]
            got: [5, 1, 2, 3, 4]

       (compared using ==)
     Shared Example Group: "returns correct data from both files" called from ./spec/active_yaml/aliases_spec.rb:106
     # ./spec/active_yaml/aliases_spec.rb:79:in `block (4 levels) in <top (required)>'

There are cases where there is no ID, but basically it would be more intuitive to return the result sorted by ID as the initial value of record access.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions