ar-query-matchers 0.4.0 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +15 -1
- data/lib/ar_query_matchers.rb +10 -3
- data/lib/ar_query_matchers/queries/load_counter.rb +1 -1
- data/lib/ar_query_matchers/queries/query_counter.rb +4 -3
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ab18e9a530a6577ba47fbc78341913bc83ec6c2976dfd3404be8819d346fcab8
|
4
|
+
data.tar.gz: 9801846663ecc57468f79a66a7c12f170f9ddb2fef79d35aa33c36910c8d9a8a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aa331847dc1130257f6af7cc567e636ccedc8c7cecbd90383606f4c17050ab7c05546b51aa3cea040f1416082e45d98c04182341a13fddcfbbb66148183b9c43
|
7
|
+
data.tar.gz: 4becc7972b419b4e55d0f5cf55292ec8dfad6d6a1804bf9315c4368e842ec8a6c98506028b1ae7df41d2736f8a80b67fb745f1c9bb9e98328092db745baf8619
|
data/CHANGELOG.md
CHANGED
@@ -6,6 +6,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
6
6
|
|
7
7
|
## [Unreleased]
|
8
8
|
|
9
|
+
## [0.5.2] - 2021-05-26
|
10
|
+
### Changed
|
11
|
+
- Update MODEL_SQL_PATTERN to allow for more granular matches; remove MODEL_LOAD_PATTERN
|
12
|
+
|
13
|
+
## [0.5.1] - 2020-11-19
|
14
|
+
### Changed
|
15
|
+
- Removes zero count expectations from hash before comparing
|
16
|
+
|
17
|
+
## [0.5.0] - 2020-07-23
|
18
|
+
### Changed
|
19
|
+
- Add time information to query counter
|
20
|
+
|
9
21
|
## [0.4.0] - 2020-07-20
|
10
22
|
### Changed
|
11
23
|
- Upgrade the Rails dependency to allow for Rails 6.1
|
@@ -23,7 +35,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
23
35
|
### Added
|
24
36
|
- First versions as a public ruby gem.
|
25
37
|
|
26
|
-
[Unreleased]: https://github.com/gusto/ar-query-matchers/compare/v0.
|
38
|
+
[Unreleased]: https://github.com/gusto/ar-query-matchers/compare/v0.5.1...HEAD
|
39
|
+
[0.5.1]: https://github.com/gusto/ar-query-matchers/releases/tag/v0.5.1
|
40
|
+
[0.5.0]: https://github.com/gusto/ar-query-matchers/releases/tag/v0.5.0
|
27
41
|
[0.4.0]: https://github.com/gusto/ar-query-matchers/releases/tag/v0.4.0
|
28
42
|
[0.3.0]: https://github.com/gusto/ar-query-matchers/releases/tag/v0.3.0
|
29
43
|
[0.2.0]: https://github.com/gusto/ar-query-matchers/releases/tag/v0.2.0
|
data/lib/ar_query_matchers.rb
CHANGED
@@ -3,9 +3,16 @@
|
|
3
3
|
require 'ar_query_matchers/queries/create_counter'
|
4
4
|
require 'ar_query_matchers/queries/load_counter'
|
5
5
|
require 'ar_query_matchers/queries/update_counter'
|
6
|
+
require 'bigdecimal'
|
6
7
|
|
7
8
|
module ArQueryMatchers
|
8
9
|
module ArQueryMatchers
|
10
|
+
class Utility
|
11
|
+
def self.remove_superfluous_expectations(expected)
|
12
|
+
expected.select { |_, v| v.positive? }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
9
16
|
module CreateModels
|
10
17
|
# The following will succeed:
|
11
18
|
# expect {
|
@@ -22,7 +29,7 @@ module ArQueryMatchers
|
|
22
29
|
|
23
30
|
match do |block|
|
24
31
|
@query_stats = Queries::CreateCounter.instrument(&block)
|
25
|
-
expected == @query_stats.query_counts
|
32
|
+
Utility.remove_superfluous_expectations(expected) == @query_stats.query_counts
|
26
33
|
end
|
27
34
|
|
28
35
|
def failure_text
|
@@ -82,7 +89,7 @@ module ArQueryMatchers
|
|
82
89
|
|
83
90
|
match do |block|
|
84
91
|
@query_stats = Queries::LoadCounter.instrument(&block)
|
85
|
-
expected == @query_stats.query_counts
|
92
|
+
Utility.remove_superfluous_expectations(expected) == @query_stats.query_counts
|
86
93
|
end
|
87
94
|
|
88
95
|
def failure_text
|
@@ -146,7 +153,7 @@ module ArQueryMatchers
|
|
146
153
|
|
147
154
|
match do |block|
|
148
155
|
@query_stats = Queries::UpdateCounter.instrument(&block)
|
149
|
-
expected == @query_stats.query_counts
|
156
|
+
Utility.remove_superfluous_expectations(expected) == @query_stats.query_counts
|
150
157
|
end
|
151
158
|
|
152
159
|
def failure_text
|
@@ -21,7 +21,7 @@ module ArQueryMatchers
|
|
21
21
|
|
22
22
|
# Matches unnamed SQL operations like the following:
|
23
23
|
# "SELECT COUNT(*) FROM `users` ..."
|
24
|
-
MODEL_SQL_PATTERN = /SELECT
|
24
|
+
MODEL_SQL_PATTERN = /SELECT (?:(?!SELECT).)* FROM [`"](?<table_name>[^`"]+)[`"]/.freeze
|
25
25
|
|
26
26
|
def filter_map(name, sql)
|
27
27
|
# First check for a `SELECT * FROM` query that ActiveRecord has
|
@@ -62,7 +62,7 @@ module ArQueryMatchers
|
|
62
62
|
# @param [block] block to instrument
|
63
63
|
# @return [QueryStats] stats about all the SQL queries executed during the block
|
64
64
|
def instrument(&block)
|
65
|
-
queries = Hash.new { |h, k| h[k] = { count: 0, lines: [] } }
|
65
|
+
queries = Hash.new { |h, k| h[k] = { count: 0, lines: [], time: BigDecimal(0) } }
|
66
66
|
ActiveSupport::Notifications.subscribed(to_proc(queries), 'sql.active_record', &block)
|
67
67
|
QueryStats.new(queries)
|
68
68
|
end
|
@@ -75,11 +75,11 @@ module ArQueryMatchers
|
|
75
75
|
private_constant :MARGINALIA_SQL_COMMENT_PATTERN
|
76
76
|
|
77
77
|
def to_proc(queries)
|
78
|
-
lambda do |_name,
|
78
|
+
lambda do |_name, start, finish, _message_id, payload|
|
79
79
|
return if payload[:cached]
|
80
80
|
|
81
81
|
# Given a `sql.active_record` event, figure out which model is being
|
82
|
-
# accessed. Some of the simpler queries have a :
|
82
|
+
# accessed. Some of the simpler queries have a :name key that makes this
|
83
83
|
# really easy. Others require parsing the SQL by hand.
|
84
84
|
model_name = @query_filter.filter_map(payload[:name] || '', payload[:sql] || '')&.model_name
|
85
85
|
|
@@ -87,6 +87,7 @@ module ArQueryMatchers
|
|
87
87
|
comment = payload[:sql].match(MARGINALIA_SQL_COMMENT_PATTERN)
|
88
88
|
queries[model_name][:lines] << comment[:line] if comment
|
89
89
|
queries[model_name][:count] += 1
|
90
|
+
queries[model_name][:time] += (finish - start).round(6) # Round to microseconds
|
90
91
|
end
|
91
92
|
end
|
92
93
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ar-query-matchers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matan Zruya
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-06-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -190,7 +190,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
190
190
|
- !ruby/object:Gem::Version
|
191
191
|
version: '0'
|
192
192
|
requirements: []
|
193
|
-
rubygems_version: 3.0.3
|
193
|
+
rubygems_version: 3.0.3.1
|
194
194
|
signing_key:
|
195
195
|
specification_version: 4
|
196
196
|
summary: Ruby test matchers for instrumenting ActiveRecord query counts
|