GnuCash  5.6-150-g038405b370+
test_session.py
1 # test cases for Session wrapper object
2 #
3 # test for get_book may belong in test_book but it makes sense here
4 # to see if get_current_session works
5 # test for app_utils on the other hand could go to a subfolder of
6 # /libgnucash/app-utils
7 #
8 # @date 2020-04-03
9 # @author Christoph Holtermann <mail@c-holtermann.net>
10 
11 from unittest import TestCase, main
12 
13 from gnucash import (
14  Session,
15  SessionOpenMode
16 )
17 
18 from gnucash.gnucash_core import GnuCashBackendException
19 
20 class TestSession(TestCase):
21  def test_create_empty_session(self):
22  self.ses = Session()
23 
25  """use deprecated arguments ignore_lock, is_new, force_new"""
26  self.ses = Session(ignore_lock=False, is_new=True, force_new=False)
27 
28  def test_session_mode(self):
29  """use mode argument"""
30  self.ses = Session(mode=SessionOpenMode.SESSION_NORMAL_OPEN)
31 
33  """create Session with new xml file"""
34  from tempfile import TemporaryDirectory
35  from urllib.parse import urlunparse
36  with TemporaryDirectory() as tempdir:
37  uri = urlunparse(("xml", tempdir, "tempfile", "", "", ""))
38  with Session(uri, SessionOpenMode.SESSION_NEW_STORE) as ses:
39  pass
40 
41  # try to open nonexistent file without NEW mode - should raise Exception
42  uri = urlunparse(("xml", tempdir, "tempfile2", "", "", ""))
43  with Session() as ses:
44  with self.assertRaises(GnuCashBackendException):
45  ses.begin(uri, mode=SessionOpenMode.SESSION_NORMAL_OPEN)
46 
47  # try to open nonexistent file without NEW mode - should raise Exception
48  # use deprecated arg is_new
49  uri = urlunparse(("xml", tempdir, "tempfile2", "", "", ""))
50  with Session() as ses:
51  with self.assertRaises(GnuCashBackendException):
52  ses.begin(uri, is_new=False)
53 
54  uri = urlunparse(("xml", tempdir, "tempfile3", "", "", ""))
55  with Session() as ses:
56  ses.begin(uri, mode=SessionOpenMode.SESSION_NEW_STORE)
57 
58  # test using deprecated args
59  uri = urlunparse(("xml", tempdir, "tempfile4", "", "", ""))
60  with Session() as ses:
61  ses.begin(uri, is_new=True)
62 
63 
64  def test_app_utils_get_current_session(self):
65  from gnucash import _sw_app_utils
66  self.ses_instance = _sw_app_utils.gnc_get_current_session()
67  self.ses = Session(instance = self.ses_instance)
68  self.assertIsInstance(obj = self.ses, cls = Session)
69 
70  def test_get_book_from_current_session(self):
71  from gnucash import _sw_app_utils
72  from gnucash import Book
73  self.ses_instance = _sw_app_utils.gnc_get_current_session()
74  self.ses = Session(instance = self.ses_instance)
75  self.book = self.ses.get_book()
76  self.assertIsInstance(obj = self.book, cls = Book)
77 
78 if __name__ == '__main__':
79  main()
def test_session_deprecated_arguments(self)
Definition: test_session.py:24
def test_session_with_new_file(self)
Definition: test_session.py:32