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

Avoid mocking Anki modules globally #175

Merged
merged 4 commits into from
Nov 7, 2022
Merged
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
87 changes: 52 additions & 35 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions test/config/config_settings_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
from expects import expect, contain
from mamba import describe, it, context

from test_utils.anki import mock_anki_modules
from test_utils.anki import MockAnkiModules

mock_anki_modules = MockAnkiModules()

mock_anki_modules()
from aqt import mw

from crowd_anki.config.config_settings import ConfigSettings, NoteSortingMethods
Expand Down Expand Up @@ -94,3 +95,5 @@

expect(config._config.items()).to(contain(*list(new_settings.items())))
addon_manager_mock.writeConfig.assert_called_once()

mock_anki_modules.unmock()
6 changes: 6 additions & 0 deletions test/export/anki_exporter_wrapper_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

from mamba import describe, it, context

from test_utils.anki import MockAnkiModules

mock_anki_modules = MockAnkiModules(["win32file", "win32pipe", "pywintypes", "winerror"]) # Anki on Windows uses pywin32

from crowd_anki.export.anki_exporter_wrapper import AnkiJsonExporterWrapper

DUMMY_EXPORT_DIRECTORY = "/tmp"
Expand All @@ -23,3 +27,5 @@

notifier_mock.warning.assert_called_once()
exporter_mock.export_to_directory.assert_not_called()

mock_anki_modules.unmock()
27 changes: 21 additions & 6 deletions test_utils/anki/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
from typing import List
from typing import Optional
import sys

from unittest.mock import MagicMock


def mock_anki_modules():
class MockAnkiModules:
"""
I'd like to get rid of the situation when this is required, but for now this helps with the situation that
anki modules are not available during test runtime.
"""
module_names_list = ['anki', 'anki.hooks', 'anki.exporting', 'anki.decks', 'anki.utils', 'anki.cards', 'anki.models',
'anki.notes', 'aqt', 'aqt.qt', 'aqt.exporting', 'aqt.utils']

def __init__(self, module_names_list: Optional[List[str]] = None):
if module_names_list is None:
module_names_list = self.module_names_list

self.shadowed_modules = {}

modules_list = ['anki', 'anki.hooks', 'anki.exporting', 'anki.decks', 'anki.utils', 'anki.cards', 'anki.models',
'anki.notes', 'aqt''', 'aqt.qt', 'aqt.exporting', 'aqt.utils']
for module_name in module_names_list:
self.shadowed_modules[module_name] = sys.modules.get(module_name)
sys.modules[module_name] = MagicMock()

for module in modules_list:
sys.modules[module] = MagicMock()
def unmock(self):
for module_name, module in self.shadowed_modules.items():
if module is not None:
sys.modules[module_name] = module
else:
if module_name in sys.modules:
del sys.modules[module_name]