refactor scanner, add progress bar

This commit is contained in:
evilchili
2022-12-21 15:17:13 -08:00
parent fe671194a0
commit 7c82226ff9
13 changed files with 339 additions and 173 deletions
+1
View File
@@ -19,6 +19,7 @@ def env():
load_dotenv(Path('test/fixtures/env'))
os.environ['GROOVE_ON_DEMAND_ROOT'] = str(root)
os.environ['MEDIA_ROOT'] = str(root / Path('media'))
os.environ['DATABASE_PATH'] = ''
return os.environ
-3
View File
@@ -2,9 +2,6 @@
GROOVE_ON_DEMAND_ROOT=.
MEDIA_ROOT=.
# where to store the database
DATABASE_PATH=test.db
# Admin user credentials
USERNAME=test_username
PASSWORD=test_password
+7 -1
View File
@@ -14,10 +14,12 @@ def test_missing_media_root(monkeypatch, root):
with pytest.raises(ConfigurationError):
path.media_root()
def test_static(monkeypatch):
assert path.static('foo')
assert path.static('foo', theme=themes.load_theme('default_theme'))
@pytest.mark.parametrize('root', ['/dev/null/missing', None])
def test_missing_theme_root(monkeypatch, root):
broken_env = {k: v for (k, v) in os.environ.items()}
@@ -32,5 +34,9 @@ def test_theme_no_path():
path.theme('nope')
def test_database_default(env):
assert path.database().relative_to(path.root())
def test_database(env):
assert env['DATABASE_PATH'] in path.database().name
assert env['DATABASE_PATH'] in str(path.database().absolute())
+6 -38
View File
@@ -8,55 +8,23 @@ import groove.exceptions
from groove.db import scanner, track
fixture_tracks = [
"/test/Spookey Ruben/Modes of Transportation, Volume 1/Spookey Ruben - Modes of Transportation, Volume 1 - 01 Terra Magnifica.flac",
"/test/Spookey Ruben/Modes of Transportation, Volume 1/Spookey Ruben - Modes of Transportation, Volume 1 - 02 These Days Are Old.flac",
"/test/Spookey Ruben/Modes of Transportation, Volume 1/Spookey Ruben - Modes of Transportation, Volume 1 - 03 Crystal Cradle.flac",
"/test/Spookey Ruben/Modes of Transportation, Volume 1/Spookey Ruben - Modes of Transportation, Volume 1 - 04 Running Away.flac",
"/test/Spookey Ruben/Modes of Transportation, Volume 1/Spookey Ruben - Modes of Transportation, Volume 1 - 05 Welcome to the House of Food.flac",
"/test/Spookey Ruben/Modes of Transportation, Volume 1/Spookey Ruben - Modes of Transportation, Volume 1 - 06 Wendy McDonald.flac",
"/test/Spookey Ruben/Modes of Transportation, Volume 1/Spookey Ruben - Modes of Transportation, Volume 1 - 07 The Size of You.flac",
"/test/Spookey Ruben/Modes of Transportation, Volume 1/Spookey Ruben - Modes of Transportation, Volume 1 - 08 Its Not What You Do Its You.flac",
"/test/Spookey Ruben/Modes of Transportation, Volume 1/Spookey Ruben - Modes of Transportation, Volume 1 - 09 Mars.flac",
"/test/Spookey Ruben/Modes of Transportation, Volume 1/Spookey Ruben - Modes of Transportation, Volume 1 - 10 Leave the City.flac",
"/test/Spookey Ruben/Modes of Transportation, Volume 1/Spookey Ruben - Modes of Transportation, Volume 1 - 11 Growing Up is Over.flac",
"/test/Spookey Ruben/Modes of Transportation, Volume 1/Spookey Ruben - Modes of Transportation, Volume 1 - 12 Donate Your Heart to a Stranger....flac",
"/test/Spookey Ruben/Modes of Transportation, Volume 1/Spookey Ruben - Modes of Transportation, Volume 1 - 13 Life Insurance.flac",
]
@pytest.fixture
def media():
def fixture():
for t in fixture_tracks:
yield Path(t)
return fixture
def test_scanner(monkeypatch, in_memory_db, media):
# replace the filesystem glob with the test fixture generator
monkeypatch.setattr(scanner.MediaScanner, 'find_sources', MagicMock(return_value=media()))
def test_scanner(monkeypatch, in_memory_db):
def mock_loader(path):
return {
'artist': 'foo',
'title': 'bar',
}
# replace music_tag so it doesn't try to read things
monkeypatch.setattr(scanner.MediaScanner, '_get_tags', MagicMock(side_effect=mock_loader))
test_scanner = scanner.media_scanner(root=Path('/test'), db=in_memory_db)
expected = len(fixture_tracks)
test_scanner = scanner.MediaScanner(path=Path('UNKLE'), db=in_memory_db)
# verify all entries are scanned
assert test_scanner.scan() == expected
assert test_scanner.scan() == 1
# readback; verify entries are in the db
query = func.count(track.c.relpath)
query = query.filter(track.c.relpath.ilike('%Spookey%'))
assert in_memory_db.query(query).scalar() == expected
query = query.filter(track.c.relpath.ilike('%UNKLE%'))
assert in_memory_db.query(query).scalar() == 1
# verify idempotency
assert test_scanner.scan() == 0
@@ -65,4 +33,4 @@ def test_scanner(monkeypatch, in_memory_db, media):
def test_scanner_no_media_root(in_memory_db):
del os.environ['MEDIA_ROOT']
with pytest.raises(groove.exceptions.ConfigurationError):
assert scanner.media_scanner(root=None, db=in_memory_db)
assert scanner.MediaScanner(path=None, db=in_memory_db)