query_matchers 0.0.6 → 0.0.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/query_matchers/query_counter.rb +6 -1
- data/lib/query_matchers/version.rb +1 -1
- data/query_matchers.gemspec +3 -3
- data/spec/query_counter_spec.rb +26 -7
- data/spec/query_execution_matcher_spec.rb +9 -9
- metadata +15 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d28aef64bb82469c2a69de6dc16ce385aba2bd7d5d00f4fbef09445188ff62bb
|
4
|
+
data.tar.gz: 691c45802a5e3d2831dbde542e4117f7230831a2e5c92ba82b4e600d4d35b554
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 53023a711f67eb7e07ff5456549585bdae5f178fc696d6e1a5f337be536e79a48498e11259d87ff2068390a284d10385b9b31d71ce0367f53dd6b94dba032ddf
|
7
|
+
data.tar.gz: bbc6cb2cfcc9248ac45182578c5f07620db6f432131df12be3747d0bbe3c3b04a20490463d37f450c18813de8eed6dc6e7c1f182675fa9624822ed72ae8f016b
|
@@ -1,6 +1,7 @@
|
|
1
1
|
module QueryMatchers
|
2
2
|
class QueryCounter
|
3
3
|
OPERATIONS = %w(SELECT INSERT UPDATE DELETE)
|
4
|
+
RAILS5_INFORMATION_SCHEMA_REGEX = /^\s*SELECT.+FROM information_schema\./m
|
4
5
|
|
5
6
|
def initialize
|
6
7
|
@events = []
|
@@ -28,7 +29,11 @@ module QueryMatchers
|
|
28
29
|
end
|
29
30
|
|
30
31
|
def count_query?(sql)
|
31
|
-
OPERATIONS.any? {|op| sql.start_with?(op) }
|
32
|
+
OPERATIONS.any? {|op| sql.lstrip.start_with?(op) } && !ignore_query?(sql)
|
33
|
+
end
|
34
|
+
|
35
|
+
def ignore_query?(sql)
|
36
|
+
sql.match?(RAILS5_INFORMATION_SCHEMA_REGEX)
|
32
37
|
end
|
33
38
|
end
|
34
39
|
end
|
data/query_matchers.gemspec
CHANGED
@@ -17,9 +17,9 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
18
|
spec.require_paths = ["lib"]
|
19
19
|
|
20
|
-
spec.add_runtime_dependency "activesupport", ["> 3.0"
|
20
|
+
spec.add_runtime_dependency "activesupport", ["> 3.0"]
|
21
21
|
|
22
|
-
spec.add_development_dependency "bundler"
|
22
|
+
spec.add_development_dependency "bundler"
|
23
23
|
spec.add_development_dependency "rake"
|
24
|
-
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
25
|
end
|
data/spec/query_counter_spec.rb
CHANGED
@@ -8,44 +8,63 @@ describe QueryMatchers::QueryCounter do
|
|
8
8
|
query = "SELECT * FROM somewhere"
|
9
9
|
counter.execute!(sql_target(query))
|
10
10
|
|
11
|
-
counter.queries.
|
11
|
+
expect(counter.queries).to eq([query])
|
12
12
|
end
|
13
13
|
|
14
14
|
it "counts the number of queries performed in the target block" do
|
15
15
|
target = proc { 3.times { perform_sql("INSERT INTO jokes") } }
|
16
16
|
counter.execute!(target)
|
17
17
|
|
18
|
-
counter.query_count.
|
18
|
+
expect(counter.query_count).to eq(3)
|
19
19
|
end
|
20
20
|
|
21
21
|
it "counts INSERT queries" do
|
22
22
|
counter.execute!(sql_target("INSERT INTO jokes"))
|
23
23
|
|
24
|
-
counter.query_count.
|
24
|
+
expect(counter.query_count).to eq(1)
|
25
25
|
end
|
26
26
|
|
27
27
|
it "counts UPDATE queries" do
|
28
28
|
counter.execute!(sql_target("UPDATE mood SET laughing = 0"))
|
29
29
|
|
30
|
-
counter.query_count.
|
30
|
+
expect(counter.query_count).to eq(1)
|
31
31
|
end
|
32
32
|
|
33
33
|
it "counts DELETE queries" do
|
34
34
|
counter.execute!(sql_target("DELETE FROM goodwill"))
|
35
35
|
|
36
|
-
counter.query_count.
|
36
|
+
expect(counter.query_count).to eq(1)
|
37
37
|
end
|
38
38
|
|
39
39
|
it "counts SELECT queries" do
|
40
40
|
counter.execute!(sql_target("SELECT FROM inventory"))
|
41
41
|
|
42
|
-
counter.query_count.
|
42
|
+
expect(counter.query_count).to eq(1)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "counts queries with a bit of whitespace" do
|
46
|
+
counter.execute!(sql_target(" SELECT FROM inventory"))
|
47
|
+
|
48
|
+
expect(counter.query_count).to eq(1)
|
43
49
|
end
|
44
50
|
|
45
51
|
it "doesn't count any other type of query" do
|
46
52
|
counter.execute!(sql_target("BREAKDANCE"))
|
47
53
|
|
48
|
-
counter.query_count.
|
54
|
+
expect(counter.query_count).to eq(0)
|
55
|
+
end
|
56
|
+
|
57
|
+
it "ignores Rails 5's schema queries" do
|
58
|
+
counter.execute!(sql_target(<<-SQL))
|
59
|
+
SELECT column_name
|
60
|
+
FROM information_schema.key_column_usage
|
61
|
+
WHERE constraint_name = 'PRIMARY'
|
62
|
+
AND table_schema = DATABASE()
|
63
|
+
AND table_name = 'jokes'
|
64
|
+
ORDER BY ordinal_position
|
65
|
+
SQL
|
66
|
+
|
67
|
+
expect(counter.query_count).to eq(0)
|
49
68
|
end
|
50
69
|
|
51
70
|
def sql_target(sql)
|
@@ -7,22 +7,22 @@ describe QueryMatchers::QueryExecutionMatcher do
|
|
7
7
|
|
8
8
|
describe "#matches?" do
|
9
9
|
before do
|
10
|
-
counter.
|
10
|
+
allow(counter).to receive(:execute!)
|
11
11
|
end
|
12
12
|
|
13
13
|
it "executes the target" do
|
14
14
|
matcher.matches?(:whatever)
|
15
|
-
counter.
|
15
|
+
expect(counter).to have_received(:execute!).with(:whatever)
|
16
16
|
end
|
17
17
|
|
18
18
|
it "returns true if the number of queries performed matched the expectation" do
|
19
|
-
counter.
|
20
|
-
matcher.matches?(:whatever).
|
19
|
+
allow(counter).to receive(:query_count) { 4 }
|
20
|
+
expect(matcher.matches?(:whatever)).to eq(true)
|
21
21
|
end
|
22
22
|
|
23
23
|
it "returns false if the number of queries performed doesn't match the expectation" do
|
24
|
-
counter.
|
25
|
-
matcher.matches?(:whatever).
|
24
|
+
allow(counter).to receive(:query_count) { 3 }
|
25
|
+
expect(matcher.matches?(:whatever)).to eq(false)
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
@@ -31,10 +31,10 @@ describe QueryMatchers::QueryExecutionMatcher do
|
|
31
31
|
query1 = "SELECT FROM jokes WHERE puns > 3"
|
32
32
|
query2 = "DELETE FROM jokes WHERE inappropriate = 1"
|
33
33
|
|
34
|
-
counter.
|
35
|
-
counter.
|
34
|
+
allow(counter).to receive(:query_count) { 99 }
|
35
|
+
allow(counter).to receive(:queries) { [query1, query2] }
|
36
36
|
|
37
|
-
matcher.failure_message.
|
37
|
+
expect(matcher.failure_message).to eq <<-MESSAGE.strip_heredoc.chomp
|
38
38
|
expected block to execute 4 SQL queries, but executed 99:
|
39
39
|
|
40
40
|
- SELECT FROM jokes WHERE puns > 3
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: query_matchers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Schierbeck
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-12-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -17,9 +17,6 @@ dependencies:
|
|
17
17
|
- - ">"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '3.0'
|
20
|
-
- - "<"
|
21
|
-
- !ruby/object:Gem::Version
|
22
|
-
version: '5.0'
|
23
20
|
type: :runtime
|
24
21
|
prerelease: false
|
25
22
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -27,23 +24,20 @@ dependencies:
|
|
27
24
|
- - ">"
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '3.0'
|
30
|
-
- - "<"
|
31
|
-
- !ruby/object:Gem::Version
|
32
|
-
version: '5.0'
|
33
27
|
- !ruby/object:Gem::Dependency
|
34
28
|
name: bundler
|
35
29
|
requirement: !ruby/object:Gem::Requirement
|
36
30
|
requirements:
|
37
|
-
- - "
|
31
|
+
- - ">="
|
38
32
|
- !ruby/object:Gem::Version
|
39
|
-
version: '
|
33
|
+
version: '0'
|
40
34
|
type: :development
|
41
35
|
prerelease: false
|
42
36
|
version_requirements: !ruby/object:Gem::Requirement
|
43
37
|
requirements:
|
44
|
-
- - "
|
38
|
+
- - ">="
|
45
39
|
- !ruby/object:Gem::Version
|
46
|
-
version: '
|
40
|
+
version: '0'
|
47
41
|
- !ruby/object:Gem::Dependency
|
48
42
|
name: rake
|
49
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -62,17 +56,17 @@ dependencies:
|
|
62
56
|
name: rspec
|
63
57
|
requirement: !ruby/object:Gem::Requirement
|
64
58
|
requirements:
|
65
|
-
- - "
|
59
|
+
- - ">="
|
66
60
|
- !ruby/object:Gem::Version
|
67
|
-
version:
|
61
|
+
version: '0'
|
68
62
|
type: :development
|
69
63
|
prerelease: false
|
70
64
|
version_requirements: !ruby/object:Gem::Requirement
|
71
65
|
requirements:
|
72
|
-
- - "
|
66
|
+
- - ">="
|
73
67
|
- !ruby/object:Gem::Version
|
74
|
-
version:
|
75
|
-
description:
|
68
|
+
version: '0'
|
69
|
+
description:
|
76
70
|
email:
|
77
71
|
- dasch@zendesk.com
|
78
72
|
executables: []
|
@@ -95,7 +89,7 @@ homepage: https://github.com/dasch/query_matchers
|
|
95
89
|
licenses:
|
96
90
|
- MIT
|
97
91
|
metadata: {}
|
98
|
-
post_install_message:
|
92
|
+
post_install_message:
|
99
93
|
rdoc_options: []
|
100
94
|
require_paths:
|
101
95
|
- lib
|
@@ -110,12 +104,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
110
104
|
- !ruby/object:Gem::Version
|
111
105
|
version: '0'
|
112
106
|
requirements: []
|
113
|
-
|
114
|
-
|
115
|
-
signing_key:
|
107
|
+
rubygems_version: 3.2.2
|
108
|
+
signing_key:
|
116
109
|
specification_version: 4
|
117
110
|
summary: Match the number of queries performed in any block of code
|
118
111
|
test_files:
|
119
112
|
- spec/query_counter_spec.rb
|
120
113
|
- spec/query_execution_matcher_spec.rb
|
121
|
-
has_rdoc:
|