grooveondemand/test/conftest.py

91 lines
2.9 KiB
Python
Raw Normal View History

2022-11-20 01:00:54 -08:00
import pytest
2022-12-05 01:06:57 -08:00
import os
from pathlib import Path
from dotenv import load_dotenv
2022-11-20 01:00:54 -08:00
import groove.db
2022-12-07 23:10:41 -08:00
from groove.playlist import Playlist
2022-11-20 01:00:54 -08:00
from sqlalchemy import create_engine, insert
from sqlalchemy.orm import sessionmaker
2022-12-17 12:49:08 -08:00
from unittest.mock import MagicMock
2022-11-20 01:00:54 -08:00
2022-12-05 01:06:57 -08:00
@pytest.fixture(autouse=True, scope='function')
2022-12-21 22:27:30 -08:00
def env(monkeypatch):
2022-12-05 01:06:57 -08:00
root = Path(__file__).parent / Path('fixtures')
2022-12-21 22:27:30 -08:00
monkeypatch.setattr('groove.path.root', MagicMock(return_value=str(root)))
2022-12-05 01:06:57 -08:00
load_dotenv(Path('test/fixtures/env'))
os.environ['MEDIA_ROOT'] = str(root / Path('media'))
return os.environ
@pytest.fixture(scope='function')
def auth():
return (os.environ.get('USERNAME'), os.environ.get('PASSWORD'))
2022-11-20 01:00:54 -08:00
@pytest.fixture(scope='function')
2022-12-17 12:49:08 -08:00
def in_memory_engine(monkeypatch):
engine = create_engine('sqlite:///:memory:', future=True)
monkeypatch.setattr('groove.db.manager.create_engine',
MagicMock(return_value=engine))
return engine
2022-12-01 00:32:48 -08:00
@pytest.fixture(scope='function')
def in_memory_db(in_memory_engine):
2022-11-20 01:00:54 -08:00
"""
An (empty) in-memory SQLite3 database
"""
2022-12-01 00:32:48 -08:00
Session = sessionmaker(bind=in_memory_engine, future=True)
2022-11-20 01:00:54 -08:00
session = Session()
2022-12-01 00:32:48 -08:00
groove.db.metadata.create_all(bind=in_memory_engine)
2022-11-20 01:00:54 -08:00
yield session
session.close()
@pytest.fixture(scope='function')
def db(in_memory_db):
"""
Populate the in-memory sqlite database with fixture data.
"""
# create tracks
query = insert(groove.db.track)
in_memory_db.execute(query, [
2022-12-07 23:10:41 -08:00
{'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'}
2022-11-20 01:00:54 -08:00
])
# create playlists
query = insert(groove.db.playlist)
in_memory_db.execute(query, [
{'id': 1, 'name': 'playlist one', 'description': 'the first one', 'slug': 'playlist-one'},
{'id': 2, 'name': 'playlist two', 'description': 'the second one', 'slug': 'playlist-two'},
{'id': 3, 'name': 'playlist three', 'description': 'the threerd one', 'slug': 'playlist-three'},
{'id': 4, 'name': 'empty playlist', 'description': 'no tracks', 'slug': 'empty-playlist'}
2022-11-20 01:00:54 -08:00
])
# populate the playlists
query = insert(groove.db.entry)
in_memory_db.execute(query, [
{'playlist_id': '1', 'track': '1', 'track_id': '1'},
{'playlist_id': '1', 'track': '2', 'track_id': '2'},
{'playlist_id': '1', 'track': '3', 'track_id': '3'},
{'playlist_id': '2', 'track': '1', 'track_id': '1'},
{'playlist_id': '3', 'track': '6', 'track_id': '2'},
{'playlist_id': '3', 'track': '2', 'track_id': '3'},
])
yield in_memory_db
2022-12-07 18:19:38 -08:00
@pytest.fixture(scope='function')
def empty_playlist(db):
2022-12-07 23:10:41 -08:00
return Playlist.by_slug('empty-playlist', session=db)