From 4759bdcd334d8501902330b6d621ae38cd99c5c7 Mon Sep 17 00:00:00 2001 From: Ad5001 Date: Wed, 18 Sep 2024 00:31:17 +0200 Subject: [PATCH] Continuing python tests --- tests/python/test_config.py | 44 ++++++++++++++++++++++++++++++++----- tests/python/test_helper.py | 13 +++++++++++ tests/python/test_pyjs.py | 34 ++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 6 deletions(-) diff --git a/tests/python/test_config.py b/tests/python/test_config.py index 493b0f1..c919c6f 100644 --- a/tests/python/test_config.py +++ b/tests/python/test_config.py @@ -1,10 +1,42 @@ -import unittest +import pytest +from LogarithmPlotter.util import config +from tempfile import TemporaryDirectory +from os.path import join -class MyTestCase(unittest.TestCase): - def test_something(self): - self.assertEqual(True, False) # add assertion here +@pytest.fixture() +def resource(): + directory = TemporaryDirectory() + config.CONFIG_FILE = join(directory.name, "config.json") + config.init() + yield config.CONFIG_FILE + directory.cleanup() -if __name__ == '__main__': - unittest.main() +class TestConfig: + + def test_init(self, resource): + assert config.current_config == config.DEFAULT_SETTINGS + + def test_get(self, resource): + assert config.getSetting("expression_editor.autoclose") == True + with pytest.raises(config.UnknownNamespaceError): + config.getSetting("unknown_setting") + + def test_set(self, resource): + assert config.setSetting("expression_editor.autoclose", False) is None + assert config.getSetting("expression_editor.autoclose") == False # Ensure set is working. + with pytest.raises(config.UnknownNamespaceError): + config.setSetting("unknown_dict.unknown_setting", False) + + def test_reinit(self, resource): + default_value = config.getSetting("expression_editor.autoclose") + config.setSetting("expression_editor.autoclose", not default_value) + config.init() + assert config.getSetting("expression_editor.autoclose") != default_value # Ensure setting has been reset. + + def test_save(self, resource): + config.setSetting("expression_editor.autoclose", False) + config.save(resource) + config.init() + assert config.getSetting("expression_editor.autoclose") == False # Ensure setting has been saved. diff --git a/tests/python/test_helper.py b/tests/python/test_helper.py index e69de29..e57ec61 100644 --- a/tests/python/test_helper.py +++ b/tests/python/test_helper.py @@ -0,0 +1,13 @@ +import pytest +from os.path import join +from tempfile import TemporaryDirectory +from LogarithmPlotter.util import config + + +@pytest.fixture() +def resource(): + directory = TemporaryDirectory() + config.CONFIG_PATH = join(directory.name, "config.json") + tmpfile = join(directory.name, 'graph.png') + yield tmpfile + directory.cleanup() \ No newline at end of file diff --git a/tests/python/test_pyjs.py b/tests/python/test_pyjs.py index e69de29..7765f30 100644 --- a/tests/python/test_pyjs.py +++ b/tests/python/test_pyjs.py @@ -0,0 +1,34 @@ + +import pytest +from PySide6.QtQml import QJSEngine, QJSValue +from PySide6.QtWidgets import QApplication + +from LogarithmPlotter.util.js import PyJSValue, InvalidAttributeValueException + +app = QApplication() +engine = QJSEngine() +obj = PyJSValue(engine.globalObject()) + +class TestPyJS: + def test_set(self): + obj.num1 = 2 + obj.num2 = QJSValue(2) + with pytest.raises(InvalidAttributeValueException): + obj.num3 = object() + + def test_eq(self): + obj.num = QJSValue(2) + assert obj.num == 2 + assert obj.num == QJSValue(2) + assert obj.num != object() + + def test_function(self): + function = PyJSValue(engine.evaluate("(function(argument) {return argument*2})")) + assert function(3) == 6 + assert function(10) == 20 + function2 = PyJSValue(engine.evaluate("(function(argument) {return argument+3})"), obj.qjs_value) + assert function2(3) == 6 + assert function2(10) == 13 + function3 = PyJSValue(engine.evaluate("2+2")) + with pytest.raises(InvalidAttributeValueException): + function3()