Module: QueryOwl::TestHelper

Defined in:
lib/query_owl/test_helper.rb

Defined Under Namespace

Classes: EventTypeMatcher

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.capture_eventsObject

Runs block with QueryOwl’s trackers active and returns detected events. Isolated from config.enabled and config.raise_on_n_plus_one.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/query_owl/test_helper.rb', line 7

def self.capture_events
  QueryTracker.start!
  EagerLoadTracker.start!
  yield
  queries    = QueryTracker.stop!
  eager_data = EagerLoadTracker.stop!
  Detector.detect_n_plus_one(queries) +
    Detector.detect_slow_queries(queries) +
    Detector.detect_unused_eager_loads(eager_data)
rescue
  QueryTracker.stop!
  EagerLoadTracker.stop!
  raise
end

Instance Method Details

#assert_no_n_plus_one(msg = nil, &block) ⇒ Object

Minitest assertions — call assert_no_n_plus_one { } inside a test method



38
39
40
41
42
# File 'lib/query_owl/test_helper.rb', line 38

def assert_no_n_plus_one(msg = nil, &block)
  events = QueryOwl::TestHelper.capture_events(&block)
  count  = events.count { |e| e[:type] == :n_plus_one }
  assert count.zero?, msg || "Expected no N+1 queries, but #{count} detected"
end

#assert_no_slow_query(msg = nil, &block) ⇒ Object



44
45
46
47
48
# File 'lib/query_owl/test_helper.rb', line 44

def assert_no_slow_query(msg = nil, &block)
  events = QueryOwl::TestHelper.capture_events(&block)
  count  = events.count { |e| e[:type] == :slow_query }
  assert count.zero?, msg || "Expected no slow queries, but #{count} detected"
end

#trigger_n_plus_oneObject

RSpec block matchers — use with expect { }.to / not_to



24
25
26
# File 'lib/query_owl/test_helper.rb', line 24

def trigger_n_plus_one
  EventTypeMatcher.new(:n_plus_one)
end

#trigger_slow_queryObject



28
29
30
# File 'lib/query_owl/test_helper.rb', line 28

def trigger_slow_query
  EventTypeMatcher.new(:slow_query)
end

#trigger_unused_eager_loadObject



32
33
34
# File 'lib/query_owl/test_helper.rb', line 32

def trigger_unused_eager_load
  EventTypeMatcher.new(:unused_eager_load)
end