iteration1
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
import sqlite3
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
@@ -15,8 +16,17 @@ if str(SRC_ROOT) not in sys.path:
|
||||
|
||||
from ffx.constants import DATABASE_VERSION # noqa: E402
|
||||
from ffx.database import DATABASE_VERSION_KEY, databaseContext, getDatabaseVersion # noqa: E402
|
||||
from ffx.model.shifted_season import ShiftedSeason # noqa: E402
|
||||
from ffx.model.property import Property # noqa: E402
|
||||
from ffx.model.show import Base # noqa: E402
|
||||
from ffx.show_controller import ShowController # noqa: E402
|
||||
from ffx.show_descriptor import ShowDescriptor # noqa: E402
|
||||
from ffx.shifted_season_controller import ShiftedSeasonController # noqa: E402
|
||||
|
||||
|
||||
class StaticConfig:
|
||||
def getData(self):
|
||||
return {}
|
||||
|
||||
|
||||
class DatabaseContextTests(unittest.TestCase):
|
||||
@@ -78,6 +88,106 @@ class DatabaseContextTests(unittest.TestCase):
|
||||
|
||||
mocked_create_all.assert_not_called()
|
||||
|
||||
def test_database_context_migrates_v2_shifted_seasons_schema_to_v3(self):
|
||||
database_context = databaseContext(str(self.database_path))
|
||||
context = {
|
||||
"database": database_context,
|
||||
"config": StaticConfig(),
|
||||
"logger": object(),
|
||||
}
|
||||
try:
|
||||
ShowController(context).updateShow(
|
||||
ShowDescriptor(id=1, name="Demo", year=2000)
|
||||
)
|
||||
shifted_season_id = ShiftedSeasonController(context).addShiftedSeason(
|
||||
showId=1,
|
||||
shiftedSeasonObj={
|
||||
"original_season": 1,
|
||||
"first_episode": 1,
|
||||
"last_episode": 10,
|
||||
"season_offset": 1,
|
||||
"episode_offset": -10,
|
||||
},
|
||||
)
|
||||
finally:
|
||||
database_context["engine"].dispose()
|
||||
|
||||
connection = sqlite3.connect(self.database_path)
|
||||
try:
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("DROP INDEX IF EXISTS ix_shifted_seasons_show_id")
|
||||
cursor.execute("DROP INDEX IF EXISTS ix_shifted_seasons_pattern_id")
|
||||
cursor.execute(
|
||||
"ALTER TABLE shifted_seasons RENAME TO shifted_seasons_v3_current"
|
||||
)
|
||||
cursor.execute(
|
||||
"""
|
||||
CREATE TABLE shifted_seasons (
|
||||
id INTEGER PRIMARY KEY,
|
||||
show_id INTEGER,
|
||||
original_season INTEGER,
|
||||
first_episode INTEGER DEFAULT -1,
|
||||
last_episode INTEGER DEFAULT -1,
|
||||
season_offset INTEGER DEFAULT 0,
|
||||
episode_offset INTEGER DEFAULT 0,
|
||||
FOREIGN KEY(show_id) REFERENCES shows(id) ON DELETE CASCADE
|
||||
)
|
||||
"""
|
||||
)
|
||||
cursor.execute(
|
||||
"""
|
||||
INSERT INTO shifted_seasons (
|
||||
id,
|
||||
show_id,
|
||||
original_season,
|
||||
first_episode,
|
||||
last_episode,
|
||||
season_offset,
|
||||
episode_offset
|
||||
)
|
||||
SELECT
|
||||
id,
|
||||
show_id,
|
||||
original_season,
|
||||
first_episode,
|
||||
last_episode,
|
||||
season_offset,
|
||||
episode_offset
|
||||
FROM shifted_seasons_v3_current
|
||||
"""
|
||||
)
|
||||
cursor.execute("DROP TABLE shifted_seasons_v3_current")
|
||||
cursor.execute(
|
||||
"UPDATE properties SET value = '2' WHERE key = ?",
|
||||
(DATABASE_VERSION_KEY,),
|
||||
)
|
||||
connection.commit()
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
reopened_context = databaseContext(str(self.database_path))
|
||||
try:
|
||||
self.assertEqual(DATABASE_VERSION, getDatabaseVersion(reopened_context))
|
||||
|
||||
Session = reopened_context["session"]
|
||||
session = Session()
|
||||
try:
|
||||
migrated_shifted_season = (
|
||||
session.query(ShiftedSeason)
|
||||
.filter(ShiftedSeason.id == shifted_season_id)
|
||||
.first()
|
||||
)
|
||||
self.assertIsNotNone(migrated_shifted_season)
|
||||
self.assertEqual(1, migrated_shifted_season.getShowId())
|
||||
self.assertIsNone(migrated_shifted_season.getPatternId())
|
||||
self.assertEqual(1, migrated_shifted_season.getOriginalSeason())
|
||||
self.assertEqual(1, migrated_shifted_season.getFirstEpisode())
|
||||
self.assertEqual(10, migrated_shifted_season.getLastEpisode())
|
||||
finally:
|
||||
session.close()
|
||||
finally:
|
||||
reopened_context["engine"].dispose()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
191
tests/unit/test_shifted_season_controller.py
Normal file
191
tests/unit/test_shifted_season_controller.py
Normal file
@@ -0,0 +1,191 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
|
||||
SRC_ROOT = Path(__file__).resolve().parents[2] / "src"
|
||||
|
||||
if str(SRC_ROOT) not in sys.path:
|
||||
sys.path.insert(0, str(SRC_ROOT))
|
||||
|
||||
|
||||
from ffx.database import databaseContext # noqa: E402
|
||||
from ffx.model.pattern import Pattern # noqa: E402
|
||||
from ffx.model.track import Track # noqa: E402
|
||||
from ffx.show_controller import ShowController # noqa: E402
|
||||
from ffx.show_descriptor import ShowDescriptor # noqa: E402
|
||||
from ffx.shifted_season_controller import ShiftedSeasonController # noqa: E402
|
||||
from ffx.track_type import TrackType # noqa: E402
|
||||
|
||||
|
||||
class StaticConfig:
|
||||
def __init__(self, data: dict | None = None):
|
||||
self._data = data or {}
|
||||
|
||||
def getData(self):
|
||||
return self._data
|
||||
|
||||
|
||||
def make_logger(name: str) -> logging.Logger:
|
||||
logger = logging.getLogger(name)
|
||||
logger.handlers = []
|
||||
logger.setLevel(logging.DEBUG)
|
||||
logger.propagate = False
|
||||
logger.addHandler(logging.NullHandler())
|
||||
return logger
|
||||
|
||||
|
||||
def make_context(database_path: Path) -> dict:
|
||||
return {
|
||||
"logger": make_logger(f"ffx-test-shifted-{database_path.stem}"),
|
||||
"config": StaticConfig(),
|
||||
"database": databaseContext(str(database_path)),
|
||||
}
|
||||
|
||||
|
||||
class ShiftedSeasonControllerTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tempdir = tempfile.TemporaryDirectory()
|
||||
self.database_path = Path(self.tempdir.name) / "shifted-season-test.db"
|
||||
self.context = make_context(self.database_path)
|
||||
self.show_controller = ShowController(self.context)
|
||||
self.shifted_season_controller = ShiftedSeasonController(self.context)
|
||||
|
||||
def tearDown(self):
|
||||
self.context["database"]["engine"].dispose()
|
||||
self.tempdir.cleanup()
|
||||
|
||||
def add_show(self, show_id: int, name: str = "Demo Show"):
|
||||
self.show_controller.updateShow(
|
||||
ShowDescriptor(id=show_id, name=name, year=2000 + show_id)
|
||||
)
|
||||
|
||||
def add_pattern(self, show_id: int, expression: str) -> int:
|
||||
self.add_show(show_id)
|
||||
Session = self.context["database"]["session"]
|
||||
session = Session()
|
||||
try:
|
||||
pattern = Pattern(show_id=show_id, pattern=expression)
|
||||
session.add(pattern)
|
||||
session.flush()
|
||||
session.add(
|
||||
Track(
|
||||
pattern_id=pattern.getId(),
|
||||
track_type=TrackType.VIDEO.index(),
|
||||
codec_name="h264",
|
||||
index=0,
|
||||
source_index=0,
|
||||
disposition_flags=0,
|
||||
audio_layout=0,
|
||||
)
|
||||
)
|
||||
session.commit()
|
||||
return pattern.getId()
|
||||
finally:
|
||||
session.close()
|
||||
|
||||
def test_shift_season_uses_show_mapping_when_no_pattern_mapping_exists(self):
|
||||
pattern_id = self.add_pattern(1, r"^demo_(s[0-9]+e[0-9]+)\.mkv$")
|
||||
self.shifted_season_controller.addShiftedSeason(
|
||||
showId=1,
|
||||
shiftedSeasonObj={
|
||||
"original_season": 1,
|
||||
"first_episode": 1,
|
||||
"last_episode": 10,
|
||||
"season_offset": 2,
|
||||
"episode_offset": 5,
|
||||
},
|
||||
)
|
||||
|
||||
shifted_season, shifted_episode = self.shifted_season_controller.shiftSeason(
|
||||
showId=1,
|
||||
patternId=pattern_id,
|
||||
season=1,
|
||||
episode=3,
|
||||
)
|
||||
|
||||
self.assertEqual((3, 8), (shifted_season, shifted_episode))
|
||||
|
||||
def test_shift_season_prefers_pattern_mapping_over_show_mapping(self):
|
||||
pattern_id = self.add_pattern(1, r"^demo_(s[0-9]+e[0-9]+)\.mkv$")
|
||||
self.shifted_season_controller.addShiftedSeason(
|
||||
showId=1,
|
||||
shiftedSeasonObj={
|
||||
"original_season": 1,
|
||||
"first_episode": 1,
|
||||
"last_episode": 10,
|
||||
"season_offset": 2,
|
||||
"episode_offset": 5,
|
||||
},
|
||||
)
|
||||
self.shifted_season_controller.addShiftedSeason(
|
||||
patternId=pattern_id,
|
||||
shiftedSeasonObj={
|
||||
"original_season": 1,
|
||||
"first_episode": 1,
|
||||
"last_episode": 10,
|
||||
"season_offset": 1,
|
||||
"episode_offset": -2,
|
||||
},
|
||||
)
|
||||
|
||||
shifted_season, shifted_episode = self.shifted_season_controller.shiftSeason(
|
||||
showId=1,
|
||||
patternId=pattern_id,
|
||||
season=1,
|
||||
episode=3,
|
||||
)
|
||||
|
||||
self.assertEqual((2, 1), (shifted_season, shifted_episode))
|
||||
|
||||
def test_shift_season_pattern_zero_offsets_override_show_mapping_to_identity(self):
|
||||
pattern_id = self.add_pattern(1, r"^demo_(s[0-9]+e[0-9]+)\.mkv$")
|
||||
self.shifted_season_controller.addShiftedSeason(
|
||||
showId=1,
|
||||
shiftedSeasonObj={
|
||||
"original_season": 1,
|
||||
"first_episode": 1,
|
||||
"last_episode": 10,
|
||||
"season_offset": 2,
|
||||
"episode_offset": 5,
|
||||
},
|
||||
)
|
||||
self.shifted_season_controller.addShiftedSeason(
|
||||
patternId=pattern_id,
|
||||
shiftedSeasonObj={
|
||||
"original_season": 1,
|
||||
"first_episode": 1,
|
||||
"last_episode": 10,
|
||||
"season_offset": 0,
|
||||
"episode_offset": 0,
|
||||
},
|
||||
)
|
||||
|
||||
shifted_season, shifted_episode = self.shifted_season_controller.shiftSeason(
|
||||
showId=1,
|
||||
patternId=pattern_id,
|
||||
season=1,
|
||||
episode=3,
|
||||
)
|
||||
|
||||
self.assertEqual((1, 3), (shifted_season, shifted_episode))
|
||||
|
||||
def test_shift_season_falls_back_to_identity_when_no_rule_matches(self):
|
||||
pattern_id = self.add_pattern(1, r"^demo_(s[0-9]+e[0-9]+)\.mkv$")
|
||||
|
||||
shifted_season, shifted_episode = self.shifted_season_controller.shiftSeason(
|
||||
showId=1,
|
||||
patternId=pattern_id,
|
||||
season=4,
|
||||
episode=20,
|
||||
)
|
||||
|
||||
self.assertEqual((4, 20), (shifted_season, shifted_episode))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user