Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add more tests #849

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/program/media/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,8 @@ def get_season_index_by_id(self, item_id):
return None

def _determine_state(self):
if len(self.seasons) == 0:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An unwanted result shouldnt be the first thing we calculate

return States.Unknown
if all(season.state == States.Completed for season in self.seasons):
return States.Completed
if any(season.state in [States.Ongoing, States.Unreleased] for season in self.seasons):
Expand Down
2 changes: 2 additions & 0 deletions src/program/state_transition.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,6 @@ def process_event(emitted_by: Service, existing_item: MediaItem | None = None, c
else:
return no_further_processing

items_to_submit = list(set(items_to_submit))

return next_service, items_to_submit
101 changes: 33 additions & 68 deletions src/tests/test_alldebrid_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,20 @@
from program.services.downloaders import alldebrid
from program.services.downloaders.alldebrid import (
AllDebridDownloader,
add_torrent,
get_instant_availability,
get_status,
get_torrents,
AllDebridAPI
)
from program.settings.manager import settings_manager as settings


@pytest.fixture
def downloader(instant, upload, status, status_all, delete):
def downloader(instant, upload, status, delete):
"""Instance of AllDebridDownloader with API calls mocked"""
# mock API calls
_get = alldebrid.get
def get(url, **params):
_request = AllDebridAPI._request
def get(self, method, url, **params):
match url:
case "user":
return {"data": { "user": { "isPremium": True, "premiumUntil": 1735514599, } } }
return { "user": { "isPremium": True, "premiumUntil": 1735514599, } }
case "magnet/instant":
return instant(url, **params)
case "magnet/upload":
Expand All @@ -31,11 +28,9 @@ def get(url, **params):
case "magnet/status":
if params.get("id", False):
return status(url, **params)
else:
return status_all(url, **params)
case _:
raise Exception("unmatched api call")
alldebrid.get = get
AllDebridAPI._request = get

alldebrid_settings = settings.settings.downloaders.all_debrid
alldebrid_settings.enabled = True
Expand All @@ -46,72 +41,50 @@ def get(url, **params):
yield downloader

# tear down mock
alldebrid.get = get


## Downloader tests
def test_process_hashes(downloader):
hashes = downloader.process_hashes(["abc"], None, [False, True])
assert len(hashes) == 1


def test_download_cached(downloader):
torrent_id = downloader.download_cached({"infohash": "abc"})
assert torrent_id == MAGNET_ID


def test_get_torrent_names(downloader):
names = downloader.get_torrent_names(123)
assert names == ("Ubuntu 24.04", None)
AllDebridAPI._request = _request


## API parsing tests
def test_get_instant_availability(instant):
alldebrid.get = instant
def test_get_instant_availability(instant, downloader):
AllDebridAPI._request = instant
infohashes = [UBUNTU]
availability = get_instant_availability(infohashes)
availability = downloader.get_instant_availability(infohashes)
assert len(availability[0].get("files", [])) == 2


def test_get_instant_availability_unavailable(instant_unavailable):
alldebrid.get = instant_unavailable
def test_get_instant_availability_unavailable(instant_unavailable, downloader):
AllDebridAPI._request = instant_unavailable
infohashes = [UBUNTU]
availability = get_instant_availability(infohashes)
availability = downloader.get_instant_availability(infohashes)
assert availability[0]["hash"] == UBUNTU


def test_add_torrent(upload):
alldebrid.get = upload
torrent_id = add_torrent(UBUNTU)
def test_add_torrent(upload, downloader):
AllDebridAPI._request = upload
torrent_id = downloader.add_torrent(UBUNTU)
assert torrent_id == 251993753


def test_add_torrent_cached(upload_ready):
alldebrid.get = upload_ready
torrent_id = add_torrent(UBUNTU)
def test_add_torrent_cached(upload_ready, downloader):
AllDebridAPI._request = upload_ready
torrent_id = downloader.add_torrent(UBUNTU)
assert torrent_id == 251993753


def test_get_status(status):
alldebrid.get = status
torrent_status = get_status(251993753)
def test_get_status(status, downloader):
AllDebridAPI._request = status
torrent_status = downloader.get_status(251993753)
assert torrent_status["filename"] == "Ubuntu 24.04"


def test_get_status_unfinished(status_downloading):
alldebrid.get = status_downloading
torrent_status = get_status(251993753)
assert torrent_status["status"] == "Downloading"


def test_get_torrents(status_all):
alldebrid.get = status_all
torrents = get_torrents()
assert torrents[0]["status"] == "Ready"
def test_get_status_unfinished(status_downloading, downloader):
AllDebridAPI._request = status_downloading
torrent_info = downloader.get_torrent_info(251993753)
assert torrent_info["status"] == "Downloading"


def test_delete(delete):
alldebrid.get = delete
AllDebridAPI._request = delete
delete(123)


Expand All @@ -123,50 +96,42 @@ def instant():
"""GET /magnet/instant?magnets[0]=infohash (torrent available)"""
with open("src/tests/test_data/alldebrid_magnet_instant.json") as f:
body = json.load(f)
return lambda url, **params: body
return lambda self, method, url, **params: body

@pytest.fixture
def instant_unavailable():
"""GET /magnet/instant?magnets[0]=infohash (torrent unavailable)"""
with open("src/tests/test_data/alldebrid_magnet_instant_unavailable.json") as f:
body = json.load(f)
return lambda url, **params: body
return lambda self, method, url, **params: body

@pytest.fixture
def upload():
"""GET /magnet/upload?magnets[]=infohash (torrent not ready yet)"""
with open("src/tests/test_data/alldebrid_magnet_upload_not_ready.json") as f:
body = json.load(f)
return lambda url, **params: body
return lambda self, method, url, **params: body

@pytest.fixture
def upload_ready():
"""GET /magnet/upload?magnets[]=infohash (torrent ready)"""
with open("src/tests/test_data/alldebrid_magnet_upload_ready.json") as f:
body = json.load(f)
return lambda url, **params: body
return lambda self, method, url, **params: body

@pytest.fixture
def status():
"""GET /magnet/status?id=123 (debrid links ready)"""
with open("src/tests/test_data/alldebrid_magnet_status_one_ready.json") as f:
body = json.load(f)
return lambda url, **params: body
return lambda self, method, url, **params: body

@pytest.fixture
def status_downloading():
"""GET /magnet/status?id=123 (debrid links not ready yet)"""
with open("src/tests/test_data/alldebrid_magnet_status_one_downloading.json") as f:
body = json.load(f)
return lambda url, **params: body

@pytest.fixture
def status_all():
"""GET /magnet/status (gets a list of all links instead of a single object)"""
# The body is the same as a single item, but with all your magnets in a list.
with open("src/tests/test_data/alldebrid_magnet_status_one_ready.json") as f:
body = json.load(f)
return lambda url, **params: {"status": "success", "data": {"magnets": [body["data"]["magnets"]]}}
return lambda self, method, url, **params: body

@pytest.fixture
def delete():
Expand Down
Loading
Loading