Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_create_specific_currency(self):
b = create_book(currency="USD")
CUR = b.commodities[0]
assert CUR.mnemonic == "USD"
assert CUR.namespace == "CURRENCY"
b = create_book(currency="CHF")
CUR = b.commodities[0]
assert CUR.mnemonic == "CHF"
assert CUR.namespace == "CURRENCY"
with pytest.raises(ValueError):
b = create_book(currency="ZIE")
def test_create_with_FK(self):
# create and keep FK
b = create_book(uri_conn=db_sqlite_uri, keep_foreign_keys=True, overwrite=True)
b.session.close()
insp = Inspector.from_engine(create_engine(db_sqlite_uri))
fk_total = []
for tbl in insp.get_table_names():
fk_total.append(insp.get_foreign_keys(tbl))
assert len(fk_total) == 25
def new_book_USD(request):
name = request.param
if name and database_exists(name):
drop_database(name)
with create_book(uri_conn=name, currency="USD", keep_foreign_keys=False) as b:
yield b
if name and database_exists(name):
drop_database(name)
assert os.path.exists(db_sqlite)
t = os.path.getmtime(db_sqlite)
# ensure error if no overwrite
with pytest.raises(GnucashException):
b = create_book(db_sqlite)
assert os.path.getmtime(db_sqlite) == t
with pytest.raises(GnucashException):
b = create_book(uri_conn="sqlite:///{}".format(db_sqlite))
assert os.path.getmtime(db_sqlite) == t
with pytest.raises(GnucashException):
b = create_book(db_sqlite, overwrite=False)
assert os.path.getmtime(db_sqlite) == t
# if overwrite, DB is recreated
b = create_book(db_sqlite, overwrite=True)
assert os.path.getmtime(db_sqlite) > t
# clean test
os.remove(db_sqlite)
def test_create_named_sqlite_book(self):
# remove file if left from previous test
if os.path.exists(db_sqlite):
os.remove(db_sqlite)
# assert error if both sqlite_file and uri_conn are defined
with pytest.raises(ValueError):
b = create_book(db_sqlite, db_sqlite_uri)
# assert creation of file
b = create_book(db_sqlite)
assert os.path.exists(db_sqlite)
t = os.path.getmtime(db_sqlite)
# ensure error if no overwrite
with pytest.raises(GnucashException):
b = create_book(db_sqlite)
assert os.path.getmtime(db_sqlite) == t
with pytest.raises(GnucashException):
b = create_book(uri_conn="sqlite:///{}".format(db_sqlite))
assert os.path.getmtime(db_sqlite) == t
with pytest.raises(GnucashException):
b = create_book(db_sqlite, overwrite=False)
assert os.path.getmtime(db_sqlite) == t
# if overwrite, DB is recreated
b = create_book(db_sqlite, overwrite=True)
# trying to create an unbalanced transaction trigger an exception
# (there is not automatic creation of an imbalance split)
tr3 = Transaction(currency=c1,
description="transfer imb",
splits=[
Split(account=a1, value=-100),
Split(account=a2, value=100, quantity=30)
])
print(tr3.ledger_str())
s.flush()
fdsfdsfds
if True:
from piecash import create_book, Account
with create_book(currency="EUR") as s:
# retrieve the default currency
EUR = s.commodities.get(mnemonic="EUR")
# creating a placeholder account
acc = Account(name="My account",
type="ASSET",
parent=s.book.root_account,
commodity=EUR,
placeholder=True, )
# creating a detailed sub-account
subacc = Account(name="My sub account",
type="BANK",
parent=acc,
commodity=EUR,
commodity_scu=1000,
from __future__ import print_function
from decimal import Decimal
from datetime import datetime
import decimal
import inspect
from sqlalchemy.orm import object_session
from piecash import open_book, Budget
from piecash._common import Recurrence
from piecash import create_book, Account, Transaction, Split, Commodity
b = create_book("mytest.gnucash", currency="EUR", keep_foreign_keys=False, overwrite=True)
# create some accounts
curr = b.currencies[0]
cdty = Commodity(namespace="échange",mnemonic="ïoà", fullname="Example of unicode déta")
a = Account(name="asset", type="ASSET", commodity=curr, parent=b.root_account)
Account(name="broker", type="STOCK", commodity=cdty, parent=a)
Account(name="exp", type="EXPENSE", commodity=curr, parent=b.root_account)
Account(name="inc", type="INCOME", commodity=curr, parent=b.root_account)
b.flush()
EUR = b.commodities(namespace="CURRENCY")
racc = b.root_account
a = b.accounts(name="asset")
s = b.accounts(name="broker")
b.book.use_trading_accounts=True
tr = Transaction(currency=EUR, description="buy stock", notes="on St-Eugène day",
post_date=datetime(2014, 1, 2),
enter_date=datetime(2014, 1, 3),
def create_book(self):
"""Creates a new in-memory book"""
# piecash.create_book(filename)
return piecash.create_book()
root = book.root_account
# accessing children accounts of root
r = s.book.root_account.children(name="Assets").children[0]
for acc in s.book.root_account.children(name="Assets").children[0].children:
print(acc)
print(inspect.getmro(Budget))
print(inspect.getmro(Recurrence))
b = Budget(name=lambda x: x, foo="3")
b = Budget(name=lambda x: x, foo="3")
fdsd
print(b)
fdsfds
with create_book() as s:
# retrieve list of slots
print(s.book.slots)
# set slots
s.book["myintkey"] = 3
s.book["mystrkey"] = "hello"
s.book["myboolkey"] = True
s.book["mydatekey"] = datetime.datetime.today().date()
s.book["mydatetimekey"] = datetime.datetime.today()
s.book["mynumerickey"] = decimal.Decimal("12.34567")
s.book["account"] = s.book.root_account
# iterate over all slots
for k, v in s.book.iteritems():
print("slot={v} has key={k} and value={v.value} of type {t}".format(k=k, v=v, t=type(v.value)))
def sql_create(book):
"""Create an empty book with gnucash
"""
with piecash.create_book(book, overwrite=True,keep_foreign_keys=False) as b:
b.save()