Sha256: e6096b24480034257c81c26470343bf34121d630c76e2e0f9193129ac9c3e676

Contents?: true

Size: 1.15 KB

Versions: 7

Compression:

Stored size: 1.15 KB

Contents

require 'cases/helper'

class IncludesTest < ActiveRecord::CountLoader::TestCase
  def setup
    tweets_count.times do |i|
      tweet = Tweet.create
      i.times do |j|
        Favorite.create(tweet: tweet, user_id: j + 1)
      end
    end
  end

  def teardown
    [Tweet, Favorite].each(&:delete_all)
  end

  def tweets_count
    3
  end

  def test_includes_does_not_execute_n_1_queries
    assert_queries(1 + tweets_count) { Tweet.all.map { |t| t.favorites.count } }
    assert_queries(1 + tweets_count) { Tweet.all.map(&:favorites_count) }
    assert_queries(2) { Tweet.includes(:favorites_count).map(&:favorites_count) }
  end

  def test_included_count_loader_counts_properly
    expected = Tweet.all.map { |t| t.favorites.count }
    assert_equal(expected, Tweet.all.map(&:favorites_count))
    assert_equal(expected, Tweet.includes(:favorites_count).map(&:favorites_count))
  end

  def test_included_count_loader_with_scope_counts_properly
    expected = Tweet.all.map { |t| t.my_favorites.count }
    assert_equal(expected, Tweet.all.map(&:my_favorites_count))
    assert_equal(expected, Tweet.includes(:my_favorites_count).map(&:my_favorites_count))
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
activerecord-precount-0.7.0 test/cases/associations/includes_test.rb
activerecord-precount-0.7.0.beta1 test/cases/associations/includes_test.rb
activerecord-precount-0.6.3 test/cases/associations/includes_test.rb
activerecord-precount-0.6.2 test/cases/associations/includes_test.rb
activerecord-precount-0.6.1 test/cases/associations/includes_test.rb
activerecord-precount-0.6.0 test/cases/associations/includes_test.rb
activerecord-precount-0.5.1 test/cases/associations/includes_test.rb