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

Constants included from Logging

Logging::COLORS

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Logging

build_log_msg, colorize, log

Class Attribute Details

.countObject (readonly)

Returns the value of attribute count.



43
44
45
# File 'lib/test_prof/factory_doctor.rb', line 43

def count
  @count
end

.eventObject (readonly)

Returns the value of attribute event.



42
43
44
# File 'lib/test_prof/factory_doctor.rb', line 42

def event
  @event
end

.queries_countObject (readonly)

Returns the value of attribute queries_count.



43
44
45
# File 'lib/test_prof/factory_doctor.rb', line 43

def queries_count
  @queries_count
end

.timeObject (readonly)

Returns the value of attribute time.



43
44
45
# File 'lib/test_prof/factory_doctor.rb', line 43

def time
  @time
end

Class Method Details

.ignoreObject

Do not analyze code within the block



81
82
83
84
85
86
87
# File 'lib/test_prof/factory_doctor.rb', line 81

def ignore
  @ignored = true
  res = yield
ensure
  @ignored = false
  res
end

.init(event = 'sql.active_record') ⇒ Object

Patch factory lib, init counters



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/test_prof/factory_doctor.rb', line 46

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.tags = @stamp if stamp?
end

.resultObject



76
77
78
# File 'lib/test_prof/factory_doctor.rb', line 76

def result
  Result.new(count, time, queries_count)
end

.stamp?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/test_prof/factory_doctor.rb', line 63

def stamp?
  !@stamp.nil?
end

.startObject



67
68
69
70
# File 'lib/test_prof/factory_doctor.rb', line 67

def start
  reset!
  @running = true
end

.stopObject



72
73
74
# File 'lib/test_prof/factory_doctor.rb', line 72

def stop
  @running = false
end

.within_factory(strategy) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/test_prof/factory_doctor.rb', line 89

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