rspec-activerecord-formatter 0.1.0 → 0.2.0
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 +5 -5
- data/.gitignore +2 -0
- data/.rspec +2 -0
- data/.simplecov +10 -0
- data/.travis.yml +8 -0
- data/README.md +10 -4
- data/lib/rspec-activerecord-formatter.rb +1 -0
- data/lib/rspec/activerecord/formatter.rb +69 -3
- data/lib/rspec/activerecord/{formatter → helpers}/collector.rb +1 -1
- data/rspec-activerecord-formatter.gemspec +3 -3
- data/spec/collector_spec.rb +2 -0
- data/spec/formatter_spec.rb +2 -0
- data/spec/spec_helper.rb +54 -0
- metadata +34 -15
- data/lib/rspec/activerecord/formatter/formatter.rb +0 -71
- data/lib/rspec/activerecord/formatter/version.rb +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3c8cb5d2c352d6067bb7b6a1005ce9d80f257ef818e94da07b03530e705d5934
|
4
|
+
data.tar.gz: a21d87f0badc6b92de0906f3bc7782b6c13dbe89ee40b59ec1b07bf2a40ca1f1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d31c6f727a9c54acde509ce7f8b66185eedeb7825ff18af6b7da27f583ed460e38fafcb17a65bd66b0ceadef2b5a2a2db9cdd7a6821d199568e4b2170a81f23f
|
7
|
+
data.tar.gz: 1265045f094776141c99699dc6f35514de052cb60c7c40893808bb6ebe4ace017c2244c55d3681adb16c3cc7472dcd197bd3e41566ee6cd17ad9a4e102cf556e
|
data/.gitignore
CHANGED
data/.rspec
ADDED
data/.simplecov
ADDED
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -22,14 +22,18 @@ Normal bundle stuff.
|
|
22
22
|
Usage
|
23
23
|
------------
|
24
24
|
|
25
|
-
The current answer
|
25
|
+
The current answer is to change your `.rspec` initializer to include the following:
|
26
26
|
|
27
|
-
|
28
|
-
|
27
|
+
--require rails_helper
|
28
|
+
--format ActiveRecordFormatter
|
29
29
|
|
30
30
|
We have to include the rails_helper so that ActiveRecord is loaded prior to trying to load the
|
31
31
|
formatter. That way, we can hook into AR creations.
|
32
32
|
|
33
|
+
You can also run the formatter as a one-off option to rspec:
|
34
|
+
|
35
|
+
rspec path/to/example_spec.rb --require rails_helper --format ActiveRecordFormatter
|
36
|
+
|
33
37
|
Once you set the formatter, you should now see the number of objects created and total queries
|
34
38
|
for each of your tests:
|
35
39
|
|
@@ -43,8 +47,10 @@ Next Steps
|
|
43
47
|
------------
|
44
48
|
* The method I was using to count AR objects doesn't work well with DatabaseCleaner when not explicitly wiring the library into `before` blocks.
|
45
49
|
I'd like to be able to go back to a method other than scanning for `INSERT INTO` strings.
|
46
|
-
* Configuration, especially
|
50
|
+
* Configuration, especially formatting the output to optionally outdent the counts.
|
47
51
|
* Add a `--profile`-like behavior to output the most offending tests.
|
52
|
+
* The current dependency versions are a vague guess. They can and should clearly be more lenient.
|
53
|
+
* I dunno, tests.
|
48
54
|
|
49
55
|
Contributing
|
50
56
|
------------
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'rspec/activerecord/formatter'
|
@@ -1,5 +1,71 @@
|
|
1
|
-
require_relative "
|
2
|
-
require_relative "formatter/formatter"
|
1
|
+
require_relative "helpers/collector"
|
3
2
|
|
4
|
-
|
3
|
+
class ActiveRecordFormatter < ::RSpec::Core::Formatters::DocumentationFormatter
|
4
|
+
attr_reader :collector, :colorizer, :configuration
|
5
|
+
|
6
|
+
::RSpec::Core::Formatters.register self, :start, :dump_summary,
|
7
|
+
:example_started, :example_passed, :example_failed
|
8
|
+
|
9
|
+
def initialize(output)
|
10
|
+
super
|
11
|
+
|
12
|
+
@colorizer = ::RSpec::Core::Formatters::ConsoleCodes
|
13
|
+
@collector = ActiveRecordFormatterHelpers::Collector.new
|
14
|
+
end
|
15
|
+
|
16
|
+
def start(start_notification)
|
17
|
+
output.puts "Recording and reporting ActiveRecord select and creation counts."
|
18
|
+
end
|
19
|
+
|
20
|
+
def example_started(example)
|
21
|
+
collector.reset_example
|
22
|
+
end
|
23
|
+
|
24
|
+
def dump_summary(summary)
|
25
|
+
formatted = "\nFinished in #{summary.formatted_duration} " \
|
26
|
+
"(files took #{summary.formatted_load_time} to load)\n" \
|
27
|
+
"#{colorized_expanded_totals(summary)}\n"
|
28
|
+
|
29
|
+
unless summary.failed_examples.empty?
|
30
|
+
formatted << summary.colorized_rerun_commands(colorizer) << "\n"
|
31
|
+
end
|
32
|
+
|
33
|
+
output.puts formatted
|
34
|
+
end
|
35
|
+
|
36
|
+
protected
|
37
|
+
|
38
|
+
def passed_output(example)
|
39
|
+
"#{current_indentation}#{example_counts}" +
|
40
|
+
colorizer.wrap(example.description.strip, :success)
|
41
|
+
end
|
42
|
+
|
43
|
+
def failure_output(example)
|
44
|
+
"#{current_indentation}#{example_counts}" +
|
45
|
+
colorizer.wrap("#{example.description.strip} (FAILED - #{next_failure_index})", :failure)
|
46
|
+
end
|
47
|
+
|
48
|
+
def example_counts(suffix: " ")
|
49
|
+
"(%02d, %02d)#{suffix}" % [collector.objects_count, collector.query_count]
|
50
|
+
end
|
51
|
+
|
52
|
+
def colorized_expanded_totals(summary)
|
53
|
+
if summary.failure_count > 0
|
54
|
+
colorizer.wrap(expanded_totals_line(summary), RSpec.configuration.failure_color)
|
55
|
+
elsif summary.pending_count > 0
|
56
|
+
colorizer.wrap(expanded_totals_line(summary), RSpec.configuration.pending_color)
|
57
|
+
else
|
58
|
+
colorizer.wrap(expanded_totals_line(summary), RSpec.configuration.success_color)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def expanded_totals_line(summary)
|
63
|
+
summary_text = ::RSpec::Core::Formatters::Helpers.pluralize(summary.example_count, "example")
|
64
|
+
summary_text << ", " << ::RSpec::Core::Formatters::Helpers.pluralize(summary.failure_count, "failure")
|
65
|
+
summary_text << ", #{summary.pending_count} pending" if summary.pending_count > 0
|
66
|
+
summary_text << ", #{collector.total_objects} AR objects"
|
67
|
+
summary_text << ", #{collector.total_queries} AR queries"
|
68
|
+
|
69
|
+
summary_text
|
70
|
+
end
|
5
71
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'active_support/notifications'
|
2
2
|
|
3
|
-
module
|
3
|
+
module ActiveRecordFormatterHelpers
|
4
4
|
class Collector
|
5
5
|
attr_reader :query_count, :objects_count, :total_queries, :total_objects
|
6
6
|
SKIP_QUERIES = ["SELECT tablename FROM pg_tables", "select sum(ct) from (select count(*) ct from"]
|
@@ -3,12 +3,10 @@
|
|
3
3
|
lib_dir = File.join(File.dirname(__FILE__),'lib')
|
4
4
|
$LOAD_PATH << lib_dir unless $LOAD_PATH.include?(lib_dir)
|
5
5
|
|
6
|
-
require 'rspec/activerecord/formatter/version'
|
7
|
-
|
8
6
|
Gem::Specification.new do |gem|
|
9
7
|
|
10
8
|
gem.name = "rspec-activerecord-formatter"
|
11
|
-
gem.version =
|
9
|
+
gem.version = "0.2.0"
|
12
10
|
|
13
11
|
gem.summary = "Adds object creations and queries to Rspec output."
|
14
12
|
gem.description = "Creates a new formatter for ActiveRecord that can help you diagnose performance issues in RSpec"
|
@@ -31,4 +29,6 @@ Gem::Specification.new do |gem|
|
|
31
29
|
gem.add_dependency "bundler", "~> 1.9"
|
32
30
|
gem.add_dependency "activesupport", ">= 4.0"
|
33
31
|
gem.add_dependency "rspec", "~> 3.4"
|
32
|
+
|
33
|
+
gem.add_development_dependency "coveralls"
|
34
34
|
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
|
3
|
+
require 'rspec/activerecord/formatter'
|
4
|
+
|
5
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
6
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
7
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
8
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
9
|
+
# files.
|
10
|
+
#
|
11
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
12
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
13
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
14
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
15
|
+
# a separate helper file that requires the additional dependencies and performs
|
16
|
+
# the additional setup, and require it from the spec files that actually need
|
17
|
+
# it.
|
18
|
+
#
|
19
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
20
|
+
# users commonly want.
|
21
|
+
#
|
22
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
23
|
+
|
24
|
+
RSpec.configure do |config|
|
25
|
+
# This allows you to limit a spec run to individual examples or groups
|
26
|
+
# you care about by tagging them with `:focus` metadata. When nothing
|
27
|
+
# is tagged with `:focus`, all examples get run. RSpec also provides
|
28
|
+
# aliases for `it`, `describe`, and `context` that include `:focus`
|
29
|
+
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
|
30
|
+
config.filter_run_when_matching :focus
|
31
|
+
|
32
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
33
|
+
# recommended. For more details, see:
|
34
|
+
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
|
35
|
+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
36
|
+
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
|
37
|
+
config.disable_monkey_patching!
|
38
|
+
|
39
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
40
|
+
# be too noisy due to issues in dependencies.
|
41
|
+
config.warnings = true
|
42
|
+
|
43
|
+
# Run specs in random order to surface order dependencies. If you find an
|
44
|
+
# order dependency and want to debug it, you can fix the order by providing
|
45
|
+
# the seed, which is printed after each run.
|
46
|
+
# --seed 1234
|
47
|
+
config.order = :random
|
48
|
+
|
49
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
50
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
51
|
+
# test failures related to randomization by passing the same `--seed` value
|
52
|
+
# as the one that triggered the failure.
|
53
|
+
Kernel.srand config.seed
|
54
|
+
end
|
metadata
CHANGED
@@ -1,57 +1,71 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec-activerecord-formatter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joseph Mastey
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-02-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.9'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - ~>
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.9'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: activesupport
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '4.0'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '4.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - ~>
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '3.4'
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - ~>
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.4'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: coveralls
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
55
69
|
description: Creates a new formatter for ActiveRecord that can help you diagnose performance
|
56
70
|
issues in RSpec
|
57
71
|
email: jmmastey@gmail.com
|
@@ -61,18 +75,23 @@ extra_rdoc_files:
|
|
61
75
|
- CODE_OF_CONDUCT.md
|
62
76
|
- README.md
|
63
77
|
files:
|
64
|
-
- .gitignore
|
78
|
+
- ".gitignore"
|
79
|
+
- ".rspec"
|
80
|
+
- ".simplecov"
|
81
|
+
- ".travis.yml"
|
65
82
|
- CODE_OF_CONDUCT.md
|
66
83
|
- Gemfile
|
67
84
|
- MIT-LICENSE
|
68
85
|
- README.md
|
69
86
|
- doc/images/demo_1.png
|
70
87
|
- doc/images/demo_2.png
|
88
|
+
- lib/rspec-activerecord-formatter.rb
|
71
89
|
- lib/rspec/activerecord/formatter.rb
|
72
|
-
- lib/rspec/activerecord/
|
73
|
-
- lib/rspec/activerecord/formatter/formatter.rb
|
74
|
-
- lib/rspec/activerecord/formatter/version.rb
|
90
|
+
- lib/rspec/activerecord/helpers/collector.rb
|
75
91
|
- rspec-activerecord-formatter.gemspec
|
92
|
+
- spec/collector_spec.rb
|
93
|
+
- spec/formatter_spec.rb
|
94
|
+
- spec/spec_helper.rb
|
76
95
|
homepage: http://github.com/jmmastey/rspec-activerecord-formatter
|
77
96
|
licenses:
|
78
97
|
- MIT
|
@@ -83,17 +102,17 @@ require_paths:
|
|
83
102
|
- lib
|
84
103
|
required_ruby_version: !ruby/object:Gem::Requirement
|
85
104
|
requirements:
|
86
|
-
- -
|
105
|
+
- - ">="
|
87
106
|
- !ruby/object:Gem::Version
|
88
107
|
version: '0'
|
89
108
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
109
|
requirements:
|
91
|
-
- -
|
110
|
+
- - ">="
|
92
111
|
- !ruby/object:Gem::Version
|
93
112
|
version: '0'
|
94
113
|
requirements: []
|
95
114
|
rubyforge_project:
|
96
|
-
rubygems_version: 2.
|
115
|
+
rubygems_version: 2.7.6
|
97
116
|
signing_key:
|
98
117
|
specification_version: 4
|
99
118
|
summary: Adds object creations and queries to Rspec output.
|
@@ -1,71 +0,0 @@
|
|
1
|
-
module ActiveRecordFormatter
|
2
|
-
class Formatter < ::RSpec::Core::Formatters::DocumentationFormatter
|
3
|
-
attr_reader :collector, :colorizer, :configuration
|
4
|
-
|
5
|
-
::RSpec::Core::Formatters.register self, :start, :dump_summary,
|
6
|
-
:example_started, :example_passed, :example_failed
|
7
|
-
|
8
|
-
def initialize(output)
|
9
|
-
super
|
10
|
-
|
11
|
-
@colorizer = ::RSpec::Core::Formatters::ConsoleCodes
|
12
|
-
@collector = ActiveRecordFormatter::Collector.new
|
13
|
-
end
|
14
|
-
|
15
|
-
def start(start_notification)
|
16
|
-
output.puts "Recording and reporting ActiveRecord select and creation counts."
|
17
|
-
end
|
18
|
-
|
19
|
-
def example_started(example)
|
20
|
-
collector.reset_example
|
21
|
-
end
|
22
|
-
|
23
|
-
def dump_summary(summary)
|
24
|
-
formatted = "\nFinished in #{summary.formatted_duration} " \
|
25
|
-
"(files took #{summary.formatted_load_time} to load)\n" \
|
26
|
-
"#{colorized_expanded_totals(summary)}\n"
|
27
|
-
|
28
|
-
unless summary.failed_examples.empty?
|
29
|
-
formatted << summary.colorized_rerun_commands(colorizer) << "\n"
|
30
|
-
end
|
31
|
-
|
32
|
-
output.puts formatted
|
33
|
-
end
|
34
|
-
|
35
|
-
protected
|
36
|
-
|
37
|
-
def passed_output(example)
|
38
|
-
"#{current_indentation}#{example_counts}" +
|
39
|
-
colorizer.wrap(example.description.strip, :success)
|
40
|
-
end
|
41
|
-
|
42
|
-
def failure_output(example)
|
43
|
-
"#{current_indentation}#{example_counts}" +
|
44
|
-
colorizer.wrap("#{example.description.strip} (FAILED - #{next_failure_index})", :failure)
|
45
|
-
end
|
46
|
-
|
47
|
-
def example_counts(suffix: " ")
|
48
|
-
"(%02d, %02d)#{suffix}" % [collector.objects_count, collector.query_count]
|
49
|
-
end
|
50
|
-
|
51
|
-
def colorized_expanded_totals(summary)
|
52
|
-
if summary.failure_count > 0
|
53
|
-
colorizer.wrap(expanded_totals_line(summary), RSpec.configuration.failure_color)
|
54
|
-
elsif summary.pending_count > 0
|
55
|
-
colorizer.wrap(expanded_totals_line(summary), RSpec.configuration.pending_color)
|
56
|
-
else
|
57
|
-
colorizer.wrap(expanded_totals_line(summary), RSpec.configuration.success_color)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
def expanded_totals_line(summary)
|
62
|
-
summary_text = ::RSpec::Core::Formatters::Helpers.pluralize(summary.example_count, "example")
|
63
|
-
summary_text << ", " << ::RSpec::Core::Formatters::Helpers.pluralize(summary.failure_count, "failure")
|
64
|
-
summary_text << ", #{summary.pending_count} pending" if summary.pending_count > 0
|
65
|
-
summary_text << ", #{collector.total_objects} AR objects"
|
66
|
-
summary_text << ", #{collector.total_queries} AR queries"
|
67
|
-
|
68
|
-
summary_text
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|