-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 86bac2a
Showing
24 changed files
with
511 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
github: ezekg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: CI | ||
on: | ||
push: | ||
branches: | ||
- master | ||
pull_request: | ||
branches: | ||
- master | ||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
ruby: | ||
- '3.3' | ||
- '3.2' | ||
- '3.1' | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
- name: Install | ||
uses: ruby/setup-ruby@v1 | ||
with: | ||
ruby-version: ${{matrix.ruby}} | ||
bundler-cache: true | ||
- name: Test | ||
run: bundle exec rake test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/.bundle/ | ||
/doc/ | ||
/log/*.log | ||
/pkg/ | ||
/tmp/ | ||
/test/dummy/db/*.sqlite3 | ||
/test/dummy/db/*.sqlite3-* | ||
/test/dummy/log/*.log | ||
/test/dummy/storage/ | ||
/test/dummy/tmp/ | ||
.rspec_status | ||
Gemfile.lock | ||
*.gem | ||
*.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--format documentation | ||
--color | ||
--require spec_helper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Changelog | ||
|
||
## [Unreleased] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
## Security issues | ||
|
||
If you have found a security related issue, please do not file an issue on | ||
GitHub or send a PR addressing the issue. Contact [Keygen](mailto:[email protected]) | ||
directly. You will be given public credit for your disclosure. | ||
|
||
## Reporting issues | ||
|
||
Please try to answer the following questions in your bug report: | ||
|
||
- What did you do? | ||
- What did you expect to happen? | ||
- What happened instead? | ||
|
||
Make sure to include as much relevant information as possible. Ruby version, | ||
Rails version, `sql_matchers` version, OS version and any stack traces | ||
you have are very valuable. | ||
|
||
## Pull Requests | ||
|
||
- **Add tests!** Your patch won't be accepted if it doesn't have tests. | ||
|
||
- **Document any change in behaviour**. Make sure the README and any other | ||
relevant documentation are kept up-to-date. | ||
|
||
- **Create topic branches**. Please don't ask us to pull from your master branch. | ||
|
||
- **One pull request per feature**. If you want to do more than one thing, send | ||
multiple pull requests. | ||
|
||
- **Send coherent history**. Make sure each individual commit in your pull | ||
request is meaningful. If you had to make multiple intermediate commits while | ||
developing, please squash them before sending them to us. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
source "https://rubygems.org" | ||
|
||
# Specify your gem's dependencies in sql_matchers.gemspec | ||
gemspec | ||
|
||
gem "rake", "~> 13.0" | ||
|
||
gem "rspec", "~> 3.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Copyright 2024 Keygen LLC | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
# 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` to create temporary tables and models in RSpec specs, | ||
rather than create and maintain a dummy Rails application or messy block-level | ||
constants. | ||
|
||
This gem was extracted from [Keygen](https://keygen.sh). | ||
|
||
Sponsored by: | ||
|
||
<a href="https://keygen.sh?ref=sql_matchers"> | ||
<div> | ||
<img src="https://keygen.sh/images/logo-pill.png" width="200" alt="Keygen"> | ||
</div> | ||
</a> | ||
|
||
_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 | ||
|
||
### `temporary_table` | ||
|
||
To define a temporary table: | ||
|
||
```ruby | ||
describe Example do | ||
temporary_table :user do |t| | ||
t.string :email | ||
t.string :first_name | ||
t.string :last_name | ||
t.index :email, unique: true | ||
end | ||
|
||
it 'should define a table' do | ||
expect(ActiveRecord::Base.connection.table_exists?(:user)).to be true | ||
end | ||
end | ||
``` | ||
|
||
The full Active Record schema API is available. | ||
|
||
### `temporary_model` | ||
|
||
To define an Active Record: | ||
|
||
```ruby | ||
describe Example do | ||
temporary_model :user do | ||
has_many :posts | ||
end | ||
|
||
it 'should define a record' do | ||
expect(const_defined?(:User)).to be true | ||
end | ||
end | ||
``` | ||
|
||
To define an Active Model: | ||
|
||
```ruby | ||
describe Example do | ||
temporary_model :guest_user, table_name: nil, base_class: nil do | ||
include ActiveModel::Model | ||
end | ||
|
||
it 'should define a model' do | ||
expect(const_defined?(:GuestUser)).to be true | ||
end | ||
end | ||
``` | ||
|
||
To define a PORO: | ||
|
||
```ruby | ||
describe Example do | ||
temporary_model :null_user, table_name: nil, base_class: nil do | ||
# ... | ||
end | ||
|
||
it 'should define a PORO' do | ||
expect(const_defined?(:NullUser)).to be true | ||
end | ||
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require 'bundler/gem_tasks' | ||
require 'rspec/core/rake_task' | ||
|
||
RSpec::Core::RakeTask.new(:spec) | ||
task test: :spec | ||
|
||
task default: :test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Security Policy | ||
|
||
## Reporting a vulnerability | ||
|
||
If you find a vulnerability in `sql_matchers`, please contact Keygen via | ||
[email](mailto:[email protected]). | ||
|
||
You will be given public credit for your disclosure. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
require "bundler/setup" | ||
require "sql_matchers" | ||
|
||
# You can add fixtures and/or initialization code here to make experimenting | ||
# with your gem easier. You can also use a different console, if you like. | ||
|
||
require "irb" | ||
IRB.start(__FILE__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/usr/bin/env bash | ||
set -euo pipefail | ||
IFS=$'\n\t' | ||
set -vx | ||
|
||
bundle install | ||
|
||
# Do any other automated setup that you need to do here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'active_record' | ||
|
||
require_relative 'sql_matchers/methods' | ||
require_relative 'sql_matchers/version' | ||
|
||
module SqlMatchers; end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module SqlMatchers | ||
module Loggers; end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
module SqlMatchers | ||
module Loggers | ||
class Query | ||
IGNORED_STATEMENTS = %w[CACHE SCHEMA] | ||
IGNORED_STATEMENT_RE = %r{^(?:ROLLBACK|BEGIN|COMMIT|SAVEPOINT|RELEASE)} | ||
IGNORED_COMMENT_RE = %r{ | ||
/\*(\w+='\w+',?)+\*/ # query log tags | ||
}x | ||
|
||
def initialize | ||
@queries = [] | ||
end | ||
|
||
def self.subscribe(&) = new.subscribe(&) | ||
def subscribe(&block) | ||
ActiveSupport::Notifications.subscribed( | ||
logger_proc, | ||
'sql.active_record', | ||
&proc { | ||
result = block.call | ||
result.load if result in ActiveRecord::Relation # autoload relations | ||
} | ||
) | ||
|
||
@queries | ||
end | ||
|
||
private | ||
|
||
def logger_proc = proc(&method(:logger)) | ||
def logger(event) | ||
unless IGNORED_STATEMENTS.include?(event.payload[:name]) || IGNORED_STATEMENT_RE.match(event.payload[:sql]) | ||
query = event.payload[:sql].gsub(IGNORED_COMMENT_RE, '') | ||
.squish | ||
|
||
@queries << query | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'matchers/query' | ||
require_relative 'matchers/sql' | ||
|
||
module SqlMatchers | ||
module Matchers | ||
def match_query(...) = Query.new(...) | ||
def match_queries(...) = match_query(...) | ||
def match_sql(...) = Sql.new(...) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../loggers/query' | ||
|
||
module SqlMatchers | ||
module Matchers | ||
class Query | ||
def initialize(count: nil, &block) | ||
@count = count | ||
@block = block | ||
end | ||
|
||
def supports_block_expectations? = true | ||
def supports_value_expectations? = true | ||
|
||
def matches?(block) | ||
@queries = Loggers::Query.subscribe(&block) | ||
|
||
(@count.nil? || @queries.size == @count) && ( | ||
@block.nil? || @block.call(@queries) | ||
) | ||
end | ||
|
||
def failure_message | ||
"expected to match #{@count} queries but got #{@queries.size}" | ||
end | ||
|
||
def failure_message_when_negated | ||
"expected to not match #{@count} queries" | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.