Module: QueryGuard::RSpec
- Defined in:
- lib/query_guard/rspec.rb
Instance Method Summary collapse
-
#within_query_budget(**limits, &block) ⇒ Object
Convenience helper to check if code stays within a query budget.
Instance Method Details
#within_query_budget(**limits, &block) ⇒ Object
Convenience helper to check if code stays within a query budget
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/query_guard/rspec.rb', line 90 def within_query_budget(**limits, &block) _, report = QueryGuard::Trace.trace("within_query_budget") do block.call end violations = [] if limits[:count] && report.query_count > limits[:count] violations << "Query count exceeded: #{report.query_count} > #{limits[:count]}" end if limits[:duration_ms] && report.total_duration_ms > limits[:duration_ms] violations << "Duration exceeded: #{report.total_duration_ms.round(2)}ms > #{limits[:duration_ms]}ms" end if violations.any? raise QueryGuard::Budget::Violation, violations.join("; ") end report end |