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

Add doc for assert_queries and assert_no_queries #50334

Merged
merged 1 commit into from
Dec 14, 2023

Conversation

yykamei
Copy link
Contributor

@yykamei yykamei commented Dec 12, 2023

Follow-up to #50281

Motivation / Background

I think assert_queries and assert_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 named test_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 of String#match, which may cause ArgumentError: invalid byte sequence in UTF-8.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.
    • This does not include behavior changes

Copy link
Member

@skipkayhil skipkayhil left a 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 👍

@skipkayhil skipkayhil added the ready PRs ready to merge label Dec 12, 2023
@yahonda
Copy link
Member

yahonda commented Dec 12, 2023

QueryLogsTest#test_invalid_encoding_query test gets ArgumentError: invalid byte sequence in UTF-8 for most of Ruby versions and database adapters. I understand this commit does not change the QueryLogsTest#test_invalid_encoding_query test itself, but not sure if they are not related.

https://buildkite.com/rails/rails/builds/102711#018c5c47-a1c3-46da-aea1-457b9f7d70bc/1096-1105

@skipkayhil
Copy link
Member

I understand this commit does not change the QueryLogsTest#test_invalid_encoding_query test itself, but not sure if they are not related.

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)
Copy link
Member

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?

Copy link
Contributor Author

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...

image

@p8
Copy link
Member

p8 commented Dec 12, 2023

It seems QueryLogsTest is using these new assertions, which shouldn’t happen.

@skipkayhil
Copy link
Member

skipkayhil commented Dec 12, 2023

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 assert_queries was never called with a matcher, so it is in fact this PR that is introducing the flakiness

@yahonda
Copy link
Member

yahonda commented Dec 12, 2023

Would you squash your commits?

@yykamei
Copy link
Contributor Author

yykamei commented Dec 12, 2023

Thank you. I squashed it, but I'm concerned about it. Is it OK to merge?

#50334 (comment)

@yahonda
Copy link
Member

yahonda commented Dec 12, 2023

(Updated)

Looks like this end closes QueryAssertionsTest class so test_assert_queries_with_matcher and test_assert_no_queries are not executed because they are out of QueryAssertionsTest class.

@yahonda
Copy link
Member

yahonda commented Dec 12, 2023

Opened a feature request at rubocop-minitest rubocop/rubocop-minitest#279 to find tests that are not executed.

@yykamei
Copy link
Contributor Author

yykamei commented Dec 12, 2023

Thank you! I understood the reason that the two cases were not run. I changed assert_queries a bit to make sure ActiveSupport::Notifications.unsubscribe("sql.active_record") is called in order to prevent other test cases from failing because of unexpected match operation.

Edit: This didn't work 😱

@p8
Copy link
Member

p8 commented Dec 12, 2023

@yykamei It seems you just had bad luck with that CI run, the failure seems unrelated.
I'd just run the tests again.

@yykamei
Copy link
Contributor Author

yykamei commented Dec 12, 2023

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`.
@byroot byroot merged commit 9517841 into rails:main Dec 14, 2023
4 checks passed
@yykamei yykamei deleted the add_doc_for_assert_queries branch December 14, 2023 23:58
yahonda added a commit to yahonda/rails that referenced this pull request Dec 17, 2023
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
$
```
yahonda added a commit to yahonda/rails that referenced this pull request Dec 17, 2023
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
```
rafaelfranca pushed a commit to yahonda/rails that referenced this pull request Jan 3, 2024
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
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
activerecord ready PRs ready to merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants