spec/goldiloader/goldiloader_spec.rb in goldiloader-0.0.4 vs spec/goldiloader/goldiloader_spec.rb in goldiloader-0.0.5

- old
+ new

@@ -148,10 +148,23 @@ expect(blogs.first.authors).to match_array([author1, author2]) expect(blogs.second.authors).to match_array([author3, author1]) expect(User).to have_received(:find_by_sql).once end + it "auto eager loads nested has_many through associations" do + blogs = Blog.order(:name).to_a + blogs.first.addresses.to_a + + blogs.each do |blog| + expect(blog.association(:addresses)).to be_loaded + end + + expect(blogs.first.addresses).to match_array([author1, author2].map(&:address)) + expect(blogs.second.addresses).to match_array([author3, author1].map(&:address)) + expect(Address).to have_received(:find_by_sql).once + end + it "auto eager loads associations when the model is loaded via find" do blog = Blog.find(blog1.id) blog.posts.to_a.first.author blog.posts.each do |blog| @@ -233,12 +246,12 @@ context "with associations that can't be eager loaded" do let(:blogs) { Blog.order(:name).to_a } before do - blog1.posts.create!(title: 'blog1-post3') - blog2.posts.create!(title: 'blog2-post3') + blog1.posts.create!(title: 'blog1-post3', author: author1) + blog2.posts.create!(title: 'blog2-post3', author: author1) end shared_examples "it doesn't auto eager the association" do |association_name| specify do blogs.drop(1).each do |blog| @@ -281,19 +294,84 @@ end it_behaves_like "it doesn't auto eager the association", :offset_posts end - context "associations with an overridden from" do - before do - blogs.first.from_posts.to_a + if ActiveRecord::VERSION::MAJOR >= 4 + context "associations with an overridden from" do + before do + blogs.first.from_posts.to_a + end + + it "applies the from correctly" do + expect(blogs.first.from_posts.to_a.size).to eq 1 + end + + it_behaves_like "it doesn't auto eager the association", :from_posts end + end - it "applies the from correctly" do - expect(blogs.first.from_posts.to_a.size).to eq 1 + if Goldiloader::Compatibility.association_finder_sql_enabled? + context "associations with finder_sql" do + before do + blogs.first.finder_sql_posts.to_a + end + + it "applies the finder_sql correctly" do + expect(blogs.first.finder_sql_posts.to_a.size).to eq 1 + end + + it_behaves_like "it doesn't auto eager the association", :finder_sql_posts end + end - it_behaves_like "it doesn't auto eager the association", :from_posts + if ActiveRecord::VERSION::MAJOR >= 4 + context "associations with a join" do + before do + blogs.first.posts_ordered_by_author.to_a + end + + it "applies the join correctly" do + expect(blogs.first.posts_ordered_by_author.to_a.size).to eq 3 + end + + it_behaves_like "it doesn't auto eager the association", :posts_ordered_by_author + end + + context "associations with a join in a has_many_through" do + before do + blogs.first.authors_with_join.to_a + end + + it "applies the join correctly" do + expect(blogs.first.authors_with_join.to_a.size).to eq 3 + end + + it_behaves_like "it doesn't auto eager the association", :authors_with_join + end + end + + if Goldiloader::Compatibility.unscope_query_method_enabled? + context "associations with an unscoped" do + let(:authors) { User.order(:id).to_a } + + before do + author1.address.update_attributes!(city: 'Boston') + author2.address.update_attributes!(city: 'Philadelphia') + author3.address.update_attributes!(city: 'Philadelphia') + authors.first.scoped_address_with_default_scope_remove + end + + it "applies the unscope correctly" do + expect(authors.first.scoped_address_with_default_scope_remove).to be_present + end + + it "doesn't auto eager the association" do + authors.drop(1).each do |author| + expect(author.association(:scoped_address_with_default_scope_remove)).to_not be_loaded + end + end + end end end context "associations with a uniq" do let!(:post1) do