Skip to content

Commit c8341a1

Browse files
authored
Updated with cleaning of expectation counts that are zero (#24)
* Updated with cleaning of expectation counts that are zero * Updated change log * Incorporated commentary from PR review * Updated with some fixes from build errors * Updated with another fix from build errors * Adjusted rubocop rules and regenerated todo YAML file
1 parent d47619a commit c8341a1

File tree

7 files changed

+64
-24
lines changed

7 files changed

+64
-24
lines changed

.rubocop.yml

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
inherit_from: .rubocop_todo.yml
22
AllCops:
33
Exclude:
4-
- 'gemfiles/**/*'
4+
- 'gemfiles/**/*'
5+
6+
Metrics/AbcSize:
7+
Enabled: false
8+
9+
Metrics/BlockLength:
10+
Enabled: false
11+
12+
Lint/MixedRegexpCaptureTypes:
13+
Enabled: false
14+
15+
Gemspec/RequiredRubyVersion:
16+
Enabled: false
17+
18+
Lint/ConstantDefinitionInBlock:
19+
Enabled: false

.rubocop_todo.yml

+6-16
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,25 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2019-07-27 09:03:07 -0700 using RuboCop version 0.73.0.
3+
# on 2020-11-23 21:52:22 UTC using RuboCop version 1.3.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 2
10-
Metrics/AbcSize:
11-
Max: 20
12-
13-
# Offense count: 3
14-
# Configuration parameters: CountComments, ExcludedMethods.
15-
# ExcludedMethods: refine
16-
Metrics/BlockLength:
17-
Max: 121
18-
199
# Offense count: 1
20-
# Configuration parameters: CountComments, ExcludedMethods.
10+
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods.
2111
Metrics/MethodLength:
2212
Max: 11
2313

2414
# Offense count: 4
25-
# Configuration parameters: EnforcedStyle.
15+
# Configuration parameters: EnforcedStyle, CheckMethodNames, CheckSymbols, AllowedIdentifiers.
2616
# SupportedStyles: snake_case, normalcase, non_integer
2717
Naming/VariableNumber:
2818
Exclude:
2919
- 'spec/ar_query_matchers/queries/create_counter_spec.rb'
3020
- 'spec/ar_query_matchers/queries/update_counter_spec.rb'
3121

32-
# Offense count: 8
22+
# Offense count: 9
3323
Style/Documentation:
3424
Exclude:
3525
- 'spec/**/*'
@@ -52,9 +42,9 @@ Style/IfUnlessModifier:
5242
Exclude:
5343
- 'spec/ar_query_matchers/mock_data_model.rb'
5444

55-
# Offense count: 52
45+
# Offense count: 7
5646
# Cop supports --auto-correct.
5747
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
5848
# URISchemes: http, https
59-
Metrics/LineLength:
49+
Layout/LineLength:
6050
Max: 282

CHANGELOG.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [0.5.1] - 2020-11-19
10+
### Changed
11+
- Removes zero count expectations from hash before comparing
12+
913
## [0.5.0] - 2020-07-23
1014
### Changed
1115
- Add time information to query counter
@@ -27,7 +31,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2731
### Added
2832
- First versions as a public ruby gem.
2933

30-
[Unreleased]: https://github.com/gusto/ar-query-matchers/compare/v0.5.0...HEAD
34+
[Unreleased]: https://github.com/gusto/ar-query-matchers/compare/v0.5.1...HEAD
35+
[0.5.1]: https://github.com/gusto/ar-query-matchers/releases/tag/v0.5.1
3136
[0.5.0]: https://github.com/gusto/ar-query-matchers/releases/tag/v0.5.0
3237
[0.4.0]: https://github.com/gusto/ar-query-matchers/releases/tag/v0.4.0
3338
[0.3.0]: https://github.com/gusto/ar-query-matchers/releases/tag/v0.3.0

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.5.0
1+
0.5.1

lib/ar_query_matchers.rb

+9-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77

88
module ArQueryMatchers
99
module ArQueryMatchers
10+
class Utility
11+
def self.remove_superfluous_expectations(expected)
12+
expected.select { |_, v| v.positive? }
13+
end
14+
end
15+
1016
module CreateModels
1117
# The following will succeed:
1218
# expect {
@@ -23,7 +29,7 @@ module CreateModels
2329

2430
match do |block|
2531
@query_stats = Queries::CreateCounter.instrument(&block)
26-
expected == @query_stats.query_counts
32+
Utility.remove_superfluous_expectations(expected) == @query_stats.query_counts
2733
end
2834

2935
def failure_text
@@ -83,7 +89,7 @@ module LoadModels
8389

8490
match do |block|
8591
@query_stats = Queries::LoadCounter.instrument(&block)
86-
expected == @query_stats.query_counts
92+
Utility.remove_superfluous_expectations(expected) == @query_stats.query_counts
8793
end
8894

8995
def failure_text
@@ -147,7 +153,7 @@ class UpdateModels
147153

148154
match do |block|
149155
@query_stats = Queries::UpdateCounter.instrument(&block)
150-
expected == @query_stats.query_counts
156+
Utility.remove_superfluous_expectations(expected) == @query_stats.query_counts
151157
end
152158

153159
def failure_text

lib/ar_query_matchers/queries/query_counter.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def to_proc(queries)
7979
return if payload[:cached]
8080

8181
# Given a `sql.active_record` event, figure out which model is being
82-
# accessed. Some of the simpler queries have a :ame key that makes this
82+
# accessed. Some of the simpler queries have a :name key that makes this
8383
# really easy. Others require parsing the SQL by hand.
8484
model_name = @query_filter.filter_map(payload[:name] || '', payload[:sql] || '')&.model_name
8585

spec/ar_query_matchers/query_matchers_spec.rb

+25-1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ def updates(amount)
9090
end.to only_update_models('MockUser' => 5)
9191
end
9292

93+
it 'succeeds after cleaning the 0 count expectation' do
94+
expect do
95+
updates(5)
96+
loads(1)
97+
creates(1)
98+
end.to only_update_models({ 'MockPost' => 0, 'MockUser' => 5 })
99+
end
100+
93101
it 'fails' do
94102
expect do
95103
expect do
@@ -111,6 +119,14 @@ def updates(amount)
111119
end.to only_create_models('MockUser' => 5)
112120
end
113121

122+
it 'succeeds after cleaning the 0 count expectation' do
123+
expect do
124+
updates(1)
125+
loads(1)
126+
creates(5)
127+
end.to only_create_models({ 'MockPost' => 0, 'MockUser' => 5 })
128+
end
129+
114130
it 'fails' do
115131
expect do
116132
expect do
@@ -129,7 +145,15 @@ def updates(amount)
129145
updates(1)
130146
loads(5)
131147
creates(1)
132-
end.to only_load_models('MockUser' => 5)
148+
end.to only_load_models({ 'MockUser' => 5 })
149+
end
150+
151+
it 'succeeds after cleaning the 0 count expectation' do
152+
expect do
153+
updates(1)
154+
loads(5)
155+
creates(1)
156+
end.to only_load_models({ 'MockPost' => 0, 'MockUser' => 5 })
133157
end
134158

135159
it 'fails' do

0 commit comments

Comments
 (0)