Skip to content
Snippets Groups Projects
Commit 5826b8a5 authored by David Trattnig's avatar David Trattnig
Browse files

test: mocked fetching cases

parent 9602f3dc
No related branches found
No related tags found
1 merge request!35ORM-less scheduling
...@@ -26,6 +26,7 @@ from unittest import mock ...@@ -26,6 +26,7 @@ from unittest import mock
from aura_engine.base.api import SimpleCachedRestApi from aura_engine.base.api import SimpleCachedRestApi
from aura_engine.base.config import AuraConfig from aura_engine.base.config import AuraConfig
from aura_engine.base.lang import DotDict
from aura_engine.base.utils import SimpleUtil as SU from aura_engine.base.utils import SimpleUtil as SU
from aura_engine.scheduling.api import ApiFetcher from aura_engine.scheduling.api import ApiFetcher
from aura_engine.scheduling.domain import Playlist, PlaylistItem, PlaylistType, Timeslot from aura_engine.scheduling.domain import Playlist, PlaylistItem, PlaylistType, Timeslot
...@@ -47,57 +48,26 @@ class TestSchedulingTimetable(unittest.TestCase): ...@@ -47,57 +48,26 @@ class TestSchedulingTimetable(unittest.TestCase):
# Mock # Mock
# #
# Mock `requests.get` in `SimpleRestApi` class MockedApiFetcher:
def mocked_requests_get(*args, **kwargs): def __init__(self, code, message, timeslots, exception):
class MockResponse: self.code = code
def __init__(self, json_data, status_code): self.message = message
self.json_data = json_data self.timeslots = timeslots
self.status_code = status_code self.exception = exception
def json(self): def start(self):
return self.json_data print("Called mocked 'start()'")
print(f"Calling mocked 'requests.get' with '{args[0]}'") def fetch(self):
if "/api/v1/playout" in args[0]: print("Called mocked 'fetch()'")
return MockResponse(TestSchedulingTimetable.mocked_steering_json, 200) return DotDict(
elif "/api/v1/playlists/1" in args[0]: {
return MockResponse(TestSchedulingTimetable.mocked_tank_json1, 200) "code": self.code,
elif "/api/v1/playlists/2" in args[0]: "message": self.message,
return MockResponse(TestSchedulingTimetable.mocked_tank_json2, 200) "timeslots": self.timeslots,
"exception": self.exception,
return MockResponse(None, 404) }
)
# Mock `requests.get` in `SimpleRestApi`
def mocked_requests_no_data(*args, **kwargs):
class MockResponse:
def __init__(self, json_data, status_code):
self.json_data = json_data
self.status_code = status_code
def json(self):
return self.json_data
print(f"Calling mocked 'requests.get' with '{args[0]}'")
if "/api/v1/playout" in args[0]:
return MockResponse(None, 200)
elif "/api/v1/playlists/1" in args[0]:
return MockResponse(None, 200)
return MockResponse(None, 404)
# Mock `requests.get` in `SimpleRestApi`
def mocked_requests_get_exception(*args, **kwargs):
class MockResponse:
def __init__(self, json_data, status_code):
self.json_data = json_data
self.status_code = status_code
def json(self):
invalid_timeslots = [{"id": 18, "start": "2023-05-16T13:00:00"}]
return invalid_timeslots
print(f"Calling mocked 'requests.get' with '{args[0]}'")
return MockResponse(None, 200)
# #
# Setup and teardown # Setup and teardown
...@@ -107,30 +77,9 @@ class TestSchedulingTimetable(unittest.TestCase): ...@@ -107,30 +77,9 @@ class TestSchedulingTimetable(unittest.TestCase):
self.config = AuraConfig() self.config = AuraConfig()
cache_location = self.config.get("cache_dir") cache_location = self.config.get("cache_dir")
self.timetable = TimetableService(cache_location) self.timetable = TimetableService(cache_location)
self.api_fetcher = ApiFetcher()
with open("./tests/json/steering-api-v1-playout.json", "r") as file:
TestSchedulingTimetable.mocked_steering_json = json.load(file)
with open("./tests/json/tank-api-v1-playlists-1.json", "r") as file:
TestSchedulingTimetable.mocked_tank_json1 = json.load(file)
with open("./tests/json/tank-api-v1-playlists-2.json", "r") as file:
TestSchedulingTimetable.mocked_tank_json2 = json.load(file)
# Update dates that they are in the future
# Format e.g. "start":"2023-05-16T10:06:00"
now = datetime.now()
hour: int = -1
for timeslot in TestSchedulingTimetable.mocked_steering_json:
start = (now + timedelta(hours=hour)).strftime("%Y-%m-%dT%H:%M:%S")
end = (now + timedelta(hours=hour + 1)).strftime("%Y-%m-%dT%H:%M:%S")
timeslot["start"] = start
timeslot["end"] = end
hour += 1
def tearDown(self): def tearDown(self):
# Clean up cache when finished # Clean up cache when finished
self.api_fetcher.api.prune_cache_dir()
self.timetable.prune_cache_dir() self.timetable.prune_cache_dir()
# #
...@@ -275,29 +224,58 @@ class TestSchedulingTimetable(unittest.TestCase): ...@@ -275,29 +224,58 @@ class TestSchedulingTimetable(unittest.TestCase):
def test_timetable_merge(self): def test_timetable_merge(self):
print(self._testMethodName) print(self._testMethodName)
# @mock.patch("aura_engine.scheduling.programme.api_fetcher", side_effect=mocked_fetch) # TODO
@mock.patch("aura_engine.base.api.requests.get", side_effect=mocked_requests_get)
def test_fetch_data(self, mock_get): def test_fetch_data(self):
print(self._testMethodName) print(self._testMethodName)
now = SU.timestamp()
ts1 = Timeslot(
id=1, repetition_id=None, start=now - 180, end=now - 120, show=None, episode=None
)
ts2 = Timeslot(
id=1, repetition_id=None, start=now - 120, end=now + 120, show=None, episode=None
)
ts3 = Timeslot(
id=1, repetition_id=None, start=now + 120, end=now + 180, show=None, episode=None
)
ts4 = Timeslot(
id=1, repetition_id=None, start=now + 180, end=now + 360, show=None, episode=None
)
pl = Playlist("999", "Playlist X", False)
alpha = PlaylistItem("alpha.flac", 20, 100, None)
beta = PlaylistItem("beta.flac", 100, 100, None)
gamma = PlaylistItem("gamma.flac", 20, 100, None)
pl.add(alpha)
pl.add(beta)
pl.add(gamma)
ts2.set_playlists(pl, None, None)
timeslots = [ts1, ts2, ts3, ts4]
fetcher = self.MockedApiFetcher(0, "Success", timeslots, None)
self.timetable.refresh(fetcher)
# Clear cache to keep test cases isolated # Clear cache to keep test cases isolated
self.api_fetcher.api.prune_cache_dir()
self.timetable.prune_cache_dir() self.timetable.prune_cache_dir()
@mock.patch("aura_engine.base.api.requests.get", side_effect=mocked_requests_no_data) def test_fetch_no_data(self):
def test_fetch_no_data(self, mock_get):
print(self._testMethodName) print(self._testMethodName)
fetcher = self.MockedApiFetcher(1, "No timeslots", None, None)
self.timetable.refresh(fetcher)
# Clear cache to keep test cases isolated # Clear cache to keep test cases isolated
self.api_fetcher.api.prune_cache_dir()
self.timetable.prune_cache_dir() self.timetable.prune_cache_dir()
@mock.patch("aura_engine.base.api.requests.get", side_effect=mocked_requests_get_exception) def test_fetch_exception(self):
def test_fetch_exception(self, mock_get):
print(self._testMethodName) print(self._testMethodName)
fetcher = self.MockedApiFetcher(1, "Some weird exception", None, "Quack")
self.timetable.refresh(fetcher)
# Clear cache to keep test cases isolated # Clear cache to keep test cases isolated
self.api_fetcher.api.prune_cache_dir()
self.timetable.prune_cache_dir() self.timetable.prune_cache_dir()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment