# N+1 control

> The tool offers RSpec and Minitest matchers designed to prevent the "N+1" queries problem.

- URL: https://evilmartians.com/opensource/n-plus-one-control
- GitHub: https://github.com/palkan/n_plus_one_control
- Authors: Vladimir Dementyev

---

The tool offers RSpec and Minitest matchers designed to prevent the "N+1" queries problem.

### Why yet another gem to assert DB queries?

Unlike other libraries (such as [db-query-matchers](https://github.com/brigade/db-query-matchers), [rspec-sqlimit](https://github.com/nepalez/rspec-sqlimit), etc.), with `n_plus_one_control` you don't have to specify exact expectations to control your code behaviour (e.g. `expect { subject }.to query(2).times`).

Such expectations are rather hard to maintain, because there is a big chance of adding more queries, not related to the system under test.

`n_plus_one_control` works differently. It evaluates the code under consideration several times with different scale factors to make sure that the number of DB queries behaves as expected (i.e. O(1) instead of O(N)).

*Read also Evil Martians' article*: https://evilmartians.com/chronicles/squash-n-plus-one-queries-early-with-n-plus-one-control-test-matchers-for-ruby-and-rails

So, it's for _performance_ testing and not _feature_ testing.

### Why not just use [`bullet`](https://github.com/flyerhzm/bullet)?

Of course, it's possible to use Bullet in tests (see more [here](https://evilmartians.com/chronicles/fighting-the-hydra-of-n-plus-one-queries)), but it's not a _silver bullet_: there can be both false positives and true negatives.

*Read also Evil Martians' article*: https://evilmartians.com/chronicles/fighting-the-hydra-of-n-plus-one-queries

This gem was born after I've found myself not able to verify with a test yet another N+1 problem.

## Usage

### RSpec

First, add NPlusOneControl to your `spec_helper.rb`:

```ruby
# spec_helper.rb
require "n_plus_one_control/rspec"
```

Then:

```ruby
# Wrap example into a context with :n_plus_one tag
context "N+1", :n_plus_one do
  # Define `populate` callbacks which is responsible for data
  # generation (and whatever else).
  #
  # It accepts one argument, the scale factor (read below)
  populate { |n| create_list(:post, n) }

  specify do
    expect { get :index }.to perform_constant_number_of_queries
  end
end
```

Available modifiers:

```ruby
# You can specify the RegExp to filter queries.
# By default, it only considers SELECT queries.
expect { get :index }.to perform_constant_number_of_queries.matching(/INSERT/)

# You can also provide custom scale factors
expect { get :index }.to perform_constant_number_of_queries.with_scale_factors(10, 100)
```

#### Using scale factor in spec

Let's suppose your action accepts a parameter which can make an impact on the number of returned records:

```ruby
get :index, params: {per_page: 10}
```

Then it is enough to just change `per_page` parameter between executions and do not recreate records in DB. For this purpose, you can use `current_scale` method in your example:

```ruby
context "N+1", :n_plus_one do
  before { create_list :post, 3 }

  specify do
    expect { get :index, params: {per_page: current_scale} }.to perform_constant_number_of_queries
  end
end
```

### Expectations in execution block

Both RSpec matchers allows you to put additional expectations inside execution block to ensure that tested piece of code actually does what expected.

```ruby
context "N+1", :n_plus_one do
  specify do
    expect do
      expect(my_query).to eq(actuall_results)
    end.to perform_constant_number_of_queries
  end
end
```
