Skip to content

Commit d91e8db

Browse files
committed
test: adapt to impl and fixes
1 parent c7d1450 commit d91e8db

File tree

5 files changed

+118
-37
lines changed

5 files changed

+118
-37
lines changed

test/data/navidrome.db

0 Bytes
Binary file not shown.

test/test_beets.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import pytest
2+
3+
from ndtoolbox.beets import BeetsClient
4+
from ndtoolbox.config import config
5+
6+
config.set_file("test/config/config.yaml")
7+
8+
9+
def test_beets_client():
10+
"""Test Beets client."""
11+
client = BeetsClient()
12+
assert client is not None

test/test_model.py

Lines changed: 70 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
"""Test models."""
22

3+
import subprocess
34
from typing import Generator
45

56
import pytest
67
from easydict import EasyDict
78

89
from ndtoolbox.beets import BeetsClient
10+
from ndtoolbox.config import config
911
from ndtoolbox.model import Folder, MediaFile
10-
from ndtoolbox.utils import ToolboxConfig
1112

12-
ND_DATABASE_PATH = "test/data/navidrome.db"
13-
ND_DATA_FILE = "data/nd-toolbox-data.json"
14-
ND_DATA_FILE = "test/data/nd-toolbox-data.json"
15-
DATA_DIR = "test/data"
16-
BEETS_BASE_PATH = "/music"
17-
ND_BASE_PATH = "/music/library"
18-
config = ToolboxConfig(False)
13+
config.set_file("test/config/config.yaml")
1914

2015

2116
@pytest.fixture(scope="session")
@@ -32,16 +27,38 @@ def infos2() -> Generator[EasyDict]:
3227
yield [info, info]
3328

3429

30+
@pytest.fixture(scope="session")
31+
def subprocess_out():
32+
"""Fixture to provide Beets subprocess output."""
33+
result = """:::0:::-3:::False
34+
Tschuldigung.:::11:::10:::False
35+
Herz für die Sache:::0:::-94:::True
36+
Keine Macht für Niemand:::16:::11:::True
37+
Tschuldigung.:::11:::8:::True"""
38+
yield result
39+
40+
3541
def test_album_folder(infos, mocker):
3642
"""Test album folder."""
3743
mocker.patch.object(BeetsClient, "get_album_info", autospec=True)
3844
BeetsClient.get_album_info.return_value = infos
3945

4046
media = MediaFile(
41-
"1", "/music/artist/album/track.mp3", "title", 2003, 1, 33, 64, "1", "artist", "2", "album", "mbz3"
47+
"1",
48+
"/music/artist/album/track.mp3",
49+
"title",
50+
2003,
51+
1,
52+
33,
53+
64,
54+
"1",
55+
"artist",
56+
"2",
57+
"album",
58+
"mbz3",
59+
beets_path="/music/artist/album/track.mp",
4260
)
43-
media.beets_path = "/music/artist/album/track.mp3"
44-
folder = Folder(media)
61+
folder = Folder.of_media(media)
4562
assert folder.beets_path == "/music/artist/album"
4663
assert folder.has_keepable is False
4764
assert folder.is_dirty is False
@@ -54,11 +71,49 @@ def test_album_folder_dirty(infos2, mocker):
5471
BeetsClient.get_album_info.return_value = infos2
5572

5673
media = MediaFile(
57-
"1", "/music/artist/album/track.mp3", "title", 2003, 1, 33, 64, "1", "artist", "2", "album", "mbz3"
74+
"1",
75+
"/music/artist/album/track.mp3",
76+
"title",
77+
2003,
78+
1,
79+
33,
80+
64,
81+
"1",
82+
"artist",
83+
"2",
84+
"album",
85+
"mbz3",
86+
beets_path="/music/artist/album/track.mp",
5887
)
59-
media.beets_path = "/music/artist/album/track.mp3"
60-
folder = Folder(media)
88+
folder = media.folder
6189
assert folder.beets_path == "/music/artist/album"
6290
assert folder.has_keepable is False
6391
assert folder.is_dirty is True
64-
assert folder.type == Folder.Type.ALBUM
92+
assert folder.type == Folder.Type.UNKNOWN
93+
94+
95+
def test_album_folder_dirty_beets(subprocess_out, mocker):
96+
"""Test mayday folder."""
97+
mocker.patch.object(subprocess, "check_output", autospec=True)
98+
subprocess.check_output.return_value = subprocess_out
99+
100+
media = MediaFile(
101+
"1",
102+
"/music/artist/album/track.mp3",
103+
"title",
104+
2003,
105+
1,
106+
33,
107+
64,
108+
"1",
109+
"artist",
110+
"2",
111+
"album",
112+
"mbz3",
113+
beets_path="/music/artist/album/track.mp",
114+
)
115+
folder = media.folder
116+
assert folder.beets_path == "/music/artist/album"
117+
assert folder.has_keepable is False
118+
assert folder.is_dirty is True
119+
assert folder.type == Folder.Type.UNKNOWN

test/test_navidrome_db.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22

33
import pytest
44

5+
from ndtoolbox.app import DataCache
6+
from ndtoolbox.config import config
57
from ndtoolbox.db import Annotation, NavidromeDb, NavidromeDbConnection
68
from ndtoolbox.model import MediaFile
7-
from ndtoolbox.utils import ToolboxConfig
9+
10+
config.set_file("test/config/config.yaml")
811

912
# Example data to use for testing
10-
TEST_DB_PATH = "test/data/navidrome.db"
1113
TEST_USER_ID = "b67d5135-cf67-4544-8013-d0f7c2d8a65a"
1214

1315
test_media_file = MediaFile(
1416
id="111",
15-
path="/music/foobar/dummy1.mp3",
17+
path="/music/library/foobar/dummy1.mp3",
1618
title="Foo Bar",
1719
year=2005,
1820
track_number=3,
@@ -24,6 +26,7 @@
2426
album_name=None,
2527
mbz_recording_id=None,
2628
)
29+
test_media_file.beets_path = "/music/foobar/dummy1.mp3"
2730

2831
test_anno = Annotation(
2932
item_type=Annotation.Type.media_file,
@@ -39,8 +42,8 @@
3942
@pytest.fixture(scope="session")
4043
def db():
4144
"""Fixture to provide a database connection for tests."""
42-
ToolboxConfig()
43-
db = NavidromeDb(db_path=TEST_DB_PATH)
45+
db_path = config["navidrome"]["database"].get(str)
46+
db = NavidromeDb(db_path, DataCache())
4447
yield db
4548

4649

@@ -60,7 +63,7 @@ def test_get_media(db, mocker):
6063

6164
# Retrieve the media file from the database
6265
with NavidromeDbConnection() as conn:
63-
media_file = db.get_media(test_media_file.path, conn)
66+
media_file = db.get_media((test_media_file.beets_path, test_media_file.path), conn)
6467

6568
print(f"Media file: {media_file}")
6669
print(f"File annotation: {media_file.annotation}")

test/test_processor.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,10 @@
77
import pytest
88

99
from ndtoolbox.app import DuplicateProcessor
10+
from ndtoolbox.config import config
1011
from ndtoolbox.model import Album, Annotation, Artist, MediaFile
11-
from ndtoolbox.utils import ToolboxConfig
1212

13-
ND_DATABASE_PATH = "test/data/navidrome.db"
14-
ND_DATA_FILE = "data/nd-toolbox-data.json"
15-
ND_DATA_FILE = "test/data/nd-toolbox-data.json"
16-
DATA_DIR = "test/data"
17-
BEETS_BASE_PATH = "/app/music"
18-
ND_BASE_PATH = "/music/library"
13+
config.set_file("test/config/config.yaml")
1914

2015
FILES = [
2116
MediaFile(
@@ -31,6 +26,7 @@
3126
album_id=None,
3227
album_name=None,
3328
mbz_recording_id="recording-1",
29+
beets_path="/music/path/library/to/file1.mp3",
3430
),
3531
MediaFile(
3632
id="22",
@@ -45,6 +41,7 @@
4541
album_id=None,
4642
album_name=None,
4743
mbz_recording_id="recording-2",
44+
beets_path="/music/path/library/to/file2 2.mp3",
4845
),
4946
MediaFile(
5047
id="33",
@@ -59,6 +56,7 @@
5956
album_id=None,
6057
album_name=None,
6158
mbz_recording_id="recording-3",
59+
beets_path="/music/path/library/to/file3.mp3",
6260
),
6361
MediaFile(
6462
id="44",
@@ -73,6 +71,7 @@
7371
album_id=None,
7472
album_name=None,
7573
mbz_recording_id="recording-3",
74+
beets_path="/music/path/library/to/file4.mp3",
7675
),
7776
]
7877
# Set annotations for each file
@@ -91,19 +90,14 @@
9190
@pytest.fixture(scope="session")
9291
def processor():
9392
"""Fixture to create a DuplicateProcessor instance."""
94-
config = ToolboxConfig(False)
95-
config.navidrome_db_path = ND_DATABASE_PATH
96-
config.data_folder = DATA_DIR
97-
config.source_base = BEETS_BASE_PATH
98-
config.target_base = ND_BASE_PATH
99-
processor = DuplicateProcessor(config)
93+
processor = DuplicateProcessor()
10094
yield processor
10195

10296

10397
def test_encode_decode_json_pickle(processor: DuplicateProcessor):
10498
"""Test encoding and decoding of JSON using jsonpickle."""
10599
file_path = "test/data/test-data.json"
106-
data = {"dups_media_files": FILES, "stats": "bbb", "errors": []}
100+
data = {"duplicates": FILES, "stats": "bbb", "errors": []}
107101
with open(file_path, "w", encoding="utf-8") as file:
108102
file.write(jsonpickle.encode(data, indent=4, keys=True))
109103

@@ -124,6 +118,7 @@ def test_merge_annotation_data(processor: DuplicateProcessor):
124118
album_id=None,
125119
album_name=None,
126120
mbz_recording_id="recording-1",
121+
beets_path="/music/path/library/to/file1.mp3",
127122
)
128123
a.annotation = Annotation(
129124
item_id="1",
@@ -148,6 +143,7 @@ def test_merge_annotation_data(processor: DuplicateProcessor):
148143
album_id=None,
149144
album_name=None,
150145
mbz_recording_id="recording-2",
146+
beets_path="/music/path/library/to/file1.mp3",
151147
)
152148
b.annotation = Annotation(
153149
item_id="2",
@@ -172,6 +168,7 @@ def test_merge_annotation_data(processor: DuplicateProcessor):
172168
album_id=None,
173169
album_name=None,
174170
mbz_recording_id="recording-2",
171+
beets_path="/music/path/library/to/file3.mp3",
175172
)
176173
c.annotation = Annotation(
177174
item_id="3",
@@ -280,6 +277,7 @@ def test_get_keepable_media(processor: DuplicateProcessor):
280277
album_id=0,
281278
album_name=None,
282279
mbz_recording_id=None,
280+
beets_path="/music/path/library/to/file1.mp3",
283281
),
284282
MediaFile(
285283
id="12",
@@ -294,6 +292,7 @@ def test_get_keepable_media(processor: DuplicateProcessor):
294292
album_id=0,
295293
album_name=None,
296294
mbz_recording_id=None,
295+
beets_path="/music/path/library/to/file1.mp3",
297296
),
298297
MediaFile(
299298
id="13",
@@ -308,6 +307,7 @@ def test_get_keepable_media(processor: DuplicateProcessor):
308307
album_id=0,
309308
album_name=None,
310309
mbz_recording_id=None,
310+
beets_path="/music/path/library/to/file3.mp3",
311311
),
312312
MediaFile(
313313
id="14",
@@ -322,6 +322,7 @@ def test_get_keepable_media(processor: DuplicateProcessor):
322322
album_id=0,
323323
album_name=None,
324324
mbz_recording_id=None,
325+
beets_path="/music/path/library/to/file4.mp3",
325326
),
326327
MediaFile(
327328
id="15",
@@ -336,6 +337,7 @@ def test_get_keepable_media(processor: DuplicateProcessor):
336337
album_id=0,
337338
album_name=None,
338339
mbz_recording_id=None,
340+
beets_path="/music/path/library/to/file4.mp3",
339341
),
340342
MediaFile(
341343
id="16",
@@ -350,6 +352,7 @@ def test_get_keepable_media(processor: DuplicateProcessor):
350352
album_id=0,
351353
album_name=None,
352354
mbz_recording_id="musicBrainzID",
355+
beets_path="/music/path/library/to/file4.mp3",
353356
),
354357
MediaFile(
355358
id="17",
@@ -364,6 +367,7 @@ def test_get_keepable_media(processor: DuplicateProcessor):
364367
album_id=0,
365368
album_name=None,
366369
mbz_recording_id="musicBrainzID",
370+
beets_path="/music/path/library/to/file4.mp3",
367371
),
368372
# That's a keeper:
369373
MediaFile(
@@ -379,6 +383,7 @@ def test_get_keepable_media(processor: DuplicateProcessor):
379383
album_id=0,
380384
album_name=None,
381385
mbz_recording_id="musicBrainzID",
386+
beets_path="/music/path/library/to/file4.mp3",
382387
),
383388
]
384389

@@ -397,6 +402,7 @@ def test_get_keepable_media(processor: DuplicateProcessor):
397402
album_id=0,
398403
album_name=None,
399404
mbz_recording_id="musicBrainzID",
405+
beets_path="/music/path/library/to/file4.mp3",
400406
),
401407
MediaFile(
402408
id="2000",
@@ -411,6 +417,7 @@ def test_get_keepable_media(processor: DuplicateProcessor):
411417
album_id=0,
412418
album_name=None,
413419
mbz_recording_id=None,
420+
beets_path="/music/path/library/to/file4.mp3",
414421
),
415422
]
416423

@@ -429,6 +436,7 @@ def test_get_keepable_media(processor: DuplicateProcessor):
429436
album_id=0,
430437
album_name=None,
431438
mbz_recording_id=None,
439+
beets_path="/music/path/library/to/file4.mp3",
432440
),
433441
MediaFile(
434442
id="artist_keeper_id",
@@ -443,6 +451,7 @@ def test_get_keepable_media(processor: DuplicateProcessor):
443451
album_id=0,
444452
album_name=None,
445453
mbz_recording_id=None,
454+
beets_path="/music/path/library/to/file4.mp3",
446455
),
447456
]
448457

@@ -459,15 +468,17 @@ def test_get_keepable_media(processor: DuplicateProcessor):
459468
keeper = processor._get_keepable_media(dups2)
460469
assert keeper.id == dups2[0].id
461470
assert keeper.album is not None
462-
assert keeper.album.has_keepable is True
471+
assert keeper.folder is not None
472+
assert keeper.folder.has_keepable is True
463473
assert keeper.title == "The Song"
464474
assert keeper.bitrate == 1024
465475

466476
# Now, eval the first list
467477
keeper = processor._get_keepable_media(dups)
468478
assert keeper.id == dups[7].id
469479
assert keeper.album is not None
470-
assert keeper.album.has_keepable is True
480+
assert keeper.folder is not None
481+
assert keeper.folder.has_keepable is True
471482
assert keeper.title == "File 8"
472483
assert keeper.bitrate == 320
473484

0 commit comments

Comments
 (0)