test coverage
This commit is contained in:
+5
-10
@@ -5,7 +5,7 @@ from pathlib import Path
|
||||
from dotenv import load_dotenv
|
||||
|
||||
import groove.db
|
||||
from groove.playlist import Playlist
|
||||
from groove.playlist import Playlist
|
||||
|
||||
from sqlalchemy import create_engine, insert
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
@@ -51,9 +51,9 @@ def db(in_memory_db):
|
||||
# create tracks
|
||||
query = insert(groove.db.track)
|
||||
in_memory_db.execute(query, [
|
||||
{'id': 1, 'relpath': 'UNKLE/Psyence Fiction/01 Guns Blazing (Drums of Death, Part 1).flac'},
|
||||
{'id': 2, 'relpath': 'UNKLE/Psyence Fiction/02 UNKLE (Main Title Theme).flac'},
|
||||
{'id': 3, 'relpath': 'UNKLE/Psyence Fiction/03 Bloodstain.flac'}
|
||||
{'id': 1, 'artist': 'UNKLE', 'title': 'Guns Blazing', 'relpath': 'UNKLE/Psyence Fiction/01 Guns Blazing (Drums of Death, Part 1).flac'},
|
||||
{'id': 2, 'artist': 'UNKLE', 'title': 'UNKLE', 'relpath': 'UNKLE/Psyence Fiction/02 UNKLE (Main Title Theme).flac'},
|
||||
{'id': 3, 'artist': 'UNKLE', 'title': 'Bloodstain', 'relpath': 'UNKLE/Psyence Fiction/03 Bloodstain.flac'}
|
||||
])
|
||||
|
||||
# create playlists
|
||||
@@ -82,9 +82,4 @@ def db(in_memory_db):
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def empty_playlist(db):
|
||||
return Playlist(
|
||||
name='an empty playlist fixture',
|
||||
slug='an-empty-playlist',
|
||||
description='a fixture',
|
||||
session=db
|
||||
)
|
||||
return Playlist.by_slug('empty-playlist', session=db)
|
||||
|
||||
+123
-6
@@ -1,15 +1,38 @@
|
||||
import pytest
|
||||
from groove import playlist
|
||||
from groove.exceptions import PlaylistValidationError
|
||||
import yaml
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from groove import playlist, editor
|
||||
from groove.exceptions import PlaylistValidationError, TrackNotFoundError
|
||||
|
||||
# 166-167, 200, 203-204, 227-228, 253->255, 255->exit, 270, 346-347
|
||||
|
||||
|
||||
def test_create(empty_playlist):
|
||||
assert empty_playlist.record.id
|
||||
|
||||
|
||||
def test_get_no_create(in_memory_db):
|
||||
pl = playlist.Playlist(name='something', session=in_memory_db, create_ok=False)
|
||||
|
||||
# assert twice to ensure we cache the first db query result.
|
||||
assert pl.get_or_create() is None
|
||||
assert pl.get_or_create() is None
|
||||
|
||||
|
||||
def test_exists(db):
|
||||
pl = playlist.Playlist(name='something', session=db, create_ok=True)
|
||||
assert pl.exists
|
||||
|
||||
|
||||
def test_exists_deleted(empty_playlist):
|
||||
assert empty_playlist.exists
|
||||
empty_playlist.delete()
|
||||
assert not empty_playlist.exists
|
||||
|
||||
|
||||
@pytest.mark.parametrize('vals, expected', [
|
||||
(dict(name='missing-the-slug', ), TypeError),
|
||||
(dict(name='missing-the-slug', slug=''), PlaylistValidationError),
|
||||
(dict(slug='missing-the-name', name=''), PlaylistValidationError),
|
||||
])
|
||||
def test_create_missing_fields(vals, expected, db):
|
||||
@@ -26,7 +49,8 @@ def test_add(tracks, empty_playlist):
|
||||
|
||||
|
||||
def test_add_no_matches(empty_playlist):
|
||||
assert empty_playlist.add(('no match', )) == 0
|
||||
with pytest.raises(TrackNotFoundError):
|
||||
empty_playlist.add(('no match', ))
|
||||
|
||||
|
||||
def test_add_multiple_matches(empty_playlist):
|
||||
@@ -49,7 +73,7 @@ def test_delete_playlist_not_exist(db):
|
||||
|
||||
def test_cannot_create_after_delete(db, empty_playlist):
|
||||
empty_playlist.delete()
|
||||
with pytest.raises(RuntimeError):
|
||||
with pytest.raises(PlaylistValidationError):
|
||||
assert empty_playlist.record
|
||||
assert not empty_playlist.exists
|
||||
|
||||
@@ -71,3 +95,96 @@ def test_playlist_formatted(db, empty_playlist):
|
||||
assert repr(empty_playlist)
|
||||
assert empty_playlist.as_string
|
||||
assert empty_playlist.as_dict
|
||||
|
||||
|
||||
def test_playlist_equality(db):
|
||||
pl = playlist.Playlist(name='foo', slug='foo', session=db, create_ok=False)
|
||||
pl2 = playlist.Playlist(name='foo', slug='foo', session=db, create_ok=False)
|
||||
assert pl == pl2
|
||||
pl.save()
|
||||
assert pl == pl2
|
||||
|
||||
|
||||
def test_playlist_inequality(db):
|
||||
pl = playlist.Playlist(name='foo', slug='foo', session=db, create_ok=False)
|
||||
pl2 = playlist.Playlist(name='bar', slug='foo', session=db, create_ok=False)
|
||||
assert pl != pl2
|
||||
pl.save()
|
||||
assert pl != pl2
|
||||
|
||||
|
||||
def test_playlist_inequality_tracks_differ(db):
|
||||
pl = playlist.Playlist.from_yaml({
|
||||
'foo': {
|
||||
'description': '',
|
||||
'entries': []
|
||||
}
|
||||
}, db)
|
||||
pl2 = playlist.Playlist.from_yaml({
|
||||
'foo': {
|
||||
'description': '',
|
||||
'entries': [
|
||||
{'UNKLE': 'Guns Blazing'},
|
||||
]
|
||||
}
|
||||
}, db)
|
||||
assert pl != pl2
|
||||
|
||||
|
||||
def test_as_yaml(db):
|
||||
expected = {
|
||||
'playlist one': {
|
||||
'description': 'the first one',
|
||||
'entries': [
|
||||
{'UNKLE': 'Guns Blazing'},
|
||||
{'UNKLE': 'UNKLE'},
|
||||
{'UNKLE': 'Bloodstain'},
|
||||
]
|
||||
}
|
||||
}
|
||||
pl = playlist.Playlist.by_slug('playlist-one', db)
|
||||
assert yaml.safe_load(pl.as_yaml) == expected
|
||||
|
||||
|
||||
def test_from_yaml(db):
|
||||
pl = playlist.Playlist.by_slug('playlist-one', db)
|
||||
pl2 = playlist.Playlist.from_yaml(yaml.safe_load(pl.as_yaml), db)
|
||||
assert pl2 == pl
|
||||
|
||||
|
||||
@pytest.mark.parametrize('src', [
|
||||
{'missing description': {'entries': []}},
|
||||
{'missing entries': {'description': 'foo'}},
|
||||
{'empty': {}},
|
||||
{'': {'description': 'no name', 'entries': []}},
|
||||
])
|
||||
def test_from_yaml_missing_values(src, db):
|
||||
with pytest.raises(PlaylistValidationError):
|
||||
playlist.Playlist.from_yaml(src, db)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('edits, expected', [
|
||||
({'edited': {'description': 'the edited one', 'entries': []}}, 'edited'),
|
||||
({'empty playlist': {'description': 'no tracks', 'entries': []}}, 'empty playlist'),
|
||||
(None, 'empty playlist'),
|
||||
])
|
||||
def test_edit(monkeypatch, edits, expected, empty_playlist):
|
||||
monkeypatch.setattr(
|
||||
empty_playlist._editor, 'edit', MagicMock(spec=editor, return_value=edits)
|
||||
)
|
||||
empty_playlist.edit()
|
||||
assert empty_playlist.name == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize('slug', [None, ''])
|
||||
def test_save_no_slug(slug, empty_playlist):
|
||||
empty_playlist._slug = slug
|
||||
with pytest.raises(PlaylistValidationError):
|
||||
empty_playlist.save()@pytest.mark.parametrize('slug', [None, ''])
|
||||
|
||||
|
||||
@pytest.mark.parametrize('name', [None, ''])
|
||||
def test_save_no_name(name, empty_playlist):
|
||||
empty_playlist._name = name
|
||||
with pytest.raises(PlaylistValidationError):
|
||||
empty_playlist.save()
|
||||
|
||||
Reference in New Issue
Block a user