GnuCash  5.6-150-g038405b370+
test_transaction.py
1 from unittest import main
2 
3 from gnucash import Transaction, Book, Account, Split
4 from unittest_support import *
5 from datetime import datetime, timezone
6 
7 from test_book import BookSession
8 
10  def setUp(self):
11  self.domain1 = "gnc.engine"
12  self.domain2 = "gnc.engine.scrub"
13  level = G_LOG_LEVEL_CRITICAL
14  check1 = TestErrorStruct()
15  check1.log_domain = self.domain1
16  check1.log_level = level
17  check1.msg = "[xaccScrubUtilityGetOrMakeAccount()] No currency specified!"
18  test_add_error(check1)
19  check2 = TestErrorStruct()
20  check2.log_domain = self.domain1
21  check2.log_level = level
22  check2.msg = "[xaccTransScrubSplits()] Transaction doesn't have a currency!"
23  test_add_error(check2)
24  self.hdlr1 = test_set_list_handler(self.domain1, level, None)
25  check3 = TestErrorStruct()
26  check3.log_domain = "gnc.engine.scrub"
27  check3.log_level = level
28  check3.msg = "[xaccScrubUtilityGetOrMakeAccount()] No currency specified!"
29  self.hdlr2 = test_set_checked_handler(self.domain2, level, check3)
30  BookSession.setUp(self)
31  self.trans = Transaction(self.book)
32  #Evil bug means we must set a split for the transaction before making
33  #any other changes (is slightly useful for later tests)
34  self.split = Split(self.book)
35  self.split.SetParent(self.trans)
36 
37  self.trans.SetCurrency(self.currency)
38 
39  def tearDown(self):
40  g_log_remove_handler(self.domain1, self.hdlr1)
41  g_log_remove_handler(self.domain2, self.hdlr2)
42  test_clear_error_list ()
43 
45  def test_equal(self):
46  TRANS = self.trans
47  self.assertTrue( TRANS.Equal(self.trans, True, False, False, False) )
48  # test __eq__ implementation
49  SPLIT = Split(self.book)
50  SPLIT.SetParent(TRANS)
51  self.assertTrue( self.trans == SPLIT.GetParent() )
52  self.assertTrue( self.trans != Transaction(self.book) )
53 
54  def test_clone(self):
55  domain = "gnc.engine"
56  level = G_LOG_LEVEL_WARNING
57  check = TestErrorStruct()
58  check.log_domain = domain
59  check.log_level = level
60  check.msg = "[xaccTransEqual()] GUIDs differ"
61  hdlr = test_set_checked_handler(domain, level, check)
62 
63  TRANS = self.trans.Clone()
64  #Clone and original should have different GUIDs
65  self.assertFalse( TRANS.Equal(self.trans, True, False, False, False) )
66  #Clone and original should have the same balance
67  self.assertTrue( TRANS.Equal(self.trans, False, False, True, False) )
68 
69  g_log_remove_handler(domain, hdlr)
70 
71  def test_setcurrency(self):
72  self.assertTrue( self.currency.equal( self.trans.GetCurrency() ) )
73 
74  def test_edit(self):
75  self.assertFalse( self.trans.IsOpen() )
76  self.trans.BeginEdit()
77  self.assertTrue( self.trans.IsOpen() )
78  self.trans.CommitEdit()
79  self.assertFalse( self.trans.IsOpen() )
80 
81  def test_rollback(self):
82  self.assertEqual( '', self.trans.GetDescription() )
83  self.trans.BeginEdit()
84  DESC = 'Food'
85  self.trans.SetDescription(DESC)
86  self.assertEqual( DESC, self.trans.GetDescription() )
87  self.trans.RollbackEdit()
88  self.assertEqual( '', self.trans.GetDescription() )
89 
90  def test_findsplit(self):
91  ACCT = Account(self.book)
92  ACCT.SetCommodity(self.currency)
93  self.split.SetAccount( ACCT )
94  SPLIT = self.trans.FindSplitByAccount( ACCT )
95  self.assertTrue( SPLIT.Equal(self.split, True, False, False) )
96 
97  def test_getsplit(self):
98  SPLIT = self.trans.GetSplit(0)
99  self.assertTrue( SPLIT.Equal(self.split, True, False, False) )
100 
101  def test_getsplitindex(self):
102  self.assertEqual( 0, self.trans.GetSplitIndex(self.split) )
103 
104  def test_countsplits(self):
105  self.assertEqual( 1, self.trans.CountSplits() )
106 
107  def test_readonly(self):
108  self.assertEqual( None, self.trans.GetReadOnly() )
109  REASON = 'none'
110  self.trans.SetReadOnly(REASON)
111  self.assertEqual( REASON, self.trans.GetReadOnly() )
112  self.trans.ClearReadOnly()
113  self.assertEqual( None, self.trans.GetReadOnly() )
114 
115  def test_num(self):
116  NUM = '5'
117  self.assertEqual( '', self.trans.GetNum() )
118  self.trans.SetNum(NUM)
119  self.assertEqual( NUM, self.trans.GetNum() )
120 
121  def test_description(self):
122  DESCR = 'Groceries'
123  self.assertEqual( '', self.trans.GetDescription() )
124  self.trans.SetDescription(DESCR)
125  self.assertEqual( DESCR, self.trans.GetDescription() )
126 
127  def test_notes(self):
128  NOTE = 'For dinner party'
129  self.assertEqual( None, self.trans.GetNotes() )
130  self.trans.SetNotes(NOTE)
131  self.assertEqual( NOTE, self.trans.GetNotes() )
132 
133  def test_date(self):
134  ZERODATE=datetime(1970, 1, 1, 0, 0, 0, tzinfo=timezone.utc)
135  DATE=datetime(2020, 2, 20, 10, 59, 0, tzinfo=timezone.utc)
136  self.assertEqual(ZERODATE, self.trans.GetDate().astimezone(timezone.utc))
137  self.trans.SetDate(DATE.day, DATE.month, DATE.year)
138  self.assertEqual(DATE, self.trans.GetDate().astimezone(timezone.utc))
139 
140 if __name__ == '__main__':
141  main()
Struct to pass as user_data for the handlers.