Giter Site home page Giter Site logo

db-query-matchers's Introduction

db-query-matchers

Gem Version Build Status Code Climate Dependency Status

RSpec matchers for database queries made by ActiveRecord.

Installation

Add this line to your application's Gemfile, preferably in your test group:

gem 'db-query-matchers'

And then execute:

bundle

Or install it yourself as:

gem install db-query-matchers

Usage

describe 'MyCode' do
  context 'when we expect no queries' do
    it 'does not make database queries' do
      expect { subject.make_no_queries }.to_not make_database_queries
    end
  end

  context 'when we expect queries' do
    it 'makes database queries' do
      expect { subject.make_some_queries }.to make_database_queries
    end
  end

  context 'when we expect exactly 1 query' do
    it 'makes database queries' do
      expect { subject.make_one_query }.to make_database_queries(count: 1)
    end
  end

  context 'when we expect max 3 queries' do
    it 'makes database queries' do
      expect { subject.make_several_queries }.to make_database_queries(count: 0..3)
    end
  end

  context 'when we expect a possible range of queries' do
    it 'makes database queries' do
      expect { subject.make_several_queries }.to make_database_queries(count: 3..5)
    end
  end

  context 'when we only care about manipulative queries (INSERT, UPDATE, DELETE)' do
    it 'makes a destructive database query' do
      expect { subject.make_one_query }.to make_database_queries(manipulative: true)
    end
  end

  context 'when we only care about unscoped queries (SELECT without a WHERE or LIMIT clause))' do
    it 'makes an unscoped database query' do
      expect { subject.make_one_query }.to make_database_queries(unscoped: true)
    end
  end

  context 'when we only care about queries matching a certain pattern' do
    it 'makes a destructive database query' do
      expect { subject.make_special_queries }.to make_database_queries(matching: 'DELETE * FROM')
    end

    it 'makes a destructive database query matched with a regexp' do
      expect { subject.make_special_queries }.to make_database_queries(matching: /DELETE/)
    end
  end
end

Configuration

To exclude certain types of queries from being counted, specify an ignores configuration consisting of an array of regular expressions. If a query matches one of the patterns in this array, it will not be counted in the make_database_queries matcher.

To exclude SCHEMA queries, add schemaless to the configuration. This will help avoid failing specs due to ActiveRecord load order.

To log more about the queries being made, you can set the log_backtrace option to true. And to control what parts of the backtrace is logged, you can use backtrace_filter.

DBQueryMatchers.configure do |config|
  config.ignores = [/SHOW TABLES LIKE/]
  config.schemaless = true

  # the payload argument is described here:
  # http://edgeguides.rubyonrails.org/active_support_instrumentation.html#sql-active-record
  config.on_query_counted do |payload|
    # do something arbitrary with the query
  end

  config.log_backtrace = true
  config.backtrace_filter = Proc.new do |backtrace|
    backtrace.select { |line| line.start_with?(Rails.root.to_s) }
  end
end

db-query-matchers's People

Contributors

lencioni avatar trotzig avatar tdooner avatar sds avatar ryanmccarthypdx avatar donaldpiret avatar pikachuexe avatar stevehodges avatar lumeet avatar empact avatar eugeneius avatar johndotpub avatar thedrow avatar severinraez avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.