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_basictransaction_validation_date(self, book_basic):
EUR = book_basic.commodities(namespace="CURRENCY")
racc = book_basic.root_account
a = book_basic.accounts(name="asset")
e = book_basic.accounts(name="exp")
splits = [
Split(account=a, value=100, memo=u"mémo asset"),
Split(account=e, value=-10, memo=u"mémo exp"),
]
with pytest.raises(GncValidationError):
tr = Transaction(currency=EUR, description=u"wire from Hélène", notes=u"on St-Eugène day",
post_date=datetime(2014, 1, 1),
enter_date=datetime(2014, 1, 1),
splits=splits)
with pytest.raises(GncValidationError):
tr = Transaction(currency=EUR, description=u"wire from Hélène", notes=u"on St-Eugène day",
post_date=datetime(2014, 1, 1),
enter_date=time(10, 59, 00),
splits=splits)
with pytest.raises(GncValidationError):
tr = Transaction(currency=EUR, description=u"wire from Hélène", notes=u"on St-Eugène day",
def test_create_closedlot_addsplits(self, book_basic):
EUR = book_basic.commodities(namespace="CURRENCY")
racc = book_basic.root_account
a = book_basic.accounts(name="asset")
s = book_basic.accounts(name="broker")
l = Lot(title=u"test mé", account=s, notes=u"ïlya")
l.is_closed = 1
# raise valueerror as lot is closed
with pytest.raises(ValueError):
tr = Transaction(currency=EUR, description="trade stock", notes=u"àçö",
post_date=date(2014, 1, 1),
enter_date=datetime(2014, 1, 1),
splits=[
Split(account=a, value=10, memo=u"mémo asset"),
Split(account=s, value=- 10, quantity=-2, memo=u"mémo brok", lot=l),
])
def test_create_simpletlot_initialsplits(self, book_basic):
EUR = book_basic.commodities(namespace="CURRENCY")
racc = book_basic.root_account
a = book_basic.accounts(name="asset")
s = book_basic.accounts(name="broker")
sp = []
for i, am in enumerate([45, -35, -20]):
tr = Transaction(currency=EUR, description="trade stock", notes=u"àçö",
post_date=date(2014, 1, 1 + i),
enter_date=datetime(2014, 1, 1 + i),
splits=[
Split(account=a, value=am * 10, memo=u"mémo asset"),
Split(account=s, value=-am * 10, quantity=-am, memo=u"mémo brok"),
])
sp.append(tr.splits(account=s))
l = Lot(title=u"test mé", account=s, notes=u"ïlya", splits=sp)
book_basic.flush()
def test_create_simpletlot_addsplits(self, book_basic):
EUR = book_basic.commodities(namespace="CURRENCY")
racc = book_basic.root_account
a = book_basic.accounts(name="asset")
s = book_basic.accounts(name="broker")
l = Lot(title=u"test mé", account=s, notes=u"ïlya")
for i, am in enumerate([45, -35, -20]):
tr = Transaction(currency=EUR, description="trade stock", notes=u"àçö",
post_date=date(2014, 1, 1 + i),
enter_date=datetime(2014, 1, 1 + i),
splits=[
Split(account=a, value=am * 10, memo=u"mémo asset"),
Split(account=s, value=-am * 10, quantity=-am, memo=u"mémo brok", lot=l),
])
book_basic.flush()
def test_create_cdtytransaction(self, book_basic):
EUR = book_basic.commodities(namespace="CURRENCY")
racc = book_basic.root_account
a = book_basic.accounts(name="asset")
s = book_basic.accounts(name="broker")
tr = Transaction(currency=EUR, description="buy stock", notes=u"on St-Eugène day",
post_date=date(2014, 1, 2),
enter_date=datetime(2014, 1, 3),
splits=[
Split(account=a, value=100, memo=u"mémo asset"),
Split(account=s, value=-90, memo=u"mémo brok"),
])
# check issue with quantity for broker split not defined
with pytest.raises(GncValidationError):
book_basic.validate()
sb = tr.splits(account=s)
sb.quantity = 15
# check issue with quantity not same sign as value
with pytest.raises(GncValidationError):
book_basic.validate()
sb.quantity = -15
# verify imbalance issue
def test_create_simpletlot_addsplits(self, book_basic):
EUR = book_basic.commodities(namespace="CURRENCY")
racc = book_basic.root_account
a = book_basic.accounts(name="asset")
s = book_basic.accounts(name="broker")
l = Lot(title=u"test mé", account=s, notes=u"ïlya")
for i, am in enumerate([45, -35, -20]):
tr = Transaction(currency=EUR, description="trade stock", notes=u"àçö",
post_date=date(2014, 1, 1 + i),
enter_date=datetime(2014, 1, 1 + i),
splits=[
Split(account=a, value=am * 10, memo=u"mémo asset"),
Split(account=s, value=-am * 10, quantity=-am, memo=u"mémo brok", lot=l),
])
book_basic.flush()
cdty = Commodity(namespace=u"BEL20", mnemonic=u"GnuCash Inc.", fullname=u"GnuCash Inc. stock")
asset = Account(name="asset", type="ASSET", commodity=curr, parent=b.root_account)
foreign_asset = Account(name="foreign asset", type="ASSET", commodity=other_curr, parent=b.root_account)
stock = Account(name="broker", type="STOCK", commodity=cdty, parent=asset)
expense = Account(name="exp", type="EXPENSE", commodity=curr, parent=b.root_account)
income = Account(name="inc", type="INCOME", commodity=curr, parent=b.root_account)
tr1 = Transaction(post_date=datetime(2015, 10, 21),
description="my revenue",
currency=curr,
splits=[
Split(account=asset, value=(1000, 1)),
Split(account=income, value=(-1000, 1)),
]
)
tr2 = Transaction(post_date=datetime(2015, 10, 25),
description="my expense",
currency=curr,
splits=[
Split(account=asset, value=(-100, 1)),
Split(account=expense, value=(20, 1), memo="cost of X"),
Split(account=expense, value=(80, 1), memo="cost of Y"),
]
)
tr_stock = Transaction(post_date=datetime(2015, 10, 29),
description="my purchase of stock",
currency=curr,
splits=[
Split(account=asset, value=(-200, 1)),
Split(account=expense, value=(15, 1), memo="transaction costs"),
Split(account=stock, value=(185, 1), quantity=(6, 1), memo="purchase of stock"),
]
def test_readonly_true(self, session_readonly):
# control exception when adding object to readonly gnucash db
v = Version(table_name="sample", table_version="other sample")
sa_session_readonly = session_readonly.session
sa_session_readonly.add(v)
with pytest.raises(GnucashException):
sa_session_readonly.commit()
# control exception when deleting object to readonly gnucash db
sa_session_readonly.delete(session_readonly.query(Account).first())
with pytest.raises(GnucashException):
sa_session_readonly.commit()
# control exception when modifying object to readonly gnucash db
sa_session_readonly.query(Account).first().name = "foo"
with pytest.raises(GnucashException):
sa_session_readonly.commit()
def test_open_RW_backup(self, book_uri):
# create book
with create_book(uri_conn=book_uri) as b:
engine_type = b.session.bind.name
# open book with readonly = False (ie RW)
if engine_type != "sqlite":
# raise an exception as try to do a backup on postgres which is not supported yet
with pytest.raises(GnucashException):
b = open_book(uri_conn=book_uri, readonly=False)
elif engine_type == "sqlite":
# delete all potential existing backup files
url = book_uri[len("sqlite:///"):]
for fn in glob.glob("{}.[0-9]*.gnucash".format(url)):
os.remove(fn)
# open file in RW without a backup creation
with open_book(uri_conn=book_uri, readonly=False, do_backup=False) as b:
pass
# check no backup file creation
assert len(glob.glob("{}.[0-9]*.gnucash".format(url))) == 0
# open file in RW without a backup creation
with open_book(uri_conn=book_uri, readonly=False) as b:
def use_copied_book(request, template_filename, test_filename, check_same_thread=True):
shutil.copy(str(template_filename), str(test_filename))
# default book is readonly
s = open_book(test_filename, check_same_thread=check_same_thread)
@request.addfinalizer
def finalizer():
s.close()
os.remove(test_filename)
return s