From e025a44ea3e545bdced9f3a361de38c05218c55a Mon Sep 17 00:00:00 2001 From: oakkitten Date: Tue, 12 Apr 2022 19:50:36 +0100 Subject: [PATCH] Tests: make tests pass for Anki 2.1.50 (Qt5) * require Python 3.10 * prevent Anki from doing backups, again * restore cwd that deck exporter changes for some wild reason --- .github/workflows/main.yml | 9 ++++++++- .gitignore | 1 + plugin/__init__.py | 4 +--- tests/conftest.py | 26 +++++++++++++++++++------- tests/test_misc.py | 16 ++++++++++++++++ tox.ini | 9 ++++++--- 6 files changed, 51 insertions(+), 14 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 826f9c1..72bf0ce 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -12,8 +12,15 @@ jobs: sudo apt-get update sudo apt-get install -y pyqt5-dev-tools xvfb - - name: Setup Python + - name: Setup Python 3.8 uses: actions/setup-python@v2 + with: + python-version: 3.8 + + - name: Setup Python 3.9 + uses: actions/setup-python@v2 + with: + python-version: 3.9 - name: Install tox run: pip install tox diff --git a/.gitignore b/.gitignore index 34ac317..fc4b3d6 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ AnkiConnect.zip meta.json .idea/ +.tox/ diff --git a/plugin/__init__.py b/plugin/__init__.py index 6b50f4d..eceaa77 100644 --- a/plugin/__init__.py +++ b/plugin/__init__.py @@ -1425,9 +1425,7 @@ class AnkiConnect: if savedMid: deck['mid'] = savedMid - addCards.editor.note = ankiNote - addCards.editor.loadNote() - addCards.editor.updateTags() + addCards.editor.set_note(ankiNote) addCards.activateWindow() diff --git a/tests/conftest.py b/tests/conftest.py index 9fbbcea..c33f006 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -5,6 +5,7 @@ from dataclasses import dataclass import aqt.operations.note import pytest +import anki.collection from PyQt5 import QtTest from _pytest.monkeypatch import MonkeyPatch # noqa from pytest_anki._launch import anki_running, temporary_user # noqa @@ -77,22 +78,33 @@ def waitress_patched_to_prevent_it_from_dying(): yield +@contextmanager +def anki_patched_to_prevent_backups(): + with MonkeyPatch().context() as monkey: + if ac._anki21_version < 50: + monkey.setitem(aqt.profiles.profileConf, "numBackups", 0) + else: + monkey.setattr(anki.collection.Collection, "create_backup", + lambda *args, **kwargs: True) + yield + + @contextmanager def empty_anki_session_started(): with waitress_patched_to_prevent_it_from_dying(): - with anki_running( - qtbot=None, # noqa - enable_web_debugging=False, - profile_name="test_user", - ) as session: - yield session + with anki_patched_to_prevent_backups(): + with anki_running( + qtbot=None, # noqa + enable_web_debugging=False, + profile_name="test_user", + ) as session: + yield session @contextmanager def profile_created_and_loaded(session): with temporary_user(session.base, "test_user", "en_US"): with session.profile_loaded(): - aqt.mw.pm.profile["numBackups"] = 0 # don't try to do backups yield session diff --git a/tests/test_misc.py b/tests/test_misc.py index b5feaa7..16c8e5a 100755 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -1,4 +1,7 @@ +import os + import aqt +import pytest from conftest import ac, anki_connect_config_loaded, \ set_up_test_deck_and_test_model_and_two_notes, \ @@ -33,6 +36,19 @@ class TestProfiles: class TestExportImport: + # since Anki 2.1.50, exporting media for some wild reason + # will change the current working directory, which then gets removed. + # see `exporting.py`, ctrl-f `os.chdir(self.mediaDir)` + @pytest.fixture(autouse=True) + def current_working_directory_preserved(self): + cwd = os.getcwd() + yield + + try: + os.getcwd() + except FileNotFoundError: + os.chdir(cwd) + def test_exportPackage(self, session_with_profile_loaded, setup): filename = session_with_profile_loaded.base + "/export.apkg" ac.exportPackage(deck="test_deck", path=filename) diff --git a/tox.ini b/tox.ini index 96e56f5..b615d04 100644 --- a/tox.ini +++ b/tox.ini @@ -46,7 +46,7 @@ minversion = 3.24 skipsdist = true skip_install = true -envlist = py38-anki{45,46,47,48,49} +envlist = py38-anki{45,46,47,48,49},py39-anki50qt5 [testenv] commands = @@ -58,7 +58,7 @@ allowlist_externals = deps = pytest==7.1.1 pytest-forked==1.4.0 - pytest-anki @ git+https://github.com/oakkitten/pytest-anki.git@17d19043 + pytest-anki @ git+https://github.com/oakkitten/pytest-anki.git@97708344 anki45: anki==2.1.45 anki45: aqt==2.1.45 @@ -73,4 +73,7 @@ deps = anki48: aqt==2.1.48 anki49: anki==2.1.49 - anki49: aqt==2.1.49 \ No newline at end of file + anki49: aqt==2.1.49 + + anki50qt5: anki==2.1.50 + anki50qt5: aqt[qt5]==2.1.50