#88 #89 : use pytest + reorganise settings (#92)

- !88 pytest on existing tests
- !89 reorganise settings (! see notes for deployment)

Co-authored-by: bkfox <thomas bkfox net>
Reviewed-on: rc/aircox#92
This commit is contained in:
Thomas Kairos
2023-03-28 14:40:49 +02:00
parent 4bebc56a28
commit 0e183099ed
31 changed files with 511 additions and 368 deletions

View File

@ -1,15 +0,0 @@
from .sound_file import SoundFileTestCase
from .sound_monitor import (
ModifiedHandlerTestCase,
MonitorHandlerTestCase,
MoveHandlerTestCase,
NotifyHandlerTestCase,
)
__all__ = (
"SoundFileTestCase",
"NotifyHandlerTestCase",
"MoveHandlerTestCase",
"ModifiedHandlerTestCase",
"MonitorHandlerTestCase",
)

View File

@ -1,103 +0,0 @@
from datetime import timedelta
from django.conf import settings as conf
from django.test import TestCase
from django.utils import timezone as tz
from aircox import models
from aircox.management.sound_file import SoundFile
__all__ = ("SoundFileTestCase",)
class SoundFileTestCase(TestCase):
path_infos = {
"test/20220101_10h13_1_sample_1.mp3": {
"year": 2022,
"month": 1,
"day": 1,
"hour": 10,
"minute": 13,
"n": 1,
"name": "Sample 1",
},
"test/20220102_10h13_sample_2.mp3": {
"year": 2022,
"month": 1,
"day": 2,
"hour": 10,
"minute": 13,
"name": "Sample 2",
},
"test/20220103_1_sample_3.mp3": {
"year": 2022,
"month": 1,
"day": 3,
"n": 1,
"name": "Sample 3",
},
"test/20220104_sample_4.mp3": {
"year": 2022,
"month": 1,
"day": 4,
"name": "Sample 4",
},
"test/20220105.mp3": {
"year": 2022,
"month": 1,
"day": 5,
"name": "20220105",
},
}
subdir_prefix = "test"
sound_files = {
k: r
for k, r in (
(path, SoundFile(conf.MEDIA_ROOT + "/" + path))
for path in path_infos.keys()
)
}
def test_sound_path(self):
for path, sound_file in self.sound_files.items():
self.assertEqual(path, sound_file.sound_path)
def test_read_path(self):
for path, sound_file in self.sound_files.items():
expected = self.path_infos[path]
result = sound_file.read_path(path)
# remove None values
result = {k: v for k, v in result.items() if v is not None}
self.assertEqual(expected, result, "path: {}".format(path))
def _setup_diff(self, program, info):
episode = models.Episode(program=program, title="test-episode")
at = tz.datetime(
**{
k: info[k]
for k in ("year", "month", "day", "hour", "minute")
if info.get(k)
}
)
at = tz.make_aware(at)
diff = models.Diffusion(
episode=episode, start=at, end=at + timedelta(hours=1)
)
episode.save()
diff.save()
return diff
def test_find_episode(self):
station = models.Station(name="test-station")
program = models.Program(station=station, title="test")
station.save()
program.save()
for path, sound_file in self.sound_files.items():
infos = sound_file.read_path(path)
diff = self._setup_diff(program, infos)
sound = models.Sound(program=diff.program, file=path)
result = sound_file.find_episode(sound, infos)
self.assertEquals(diff.episode, result)
# TODO: find_playlist, sync

View File

@ -0,0 +1,111 @@
import pytest
from datetime import timedelta
from django.conf import settings as conf
from django.utils import timezone as tz
from aircox import models
from aircox.management.sound_file import SoundFile
@pytest.fixture
def path_infos():
return {
"test/20220101_10h13_1_sample_1.mp3": {
"year": 2022,
"month": 1,
"day": 1,
"hour": 10,
"minute": 13,
"n": 1,
"name": "Sample 1",
},
"test/20220102_10h13_sample_2.mp3": {
"year": 2022,
"month": 1,
"day": 2,
"hour": 10,
"minute": 13,
"name": "Sample 2",
},
"test/20220103_1_sample_3.mp3": {
"year": 2022,
"month": 1,
"day": 3,
"n": 1,
"name": "Sample 3",
},
"test/20220104_sample_4.mp3": {
"year": 2022,
"month": 1,
"day": 4,
"name": "Sample 4",
},
"test/20220105.mp3": {
"year": 2022,
"month": 1,
"day": 5,
"name": "20220105",
},
}
@pytest.fixture
def sound_files(path_infos):
return {
k: r
for k, r in (
(path, SoundFile(conf.MEDIA_ROOT + "/" + path))
for path in path_infos.keys()
)
}
def test_sound_path(sound_files):
for path, sound_file in sound_files.items():
assert path == sound_file.sound_path
def test_read_path(path_infos, sound_files):
for path, sound_file in sound_files.items():
expected = path_infos[path]
result = sound_file.read_path(path)
# remove None values
result = {k: v for k, v in result.items() if v is not None}
assert expected == result, "path: {}".format(path)
def _setup_diff(program, info):
episode = models.Episode(program=program, title="test-episode")
at = tz.datetime(
**{
k: info[k]
for k in ("year", "month", "day", "hour", "minute")
if info.get(k)
}
)
at = tz.make_aware(at)
diff = models.Diffusion(
episode=episode, start=at, end=at + timedelta(hours=1)
)
episode.save()
diff.save()
return diff
@pytest.mark.django_db(transaction=True)
def test_find_episode(sound_files):
station = models.Station(name="test-station")
program = models.Program(station=station, title="test")
station.save()
program.save()
for path, sound_file in sound_files.items():
infos = sound_file.read_path(path)
diff = _setup_diff(program, infos)
sound = models.Sound(program=diff.program, file=path)
result = sound_file.find_episode(sound, infos)
assert diff.episode == result
# TODO: find_playlist, sync

View File

@ -1,22 +1,15 @@
import pytest
import concurrent.futures as futures
import time
from datetime import datetime, timedelta
from django.test import TestCase
from aircox.management.sound_monitor import (
ModifiedHandler,
MonitorHandler,
NotifyHandler,
)
__all__ = (
"NotifyHandlerTestCase",
"MoveHandlerTestCase",
"ModifiedHandlerTestCase",
"MonitorHandlerTestCase",
)
class FakeEvent:
def __init__(self, **kwargs):
@ -31,22 +24,28 @@ class WaitHandler(NotifyHandler):
pass
class NotifyHandlerTestCase(TestCase):
@pytest.fixture
def monitor():
pool = futures.ThreadPoolExecutor(2)
return MonitorHandler("archives", pool)
class TestNotifyHandler:
pass
class MoveHandlerTestCase(TestCase):
class TestMoveHandler:
pass
class ModifiedHandlerTestCase(TestCase):
class TestModifiedHandler:
def test_wait(self):
handler = ModifiedHandler()
handler.timeout_delta = timedelta(seconds=0.1)
start = datetime.now()
handler.wait()
delta = datetime.now() - start
self.assertTrue(delta < handler.timeout_delta + timedelta(seconds=0.1))
assert delta < handler.timeout_delta + timedelta(seconds=0.1)
def test_wait_ping(self):
pool = futures.ThreadPoolExecutor(1)
@ -57,28 +56,24 @@ class ModifiedHandlerTestCase(TestCase):
time.sleep(0.3)
handler.ping()
time.sleep(0.3)
self.assertTrue(future.running())
assert future.running()
class MonitorHandlerTestCase(TestCase):
def setUp(self):
pool = futures.ThreadPoolExecutor(2)
self.monitor = MonitorHandler("archives", pool)
def test_submit_new_job(self):
class TestMonitorHandler:
def test_submit_new_job(self, monitor):
event = FakeEvent(src_path="dummy_src")
handler = NotifyHandler()
result, _ = self.monitor._submit(handler, event, "up")
self.assertIs(handler, result)
self.assertIsInstance(handler.future, futures.Future)
self.monitor.pool.shutdown()
result, _ = monitor._submit(handler, event, "up")
assert handler == result
assert isinstance(handler.future, futures.Future)
monitor.pool.shutdown()
def test_submit_job_exists(self):
def test_submit_job_exists(self, monitor):
event = FakeEvent(src_path="dummy_src")
job_1, new_1 = self.monitor._submit(WaitHandler(), event, "up")
job_2, new_2 = self.monitor._submit(NotifyHandler(), event, "up")
self.assertIs(job_1, job_2)
self.assertTrue(new_1)
self.assertFalse(new_2)
self.monitor.pool.shutdown()
job_1, new_1 = monitor._submit(WaitHandler(), event, "up")
job_2, new_2 = monitor._submit(NotifyHandler(), event, "up")
assert job_1 == job_2
assert new_1
assert not new_2
monitor.pool.shutdown()