|
| 1 | +# sql_matchers |
| 2 | + |
| 3 | +[](https://github.com/keygen-sh/sql_matchers/actions) |
| 4 | +[](https://badge.fury.io/rb/sql_matchers) |
| 5 | + |
| 6 | +Use `sql_matchers` to create temporary tables and models in RSpec specs, |
| 7 | +rather than create and maintain a dummy Rails application or messy block-level |
| 8 | +constants. |
| 9 | + |
| 10 | +This gem was extracted from [Keygen](https://keygen.sh). |
| 11 | + |
| 12 | +Sponsored by: |
| 13 | + |
| 14 | +<a href="https://keygen.sh?ref=sql_matchers"> |
| 15 | + <div> |
| 16 | + <img src="https://keygen.sh/images/logo-pill.png" width="200" alt="Keygen"> |
| 17 | + </div> |
| 18 | +</a> |
| 19 | + |
| 20 | +_A fair source software licensing and distribution API._ |
| 21 | + |
| 22 | +## Installation |
| 23 | + |
| 24 | +Add this line to your application's `Gemfile`: |
| 25 | + |
| 26 | +```ruby |
| 27 | +gem 'sql_matchers' |
| 28 | +``` |
| 29 | + |
| 30 | +And then execute: |
| 31 | + |
| 32 | +```bash |
| 33 | +$ bundle |
| 34 | +``` |
| 35 | + |
| 36 | +Or install it yourself as: |
| 37 | + |
| 38 | +```bash |
| 39 | +$ gem install sql_matchers |
| 40 | +``` |
| 41 | + |
| 42 | +## Usage |
| 43 | + |
| 44 | +### `temporary_table` |
| 45 | + |
| 46 | +To define a temporary table: |
| 47 | + |
| 48 | +```ruby |
| 49 | +describe Example do |
| 50 | + temporary_table :user do |t| |
| 51 | + t.string :email |
| 52 | + t.string :first_name |
| 53 | + t.string :last_name |
| 54 | + t.index :email, unique: true |
| 55 | + end |
| 56 | + |
| 57 | + it 'should define a table' do |
| 58 | + expect(ActiveRecord::Base.connection.table_exists?(:user)).to be true |
| 59 | + end |
| 60 | +end |
| 61 | +``` |
| 62 | + |
| 63 | +The full Active Record schema API is available. |
| 64 | + |
| 65 | +### `temporary_model` |
| 66 | + |
| 67 | +To define an Active Record: |
| 68 | + |
| 69 | +```ruby |
| 70 | +describe Example do |
| 71 | + temporary_model :user do |
| 72 | + has_many :posts |
| 73 | + end |
| 74 | + |
| 75 | + it 'should define a record' do |
| 76 | + expect(const_defined?(:User)).to be true |
| 77 | + end |
| 78 | +end |
| 79 | +``` |
| 80 | + |
| 81 | +To define an Active Model: |
| 82 | + |
| 83 | +```ruby |
| 84 | +describe Example do |
| 85 | + temporary_model :guest_user, table_name: nil, base_class: nil do |
| 86 | + include ActiveModel::Model |
| 87 | + end |
| 88 | + |
| 89 | + it 'should define a model' do |
| 90 | + expect(const_defined?(:GuestUser)).to be true |
| 91 | + end |
| 92 | +end |
| 93 | +``` |
| 94 | + |
| 95 | +To define a PORO: |
| 96 | + |
| 97 | +```ruby |
| 98 | +describe Example do |
| 99 | + temporary_model :null_user, table_name: nil, base_class: nil do |
| 100 | + # ... |
| 101 | + end |
| 102 | + |
| 103 | + it 'should define a PORO' do |
| 104 | + expect(const_defined?(:NullUser)).to be true |
| 105 | + end |
| 106 | +end |
| 107 | +``` |
| 108 | + |
| 109 | +## Future |
| 110 | + |
| 111 | +Right now, the gem only supports RSpec, but we're open to pull requests that |
| 112 | +extend the functionality to other testing frameworks. |
| 113 | + |
| 114 | +## Supported Rubies |
| 115 | + |
| 116 | +**`sql_matchers` supports Ruby 3.1 and above.** We encourage you to upgrade |
| 117 | +if you're on an older version. Ruby 3 provides a lot of great features, like |
| 118 | +better pattern matching and a new shorthand hash syntax. |
| 119 | + |
| 120 | +## Is it any good? |
| 121 | + |
| 122 | +Yes. |
| 123 | + |
| 124 | +## Contributing |
| 125 | + |
| 126 | +If you have an idea, or have discovered a bug, please open an issue or create a |
| 127 | +pull request. |
| 128 | + |
| 129 | +## License |
| 130 | + |
| 131 | +The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). |
0 commit comments