refactor(api): fix pylint errors

This commit is contained in:
jo 2022-04-01 17:29:11 +02:00 committed by Kyle Robbertze
parent bf8db3e6f0
commit 0bbd46c33f
14 changed files with 159 additions and 169 deletions

View file

@ -28,30 +28,30 @@ class TestSchedule(TestCase):
def test_get_cueout(self):
# No overlapping schedule datetimes, normal usecase:
s1_starts = datetime(year=2021, month=10, day=2, hour=1, minute=30)
s1 = self.create_schedule(s1_starts)
self.assertEqual(s1.get_cueout(), self.cue_out)
self.assertEqual(s1.get_ends(), s1_starts + self.length)
item1_starts = datetime(year=2021, month=10, day=2, hour=1, minute=30)
item1 = self.create_schedule(item1_starts)
self.assertEqual(item1.get_cueout(), self.cue_out)
self.assertEqual(item1.get_ends(), item1_starts + self.length)
# Mixed overlapping schedule datetimes (only ends is overlapping):
s2_starts = datetime(year=2021, month=10, day=2, hour=1, minute=55)
s2 = self.create_schedule(s2_starts)
self.assertEqual(s2.get_cueout(), timedelta(minutes=5))
self.assertEqual(s2.get_ends(), self.show_instance.ends)
item_2_starts = datetime(year=2021, month=10, day=2, hour=1, minute=55)
item_2 = self.create_schedule(item_2_starts)
self.assertEqual(item_2.get_cueout(), timedelta(minutes=5))
self.assertEqual(item_2.get_ends(), self.show_instance.ends)
# Fully overlapping schedule datetimes (starts and ends are overlapping):
s3_starts = datetime(year=2021, month=10, day=2, hour=2, minute=1)
s3 = self.create_schedule(s3_starts)
self.assertEqual(s3.get_cueout(), self.cue_out)
self.assertEqual(s3.get_ends(), self.show_instance.ends)
item3_starts = datetime(year=2021, month=10, day=2, hour=2, minute=1)
item3 = self.create_schedule(item3_starts)
self.assertEqual(item3.get_cueout(), self.cue_out)
self.assertEqual(item3.get_ends(), self.show_instance.ends)
def test_is_valid(self):
# Starts before the schedule ends
s1_starts = datetime(year=2021, month=10, day=2, hour=1, minute=30)
s1 = self.create_schedule(s1_starts)
self.assertTrue(s1.is_valid)
item1_starts = datetime(year=2021, month=10, day=2, hour=1, minute=30)
item1 = self.create_schedule(item1_starts)
self.assertTrue(item1.is_valid)
# Starts after the schedule ends
s2_starts = datetime(year=2021, month=10, day=2, hour=3)
s2 = self.create_schedule(s2_starts)
self.assertFalse(s2.is_valid)
item_2_starts = datetime(year=2021, month=10, day=2, hour=3)
item_2 = self.create_schedule(item_2_starts)
self.assertFalse(item_2.is_valid)

View file

@ -1,4 +1,3 @@
import os
from datetime import datetime, timedelta, timezone
from django.conf import settings
@ -20,78 +19,7 @@ class TestScheduleViewSet(APITestCase):
"storage.MusicDir",
directory=str(fixture_path),
)
f = baker.make(
"storage.File",
directory=music_dir,
mime="audio/mp3",
filepath=AUDIO_FILENAME,
length=timedelta(seconds=40.86),
cuein=timedelta(seconds=0),
cueout=timedelta(seconds=40.8131),
)
show = baker.make(
"schedule.ShowInstance",
starts=datetime.now(tz=timezone.utc) - timedelta(minutes=5),
ends=datetime.now(tz=timezone.utc) + timedelta(minutes=5),
)
scheduleItem = baker.make(
"schedule.Schedule",
starts=datetime.now(tz=timezone.utc),
ends=datetime.now(tz=timezone.utc) + f.length,
cue_out=f.cueout,
instance=show,
file=f,
)
self.client.credentials(HTTP_AUTHORIZATION=f"Api-Key {self.token}")
response = self.client.get(self.path)
self.assertEqual(response.status_code, 200)
result = response.json()
self.assertEqual(dateparse.parse_datetime(result[0]["ends"]), scheduleItem.ends)
self.assertEqual(dateparse.parse_duration(result[0]["cue_out"]), f.cueout)
def test_schedule_item_trunc(self):
music_dir = baker.make(
"storage.MusicDir",
directory=str(fixture_path),
)
f = baker.make(
"storage.File",
directory=music_dir,
mime="audio/mp3",
filepath=AUDIO_FILENAME,
length=timedelta(seconds=40.86),
cuein=timedelta(seconds=0),
cueout=timedelta(seconds=40.8131),
)
show = baker.make(
"schedule.ShowInstance",
starts=datetime.now(tz=timezone.utc) - timedelta(minutes=5),
ends=datetime.now(tz=timezone.utc) + timedelta(seconds=20),
)
scheduleItem = baker.make(
"schedule.Schedule",
starts=datetime.now(tz=timezone.utc),
ends=datetime.now(tz=timezone.utc) + f.length,
instance=show,
file=f,
)
self.client.credentials(HTTP_AUTHORIZATION=f"Api-Key {self.token}")
response = self.client.get(self.path)
self.assertEqual(response.status_code, 200)
result = response.json()
self.assertEqual(dateparse.parse_datetime(result[0]["ends"]), show.ends)
expected = show.ends - scheduleItem.starts
self.assertEqual(dateparse.parse_duration(result[0]["cue_out"]), expected)
self.assertNotEqual(
dateparse.parse_datetime(result[0]["ends"]), scheduleItem.ends
)
def test_schedule_item_invalid(self):
music_dir = baker.make(
"storage.MusicDir",
directory=str(fixture_path),
)
f = baker.make(
file = baker.make(
"storage.File",
directory=music_dir,
mime="audio/mp3",
@ -108,18 +36,91 @@ class TestScheduleViewSet(APITestCase):
schedule_item = baker.make(
"schedule.Schedule",
starts=datetime.now(tz=timezone.utc),
ends=datetime.now(tz=timezone.utc) + f.length,
cue_out=f.cueout,
ends=datetime.now(tz=timezone.utc) + file.length,
cue_out=file.cueout,
instance=show,
file=f,
file=file,
)
self.client.credentials(HTTP_AUTHORIZATION=f"Api-Key {self.token}")
response = self.client.get(self.path)
self.assertEqual(response.status_code, 200)
result = response.json()
self.assertEqual(
dateparse.parse_datetime(result[0]["ends"]), schedule_item.ends
)
self.assertEqual(dateparse.parse_duration(result[0]["cue_out"]), file.cueout)
def test_schedule_item_trunc(self):
music_dir = baker.make(
"storage.MusicDir",
directory=str(fixture_path),
)
file = baker.make(
"storage.File",
directory=music_dir,
mime="audio/mp3",
filepath=AUDIO_FILENAME,
length=timedelta(seconds=40.86),
cuein=timedelta(seconds=0),
cueout=timedelta(seconds=40.8131),
)
show = baker.make(
"schedule.ShowInstance",
starts=datetime.now(tz=timezone.utc) - timedelta(minutes=5),
ends=datetime.now(tz=timezone.utc) + timedelta(seconds=20),
)
schedule_item = baker.make(
"schedule.Schedule",
starts=datetime.now(tz=timezone.utc),
ends=datetime.now(tz=timezone.utc) + file.length,
instance=show,
file=file,
)
self.client.credentials(HTTP_AUTHORIZATION=f"Api-Key {self.token}")
response = self.client.get(self.path)
self.assertEqual(response.status_code, 200)
result = response.json()
self.assertEqual(dateparse.parse_datetime(result[0]["ends"]), show.ends)
expected = show.ends - schedule_item.starts
self.assertEqual(dateparse.parse_duration(result[0]["cue_out"]), expected)
self.assertNotEqual(
dateparse.parse_datetime(result[0]["ends"]), schedule_item.ends
)
def test_schedule_item_invalid(self):
music_dir = baker.make(
"storage.MusicDir",
directory=str(fixture_path),
)
file = baker.make(
"storage.File",
directory=music_dir,
mime="audio/mp3",
filepath=AUDIO_FILENAME,
length=timedelta(seconds=40.86),
cuein=timedelta(seconds=0),
cueout=timedelta(seconds=40.8131),
)
show = baker.make(
"schedule.ShowInstance",
starts=datetime.now(tz=timezone.utc) - timedelta(minutes=5),
ends=datetime.now(tz=timezone.utc) + timedelta(minutes=5),
)
schedule_item = baker.make(
"schedule.Schedule",
starts=datetime.now(tz=timezone.utc),
ends=datetime.now(tz=timezone.utc) + file.length,
cue_out=file.cueout,
instance=show,
file=file,
)
invalid_schedule_item = baker.make(
"schedule.Schedule",
starts=show.ends + timedelta(minutes=1),
ends=show.ends + timedelta(minutes=1) + f.length,
cue_out=f.cueout,
ends=show.ends + timedelta(minutes=1) + file.length,
cue_out=file.cueout,
instance=show,
file=f,
file=file,
)
self.client.credentials(HTTP_AUTHORIZATION=f"Api-Key {self.token}")
response = self.client.get(self.path, {"is_valid": True})
@ -130,14 +131,14 @@ class TestScheduleViewSet(APITestCase):
self.assertEqual(
dateparse.parse_datetime(result[0]["ends"]), schedule_item.ends
)
self.assertEqual(dateparse.parse_duration(result[0]["cue_out"]), f.cueout)
self.assertEqual(dateparse.parse_duration(result[0]["cue_out"]), file.cueout)
def test_schedule_item_range(self):
music_dir = baker.make(
"storage.MusicDir",
directory=str(fixture_path),
)
f = baker.make(
file = baker.make(
"storage.File",
directory=music_dir,
mime="audio/mp3",
@ -156,18 +157,18 @@ class TestScheduleViewSet(APITestCase):
schedule_item = baker.make(
"schedule.Schedule",
starts=filter_point,
ends=filter_point + f.length,
cue_out=f.cueout,
ends=filter_point + file.length,
cue_out=file.cueout,
instance=show,
file=f,
file=file,
)
previous_item = baker.make(
"schedule.Schedule",
starts=filter_point - timedelta(minutes=5),
ends=filter_point - timedelta(minutes=5) + f.length,
cue_out=f.cueout,
ends=filter_point - timedelta(minutes=5) + file.length,
cue_out=file.cueout,
instance=show,
file=f,
file=file,
)
self.client.credentials(HTTP_AUTHORIZATION=f"Api-Key {self.token}")
range_start = (filter_point - timedelta(minutes=1)).isoformat(