From b9c6fc28767c130c7bc52f68c3dbaee88fe77f41 Mon Sep 17 00:00:00 2001 From: c-holtermann Date: Thu, 11 Jun 2020 17:52:02 +0200 Subject: [PATCH] add some unittests for python Session test arguments, deprecated as well as new mode arguments test creating a session with a new xml file using __init__() and begin(). Test raising exception when opening nonexistent file without respective mode setting. --- bindings/python/tests/test_session.py | 47 ++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/bindings/python/tests/test_session.py b/bindings/python/tests/test_session.py index 7751e37e66..8127e2e6a1 100644 --- a/bindings/python/tests/test_session.py +++ b/bindings/python/tests/test_session.py @@ -10,12 +10,57 @@ from unittest import TestCase, main -from gnucash import Session +from gnucash import ( + Session, + SessionOpenMode +) + +from gnucash.gnucash_core import GnuCashBackendException class TestSession(TestCase): def test_create_empty_session(self): self.ses = Session() + def test_session_deprecated_arguments(self): + """use deprecated arguments ignore_lock, is_new, force_new""" + self.ses = Session(ignore_lock=False, is_new=True, force_new=False) + + def test_session_mode(self): + """use mode argument""" + self.ses = Session(mode=SessionOpenMode.SESSION_NORMAL_OPEN) + + def test_session_with_new_file(self): + """create Session with new xml file""" + from tempfile import TemporaryDirectory + from urllib.parse import urlunparse + with TemporaryDirectory() as tempdir: + uri = urlunparse(("xml", tempdir, "tempfile", "", "", "")) + with Session(uri, SessionOpenMode.SESSION_NEW_STORE) as ses: + pass + + # try to open nonexistent file without NEW mode - should raise Exception + uri = urlunparse(("xml", tempdir, "tempfile2", "", "", "")) + with Session() as ses: + with self.assertRaises(GnuCashBackendException): + ses.begin(uri, mode=SessionOpenMode.SESSION_NORMAL_OPEN) + + # try to open nonexistent file without NEW mode - should raise Exception + # use deprecated arg is_new + uri = urlunparse(("xml", tempdir, "tempfile2", "", "", "")) + with Session() as ses: + with self.assertRaises(GnuCashBackendException): + ses.begin(uri, is_new=False) + + uri = urlunparse(("xml", tempdir, "tempfile3", "", "", "")) + with Session() as ses: + ses.begin(uri, mode=SessionOpenMode.SESSION_NEW_STORE) + + # test using deprecated args + uri = urlunparse(("xml", tempdir, "tempfile4", "", "", "")) + with Session() as ses: + ses.begin(uri, is_new=True) + + def test_app_utils_get_current_session(self): from gnucash import _sw_app_utils self.ses_instance = _sw_app_utils.gnc_get_current_session()