diff options
author | mhasbini <mohammad.hasbini@gmail.com> | 2017-04-04 13:54:58 +0300 |
---|---|---|
committer | mhasbini <mohammad.hasbini@gmail.com> | 2017-04-04 13:54:58 +0300 |
commit | e52b1df1d5391f0fa0b9ec7eb2d3492b05f64ba4 (patch) | |
tree | c9fbd2c57dae0a5872653d3df0b9a681a5441404 /spec/support/matchers | |
parent | 9fc17f6f4abdb04f3cf1b60b87bd67b894a19c39 (diff) |
Remove useless queries with false conditions (e.g 1=0)
Diffstat (limited to 'spec/support/matchers')
-rw-r--r-- | spec/support/matchers/query_matcher.rb | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/spec/support/matchers/query_matcher.rb b/spec/support/matchers/query_matcher.rb new file mode 100644 index 00000000000..ac8c4ab91d9 --- /dev/null +++ b/spec/support/matchers/query_matcher.rb @@ -0,0 +1,33 @@ +RSpec::Matchers.define :make_queries_matching do |matcher, expected_count = nil| + supports_block_expectations + + match do |block| + @counter = query_count(matcher, &block) + if expected_count + @counter.count == expected_count + else + @counter.count > 0 + end + end + + failure_message_when_negated do |_| + if expected_count + "expected #{matcher} not to match #{expected_count} queries, got #{@counter.count} matches:\n\n#{@counter.inspect}" + else + "expected #{matcher} not to match any query, got #{@counter.count} matches:\n\n#{@counter.inspect}" + end + end + + failure_message do |_| + if expected_count + "expected #{matcher} to match #{expected_count} queries, got #{@counter.count} matches:\n\n#{@counter.inspect}" + else + "expected #{matcher} to match at least one query, got #{@counter.count} matches:\n\n#{@counter.inspect}" + end + end + + def query_count(regex, &block) + @recorder = ActiveRecord::QueryRecorder.new(&block).log + @recorder.select{ |q| q.match(regex) } + end +end |