| 1 |
"""Various tests for querying the library database.""" |
| 2 |
|
| 3 |
import logging |
| 4 |
import os |
| 5 |
import sys |
| 6 |
from functools import partial |
| 7 |
from pathlib import Path |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
from beets import util |
| 12 |
from beets.dbcore import AndQuery, MatchQuery, OrQuery, types |
| 13 |
from beets.dbcore.query import ( |
| 14 |
BooleanQuery, |
| 15 |
DateQuery, |
| 16 |
FalseQuery, |
| 17 |
InQuery, |
| 18 |
NoneQuery, |
| 19 |
NotQuery, |
| 20 |
NumericQuery, |
| 21 |
ParsingError, |
| 22 |
PathQuery, |
| 23 |
RegexpQuery, |
| 24 |
StringFieldQuery, |
| 25 |
StringQuery, |
| 26 |
SubstringQuery, |
| 27 |
TrueQuery, |
| 28 |
) |
| 29 |
from beets.library import Item |
| 30 |
from beets.test import _common |
| 31 |
|
| 32 |
# Because the absolute path begins with something like C:, we |
| 33 |
# can't disambiguate it from an ordinary query. |
| 34 |
WIN32_NO_IMPLICIT_PATHS = "Implicit paths are not supported on Windows" |
| 35 |
|
| 36 |
_p = pytest.param |
| 37 |
|
| 38 |
|
| 39 |
@pytest.fixture(scope="class") |
| 40 |
def helper(class_helper): |
| 41 |
"""Use class scope because each query class owns a library shape. |
| 42 |
|
| 43 |
Class fixtures add rows, albums, flex types, paths, or media files and then |
| 44 |
query whole collections, so sibling class data would change expectations. |
| 45 |
""" |
| 46 |
return class_helper |
| 47 |
|
| 48 |
|
| 49 |
class TestGet: |
| 50 |
@pytest.fixture(scope="class") |
| 51 |
def lib(self, helper): |
| 52 |
album_items = [ |
| 53 |
helper.create_item( |
| 54 |
title="first", |
| 55 |
artist="one", |
| 56 |
artists=["one", "eleven"], |
| 57 |
album="baz", |
| 58 |
year=2001, |
| 59 |
comp=True, |
| 60 |
genres=["rock"], |
| 61 |
composers=["composer"], |
| 62 |
), |
| 63 |
helper.create_item( |
| 64 |
title="second", |
| 65 |
artist="two", |
| 66 |
artists=["two", "twelve"], |
| 67 |
album="baz", |
| 68 |
year=2002, |
| 69 |
comp=True, |
| 70 |
genres=["Rock"], |
| 71 |
), |
| 72 |
] |
| 73 |
album = helper.lib.add_album(album_items) |
| 74 |
album.albumflex = "foo" |
| 75 |
album.store() |
| 76 |
|
| 77 |
helper.add_item( |
| 78 |
title="third", |
| 79 |
artist="three", |
| 80 |
artists=["three", "one"], |
| 81 |
album="foo", |
| 82 |
year=2003, |
| 83 |
comp=False, |
| 84 |
genres=["Hard Rock"], |
| 85 |
comments="caf\xe9", |
| 86 |
Flex_Attr="flex", |
| 87 |
) |
| 88 |
|
| 89 |
return helper.lib |
| 90 |
|
| 91 |
@pytest.mark.parametrize( |
| 92 |
"q, expected_titles", |
| 93 |
[ |
| 94 |
("", ["first", "second", "third"]), |
| 95 |
(None, ["first", "second", "third"]), |
| 96 |
(":oNE", []), |
| 97 |
(":one", ["first"]), |
| 98 |
(":sec :ond", ["second"]), |
| 99 |
(":second", ["second"]), |
| 100 |
("=rock", ["first"]), |
| 101 |
('=~"hard rock"', ["third"]), |
| 102 |
(":t$", ["first"]), |
| 103 |
("oNE", ["first"]), |
| 104 |
("baz", ["first", "second"]), |
| 105 |
("sec ond", ["second"]), |
| 106 |
("three", ["third"]), |
| 107 |
("albumflex:foo", ["first", "second"]), |
| 108 |
("artist::t.+r", ["third"]), |
| 109 |
("artist:thrEE", ["third"]), |
| 110 |
("artists::eleven", ["first"]), |
| 111 |
("artists::one", ["first", "third"]), |
| 112 |
("ArTiST:three", ["third"]), |
| 113 |
("comments:caf\xe9", ["third"]), |
| 114 |
("comp:true", ["first", "second"]), |
| 115 |
("comp:false", ["third"]), |
| 116 |
("flex_attr:flex", ["third"]), |
| 117 |
("Flex_Attr:flex", ["third"]), |
| 118 |
("genres:=rock", ["first"]), |
| 119 |
("genres:=Rock", ["second"]), |
| 120 |
('genres:="Hard Rock"', ["third"]), |
| 121 |
('genres:=~"hard rock"', ["third"]), |
| 122 |
("genres:=~rock", ["first", "second"]), |
| 123 |
('genres:="hard rock"', []), |
| 124 |
("popebear", []), |
| 125 |
("pope:bear", []), |
| 126 |
("singleton:true", ["third"]), |
| 127 |
("singleton:1", ["third"]), |
| 128 |
("singleton:false", ["first", "second"]), |
| 129 |
("singleton:0", ["first", "second"]), |
| 130 |
("title:ond", ["second"]), |
| 131 |
("title::sec", ["second"]), |
| 132 |
("year:2001", ["first"]), |
| 133 |
("year:2000..2002", ["first", "second"]), |
| 134 |
("xyzzy:nonsense", []), |
| 135 |
], |
| 136 |
) |
| 137 |
def test_get_query(self, lib, q, expected_titles): |
| 138 |
assert {i.title for i in lib.items(q)} == set(expected_titles) |
| 139 |
|
| 140 |
@pytest.mark.parametrize( |
| 141 |
"q, expected_titles", |
| 142 |
[ |
| 143 |
(BooleanQuery("comp", True), ("third",)), |
| 144 |
(DateQuery("added", "2000-01-01"), ("first", "second", "third")), |
| 145 |
(FalseQuery(), ("first", "second", "third")), |
| 146 |
(MatchQuery("year", "2003"), ("first", "second")), |
| 147 |
(NoneQuery("rg_track_gain"), ()), |
| 148 |
(NumericQuery("year", "2001..2002"), ("third",)), |
| 149 |
( |
| 150 |
AndQuery( |
| 151 |
[BooleanQuery("comp", True), NumericQuery("year", "2002")] |
| 152 |
), |
| 153 |
("first", "third"), |
| 154 |
), |
| 155 |
( |
| 156 |
OrQuery( |
| 157 |
[BooleanQuery("comp", True), NumericQuery("year", "2002")] |
| 158 |
), |
| 159 |
("third",), |
| 160 |
), |
| 161 |
(RegexpQuery("artist", "^t"), ("first",)), |
| 162 |
(SubstringQuery("album", "ba"), ("third",)), |
| 163 |
(TrueQuery(), ()), |
| 164 |
], |
| 165 |
) |
| 166 |
def test_query_logic(self, lib, q, expected_titles): |
| 167 |
def get_results(*args): |
| 168 |
return {i.title for i in lib.items(*args)} |
| 169 |
|
| 170 |
# not(a and b) <-> not(a) or not(b) |
| 171 |
not_q = NotQuery(q) |
| 172 |
not_q_results = get_results(not_q) |
| 173 |
assert not_q_results == set(expected_titles) |
| 174 |
|
| 175 |
# assert using OrQuery, AndQuery |
| 176 |
q_or = OrQuery([q, not_q]) |
| 177 |
|
| 178 |
q_and = AndQuery([q, not_q]) |
| 179 |
assert get_results(q_or) == {"first", "second", "third"} |
| 180 |
assert get_results(q_and) == set() |
| 181 |
|
| 182 |
# assert manually checking the item titles |
| 183 |
all_titles = get_results() |
| 184 |
q_results = get_results(q) |
| 185 |
assert q_results.union(not_q_results) == all_titles |
| 186 |
assert q_results.intersection(not_q_results) == set() |
| 187 |
|
| 188 |
# round trip |
| 189 |
not_not_q = NotQuery(not_q) |
| 190 |
assert get_results(q) == get_results(not_not_q) |
| 191 |
|
| 192 |
@pytest.mark.parametrize( |
| 193 |
"q, expected_titles", |
| 194 |
[ |
| 195 |
("-artist::t.+r", ["first", "second"]), |
| 196 |
("-:t$", ["second", "third"]), |
| 197 |
("sec -bar", ["second"]), |
| 198 |
("sec -title:bar", ["second"]), |
| 199 |
("-ond", ["first", "third"]), |
| 200 |
("^ond", ["first", "third"]), |
| 201 |
("^title:sec", ["first", "third"]), |
| 202 |
("-title:sec", ["first", "third"]), |
| 203 |
], |
| 204 |
) |
| 205 |
def test_negation_prefix(self, lib, q, expected_titles): |
| 206 |
actual_titles = {i.title for i in lib.items(q)} |
| 207 |
assert actual_titles == set(expected_titles) |
| 208 |
|
| 209 |
@pytest.mark.parametrize( |
| 210 |
"make_q", |
| 211 |
[ |
| 212 |
partial(DateQuery, "added", "2001-01-01"), |
| 213 |
partial(MatchQuery, "artist", "one"), |
| 214 |
partial(NoneQuery, "rg_track_gain"), |
| 215 |
partial(NumericQuery, "year", "2002"), |
| 216 |
partial(StringQuery, "year", "2001"), |
| 217 |
partial(RegexpQuery, "album", "^.a"), |
| 218 |
partial(SubstringQuery, "title", "x"), |
| 219 |
], |
| 220 |
) |
| 221 |
def test_fast_vs_slow(self, lib, make_q): |
| 222 |
"""Test that the results are the same regardless of the `fast` flag |
| 223 |
for negated `FieldQuery`s. |
| 224 |
""" |
| 225 |
q_fast = make_q(True) |
| 226 |
q_slow = make_q(False) |
| 227 |
|
| 228 |
assert list(map(dict, lib.items(q_fast))) == list( |
| 229 |
map(dict, lib.items(q_slow)) |
| 230 |
) |
| 231 |
|
| 232 |
@pytest.mark.parametrize( |
| 233 |
"q, legacy_field", |
| 234 |
[ |
| 235 |
pytest.param("genres::rock", None, id="non-legacy-genres-field"), |
| 236 |
pytest.param("genre::rock", "genre", id="legacy-genre-field"), |
| 237 |
pytest.param( |
| 238 |
"composers::composer", None, id="non-legacy-composer-field" |
| 239 |
), |
| 240 |
pytest.param( |
| 241 |
"composer::composer", "composer", id="legacy-composer-field" |
| 242 |
), |
| 243 |
], |
| 244 |
) |
| 245 |
def test_legacy_field(self, caplog, lib, q, legacy_field): |
| 246 |
with caplog.at_level(logging.WARNING, logger="beets"): |
| 247 |
actual_titles = {i.title for i in lib.items(q)} |
| 248 |
|
| 249 |
assert actual_titles == {"first"} |
| 250 |
if legacy_field: |
| 251 |
assert caplog.records, "No log records were captured" |
| 252 |
assert len(caplog.records) == 1 |
| 253 |
message = str(caplog.records[0].msg) |
| 254 |
assert f"The '{legacy_field}' field is deprecated" in message |
| 255 |
assert f"Use '{legacy_field}s' instead." in message |
| 256 |
|
| 257 |
|
| 258 |
class TestMatch: |
| 259 |
@pytest.fixture(scope="class") |
| 260 |
def item(self): |
| 261 |
return _common.item( |
| 262 |
album="the album", |
| 263 |
disc=6, |
| 264 |
year=1, |
| 265 |
bitrate=128000, |
| 266 |
genres=["Classical", "Baroque"], |
| 267 |
) |
| 268 |
|
| 269 |
@pytest.mark.parametrize( |
| 270 |
"q, should_match", |
| 271 |
[ |
| 272 |
(RegexpQuery("album", "^the album$"), True), |
| 273 |
(RegexpQuery("album", "^album$"), False), |
| 274 |
(RegexpQuery("disc", "^6$"), True), |
| 275 |
(SubstringQuery("album", "album"), True), |
| 276 |
(SubstringQuery("album", "ablum"), False), |
| 277 |
(SubstringQuery("disc", "6"), True), |
| 278 |
(StringQuery("album", "the album"), True), |
| 279 |
(StringQuery("album", "THE ALBUM"), True), |
| 280 |
(StringQuery("album", "album"), False), |
| 281 |
(MatchQuery("genres", "Classical"), True), |
| 282 |
(MatchQuery("genres", "Neoclassical"), False), |
| 283 |
(StringQuery("genres", "classical"), True), |
| 284 |
(StringQuery("genres", "neoclassical"), False), |
| 285 |
(NumericQuery("year", "1"), True), |
| 286 |
(NumericQuery("year", "10"), False), |
| 287 |
(NumericQuery("bitrate", "100000..200000"), True), |
| 288 |
(NumericQuery("bitrate", "200000..300000"), False), |
| 289 |
(NumericQuery("bitrate", "100000.."), True), |
| 290 |
], |
| 291 |
) |
| 292 |
def test_match(self, item, q, should_match): |
| 293 |
assert q.match(item) == should_match |
| 294 |
assert not NotQuery(q).match(item) == should_match |
| 295 |
|
| 296 |
|
| 297 |
class TestPathQuery: |
| 298 |
"""Tests for path-based querying functionality in the database system. |
| 299 |
|
| 300 |
Verifies that path queries correctly match items by their file paths, |
| 301 |
handling special characters, case sensitivity, parent directories, |
| 302 |
and path separator detection across different platforms. |
| 303 |
""" |
| 304 |
|
| 305 |
@staticmethod |
| 306 |
def abs_query_path(path: str, trailing_sep: bool = False) -> str: |
| 307 |
"""Build a platform-correct absolute query path without normalizing it. |
| 308 |
|
| 309 |
On Windows, leading-slash paths are drive-rooted but Python 3.13 no |
| 310 |
longer treats them as absolute. Prefix the current drive so explicit |
| 311 |
path queries stay absolute while preserving raw segments such as ``..``. |
| 312 |
""" |
| 313 |
if os.path.__name__ == "ntpath" and path.startswith("/"): |
| 314 |
drive, _ = os.path.splitdrive(os.fsdecode(util.normpath(os.sep))) |
| 315 |
path = drive + path |
| 316 |
|
| 317 |
path = path.replace("/", os.sep) |
| 318 |
if trailing_sep: |
| 319 |
path = os.path.join(path, "") |
| 320 |
return path.replace("\\", "\\\\") |
| 321 |
|
| 322 |
@pytest.fixture(scope="class") |
| 323 |
def lib(self, helper): |
| 324 |
helper.add_item(path=b"/aaa/bb/c.mp3", title="path item") |
| 325 |
helper.add_item(path=b"/x/y/z.mp3", title="another item") |
| 326 |
helper.add_item(path=b"/c/_/title.mp3", title="with underscore") |
| 327 |
helper.add_item(path=b"/c/%/title.mp3", title="with percent") |
| 328 |
helper.add_item(path=rb"/c/\x/title.mp3", title="with backslash") |
| 329 |
helper.add_item(path=b"/A/B/C2.mp3", title="caps path") |
| 330 |
|
| 331 |
return helper.lib |
| 332 |
|
| 333 |
@pytest.mark.parametrize( |
| 334 |
"path, expected_titles, trailing_sep", |
| 335 |
[ |
| 336 |
_p("/aaa/bb/c.mp3", ["path item"], False, id="exact-match"), |
| 337 |
_p("/aaa", ["path item"], False, id="parent-dir-no-slash"), |
| 338 |
_p("/aaa", ["path item"], True, id="parent-dir-with-slash"), |
| 339 |
_p("/aa", [], False, id="no-match-does-not-match-parent-dir"), |
| 340 |
_p("/xyzzy", [], True, id="no-match"), |
| 341 |
_p("/b", [], True, id="fragment-no-match"), |
| 342 |
_p("/x/../aaa/bb", ["path item"], False, id="non-normalized"), |
| 343 |
_p(r"c\.mp3$", ["path item"], False, id="regex"), |
| 344 |
_p("/c/_", ["with underscore"], False, id="underscore-escaped"), |
| 345 |
_p("/c/%", ["with percent"], False, id="percent-escaped"), |
| 346 |
_p(r"/c/\x", ["with backslash"], False, id="backslash-escaped"), |
| 347 |
], |
| 348 |
) |
| 349 |
def test_explicit( |
| 350 |
self, monkeypatch, lib, path, expected_titles, trailing_sep |
| 351 |
): |
| 352 |
"""Test explicit path queries with different path specifications.""" |
| 353 |
monkeypatch.setattr("beets.util.case_sensitive", lambda *_: True) |
| 354 |
if path == r"c\.mp3$": |
| 355 |
q = f"path::{path}" |
| 356 |
elif path == r"/c/\x" and os.path.__name__ != "ntpath": |
| 357 |
q = r"path:/c/\\x" |
| 358 |
else: |
| 359 |
q = f"path:{self.abs_query_path(path, trailing_sep=trailing_sep)}" |
| 360 |
|
| 361 |
assert {i.title for i in lib.items(q)} == set(expected_titles) |
| 362 |
|
| 363 |
@pytest.mark.parametrize( |
| 364 |
"query", ["path:", "path::"], ids=["path", "regex"] |
| 365 |
) |
| 366 |
def test_absolute(self, lib, helper, query): |
| 367 |
item_path = helper.lib_path / "item.mp3" |
| 368 |
bytes_path = os.fsencode(item_path) |
| 369 |
helper.add_item(path=bytes_path, title="absolute item") |
| 370 |
q = f"{query}{item_path}".replace("\\", "\\\\") |
| 371 |
|
| 372 |
assert {i.title for i in lib.items(q)} == {"absolute item"} |
| 373 |
|
| 374 |
def test_relative(self, lib, helper): |
| 375 |
item_path = helper.lib_path / "relative" / "item.mp3" |
| 376 |
bytes_path = os.fsencode(item_path) |
| 377 |
helper.add_item(path=bytes_path, title="relative item") |
| 378 |
q = "path:relative/item.mp3" |
| 379 |
|
| 380 |
assert {i.title for i in lib.items(q)} == {"relative item"} |
| 381 |
|
| 382 |
@pytest.mark.skipif(sys.platform == "win32", reason=WIN32_NO_IMPLICIT_PATHS) |
| 383 |
@pytest.mark.parametrize( |
| 384 |
"q, expected_titles", |
| 385 |
[ |
| 386 |
_p("/aaa/bb", ["path item"], id="slashed-query"), |
| 387 |
_p("/aaa/bb , /aaa", ["path item"], id="path-in-or-query"), |
| 388 |
_p("c.mp3", [], id="no-slash-no-match"), |
| 389 |
_p("title:/a/b", [], id="slash-with-explicit-field-no-match"), |
| 390 |
], |
| 391 |
) |
| 392 |
def test_implicit(self, monkeypatch, lib, q, expected_titles): |
| 393 |
"""Test implicit path detection when queries contain path separators.""" |
| 394 |
monkeypatch.setattr( |
| 395 |
"beets.dbcore.query.PathQuery.is_path_query", lambda path: True |
| 396 |
) |
| 397 |
|
| 398 |
assert {i.title for i in lib.items(q)} == set(expected_titles) |
| 399 |
|
| 400 |
@pytest.mark.parametrize( |
| 401 |
"case_sensitive, expected_titles", |
| 402 |
[ |
| 403 |
_p(True, [], id="non-caps-dont-match-caps"), |
| 404 |
_p(False, ["caps path"], id="non-caps-match-caps"), |
| 405 |
], |
| 406 |
) |
| 407 |
def test_case_sensitivity( |
| 408 |
self, lib, monkeypatch, case_sensitive, expected_titles |
| 409 |
): |
| 410 |
"""Test path matching with different case sensitivity settings.""" |
| 411 |
q = f"path:{self.abs_query_path('/a/b/c2.mp3')}" |
| 412 |
monkeypatch.setattr( |
| 413 |
"beets.util.case_sensitive", lambda *_: case_sensitive |
| 414 |
) |
| 415 |
|
| 416 |
assert {i.title for i in lib.items(q)} == set(expected_titles) |
| 417 |
|
| 418 |
# FIXME: Also create a variant of this test for windows, which tests |
| 419 |
# both os.sep and os.altsep |
| 420 |
@pytest.mark.skipif(sys.platform == "win32", reason=WIN32_NO_IMPLICIT_PATHS) |
| 421 |
@pytest.mark.parametrize( |
| 422 |
"q, is_path_query", |
| 423 |
[ |
| 424 |
("/foo/bar", True), |
| 425 |
("foo/bar", True), |
| 426 |
("foo/", True), |
| 427 |
("foo", False), |
| 428 |
("foo/:bar", True), |
| 429 |
("foo:bar/", False), |
| 430 |
("foo:/bar", False), |
| 431 |
], |
| 432 |
) |
| 433 |
def test_path_sep_detection(self, monkeypatch, tmp_path, q, is_path_query): |
| 434 |
"""Test detection of path queries based on the presence of path separators.""" |
| 435 |
monkeypatch.chdir(tmp_path) |
| 436 |
(tmp_path / "foo").mkdir() |
| 437 |
(tmp_path / "foo" / "bar").touch() |
| 438 |
if Path(q).is_absolute(): |
| 439 |
q = str(tmp_path / q[1:]) |
| 440 |
|
| 441 |
assert PathQuery.is_path_query(q) == is_path_query |
| 442 |
|
| 443 |
|
| 444 |
class TestQuery: |
| 445 |
ALBUM = "album title" |
| 446 |
SINGLE = "singleton" |
| 447 |
|
| 448 |
@pytest.fixture(scope="class") |
| 449 |
def lib(self, helper): |
| 450 |
helper.add_album( |
| 451 |
title=self.ALBUM, |
| 452 |
comp=True, |
| 453 |
flexbool=True, |
| 454 |
bpm=120, |
| 455 |
flexint=2, |
| 456 |
rg_track_gain=0, |
| 457 |
) |
| 458 |
helper.add_item( |
| 459 |
title=self.SINGLE, comp=False, flexbool=False, rg_track_gain=None |
| 460 |
) |
| 461 |
|
| 462 |
with pytest.MonkeyPatch.context() as monkeypatch: |
| 463 |
monkeypatch.setattr( |
| 464 |
Item, |
| 465 |
"_types", |
| 466 |
{"flexbool": types.Boolean(), "flexint": types.Integer()}, |
| 467 |
) |
| 468 |
yield helper.lib |
| 469 |
|
| 470 |
@pytest.mark.parametrize("query_class", [MatchQuery, StringFieldQuery]) |
| 471 |
def test_equality(self, query_class): |
| 472 |
assert query_class("foo", "bar") == query_class("foo", "bar") |
| 473 |
|
| 474 |
def test_in_query_hashable(self): |
| 475 |
# smartplaylist puts queries in a set; an InQuery whose pattern is a |
| 476 |
# list used to raise `unhashable type: 'list'`. See #5354. |
| 477 |
query = InQuery("field", [1, 2, 3]) |
| 478 |
assert query in {query} |
| 479 |
|
| 480 |
@pytest.mark.parametrize( |
| 481 |
"make_q, expected_msg", |
| 482 |
[ |
| 483 |
(lambda: NumericQuery("year", "199a"), "not an int"), |
| 484 |
(lambda: RegexpQuery("year", "199("), r"not a regular expression.*unterminated subpattern"), # noqa: E501 |
| 485 |
] |
| 486 |
) # fmt: skip |
| 487 |
def test_invalid_query(self, make_q, expected_msg): |
| 488 |
with pytest.raises(ParsingError, match=expected_msg): |
| 489 |
make_q() |
| 490 |
|
| 491 |
@pytest.mark.parametrize( |
| 492 |
"q, expected_titles", |
| 493 |
[ |
| 494 |
# Boolean value |
| 495 |
_p("comp:true", {ALBUM}, id="parse-true"), |
| 496 |
_p("flexbool:true", {ALBUM}, id="flex-parse-true"), |
| 497 |
_p("flexbool:false", {SINGLE}, id="flex-parse-false"), |
| 498 |
_p("flexbool:1", {ALBUM}, id="flex-parse-1"), |
| 499 |
_p("flexbool:0", {SINGLE}, id="flex-parse-0"), |
| 500 |
# TODO: shouldn't this match 1 / true instead? |
| 501 |
_p("flexbool:something", {SINGLE}, id="flex-parse-true"), |
| 502 |
# Integer value |
| 503 |
_p("bpm:120", {ALBUM}, id="int-exact-value"), |
| 504 |
_p("bpm:110..125", {ALBUM}, id="int-range"), |
| 505 |
_p("flexint:2", {ALBUM}, id="int-flex"), |
| 506 |
_p("flexint:3", set(), id="int-no-match"), |
| 507 |
_p("bpm:12", set(), id="int-dont-match-substring"), |
| 508 |
# None value |
| 509 |
_p(NoneQuery("album_id"), {SINGLE}, id="none-match-singleton"), |
| 510 |
_p(NoneQuery("rg_track_gain"), {SINGLE}, id="none-value"), |
| 511 |
], |
| 512 |
) |
| 513 |
def test_value_type(self, lib, q, expected_titles): |
| 514 |
assert {i.title for i in lib.items(q)} == expected_titles |
| 515 |
|
| 516 |
|
| 517 |
class TestDefaultSearchFields: |
| 518 |
@pytest.fixture(scope="class") |
| 519 |
def lib(self, helper): |
| 520 |
helper.add_album( |
| 521 |
title="title", |
| 522 |
album="album", |
| 523 |
albumartist="albumartist", |
| 524 |
catalognum="catalognum", |
| 525 |
year=2001, |
| 526 |
) |
| 527 |
|
| 528 |
return helper.lib |
| 529 |
|
| 530 |
@pytest.mark.parametrize( |
| 531 |
"entity, q, should_match", |
| 532 |
[ |
| 533 |
_p("albums", "album", True, id="album-match-album"), |
| 534 |
_p("albums", "albumartist", True, id="album-match-albumartist"), |
| 535 |
_p("albums", "catalognum", False, id="album-dont-match-catalognum"), |
| 536 |
_p("items", "title", True, id="item-match-title"), |
| 537 |
_p("items", "2001", False, id="item-dont-match-year"), |
| 538 |
], |
| 539 |
) |
| 540 |
def test_search(self, lib, entity, q, should_match): |
| 541 |
assert bool(getattr(lib, entity)(q)) == should_match |
| 542 |
|
| 543 |
|
| 544 |
class TestRelatedQueries: |
| 545 |
"""Test album-level queries with track-level filters and vice-versa.""" |
| 546 |
|
| 547 |
@pytest.fixture(scope="class") |
| 548 |
def lib(self, helper): |
| 549 |
for album_idx in range(1, 3): |
| 550 |
album_name = f"Album{album_idx}" |
| 551 |
items = [ |
| 552 |
helper.create_item( |
| 553 |
album=album_name, title=f"{album_name} Item{idx}" |
| 554 |
) |
| 555 |
for idx in range(1, 3) |
| 556 |
] |
| 557 |
album = helper.lib.add_album(items) |
| 558 |
album.artpath = f"{album_name} Artpath" |
| 559 |
album.catalognum = "ABC" |
| 560 |
album.store() |
| 561 |
|
| 562 |
return helper.lib |
| 563 |
|
| 564 |
@pytest.mark.parametrize( |
| 565 |
"q, expected_titles, expected_albums", |
| 566 |
[ |
| 567 |
_p( |
| 568 |
"title:Album1", |
| 569 |
["Album1 Item1", "Album1 Item2"], |
| 570 |
["Album1"], |
| 571 |
id="match-album-with-item-field-query", |
| 572 |
), |
| 573 |
_p( |
| 574 |
"title:Item2", |
| 575 |
["Album1 Item2", "Album2 Item2"], |
| 576 |
["Album1", "Album2"], |
| 577 |
id="match-albums-with-item-field-query", |
| 578 |
), |
| 579 |
_p( |
| 580 |
"artpath::Album1", |
| 581 |
["Album1 Item1", "Album1 Item2"], |
| 582 |
["Album1"], |
| 583 |
id="match-items-with-album-field-query", |
| 584 |
), |
| 585 |
_p( |
| 586 |
"catalognum:ABC Album1", |
| 587 |
["Album1 Item1", "Album1 Item2"], |
| 588 |
["Album1"], |
| 589 |
id="query-field-common-to-album-and-item", |
| 590 |
), |
| 591 |
], |
| 592 |
) |
| 593 |
def test_related_query(self, lib, q, expected_titles, expected_albums): |
| 594 |
assert {i.album for i in lib.albums(q)} == set(expected_albums) |
| 595 |
assert {i.title for i in lib.items(q)} == set(expected_titles) |
| 596 |
|
| 597 |
|
| 598 |
class TestHasCoverArtQuery: |
| 599 |
"""Test has_cover_art computed field for detecting embedded cover art.""" |
| 600 |
|
| 601 |
@pytest.fixture(scope="class") |
| 602 |
def lib(self, helper): |
| 603 |
item_with = helper.add_item_fixture() |
| 604 |
item_with.title = "with_art" |
| 605 |
|
| 606 |
path_with = helper.create_mediafile_fixture(images=["jpg"]) |
| 607 |
item_with["path"] = path_with |
| 608 |
item_with.store() |
| 609 |
|
| 610 |
path_without = helper.create_mediafile_fixture(images=[]) |
| 611 |
item_without = helper.add_item_fixture() |
| 612 |
item_without.title = "without_art" |
| 613 |
item_without["path"] = path_without |
| 614 |
item_without.store() |
| 615 |
|
| 616 |
return helper.lib |
| 617 |
|
| 618 |
@pytest.mark.parametrize( |
| 619 |
"query, expected_titles", |
| 620 |
[ |
| 621 |
("has_cover_art:true", {"with_art"}), |
| 622 |
("has_cover_art:false", {"without_art"}), |
| 623 |
], |
| 624 |
) |
| 625 |
def test_has_cover_art_query(self, lib, query, expected_titles): |
| 626 |
assert {i.title for i in lib.items(query)} == expected_titles |