Sha256: 324251c5f84f6aff1a911b7a0400da82b570999f8e0566f46e20814b5c77ec47
Contents?: true
Size: 1.89 KB
Versions: 1
Compression:
Stored size: 1.89 KB
Contents
require 'cases/helper' class EagerCountTest < 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 if Tweet.has_reflection?(:favs_count) if ActiveRecord::VERSION::MAJOR >= 4 && ActiveRecord::VERSION::MINOR >= 2 Tweet._reflections.delete('favs_count') else Tweet._reflections.delete(:favs_count) end end end def teardown [Tweet, Favorite].each(&:delete_all) end def tweets_count 3 end def test_eager_count_defines_count_loader assert_equal(false, Tweet.has_reflection?(:favs_count)) Tweet.eager_count(:favs).map(&:favs_count) assert_equal(true, Tweet.has_reflection?(:favs_count)) end def test_eager_count_has_many_with_count_loader_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(1) { Tweet.eager_count(:favorites).map { |t| t.favorites.count } } assert_queries(1) { Tweet.eager_count(:favorites).map(&:favorites_count) } end def test_eager_count_has_many_counts_properly expected = Tweet.order(id: :asc).map { |t| t.favorites.count } assert_equal(expected, Tweet.order(id: :asc).map(&:favorites_count)) assert_equal(expected, Tweet.order(id: :asc).eager_count(:favorites).map { |t| t.favorites.count }) assert_equal(expected, Tweet.order(id: :asc).eager_count(:favorites).map(&:favorites_count)) end def test_eager_count_has_many_with_scope_counts_properly expected = Tweet.order(id: :asc).map { |t| t.my_favs.count } assert_equal(expected, Tweet.order(id: :asc).eager_count(:my_favs).map { |t| t.my_favs.count }) assert_equal(expected, Tweet.order(id: :asc).eager_count(:my_favs).map(&:my_favs_count)) end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
activerecord-precount-0.5.1 | test/cases/associations/eager_count_test.rb |