From 3421b3c04793d50eda1863587e2f501b8a6f49f8 Mon Sep 17 00:00:00 2001 From: StSav012 Date: Tue, 2 Apr 2024 19:45:57 +0300 Subject: [PATCH] Test for imported modules --- test/test_cli.py | 41 +++++++++++++++++++++++++++++++++-- test/test_gui.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 93 insertions(+), 4 deletions(-) diff --git a/test/test_cli.py b/test/test_cli.py index 5a810c5..5e555d5 100644 --- a/test/test_cli.py +++ b/test/test_cli.py @@ -3,11 +3,48 @@ from __future__ import annotations +def _third_party_modules() -> list[str]: + import site + import sys + + prefixes: list[str] = site.getsitepackages([sys.exec_prefix, sys.prefix]) + third_party_modules: list[str] = [] + for module_name, module in sys.modules.copy().items(): + paths = getattr(module, "__path__", []) + if ( + "." not in module_name + and module_name != "_distutils_hack" + and paths + and getattr(module, "__package__", "") + and any(p.startswith(prefix) for p in paths for prefix in prefixes) + ): + third_party_modules.append(module_name) + + return third_party_modules + + def test_cli(): + import sys + from importlib.util import find_spec + from pycatsearch import main_cli as main - return main() + third_party_modules: list[str] + + third_party_modules = _third_party_modules() + assert third_party_modules == [], third_party_modules + + assert main() != 0 + + sys.argv.append("catalog.json.gz") + assert main() != 0 + + sys.argv.extend("--min-frequency 118749 --max-frequency 118751 -n oxygen".split()) + assert main() == 0 + + third_party_modules = _third_party_modules() + assert third_party_modules == ["orjson"] * (find_spec("orjson") is not None), third_party_modules if __name__ == "__main__": - exit(test_cli()) + test_cli() diff --git a/test/test_gui.py b/test/test_gui.py index 8dca01f..21b87b0 100644 --- a/test/test_gui.py +++ b/test/test_gui.py @@ -3,11 +3,63 @@ from __future__ import annotations +def _third_party_modules() -> list[str]: + import site + import sys + + prefixes: list[str] = site.getsitepackages([sys.exec_prefix, sys.prefix]) + third_party_modules: list[str] = [] + for module_name, module in sys.modules.copy().items(): + paths = getattr(module, "__path__", []) + if ( + "." not in module_name + and module_name != "_distutils_hack" + and paths + and getattr(module, "__package__", "") + and any(p.startswith(prefix) for p in paths for prefix in prefixes) + ): + third_party_modules.append(module_name) + + return third_party_modules + + def test_gui(): from pycatsearch import main_gui as main - return main() + third_party_modules: list[str] + + third_party_modules = _third_party_modules() + assert third_party_modules == [], third_party_modules + + # intentionally fail the app to let it import everything it needs + from qtpy.QtCore import QCoreApplication + + app = QCoreApplication() + assert (r := main()) != 0, r + assert app is not None + + expected_third_party_modules: list[str] = [ + "packaging", + "shiboken6", + "PySide6", + "PySide2", + "PyQt6", + "PyQt5", + "qtpy", + "orjson", + "multidict", + "attr", + "idna", + "yarl", + "frozenlist", + "aiosignal", + "aiohttp", + "qtawesome", + ] + third_party_modules = _third_party_modules() + assert third_party_modules + assert set(third_party_modules).issubset(expected_third_party_modules), third_party_modules if __name__ == "__main__": - exit(test_gui()) + test_gui()