Skip to content

Rewrite and speed up query tests #5813

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

Open
wants to merge 9 commits into
base: move-queries-types-to-respective-modules
Choose a base branch
from

Conversation

snejus
Copy link
Member

@snejus snejus commented Jun 1, 2025

Previously:

Poe => pytest -p no:cov test/test_query.py --durations=10
=============================================================================== test session starts ===============================================================================
platform linux -- Python 3.9.20, pytest-8.3.5, pluggy-1.5.0
cachedir: /tmp/pytest_cache
rootdir: /home/sarunas/repo/beets
configfile: setup.cfg
plugins: anyio-4.9.0, xdist-3.6.1, requests-mock-1.12.1, flask-1.3.0
collected 133 items                                                                                                                                                               

test/test_query.py .....................................................................................................................................                 [133/133]

============================================================================== slowest 10 durations ===============================================================================
0.13s call     test/test_query.py::NotQueryTest::test_type_none
0.12s call     test/test_query.py::NotQueryTest::test_fast_vs_slow
0.11s call     test/test_query.py::GetTest::test_singleton_0
0.11s call     test/test_query.py::NotQueryTest::test_type_substring
0.11s call     test/test_query.py::RelatedQueriesTest::test_filter_items_by_common_field
0.11s call     test/test_query.py::RelatedQueriesTest::test_get_items_filter_by_album_field
0.11s call     test/test_query.py::NotQueryTest::test_type_boolean
0.11s call     test/test_query.py::NotQueryTest::test_type_or
0.11s call     test/test_query.py::NotQueryTest::test_type_numeric
0.11s call     test/test_query.py::NotQueryTest::test_type_true
=============================================================================== 133 passed in 9.94s ===============================================================================

Now:

Poe => pytest -p no:cov test/test_query.py --durations=10
=============================================================================== test session starts ================================================================================
platform linux -- Python 3.9.20, pytest-8.3.5, pluggy-1.5.0
cachedir: /tmp/pytest_cache
rootdir: /home/sarunas/repo/beets
configfile: setup.cfg
plugins: anyio-4.9.0, xdist-3.6.1, requests-mock-1.12.1, flask-1.3.0
collected 129 items                                                                                                                                                                

test/test_query.py .................................................................................................................................                      [129/129]

=============================================================================== slowest 10 durations ===============================================================================
0.10s setup    test/test_query.py::TestRelatedQueries::test_related_query[match-album-with-item-field-query]
0.09s setup    test/test_query.py::TestGet::test_get_query[''-['first', 'second', 'third']]
0.09s setup    test/test_query.py::TestPathQuery::test_explicit[exact-match]
0.09s setup    test/test_query.py::TestQuery::test_value_type[parse-true]
0.08s setup    test/test_query.py::TestDefaultSearchFields::test_search[album-match-album]
0.02s call     test/test_query.py::TestGet::test_query_logic[SubstringQuery('album', 'ba', fast=True)-{'third'}]
0.02s call     test/test_query.py::TestGet::test_query_logic[NoneQuery('rg_track_gain', True)-set()]
0.02s call     test/test_query.py::TestGet::test_query_logic[NumericQuery('year', '2001..2002', fast=True)-{'third'}]
0.02s call     test/test_query.py::TestGet::test_query_logic[RegexpQuery('artist', re.compile('^t'), fast=True)-{'first'}]
0.02s call     test/test_query.py::TestGet::test_query_logic[OrQuery([BooleanQuery('comp', 1, fast=True), NumericQuery('year', '2002', fast=True)])-{'third'}]
=============================================================================== 129 passed in 2.53s ================================================================================

snejus added 9 commits June 1, 2025 13:36
And remove `force_implicit_query_detection` attribute from `PathQuery`
class.
The case_sensitive parameter was only used in tests, which now use
monkeypatch to control the behavior of util.case_sensitive() instead.
This simplifies the PathQuery initialization logic while maintaining
test coverage.
And rewrite test_query.py
@snejus snejus requested review from semohr and wisp3rwind June 1, 2025 14:25
Copy link

github-actions bot commented Jun 1, 2025

Thank you for the PR! The changelog has not been updated, so here is a friendly reminder to check if you need to add an entry.

@snejus snejus force-pushed the move-queries-types-to-respective-modules branch from 1a1bbbc to f0ba201 Compare June 1, 2025 14:28
@snejus snejus requested a review from Copilot June 1, 2025 14:29
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors test utilities and updates query abstractions to improve test performance and clarity.

  • Adds a pytest hook to generate readable IDs for parametrized tests.
  • Refactors the item fixture to accept keyword overrides via defaults.
  • Marks Query methods as abstract and adjusts their default implementations.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
test/conftest.py Import inspect and Query, add pytest_make_parametrize_id hook for clearer test IDs.
beets/test/_common.py Extend item() fixture to accept **kwargs and merge with defaults.
beets/dbcore/query.py Annotate clause and match as abstract, remove default returns, and change col_clause to raise.
Comments suppressed due to low confidence (3)

beets/dbcore/query.py:99

  • The default implementation of clause no longer returns (None, ()), so it implicitly returns None. Restore return None, () or explicitly raise NotImplementedError if there is no default behavior.
    def clause(self) -> tuple[str | None, Sequence[Any]]:

beets/dbcore/query.py:105

  • The abstract match method has no implementation or exception, so it will implicitly return None. Add a raise NotImplementedError or a proper fallback implementation.
    def match(self, obj: Model):

beets/dbcore/query.py:154

  • Changing col_clause to always raise NotImplementedError removes the previous default (self.field, ()) behavior. If subclasses rely on that fallback, this will break them—either restore the default return or ensure all subclasses implement it.
    def col_clause(self) -> tuple[str, Sequence[SQLiteType]]:

Copy link
Contributor

@semohr semohr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like some good stuff to me 👍

I appreciate the use of the parameterize fixture, the tests are way more understandable now!

You checked that we still have the same coverage?

@snejus snejus force-pushed the move-queries-types-to-respective-modules branch 3 times, most recently from 38bd19f to 0133f06 Compare June 22, 2025 17:07
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

Successfully merging this pull request may close these issues.

2 participants