grooveondemand/test/test_scanner.py
2022-12-21 15:17:13 -08:00

37 lines
1.0 KiB
Python

import pytest
import os
from pathlib import Path
from unittest.mock import MagicMock
from sqlalchemy import func
import groove.exceptions
from groove.db import scanner, track
def test_scanner(monkeypatch, in_memory_db):
def mock_loader(path):
return {
'artist': 'foo',
'title': 'bar',
}
monkeypatch.setattr(scanner.MediaScanner, '_get_tags', MagicMock(side_effect=mock_loader))
test_scanner = scanner.MediaScanner(path=Path('UNKLE'), db=in_memory_db)
# verify all entries are scanned
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('%UNKLE%'))
assert in_memory_db.query(query).scalar() == 1
# verify idempotency
assert test_scanner.scan() == 0
def test_scanner_no_media_root(in_memory_db):
del os.environ['MEDIA_ROOT']
with pytest.raises(groove.exceptions.ConfigurationError):
assert scanner.MediaScanner(path=None, db=in_memory_db)