Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/keygen-sh/sql_matchers
Query assertions and SQL matchers for RSpec.
https://github.com/keygen-sh/sql_matchers
Last synced: 3 months ago
JSON representation
Query assertions and SQL matchers for RSpec.
- Host: GitHub
- URL: https://github.com/keygen-sh/sql_matchers
- Owner: keygen-sh
- License: mit
- Created: 2024-08-09T16:05:36.000Z (3 months ago)
- Default Branch: master
- Last Pushed: 2024-08-13T12:31:25.000Z (3 months ago)
- Last Synced: 2024-08-14T15:33:09.014Z (3 months ago)
- Language: Ruby
- Homepage:
- Size: 19.5 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
README
# sql_matchers
[![CI](https://github.com/keygen-sh/sql_matchers/actions/workflows/test.yml/badge.svg)](https://github.com/keygen-sh/sql_matchers/actions)
[![Gem Version](https://badge.fury.io/rb/sql_matchers.svg)](https://badge.fury.io/rb/sql_matchers)Use `sql_matchers` for query assertions and SQL matchers in RSpec.
This gem was extracted from [Keygen](https://keygen.sh).
Sponsored by:
_A fair source software licensing and distribution API._
## Installation
Add this line to your application's `Gemfile`:
```ruby
gem 'sql_matchers'
```And then execute:
```bash
$ bundle
```Or install it yourself as:
```bash
$ gem install sql_matchers
```## Usage
```ruby
it 'should assert query count' do
expect { User.find_by(id: 1) }.to match_query(count: 1)
endit '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 = queriesexpect(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
)
endit '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](https://opensource.org/licenses/MIT).