Module: TestProf::FactoryDoctor
- Extended by:
- Logging
- Defined in:
- lib/test_prof/factory_doctor.rb,
lib/test_prof/factory_doctor/rspec.rb,
lib/test_prof/factory_doctor/factory_bot_patch.rb
Overview
FactoryDoctor is a tool that helps you identify tests that perform unnecessary database queries.
Defined Under Namespace
Modules: FactoryBotPatch Classes: RSpecListener, Result
Constant Summary collapse
- IGNORED_QUERIES_PATTERN =
%r{( pg_table| pg_attribute| pg_namespace| show\stables| pragma| sqlite_master/rollback| \ATRUNCATE TABLE| \AALTER TABLE| \ABEGIN| \ACOMMIT| \AROLLBACK| \ARELEASE| \ASAVEPOINT )}xi.freeze
Constants included from Logging
Class Attribute Summary collapse
-
.count ⇒ Object
readonly
Returns the value of attribute count.
-
.event ⇒ Object
readonly
Returns the value of attribute event.
-
.queries_count ⇒ Object
readonly
Returns the value of attribute queries_count.
-
.time ⇒ Object
readonly
Returns the value of attribute time.
Class Method Summary collapse
-
.ignore ⇒ Object
Do not analyze code within the block.
- .ignore! ⇒ Object
- .ignore? ⇒ Boolean
-
.init(event = 'sql.active_record') ⇒ Object
Patch factory lib, init counters.
- .result ⇒ Object
- .stamp? ⇒ Boolean
- .start ⇒ Object
- .stop ⇒ Object
- .within_factory(strategy) ⇒ Object
Methods included from Logging
Class Attribute Details
.count ⇒ Object (readonly)
Returns the value of attribute count.
44 45 46 |
# File 'lib/test_prof/factory_doctor.rb', line 44 def count @count end |
.event ⇒ Object (readonly)
Returns the value of attribute event.
43 44 45 |
# File 'lib/test_prof/factory_doctor.rb', line 43 def event @event end |
.queries_count ⇒ Object (readonly)
Returns the value of attribute queries_count.
44 45 46 |
# File 'lib/test_prof/factory_doctor.rb', line 44 def queries_count @queries_count end |
.time ⇒ Object (readonly)
Returns the value of attribute time.
44 45 46 |
# File 'lib/test_prof/factory_doctor.rb', line 44 def time @time end |
Class Method Details
.ignore ⇒ Object
Do not analyze code within the block
82 83 84 85 86 87 88 |
# File 'lib/test_prof/factory_doctor.rb', line 82 def ignore @ignored = true res = yield ensure @ignored = false res end |
.ignore! ⇒ Object
90 91 92 |
# File 'lib/test_prof/factory_doctor.rb', line 90 def ignore! @ignored = true end |
.ignore? ⇒ Boolean
94 95 96 |
# File 'lib/test_prof/factory_doctor.rb', line 94 def ignore? @ignored == true end |
.init(event = 'sql.active_record') ⇒ Object
Patch factory lib, init counters
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/test_prof/factory_doctor.rb', line 47 def init(event = 'sql.active_record') @event = event reset! log :info, "FactoryDoctor enabled" # Monkey-patch FactoryBot / FactoryGirl TestProf::FactoryBot::FactoryRunner.prepend(FactoryBotPatch) if defined?(TestProf::FactoryBot) subscribe! @stamp = ENV['FDOC_STAMP'] RSpecStamp.config. = @stamp if stamp? end |
.result ⇒ Object
77 78 79 |
# File 'lib/test_prof/factory_doctor.rb', line 77 def result Result.new(count, time, queries_count) end |
.stamp? ⇒ Boolean
64 65 66 |
# File 'lib/test_prof/factory_doctor.rb', line 64 def stamp? !@stamp.nil? end |
.start ⇒ Object
68 69 70 71 |
# File 'lib/test_prof/factory_doctor.rb', line 68 def start reset! @running = true end |
.stop ⇒ Object
73 74 75 |
# File 'lib/test_prof/factory_doctor.rb', line 73 def stop @running = false end |
.within_factory(strategy) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/test_prof/factory_doctor.rb', line 98 def within_factory(strategy) return yield if ignore? || !running? || (strategy != :create) begin ts = TestProf.now if @depth.zero? @depth += 1 @count += 1 yield ensure @depth -= 1 @time += (TestProf.now - ts) if @depth.zero? end end |