def test_create_commodity_uniqueness(self, book_basic): assert len(book_basic.commodities) == 2 cdty1 = Commodity(namespace="AMEX", mnemonic="APPLE", fullname="Apple", book=book_basic) cdty2 = Commodity(namespace="AMEX", mnemonic="APPLE", fullname="Apple", book=book_basic) with pytest.raises(ValueError): book_basic.save()
def test_single_transaction(self, book_basic): today = datetime.today() print("today=", today) factories.single_transaction( today.date(), today, "my test", Decimal(100), from_account=book_basic.accounts(name="inc"), to_account=book_basic.accounts(name="asset"), ) book_basic.save() tr = book_basic.transactions(description="my test") assert len(tr.splits) == 2 sp1, sp2 = tr.splits if sp1.value > 0: sp2, sp1 = sp1, sp2 # sp1 has negative value assert sp1.account == book_basic.accounts(name="inc") assert sp2.account == book_basic.accounts(name="asset") assert sp1.value == -sp2.value assert sp1.quantity == sp1.value assert tr.enter_date == tzlocal.get_localzone().localize( today.replace(microsecond=0)) assert tr.post_date == tzlocal.get_localzone().localize(today).date()
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="on St-Eugène day", post_date=date(2014, 1, 2), enter_date=datetime(2014, 1, 3), splits=[ Split(account=a, value=100, memo="mémo asset"), Split(account=s, value=-90, memo="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 with pytest.raises(GncImbalanceError): book_basic.validate() # adjust balance Split(account=a, value=-10, memo="missing asset corr", transaction=tr) book_basic.save() assert str(sb) assert str(sb) # changing currency of an existing transaction is not allowed tr.currency = book_basic.currencies(mnemonic="USD") with pytest.raises(GncValidationError): book_basic.validate() book_basic.cancel() # check sum of quantities are not balanced per commodity but values are d = defaultdict(lambda: Decimal(0)) for sp in tr.splits: assert sp.quantity == sp.value or sp.account != a d[sp.account.commodity] += sp.quantity d["cur"] += sp.value assert d["cur"] == 0 assert all([v != 0 for k, v in d.items() if k != "cur"])
def test_single_transaction_tz(self, book_basic): today = tzlocal.get_localzone().localize(datetime.today()) factories.single_transaction(today, today, "my test", Decimal(100), from_account=book_basic.accounts(name="inc"), to_account=book_basic.accounts(name="asset")) book_basic.save() tr = book_basic.transactions(description="my test") assert tr.post_date == today.replace(hour=11, minute=0, second=0, microsecond=0) assert tr.enter_date == today.replace(microsecond=0)
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=datetime(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.flush() sb = tr.splits(account=s) sb.quantity = 15 # check issue with quantity not same sign as value with pytest.raises(GncValidationError): book_basic.flush() sb.quantity = -15 # verify imbalance issue with pytest.raises(GncImbalanceError): book_basic.flush() # adjust balance Split(account=a, value=-10, memo="missing asset corr", transaction=tr) book_basic.flush() book_basic.save() assert str(sb) assert str(sb) # changing currency of an existing transaction is not allowed tr.currency = book_basic.currencies(mnemonic="USD") with pytest.raises(GncValidationError): book_basic.flush() book_basic.cancel() # check sum of quantities are not balanced per commodity but values are d = defaultdict(lambda: Decimal(0)) for sp in tr.splits: assert sp.quantity == sp.value or sp.account != a d[sp.account.commodity] += sp.quantity d["cur"] += sp.value assert d["cur"] == 0 assert all([v != 0 for k, v in d.items() if k != "cur"])
def test_single_transaction(self, book_basic): today = datetime.today() factories.single_transaction(today, today, "my test", Decimal(100), from_account=book_basic.accounts(name="inc"), to_account=book_basic.accounts(name="asset")) book_basic.save() tr = book_basic.transactions(description="my test") assert len(tr.splits) == 2 sp1, sp2 = tr.splits if sp1.value > 0: sp2, sp1 = sp1, sp2 # sp1 has negative value assert sp1.account == book_basic.accounts(name="inc") assert sp2.account == book_basic.accounts(name="asset") assert sp1.value == -sp2.value assert sp1.quantity == sp1.value assert tr.enter_date == tzlocal.get_localzone().localize(today.replace(microsecond=0)) assert tr.post_date == tzlocal.get_localzone().localize(today.replace(hour=11, minute=0, second=0, microsecond=0))