11 from random
import randint
12 from gnucash
import Session, SessionOpenMode, Account, Transaction, Split, \
14 from gnucash
import QOF_COMPARE_GTE, QOF_COMPARE_GT, QOF_QUERY_AND, \
15 QOF_QUERY_OR, QOF_STRING_MATCH_NORMAL, QOF_COMPARE_CONTAINS, \
17 from gnucash
import gnucash_core_c
18 from gnucash
import gnucash_core
21 uri =
"xml:///tmp/qof.gnucash" 23 def createAccounts(book):
24 root_account = book.get_root_account()
25 commodtable = book.get_table()
26 currency = commodtable.lookup(
"CURRENCY",
"EUR")
29 print(
'Create two accounts ("Account A", "Account B")')
31 accountA.SetCommodity(currency)
32 accountA.SetName(
"Account A")
33 root_account.append_child(accountA)
36 accountB.SetCommodity(currency)
37 accountB.SetName(
"Account B")
38 root_account.append_child(accountB)
42 return accountA, accountB
44 def createRandomTransactions(book, accountA, accountB):
47 currency = book.get_table().lookup(
"CURRENCY",
"EUR")
49 print(
"Create 100 random transactions")
52 trans = Transaction(book)
54 trans.SetCurrency(currency)
55 trans.SetDate(randint(1,28), randint(1,12), randint(1900,2000))
56 trans.SetDescription(
"Transaction "+str(i))
58 value = randint(0,10000)
64 split1.SetValue(value1)
65 split1.SetAccount(accountA)
66 split1.SetMemo(
"A" + str(i))
67 split1.SetParent(trans)
70 split2.SetValue(value2)
71 split2.SetAccount(accountB)
72 split2.SetMemo(
"B" + str(i))
73 split2.SetParent(trans)
77 def query_transactions(book, terms=[]):
80 query.search_for(
'Trans')
89 for transaction
in query.run():
90 transaction = Transaction(instance=transaction)
91 transactions.append(transaction)
96 def query_splits(book, terms=[]):
99 query.search_for(
'Split')
104 query.add_term(*term)
108 for split
in query.run():
109 split = Split(instance=split)
115 with Session(uri, SessionOpenMode.SESSION_NEW_STORE)
as ses:
117 book = ses.get_book()
118 accountA, accountB = createAccounts(book)
119 createRandomTransactions(book, accountA, accountB)
124 transactions_all = query_transactions(book)
125 print(
"Query all: " + str(len(transactions_all)) +
" transactions.")
128 threshold = datetime.datetime(1950,1,1)
129 QOF_DATE_MATCH_NORMAL = 2
130 terms = [([
'date-posted'], gnucash_core.QueryDatePredicate(QOF_COMPARE_GTE, QOF_DATE_MATCH_NORMAL, threshold), QOF_QUERY_AND)]
131 transactions_2 = query_transactions(book, terms)
132 print(
"Query transactions with date > 1950: " + str(len(transactions_2)) +
" (Should be about 50).")
136 terms = [([
'desc'], gnucash_core.QueryStringPredicate(QOF_COMPARE_CONTAINS,
"Transaction 5", QOF_STRING_MATCH_NORMAL, isRegex), QOF_QUERY_AND)]
137 transactions_3 = query_transactions(book, terms)
138 print(
"Query transaction with description containing 'Transaction 5': " + str(len(transactions_3)) +
" (Should be 11).")
144 terms = [([
'memo'], gnucash_core.QueryStringPredicate(QOF_COMPARE_CONTAINS,
"A22", QOF_STRING_MATCH_NORMAL, isRegex), QOF_QUERY_AND)]
145 splits_1 = query_splits(book, terms)
146 print(
"Query splits with memo containing 'A22': " + str(len(splits_1)) +
" (Should be 1).")
150 terms = [([
'trans',
'desc'], gnucash_core.QueryStringPredicate(QOF_COMPARE_CONTAINS,
"Transaction 5", QOF_STRING_MATCH_NORMAL, isRegex), QOF_QUERY_AND)]
151 splits_2 = query_splits(book, terms)
152 print(
"Query splits with transaction description containing 'Transaction 5': " + str(len(splits_2)) +
" (Should be 22).")
156 terms = [([
'memo'], gnucash_core.QueryStringPredicate(QOF_COMPARE_CONTAINS,
"A22", QOF_STRING_MATCH_NORMAL, isRegex), QOF_QUERY_AND)]
157 terms += [([
'trans',
'desc'], gnucash_core.QueryStringPredicate(QOF_COMPARE_CONTAINS,
"Transaction 55", QOF_STRING_MATCH_NORMAL, isRegex), QOF_QUERY_OR)]
158 splits_4 = query_splits(book, terms)
159 print(
"Query splits with memo containing 'A22' or transaction desc containing 'Transaction 55': " + str(len(splits_4)) +
" (Should be 3).")
163 terms = [([
"amount"], gnucash_core.QueryNumericPredicate(QOF_COMPARE_GT, QOF_NUMERIC_MATCH_ANY, threshold), QOF_QUERY_AND)]
164 splits_3 = query_splits(book, terms)
165 print(
"Query splits with amount > " + str(threshold) +
": " + str(len(splits_3)) +
" (Should be about 100).")
The primary numeric class for representing amounts and values.