<commit_before># frozen_string_literal: true

require "active_record_unit"

class RelationCacheTest < ActionView::TestCase
  tests ActionView::Helpers::CacheHelper

  def setup
    super
    view_paths     = ActionController::Base.view_paths
    lookup_context = ActionView::LookupContext.new(view_paths, {}, ["test"])
    @view_renderer = ActionView::Renderer.new(lookup_context)
    @virtual_path  = "path"
    @current_template = lookup_context.find "test/hello_world"

    controller.cache_store = ActiveSupport::Cache::MemoryStore.new
  end

  def test_cache_relation_other
    cache(Project.all) { concat("Hello World") }
    assert_equal "Hello World", controller.cache_store.read("views/test/hello_world:fa9482a68ce25bf7589b8eddad72f736/projects-#{Project.count}")
  end

  def view_cache_dependencies; []; end
end
<commit_msg>Assert query counts in cache relation test<commit_after># frozen_string_literal: true

require "active_record_unit"

class RelationCacheTest < ActionView::TestCase
  tests ActionView::Helpers::CacheHelper

  def setup
    super
    view_paths     = ActionController::Base.view_paths
    lookup_context = ActionView::LookupContext.new(view_paths, {}, ["test"])
    @view_renderer = ActionView::Renderer.new(lookup_context)
    @virtual_path  = "path"
    @current_template = lookup_context.find "test/hello_world"

    controller.cache_store = ActiveSupport::Cache::MemoryStore.new
  end

  def test_cache_relation_other
    assert_queries(1) do
      cache(Project.all) { concat("Hello World") }
    end
    assert_equal "Hello World", controller.cache_store.read("views/test/hello_world:fa9482a68ce25bf7589b8eddad72f736/projects-#{Project.count}")
  end

  def view_cache_dependencies; []; end

  def assert_queries(num)
    ActiveRecord::Base.connection.materialize_transactions
    count = 0

    ActiveSupport::Notifications.subscribe("sql.active_record") do |_name, _start, _finish, _id, payload|
      count += 1 unless ["SCHEMA", "TRANSACTION"].include? payload[:name]
    end

    result = yield
    assert_equal num, count, "#{count} instead of #{num} queries were executed."
    result
  end
end

{'fraction_non_alphanumeric': 0.10415693601120972, 'fraction_numerical': 0.01821578701541336, 'mean_word_length': 5.0, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2646062', 'n_tokens_mistral': 680, 'n_tokens_neox': 654, 'n_words': 126}