music-assistant-server

4.7 KBPY
test_helpers.py
4.7 KB115 lines • python
1"""Tests for utility/helper functions."""
2
3import pytest
4
5from music_assistant.providers.filesystem_local import helpers
6
7# ruff: noqa: S108
8
9
10def test_get_artist_dir() -> None:
11    """Test the extraction of an artist dir."""
12    album_path = "/tmp/Artist/Album"
13    artist_name = "Artist"
14    assert helpers.get_artist_dir(artist_name, album_path) == "/tmp/Artist"
15    album_path = "/tmp/artist/Album"
16    assert helpers.get_artist_dir(artist_name, album_path) == "/tmp/artist"
17    album_path = "/tmp/Album"
18    assert helpers.get_artist_dir(artist_name, album_path) is None
19    album_path = "/tmp/ARTIST!/Album"
20    assert helpers.get_artist_dir(artist_name, album_path) == "/tmp/ARTIST!"
21    album_path = "/tmp/Artist/Album"
22    artist_name = "Artist!"
23    assert helpers.get_artist_dir(artist_name, album_path) == "/tmp/Artist"
24    album_path = "/tmp/REM/Album"
25    artist_name = "R.E.M."
26    assert helpers.get_artist_dir(artist_name, album_path) == "/tmp/REM"
27    album_path = "/tmp/ACDC/Album"
28    artist_name = "AC/DC"
29    assert helpers.get_artist_dir(artist_name, album_path) == "/tmp/ACDC"
30    album_path = "/tmp/Celine Dion/Album"
31    artist_name = "Céline Dion"
32    assert helpers.get_artist_dir(artist_name, album_path) == "/tmp/Celine Dion"
33    album_path = "/tmp/Antonin Dvorak/Album"
34    artist_name = "Antonín Dvořák"
35    assert helpers.get_artist_dir(artist_name, album_path) == "/tmp/Antonin Dvorak"
36
37
38@pytest.mark.parametrize(
39    ("album_name", "track_dir", "expected"),
40    [
41        # Test literal match
42        (
43            "Selected Ambient Works 85-92",
44            "/home/user/Music/Aphex Twin/Selected Ambient Works 85-92",
45            "/home/user/Music/Aphex Twin/Selected Ambient Works 85-92",
46        ),
47        # Test artist - album format
48        (
49            "Selected Ambient Works 85-92",
50            "/home/user/Music/Aphex Twin - Selected Ambient Works 85-92",
51            "/home/user/Music/Aphex Twin - Selected Ambient Works 85-92",
52        ),
53        # Test artist - album (version) format
54        (
55            "Selected Ambient Works 85-92",
56            "/home/user/Music/Aphex Twin - Selected Ambient Works 85-92 (Remastered)",
57            "/home/user/Music/Aphex Twin - Selected Ambient Works 85-92 (Remastered)",
58        ),
59        # Test artist - album (version) format
60        (
61            "Selected Ambient Works 85-92",
62            "/home/user/Music/Aphex Twin - Selected Ambient Works 85-92 (Remastered) - WEB",
63            "/home/user/Music/Aphex Twin - Selected Ambient Works 85-92 (Remastered) - WEB",
64        ),
65        # Test tokenizer - dirname with extras
66        (
67            "Fokus - Prewersje",
68            "/home/user/Fokus-Prewersje-PL-WEB-FLAC-2021-PS_INT",
69            "/home/user/Fokus-Prewersje-PL-WEB-FLAC-2021-PS_INT",
70        ),
71        # Test tokenizer - dirname with version and extras
72        (
73            "Layo And Bushwacka - Night Works",
74            "/home/music/Layo_And_Bushwacka-Night_Works_(Reissue)-(XLCD_154X)-FLAC-2003",
75            "/home/music/Layo_And_Bushwacka-Night_Works_(Reissue)-(XLCD_154X)-FLAC-2003",
76        ),
77        # Test tokenizer - extras and approximate match on diacratics
78        (
79            "Łona i Webber - Wyślij Sobie Pocztówkę",
80            "/usr/others/Lona-Discography-PL-FLAC-2020-INT/Lona_I_Webber-Wyslij_Sobie_Pocztowke-PL-WEB-FLAC-2014-PS",
81            "/usr/others/Lona-Discography-PL-FLAC-2020-INT/Lona_I_Webber-Wyslij_Sobie_Pocztowke-PL-WEB-FLAC-2014-PS",
82        ),
83        (
84            "NIC",
85            "/nas/downloads/others/Sokol-NIC-PL-WEB-FLAC-2021",
86            "/nas/downloads/others/Sokol-NIC-PL-WEB-FLAC-2021",
87        ),
88        # Test album (version) format
89        (
90            "Aphex Twin - Selected Ambient Works 85-92",
91            "/home/user/Music/Aphex Twin/Selected Ambient Works 85-92 (Remastered)",
92            "/home/user/Music/Aphex Twin/Selected Ambient Works 85-92 (Remastered)",
93        ),
94        # Test album name in dir
95        (
96            "Aphex Twin - Selected Ambient Works 85-92",
97            "/home/user/Music/RandomDirWithAphex Twin - Selected Ambient Works 85-92InIt",
98            "/home/user/Music/RandomDirWithAphex Twin - Selected Ambient Works 85-92InIt",
99        ),
100        # Test no match
101        (
102            "NonExistentAlbumName",
103            "/home/user/Music/Aphex Twin/Selected Ambient Works 85-92",
104            None,
105        ),
106        # Test empty album name
107        ("", "/home/user/Music/Aphex Twin/Selected Ambient Works 85-92", None),
108        # Test empty track dir
109        ("Selected Ambient Works 85-92", "", None),
110    ],
111)
112def test_get_album_dir(album_name: str, track_dir: str, expected: str) -> None:
113    """Test the extraction of an album dir."""
114    assert helpers.get_album_dir(track_dir, album_name) == expected
115