Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected record order returned when using use_multiple_files and set_filenames #219

Open
paveg opened this issue Dec 22, 2020 · 0 comments

Comments

@paveg
Copy link

paveg commented Dec 22, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant