-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Add doc for assert_queries
and assert_no_queries
#50334
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, will need another review from a committer 👍
https://buildkite.com/rails/rails/builds/102711#018c5c47-a1c3-46da-aea1-457b9f7d70bc/1096-1105 |
Yeah this is really weird... the failures also seem distributed between different adapters on different version of Ruby. I'll retry a few to see if the QueryLogsTest is just flaky |
Post.where(id: 1).first | ||
end | ||
} | ||
assert_match(/0 instead of 1 queries/, error.message) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding this 🙏
Could you move this to a separate test case method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! It makes sense to me. I moved the code with 7a268ec.
By the way, I couldn't run the test on my local machine. It's weird...
It seems QueryLogsTest is using these new assertions, which shouldn’t happen. |
Ah, I think the subscriber stays active for all other tests once the QueryAssertions tests are run once, hence the flakiness. Edit: Aha! And the tests weren't flaky before because the new |
Would you squash your commits? |
7a268ec
to
9c8f964
Compare
Thank you. I squashed it, but I'm concerned about it. Is it OK to merge? |
(Updated) Looks like this |
Opened a feature request at |
Thank you! I understood the reason that the two cases were not run. I changed Edit: This didn't work 😱 |
5096ddb
to
6982acf
Compare
@yykamei It seems you just had bad luck with that CI run, the failure seems unrelated. |
Thank you 🙏 |
Follow-up to rails#50281 I think `assert_queries` and `assert_no_queries` are so useful that I want to read the documentation for the assertions. This patch adds the doc for them and updated the test case named `test_assert_queries` to verify `:matcher` works as expected. I also changed `assert_queries` to make sure the subscriber of `"sql.active_record"` is unsubscribed without affecting other test cases. Without this change, some test cases fail because of execution of `String#match`, which may cause `ArgumentError: invalid byte sequence in UTF-8`.
6982acf
to
ccc512f
Compare
This pull request enables `Minitest/NonExecutableTestMethod` cop to find non-executed test that is out of `ActiveSupport::TestCase` and its subclasses. This cop is based on the request since there was a test that is not executed found at rails#50334 (comment) and implemented to RuboCop Minitest 0.34.0 via rubocop/rubocop-minitest#279 This cop works as follows. As of right now, there is no offenses by reverting the merge commit via rails#50334 ``` $ git revert -m 1 9517841 $ bundle exec rubocop Inspecting 3254 files ... snip ... Offenses: activerecord/test/cases/assertions/query_assertions_test.rb:27:5: W: Minitest/NonExecutableTestMethod: Test method should be defined inside a test class to ensure execution. def test_assert_no_queries ... ^^^^^^^^^^^^^^^^^^^^^^^^^^ 3254 files inspected, 1 offense detected $ ```
This pull request enables `Minitest/NonExecutableTestMethod` cop to find non-executed test that is out of `ActiveSupport::TestCase` and its subclasses. This cop is based on the request since there was a test that is not executed found at rails#50334 (comment) and implemented to RuboCop Minitest 0.34.0 via rubocop/rubocop-minitest#279 This cop works as follows. As of right now, there is no offenses by reverting the merge commit via rails#50334 ``` $ git revert -m 1 9517841 $ bundle exec rubocop Inspecting 3254 files ... snip ... Offenses: activerecord/test/cases/assertions/query_assertions_test.rb:27:5: W: Minitest/NonExecutableTestMethod: Test method should be defined inside a test class to ensure execution. def test_assert_no_queries ... ^^^^^^^^^^^^^^^^^^^^^^^^^^ 3254 files inspected, 1 offense detected $ ``` * `Gemfile.lock` has been updated as follows ``` bundle update rubocop rubocop-minitest --conservative ```
This pull request enables `Minitest/NonExecutableTestMethod` cop to find non-executed test that is out of `ActiveSupport::TestCase` and its subclasses. This cop is based on the request since there was a test that is not executed found at rails#50334 (comment) and implemented to RuboCop Minitest 0.34.0 via rubocop/rubocop-minitest#279 This cop works as follows. As of right now, there is no offenses by reverting the merge commit via rails#50334 ``` $ git revert -m 1 9517841 $ bundle exec rubocop Inspecting 3254 files ... snip ... Offenses: activerecord/test/cases/assertions/query_assertions_test.rb:27:5: W: Minitest/NonExecutableTestMethod: Test method should be defined inside a test class to ensure execution. def test_assert_no_queries ... ^^^^^^^^^^^^^^^^^^^^^^^^^^ 3254 files inspected, 1 offense detected $ ``` * `Gemfile.lock` has been updated as follows ``` bundle update rubocop rubocop-minitest --conservative ```
Follow-up to #50281
Motivation / Background
I think
assert_queries
andassert_no_queries
are so useful that I want to read the documentation for the assertions. This pull request adds the doc for them and updated the test case namedtest_assert_queries
to verify:matcher
works as expected.Additional information
I also changed
assert_queries
to make sure the subscriber of"sql.active_record"
is unsubscribed without affecting other test cases. Without this change, some test cases fail because of execution ofString#match
, which may causeArgumentError: invalid byte sequence in UTF-8
.Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]