import piecash
import datetime
# open a SQLAlchemy session linked to the test.gnucash file (as sqlite3 saved Book)
s = piecash.connect_to_gnucash_book("test.gnucash", readonly=False)
# retrieve the single Book object from the session (this is a sqlalchemy standard call)
book = s.query(piecash.Book).one()
# retrieve the EUR currency
EUR = s.query(piecash.Commodity).one()
# from the book, retrieve the root account and display its children accounts
book.root_account.children
# retrieve the standard 3 default assets accounts (checking account, saving account, cash in wallet)
curacc, savacc, cash = book.root_account.children[0].children[0].children
# check splits (they should be empty if the GnuCash book was an empty Book)
savacc.splits, curacc.splits
# create a transaction of 45 € from the saving account to the checking account
tr = piecash.Transaction.single_transaction(datetime.date.today(),datetime.date.today(), "transfer of money", 45, EUR, savacc, curacc)
# check some attributes of the transaction
tr.description, tr.splits
# check the splits from the accounts point of view
savacc.splits, curacc.splits
# rollback the session (i.e. undo all changes)
s.rollback()
# check splits after the rollback (they should be unchanged)
savacc.splits, curacc.splits