aboutsummaryrefslogtreecommitdiffstats
path: root/actionview/test/activerecord/multifetch_cache_test.rb
blob: f56168bda57570ef15297d6082ec3344e7aedc30 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# frozen_string_literal: true

require "active_record_unit"
require "active_record/railties/collection_cache_association_loading"

ActionView::PartialRenderer.prepend(ActiveRecord::Railties::CollectionCacheAssociationLoading)

class MultifetchCacheTest < ActiveRecordTestCase
  fixtures :topics, :replies

  def setup
    view_paths = ActionController::Base.view_paths
    view_paths.each(&:clear_cache)
    ActionView::LookupContext.fallbacks.each(&:clear_cache)

    @view = Class.new(ActionView::Base.with_empty_template_cache) do
      def view_cache_dependencies
        []
      end

      def combined_fragment_cache_key(key)
        [ :views, key ]
      end
    end.with_view_paths(view_paths, {})
  end

  def test_only_preloading_for_records_that_miss_the_cache
    @view.render partial: "test/partial", collection: [topics(:rails)], cached: true

    @topics = Topic.preload(:replies)

    @view.render partial: "test/partial", collection: @topics, cached: true

    assert_not @topics.detect { |topic| topic.id == topics(:rails).id }.replies.loaded?
    assert     @topics.detect { |topic| topic.id != topics(:rails).id }.replies.loaded?
  end
end