Socket
Book a DemoInstallSign in
Socket

sql_matchers

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sql_matchers

1.0.0
bundlerRubygems
Version published
Maintainers
1
Created
Source

sql_matchers

CI Gem Version

Use sql_matchers for query assertions and SQL matchers for 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.

FAQs

Package last updated on 13 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.