Module: QueryOwl::TestHelper
- Defined in:
- lib/query_owl/test_helper.rb
Defined Under Namespace
Classes: EventTypeMatcher
Class Method Summary collapse
-
.capture_events ⇒ Object
Runs block with QueryOwl’s trackers active and returns detected events.
Instance Method Summary collapse
-
#assert_no_n_plus_one(msg = nil, &block) ⇒ Object
Minitest assertions — call assert_no_n_plus_one { } inside a test method.
- #assert_no_slow_query(msg = nil, &block) ⇒ Object
-
#trigger_n_plus_one ⇒ Object
RSpec block matchers — use with expect { }.to / not_to.
- #trigger_slow_query ⇒ Object
- #trigger_unused_eager_load ⇒ Object
Class Method Details
.capture_events ⇒ Object
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_one ⇒ Object
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_query ⇒ Object
28 29 30 |
# File 'lib/query_owl/test_helper.rb', line 28 def trigger_slow_query EventTypeMatcher.new(:slow_query) end |
#trigger_unused_eager_load ⇒ Object
32 33 34 |
# File 'lib/query_owl/test_helper.rb', line 32 def trigger_unused_eager_load EventTypeMatcher.new(:unused_eager_load) end |