Sha256: 44e32a66d550589ec5e55999076c4bb03b2f91285e9d32155a586afd3c0a4000

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

require 'cases/helper'

class PrecountTest < ActiveRecord::CountLoader::TestCase
  def setup
    tweets_count.times.map do |index|
      tweet = Tweet.create
      index.times { Favorite.create(tweet: tweet) }
    end
  end

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

  def tweets_count
    3
  end

  def test_precount_has_many_does_not_execute_n_1_queries
    assert_equal(Tweet.reflections['favs_count'].present?, false)
    assert_queries(1 + tweets_count) { Tweet.all.map { |t| t.favs.count } }
    assert_queries(2) { Tweet.precount(:favs).map(&:favs_count) }
  end

  def test_precount_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(2) { Tweet.precount(:favorites).map(&:favorites_count) }
  end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activerecord-precount-0.4.0 test/cases/associations/precount_test.rb