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

- old
+ new

@@ -242,10 +242,50 @@ blog = Blog.first! post = blog.posts.to_a.first expect { post.save! }.to_not raise_error end + context "with manual eager loading" do + let(:blogs) { Blog.order(:name).send(load_method, :posts).to_a } + + before do + blogs.first.posts.to_a.first.author + end + + shared_examples "it auto-eager loads associations of manually eager loaded associations" do + specify do + blogs.flat_map(&:posts).drop(1).each do |blog| + expect(blog.association(:author)).to be_loaded + end + + expect(blogs.first.posts.first.author).to eq author1 + expect(blogs.first.posts.second.author).to eq author2 + expect(blogs.second.posts.first.author).to eq author3 + expect(blogs.second.posts.second.author).to eq author1 + expect(Post).to have_received(:find_by_sql).at_most(:once) + end + end + + context "via includes" do + let(:load_method) { :includes } + + it_behaves_like "it auto-eager loads associations of manually eager loaded associations" + end + + context "via eager_load" do + let(:load_method) { :eager_load } + + it_behaves_like "it auto-eager loads associations of manually eager loaded associations" + end + + context "via preload" do + let(:load_method) { :preload } + + it_behaves_like "it auto-eager loads associations of manually eager loaded associations" + end + end + 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', author: author1) @@ -366,9 +406,37 @@ 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 + + if ActiveRecord::VERSION::MAJOR < 4 + context "unique_tags_has_and_belongs associations with a uniq" do + let!(:post1) do + Post.create! { |post| post.tags << child_tag1 << child_tag1 << child_tag3 } + end + + let!(:post2) do + Post.create! { |post| post.tags << child_tag1 << child_tag1 << child_tag2 } + end + + let(:posts) { Post.where(id: [post1.id, post2.id]).order(:id).to_a } + + before do + posts.first.unique_tags_has_and_belongs.to_a + end + + it "applies the uniq correctly" do + expect(posts.first.unique_tags_has_and_belongs.to_a.size).to eq 2 + end + + it "doesn't auto eager the association" do + posts.drop(1).each do |author| + expect(author.association(:unique_tags_has_and_belongs)).to_not be_loaded end end end end end