Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ezekg committed Aug 9, 2024
0 parents commit 86bac2a
Show file tree
Hide file tree
Showing 24 changed files with 511 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: ezekg
27 changes: 27 additions & 0 deletions .github/workflows/test.yml
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
14 changes: 14 additions & 0 deletions .gitignore
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
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--format documentation
--color
--require spec_helper
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Changelog

## [Unreleased]
33 changes: 33 additions & 0 deletions CONTRIBUTING.md
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.
10 changes: 10 additions & 0 deletions Gemfile
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"
20 changes: 20 additions & 0 deletions LICENSE
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.
131 changes: 131 additions & 0 deletions README.md
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).
7 changes: 7 additions & 0 deletions Rakefile
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
8 changes: 8 additions & 0 deletions SECURITY.md
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.
11 changes: 11 additions & 0 deletions bin/console
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__)
8 changes: 8 additions & 0 deletions bin/setup
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
8 changes: 8 additions & 0 deletions lib/sql_matchers.rb
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
5 changes: 5 additions & 0 deletions lib/sql_matchers/loggers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

module SqlMatchers
module Loggers; end
end
43 changes: 43 additions & 0 deletions lib/sql_matchers/loggers/query.rb
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
12 changes: 12 additions & 0 deletions lib/sql_matchers/matchers.rb
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
33 changes: 33 additions & 0 deletions lib/sql_matchers/matchers/query.rb
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
Loading

0 comments on commit 86bac2a

Please sign in to comment.