n_plus_one_control 0.3.1 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +41 -0
- data/LICENSE.txt +1 -1
- data/README.md +107 -15
- data/lib/n_plus_one_control.rb +102 -6
- data/lib/n_plus_one_control/executor.rb +63 -24
- data/lib/n_plus_one_control/minitest.rb +54 -9
- data/lib/n_plus_one_control/railtie.rb +13 -0
- data/lib/n_plus_one_control/rspec.rb +6 -2
- data/lib/n_plus_one_control/rspec/context.rb +1 -10
- data/lib/n_plus_one_control/rspec/dsl.rb +21 -12
- data/lib/n_plus_one_control/rspec/{matcher.rb → matchers/perform_constant_number_of_queries.rb} +6 -6
- data/lib/n_plus_one_control/rspec/matchers/perform_linear_number_of_queries.rb +53 -0
- data/lib/n_plus_one_control/version.rb +1 -1
- metadata +20 -86
- data/.gitignore +0 -11
- data/.rspec +0 -2
- data/.rubocop.yml +0 -78
- data/.travis.yml +0 -9
- data/Gemfile +0 -4
- data/Rakefile +0 -13
- data/bin/console +0 -14
- data/bin/setup +0 -8
- data/n_plus_one_control.gemspec +0 -35
- data/spec/n_plus_one_control/executor_spec.rb +0 -65
- data/spec/n_plus_one_control/rspec_spec.rb +0 -113
- data/spec/n_plus_one_control_spec.rb +0 -9
- data/spec/spec_helper.rb +0 -30
- data/spec/support/post.rb +0 -19
- data/spec/support/user.rb +0 -17
- data/tests/minitest_test.rb +0 -63
- data/tests/test_helper.rb +0 -32
data/tests/minitest_test.rb
DELETED
@@ -1,63 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative "test_helper"
|
4
|
-
|
5
|
-
class TestMinitest < Minitest::Test
|
6
|
-
def test_no_n_plus_one_error
|
7
|
-
populate = ->(n) { create_list(:post, n) }
|
8
|
-
|
9
|
-
assert_perform_constant_number_of_queries(populate: populate) do
|
10
|
-
Post.preload(:user).find_each { |p| p.user.name }
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_with_n_plus_one_error
|
15
|
-
populate = ->(n) { create_list(:post, n) }
|
16
|
-
|
17
|
-
e = assert_raises Minitest::Assertion do
|
18
|
-
assert_perform_constant_number_of_queries(populate: populate) do
|
19
|
-
Post.find_each { |p| p.user.name }
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
assert_match "Expected to make the same number of queries", e.message
|
24
|
-
assert_match "3 for N=2", e.message
|
25
|
-
assert_match "4 for N=3", e.message
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_no_n_plus_one_error_with_scale_factors
|
29
|
-
populate = ->(n) { create_list(:post, n) }
|
30
|
-
|
31
|
-
assert_perform_constant_number_of_queries(
|
32
|
-
populate: populate,
|
33
|
-
scale_factors: [1, 1]
|
34
|
-
) do
|
35
|
-
Post.find_each { |p| p.user.name }
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_no_n_plus_one_error_with_matching
|
40
|
-
populate = ->(n) { create_list(:post, n) }
|
41
|
-
|
42
|
-
assert_perform_constant_number_of_queries(
|
43
|
-
populate: populate,
|
44
|
-
matching: /posts/
|
45
|
-
) do
|
46
|
-
Post.find_each { |p| p.user.name }
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
def populate(n)
|
51
|
-
create_list(:post, n)
|
52
|
-
end
|
53
|
-
|
54
|
-
def test_fallback_to_populate_method
|
55
|
-
e = assert_raises Minitest::Assertion do
|
56
|
-
assert_perform_constant_number_of_queries do
|
57
|
-
Post.find_each { |p| p.user.name }
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
assert_match "Expected to make the same number of queries", e.message
|
62
|
-
end
|
63
|
-
end
|
data/tests/test_helper.rb
DELETED
@@ -1,32 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "minitest/autorun"
|
4
|
-
require "minitest/pride"
|
5
|
-
|
6
|
-
$LOAD_PATH << File.expand_path('../lib', __dir__)
|
7
|
-
Thread.abort_on_exception = true
|
8
|
-
|
9
|
-
require "n_plus_one_control/minitest"
|
10
|
-
require "benchmark"
|
11
|
-
require "active_record"
|
12
|
-
require "factory_girl"
|
13
|
-
require "pry-byebug"
|
14
|
-
|
15
|
-
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
|
16
|
-
|
17
|
-
Dir["#{File.dirname(__FILE__)}/../spec/support/**/*.rb"].each { |f| require f }
|
18
|
-
|
19
|
-
module TransactionalTests
|
20
|
-
def setup
|
21
|
-
ActiveRecord::Base.connection.begin_transaction(joinable: false)
|
22
|
-
super
|
23
|
-
end
|
24
|
-
|
25
|
-
def teardown
|
26
|
-
super
|
27
|
-
ActiveRecord::Base.connection.rollback_transaction
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
Minitest::Test.prepend TransactionalTests
|
32
|
-
Minitest::Test.include FactoryGirl::Syntax::Methods
|