{"id":13521151,"url":"https://github.com/nepalez/rspec-sqlimit","last_synced_at":"2025-05-14T19:08:23.768Z","repository":{"id":41092805,"uuid":"81720363","full_name":"nepalez/rspec-sqlimit","owner":"nepalez","description":"RSpec matcher to control SQL queries made by block of code","archived":false,"fork":false,"pushed_at":"2025-04-23T18:21:32.000Z","size":45,"stargazers_count":233,"open_issues_count":1,"forks_count":39,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-07T20:47:19.320Z","etag":null,"topics":["activerecord","rails","rspec","rspec-matchers","ruby","ruby-on-rails"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nepalez.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-02-12T11:13:06.000Z","updated_at":"2025-04-23T18:21:37.000Z","dependencies_parsed_at":"2025-04-01T14:00:30.764Z","dependency_job_id":"9c36625d-2195-449a-a13b-37a6dcb12f6f","html_url":"https://github.com/nepalez/rspec-sqlimit","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nepalez%2Frspec-sqlimit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nepalez%2Frspec-sqlimit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nepalez%2Frspec-sqlimit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nepalez%2Frspec-sqlimit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nepalez","download_url":"https://codeload.github.com/nepalez/rspec-sqlimit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254209859,"owners_count":22032897,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["activerecord","rails","rspec","rspec-matchers","ruby","ruby-on-rails"],"created_at":"2024-08-01T06:00:29.447Z","updated_at":"2025-05-14T19:08:22.027Z","avatar_url":"https://github.com/nepalez.png","language":"Ruby","funding_links":[],"categories":["Ruby","Matchers"],"sub_categories":[],"readme":"# Test-Driven way of fighting N + 1 queries\n\nRSpec matcher to control number of SQL queries executed by a block of code.\n\nIt wraps [the answer at Stack Overflow][stack-answer] by [Ryan Bigg][ryan-bigg], which based on Active Support [Notification][notification] and [Instrumentation][instrumentation] mechanisms.\n\nFor motivation and details see my [blog post \"Fighting the Hydra of N+1 queries\" in the Martian Chronicles][hydra].\n\n[![Gem Version][gem-badger]][gem]\n\n\u003ca href=\"https://evilmartians.com/\"\u003e\n\u003cimg src=\"https://evilmartians.com/badges/sponsored-by-evil-martians.svg\" alt=\"Sponsored by Evil Martians\" width=\"236\" height=\"54\"\u003e\u003c/a\u003e\n\n## Installation\n\n```ruby\n# Gemfile\ngem \"rspec-sqlimit\"\n```\n\n## Usage\n\nThe gem defines matcher `exceed_query_limit` that takes maximum number of SQL requests to be made inside the block.\n\n```ruby\nrequire \"rspec-sqlimit\"\n\nRSpec.describe \"N+1 safety\" do\n  it \"doesn't send unnecessary requests to db\" do\n    expect { User.create }.not_to exceed_query_limit(1)\n  end\nend\n```\n\nThe above specification fails with the following description:\n\n```\nFailure/Error: expect { User.create }.not_to exceed_query_limit(1)\n\n Expected to run maximum 1 queries\n The following 3 queries were invoked:\n    1) begin transaction (0.045 ms)\n    2) INSERT INTO \"users\" DEFAULT VALUES (0.19 ms)\n    3) commit transaction (148.935 ms)\n```\n\nYou can restrict the matcher using regex:\n\n```ruby\nrequire \"rspec-sqlimit\"\n\nRSpec.describe \"N+1 safety\" do\n  it \"doesn't send unnecessary requests to db\" do\n    expect { User.create }.not_to exceed_query_limit(1).with(/^INSERT/)\n  end\nend\n```\n\nThis time test passes.\n\nWhen a specification with a restriction fails, you'll see an error as follows:\n\n```ruby\nrequire \"rspec-sqlimit\"\n\nRSpec.describe \"N+1 safety\" do\n  it \"doesn't send unnecessary requests to db\" do\n    expect { User.create name: \"Joe\" }.not_to exceed_query_limit(0).with(/^INSERT/)\n  end\nend\n```\n\n```\nFailure/Error: expect { User.create }.not_to exceed_query_limit(0).with(/INSERT/)\n\n  Expected to run maximum 0 queries that match (?-mix:INSERT)\n  The following 1 queries were invoked among others (see mark -\u003e):\n     1) begin transaction (0.072 ms)\n  -\u003e 2) INSERT INTO \"users\" (\"name\") VALUES (?); [\"Joe\"] (0.368 ms)\n     3) commit transaction (147.559 ms)\n```\n\nIn the last example you can see that binded values are shown after the query following the Rails convention.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n\n[gem-badger]: https://img.shields.io/gem/v/rspec-sqlimit.svg?style=flat\n[gem]: https://rubygems.org/gems/rspec-sqlimit\n[stack-answer]: http://stackoverflow.com/a/5492207/1869912\n[ryan-bigg]: http://ryanbigg.com/\n[notification]: http://api.rubyonrails.org/classes/ActiveSupport/Notifications.html\n[instrumentation]: http://guides.rubyonrails.org/active_support_instrumentation.html\n[hook]: http://guides.rubyonrails.org/active_support_instrumentation.html#sql-active-record\n[hydra]: https://evilmartians.com/chronicles/fighting-the-hydra-of-n-plus-one-queries\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnepalez%2Frspec-sqlimit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnepalez%2Frspec-sqlimit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnepalez%2Frspec-sqlimit/lists"}