Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL queries matcher support #2817

Open
hugopeixoto opened this issue Nov 30, 2024 · 3 comments
Open

SQL queries matcher support #2817

hugopeixoto opened this issue Nov 30, 2024 · 3 comments

Comments

@hugopeixoto
Copy link

Rails 7 added support for a few query assertions:

assert_queries_count(1) { Post.first }
assert_queries_count { Post.first }
assert_queries_count(1, include_schema: true) { Post.columns }

assert_no_queries { post.comments }
assert_no_queries(include_schema: true) { Post.columns }

assert_queries_match(/LIMIT \?/, count: 1) { Post.first }
assert_queries_match(/LIMIT \?/) { Post.first }
assert_queries_match(/FROM pg_attribute/i, include_schema: true) { Post.columns }

assert_no_queries_match(/SELECT/i) { post.comments }
assert_no_queries_match(/FROM pg_attribute/i, include_schema: true) { Post.columns }

Does an equivalent matcher belong in rspec-rails?

I've found these rails assertions useful to ensure performance issues (like N+1 queries) don't appear and to make regression tests for when they do.

In projects that use rspec-rails, a matcher that allows to test for the number of queries executed would be helpful.

I have implemented a custom matcher that supports a subset of these:

expect { Post.first }.to execute_queries(1)
expect { Post.first }.to execute_no_queries

These could be expanded to support matching, no count, and include_schema:

expect { Post.first }.to execute_queries
expect { Post.first }.to execute_queries(1).matching(/LIMIT \?/)
expect { Post.first }.to execute_no_queries.including_schema

I'm working on making a PR to rspec-rails to support these, opening an issue now to iterate on the syntax and to check if it belongs here.

@JonRowe
Copy link
Member

JonRowe commented Nov 30, 2024

I'd love you to bring these over to rspec-rails, anything in core rails should be accessible via us, and I actually could have used these recently

@hugopeixoto
Copy link
Author

PR opened at #2818

@JonRowe
Copy link
Member

JonRowe commented Dec 2, 2024

Thanks I've seen, will try to review tonight

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants