Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RisingOrange committed Nov 9, 2023
1 parent 4d428f2 commit f98bf7a
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions tests/addon/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
_try_handle_exception,
upload_logs_in_background,
)
from ankihub.gui.media_sync import media_sync
from ankihub.gui.menu import AnkiHubLogin
from ankihub.gui.operations import deck_creation
from ankihub.gui.operations.deck_creation import (
Expand Down Expand Up @@ -163,6 +164,56 @@ def test_update_media_names_on_notes(
assert '<img src="will_not_replace.jpeg">' in " ".join(notes[2].fields)


class TestMediaSyncMediaDownload:
def test_with_exception(self, mock_function: MockFunction, qtbot: QtBot):
def raise_exception() -> None:
raise Exception("test")

update_and_download_mock = mock_function(
media_sync,
"_update_deck_media_and_download_missing_media",
side_effect=raise_exception,
)

with qtbot.captureExceptions() as exceptions:
media_sync.start_media_download()
qtbot.wait(500)

# Assert that _download_in_progress was set to False and the exception was raised
assert not media_sync._download_in_progress
assert len(exceptions) == 1
update_and_download_mock.assert_called_once() # sanity check


class TestMediaSyncMediaUpload:
def test_with_exception(
self,
anki_session_with_addon_data: AnkiSession,
mock_function: MockFunction,
qtbot: QtBot,
next_deterministic_uuid,
):
with anki_session_with_addon_data.profile_loaded():

def raise_exception() -> None:
raise Exception("test")

upload_media_mock = mock_function(
media_sync._client,
"upload_media",
side_effect=raise_exception,
)

with qtbot.captureExceptions() as exceptions:
media_sync.start_media_upload([], next_deterministic_uuid())
qtbot.wait(500)

# Assert that _amount_uploads_in_progress was was reset to 0 and the exception was raised
assert media_sync._amount_uploads_in_progress == 0
assert len(exceptions) == 1
upload_media_mock.assert_called_once()


def test_lowest_level_common_ancestor_deck_name():

deck_names = [
Expand Down

0 comments on commit f98bf7a

Please sign in to comment.