query_cop 0.1.0 → 0.1.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/.gitignore +3 -0
- data/Gemfile.lock +20 -0
- data/README.md +11 -1
- data/lib/query_cop.rb +2 -2
- data/lib/query_cop/active_record_adapter.rb +1 -0
- data/lib/query_cop/query_tracker.rb +25 -2
- data/lib/query_cop/rspec_helper.rb +10 -9
- data/lib/query_cop/version.rb +1 -1
- metadata +4 -4
- data/.DS_Store +0 -0
- data/lib/.DS_Store +0 -0
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6bd5d454c419341517523f0bca996b173c38ea9dc11d9ff1c190bd0c0b9166a9
|
|
4
|
+
data.tar.gz: 6dd0f4c6e5538efd28b8a811e70a7261deac54b887739828dcd47e221dfd21ee
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d84c696db0328f3b3a4217301b1647eea269462661652f3ad65e78b5711e4e3ff18b4dbc573cc85e04f0c43af34e00b4c973ee2492f5e30fed129f595666c0af
|
|
7
|
+
data.tar.gz: 28441cc83f21b47cfb3f711dbcb0ff5ad6fa00f198bcca8f1c974f3c37ef41483fdbee2007d733df6eb9142b10d4b0c83fb0a1db2ac682e79be0ed7621871bff
|
data/.gitignore
ADDED
data/Gemfile.lock
ADDED
data/README.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# QueryCop
|
|
2
2
|
|
|
3
|
+
# Description
|
|
4
|
+
|
|
5
|
+
A query tracker for ActiveRecord and Rspec that helps Rails developers be aware of how many database lookups their code is generating.
|
|
6
|
+
|
|
7
|
+
By setting the maximum allowed query count in the test, it also helps the query count become explicit and have more robust performance-aware regression tests.
|
|
8
|
+
|
|
9
|
+
The gem currently only works with sqlite3, psql and mysql.
|
|
10
|
+
|
|
3
11
|
## Installation
|
|
4
12
|
|
|
5
13
|
Add this line to your application's Gemfile:
|
|
@@ -18,7 +26,7 @@ Or install it yourself as:
|
|
|
18
26
|
|
|
19
27
|
## Usage
|
|
20
28
|
|
|
21
|
-
|
|
29
|
+
|
|
22
30
|
In any of your rspec examples, you can add
|
|
23
31
|
```
|
|
24
32
|
with_allowed_max_query_count(#{max_allowed_query_count}) do
|
|
@@ -42,6 +50,8 @@ Failure/Error:
|
|
|
42
50
|
SELECT "license_plates".* FROM "license_plates" WHERE "license_plates"."car_id" = $1
|
|
43
51
|
```
|
|
44
52
|
|
|
53
|
+
Within the `with_allowed_max_query_count` block, you can also directly access the current list of queries by calling `QueryCop::QueryTracker.queries`
|
|
54
|
+
|
|
45
55
|
## Development
|
|
46
56
|
|
|
47
57
|
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/query_cop.rb
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
require_relative "query_cop/query_tracker"
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
require_relative "query_cop/active_record_adapter"
|
|
3
|
+
require_relative "query_cop/rspec_helper"
|
|
4
4
|
|
|
5
5
|
ActiveRecord::ConnectionAdapters::AbstractAdapter.send(:prepend, QueryCop::ActiveRecordAdapter) if Rails.env.test?
|
|
6
6
|
|
|
@@ -1,27 +1,50 @@
|
|
|
1
1
|
module QueryCop
|
|
2
2
|
class QueryTracker
|
|
3
3
|
class << self
|
|
4
|
-
attr_accessor :queries
|
|
4
|
+
attr_accessor :queries, :enabled
|
|
5
5
|
|
|
6
6
|
def reset
|
|
7
7
|
self.queries = []
|
|
8
8
|
end
|
|
9
9
|
|
|
10
|
+
def enable
|
|
11
|
+
self.enabled = true
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def disable
|
|
15
|
+
self.enabled = false
|
|
16
|
+
end
|
|
17
|
+
|
|
10
18
|
def track(log)
|
|
11
|
-
queries.push(log) if query?(log)
|
|
19
|
+
queries.push(log) if query?(log) && enabled?
|
|
12
20
|
end
|
|
13
21
|
|
|
14
22
|
def query_count
|
|
15
23
|
queries.size
|
|
16
24
|
end
|
|
17
25
|
|
|
26
|
+
def clean_wrap
|
|
27
|
+
enable
|
|
28
|
+
reset
|
|
29
|
+
|
|
30
|
+
yield
|
|
31
|
+
|
|
32
|
+
reset
|
|
33
|
+
disable
|
|
34
|
+
end
|
|
35
|
+
|
|
18
36
|
private
|
|
19
37
|
|
|
20
38
|
def query?(log)
|
|
21
39
|
log =~ /^(select|create|update|delete|insert)\b/i
|
|
22
40
|
end
|
|
41
|
+
|
|
42
|
+
def enabled?
|
|
43
|
+
enabled == true
|
|
44
|
+
end
|
|
23
45
|
end
|
|
24
46
|
|
|
25
47
|
reset
|
|
48
|
+
disable
|
|
26
49
|
end
|
|
27
50
|
end
|
|
@@ -3,18 +3,19 @@ require_relative "active_record_adapter.rb"
|
|
|
3
3
|
module QueryCop
|
|
4
4
|
module RspecHelper
|
|
5
5
|
def with_allowed_max_query_count(max_count)
|
|
6
|
-
|
|
6
|
+
QueryTracker.clean_wrap do
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
yield
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
failure_message = <<-EOS
|
|
11
|
+
Allowed maximum query count: #{max_count}
|
|
12
|
+
Actual generated query count: #{QueryTracker.query_count}
|
|
13
|
+
Here is the list of generated queries:
|
|
14
|
+
#{QueryTracker.queries.join("\n")}
|
|
15
|
+
EOS
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
expect(QueryTracker.query_count).to be <= max_count, failure_message
|
|
18
|
+
end
|
|
18
19
|
end
|
|
19
20
|
end
|
|
20
21
|
end
|
data/lib/query_cop/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: query_cop
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Wai Phyo Maung
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2018-08-
|
|
11
|
+
date: 2018-08-20 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|
|
@@ -46,15 +46,15 @@ executables: []
|
|
|
46
46
|
extensions: []
|
|
47
47
|
extra_rdoc_files: []
|
|
48
48
|
files:
|
|
49
|
-
- ".
|
|
49
|
+
- ".gitignore"
|
|
50
50
|
- CODE_OF_CONDUCT.md
|
|
51
51
|
- Gemfile
|
|
52
|
+
- Gemfile.lock
|
|
52
53
|
- LICENSE.txt
|
|
53
54
|
- README.md
|
|
54
55
|
- Rakefile
|
|
55
56
|
- bin/console
|
|
56
57
|
- bin/setup
|
|
57
|
-
- lib/.DS_Store
|
|
58
58
|
- lib/query_cop.rb
|
|
59
59
|
- lib/query_cop/active_record_adapter.rb
|
|
60
60
|
- lib/query_cop/query_tracker.rb
|
data/.DS_Store
DELETED
|
Binary file
|
data/lib/.DS_Store
DELETED
|
Binary file
|