Skip to content

keygen-sh/sql_matchers

Repository files navigation

sql_matchers

CI Gem Version

Use sql_matchers for query assertions and SQL matchers in RSpec.

This gem was extracted from Keygen.

Sponsored by:

Keygen

A fair source software licensing and distribution API.

Installation

Add this line to your application's Gemfile:

gem 'sql_matchers'

And then execute:

$ bundle

Or install it yourself as:

$ gem install sql_matchers

Usage

it 'should assert query count' do
  expect { User.find_by(id: 1) }.to match_query(count: 1)
end

it 'should assert query matches' do
  expect { 3.times { User.find_by(id: _1 + 1) } }.to(
    match_queries(count: 3) do |queries|
      first, second, third, *rest = queries

      expect(first).to eq %(SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1)
      expect(second).to eq %(SELECT "users".* FROM "users" WHERE "users"."id" = 2 LIMIT 1)
      expect(third).to eq %(SELECT "users".* FROM "users" WHERE "users"."id" = 3 LIMIT 1)
      expect(rest).to be_empty
    end
  )
end

it 'should assert SQL matches' do
  # match_sql will attempt to normalize formatting to prevent false-negatives
  expect(User.where(id: 42).to_sql).to match_sql <<~SQL.squish
    SELECT
      users.*
    FROM
      users
    WHERE
      users.id = 42
  SQL
end

Future

Right now, the gem only supports RSpec, but we're open to pull requests that extend the functionality to other testing frameworks.

Supported Rubies

sql_matchers supports Ruby 3.1 and above. We encourage you to upgrade if you're on an older version. Ruby 3 provides a lot of great features, like better pattern matching and a new shorthand hash syntax.

Is it any good?

Yes.

Contributing

If you have an idea, or have discovered a bug, please open an issue or create a pull request.

License

The gem is available as open source under the terms of the MIT License.

About

Query assertions and SQL matchers for RSpec.

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published