Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
engine
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
AURA
engine
Commits
5826b8a5
"src/core/channels.py" did not exist on "b6362ad8838e95f214a015bd8e186026566d6adc"
Commit
5826b8a5
authored
1 year ago
by
David Trattnig
Browse files
Options
Downloads
Patches
Plain Diff
test: mocked fetching cases
parent
9602f3dc
No related branches found
No related tags found
1 merge request
!35
ORM-less scheduling
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
tests/test_scheduling_timetable.py
+60
-82
60 additions, 82 deletions
tests/test_scheduling_timetable.py
with
60 additions
and
82 deletions
tests/test_scheduling_timetable.py
+
60
−
82
View file @
5826b8a5
...
@@ -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
()
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment