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 34e17b8 commit 4d428f2
Showing 1 changed file with 59 additions and 1 deletion.
60 changes: 59 additions & 1 deletion tests/addon/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
# has to be set before importing ankihub
os.environ["SKIP_INIT"] = "1"

from ankihub.addon_ankihub_client import AddonAnkiHubClient
from ankihub.ankihub_client import (
AnkiHubClient,
AnkiHubHTTPError,
Expand All @@ -59,6 +60,7 @@
_contains_path_to_this_addon,
_normalize_url,
_try_handle_exception,
upload_logs_in_background,
)
from ankihub.gui.menu import AnkiHubLogin
from ankihub.gui.operations import deck_creation
Expand Down Expand Up @@ -107,7 +109,7 @@
mids_of_notes,
retain_nids_with_ah_note_type,
)
from ankihub.settings import ANKIWEB_ID
from ankihub.settings import ANKIWEB_ID, log_file_path


@pytest.fixture
Expand Down Expand Up @@ -1433,6 +1435,62 @@ def _mock_ask_user_to_return_false(
importlib.reload(errors)


class TestUploadLogs:
def test_basic(
self,
qtbot: QtBot,
mock_function: MockFunction,
):
on_done_mock = Mock()
upload_logs_mock = mock_function(AddonAnkiHubClient, "upload_logs")
upload_logs_in_background(on_done=on_done_mock)

qtbot.wait_until(lambda: on_done_mock.called)

upload_logs_mock.assert_called_once()
assert upload_logs_mock.call_args[1]["file"] == log_file_path()

@pytest.mark.parametrize(
"exception, expected_report_exception_called",
[
# The exception should not be reported for these two specific cases
(AnkiHubHTTPError(response=Mock(status_code=401)), False),
(
AnkiHubHTTPError(
response=Mock(status_code=406, reason=OUTDATED_CLIENT_ERROR_REASON)
),
False,
),
# The exception should be reported in all other cases
(AnkiHubHTTPError(response=Mock(status_code=500)), True),
(Exception("test"), True),
],
)
def test_with_exception(
self,
qtbot: QtBot,
mock_function: MockFunction,
exception: Exception,
expected_report_exception_called: bool,
):
def raise_exception(*args, **kwargs) -> None:
raise exception

on_done_mock = Mock()
upload_logs_mock = mock_function(
AddonAnkiHubClient, "upload_logs", side_effect=raise_exception
)
report_exception_mock = mock_function(errors, "_report_exception")
upload_logs_in_background(on_done=on_done_mock)

qtbot.wait(500)

upload_logs_mock.assert_called_once()
on_done_mock.assert_not_called()

assert report_exception_mock.called == expected_report_exception_called


class TestRateLimitedDecorator:
def test_rate_limited_decorator(self):
# Create a counter to keep track of how many times foo is executed
Expand Down

0 comments on commit 4d428f2

Please sign in to comment.