Sha256: 9da814d1e35e311671d0ea240d6bbfb0d3e7285f76cbc47dfa1c31fea9a5d002

Contents?: true

Size: 1.55 KB

Versions: 8

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

require_relative "test_helper"

class TestMinitest < Minitest::Test
  def test_no_n_plus_one_error
    populate = ->(n) { create_list(:post, n) }

    assert_perform_constant_number_of_queries(populate: populate) do
      Post.preload(:user).find_each { |p| p.user.name }
    end
  end

  def test_with_n_plus_one_error
    populate = ->(n) { create_list(:post, n) }

    e = assert_raises Minitest::Assertion do
      assert_perform_constant_number_of_queries(populate: populate) do
        Post.find_each { |p| p.user.name }
      end
    end

    assert_match "Expected to make the same number of queries", e.message
    assert_match "3 for N=2", e.message
    assert_match "4 for N=3", e.message
  end

  def test_no_n_plus_one_error_with_scale_factors
    populate = ->(n) { create_list(:post, n) }

    assert_perform_constant_number_of_queries(
      populate: populate,
      scale_factors: [1, 1]
    ) do
      Post.find_each { |p| p.user.name }
    end
  end

  def test_no_n_plus_one_error_with_matching
    populate = ->(n) { create_list(:post, n) }

    assert_perform_constant_number_of_queries(
      populate: populate,
      matching: /posts/
    ) do
      Post.find_each { |p| p.user.name }
    end
  end

  def populate(n)
    create_list(:post, n)
  end

  def test_fallback_to_populate_method
    e = assert_raises Minitest::Assertion do
      assert_perform_constant_number_of_queries do
        Post.find_each { |p| p.user.name }
      end
    end

    assert_match "Expected to make the same number of queries", e.message
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
n_plus_one_control-0.3.1 tests/minitest_test.rb
n_plus_one_control-0.3.0 tests/minitest_test.rb
n_plus_one_control-0.2.1 tests/minitest_test.rb
n_plus_one_control-0.1.3 tests/minitest_test.rb
n_plus_one_control-0.1.2 tests/minitest_test.rb
n_plus_one_control-0.1.1 tests/minitest_test.rb
n_plus_one_control-0.1.0 tests/minitest_test.rb
n_plus_one_control-0.0.3 tests/minitest_test.rb