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 book_basic(request): name = request.param if name and database_exists(name): drop_database(name) # create new book with create_book(uri_conn=name, currency="EUR", keep_foreign_keys=False) as b: # create some accounts curr = b.currencies[0] cdty = Commodity(namespace=u"échange", mnemonic=u"ïoà", fullname=u"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() yield b if name and database_exists(name): drop_database(name)
def test_create_stock_accounts_incomeaccounts(self, book_basic): broker = book_basic.accounts(name="broker") income = book_basic.accounts(name="inc") appl = Commodity(namespace="NMS", mnemonic="AAPL", fullname="Apple") appl["quoted_currency"] = "USD" acc, inc_accounts = factories.create_stock_accounts(appl, broker_account=broker, income_account=income, income_account_types="D") assert len(inc_accounts) == 1 acc, inc_accounts = factories.create_stock_accounts(appl, broker_account=broker, income_account=income, income_account_types="CL") assert len(inc_accounts) == 1 acc, inc_accounts = factories.create_stock_accounts(appl, broker_account=broker, income_account=income, income_account_types="CS") assert len(inc_accounts) == 1 acc, inc_accounts = factories.create_stock_accounts(appl, broker_account=broker, income_account=income, income_account_types="I") assert len(inc_accounts) == 1 acc, inc_accounts = factories.create_stock_accounts(appl, broker_account=broker, income_account=income, income_account_types="D/CL/CS/I") assert len(income.children) == 4 assert sorted(income.children, key=lambda x: x.guid) == sorted([_acc.parent for _acc in inc_accounts], key=lambda x: x.guid) assert broker.children == [acc]
def test_create_nobook_account(self, new_book): USD = Commodity(namespace="FOO", mnemonic="BAZ", fullname="cuz") # create account with no book attachable to it with pytest.raises(ValueError): acc = Account(name="test account", type="ASSET", commodity=USD) new_book.flush()
def test_update_stock_prices(self, book_basic): if not is_inmemory_sqlite(book_basic): print("skipping test for {}".format(book_basic)) return cdty = Commodity(mnemonic="AAPL", namespace="NASDAQ", fullname="Apple", book=book_basic) # cdty["quoted_currency"] = "USD" # assert cdty.get("quoted_currency") == "USD" cdty.update_prices() book_basic.flush() L = len(list(cdty.prices)) assert L < 7 cdty.update_prices() book_basic.flush() assert len(list(cdty.prices)) == L book_basic.validate()
def test_create_commodity(self, book_basic): assert len(book_basic.commodities) == 2 cdty = Commodity(namespace="AMEX", mnemonic="APPLE", fullname="Apple", book=book_basic) book_basic.flush() assert len(book_basic.commodities) == 3 with pytest.raises(GnucashException): cdty.base_currency cdty["quoted_currency"] = "EUR" assert cdty.base_currency == book_basic.commodities(mnemonic="EUR")
def test_create_stock_accounts_simple(self, book_basic): with pytest.raises(GnucashException): factories.create_stock_accounts(book_basic.default_currency, broker_account=book_basic.accounts(name="broker")) broker = book_basic.accounts(name="broker") appl = Commodity(namespace="NMS", mnemonic="AAPL", fullname="Apple") acc, inc_accounts = factories.create_stock_accounts(appl, broker_account=broker) assert inc_accounts == [] assert broker.children == [acc]
def test_base_currency_commodity(self, book_basic): cdty = Commodity(namespace="AMEX", mnemonic="APPLE", fullname="Apple", book=book_basic) with pytest.raises(GnucashException): cdty.base_currency # should trigger creation of USD currency cdty["quoted_currency"] = "USD" assert cdty.base_currency.mnemonic == 'USD' book_basic.flush() assert cdty.base_currency == book_basic.currencies(mnemonic="USD") cdty["quoted_currency"] = "EUR" assert cdty.base_currency == book_basic.currencies(mnemonic="EUR")
def test_update_stock_prices(self, book_basic): if not is_inmemory_sqlite(book_basic) or is_not_on_web(): print("skipping test for {}".format(book_basic)) return cdty = Commodity(mnemonic="AAPL", namespace="NASDAQ", fullname="Apple", book=book_basic) cdty["quoted_currency"] = "USD" assert cdty.get("quoted_currency") == "USD" cdty.update_prices() book_basic.flush() assert len(list(cdty.prices)) < 7 cdty.update_prices() assert len(list(cdty.prices)) < 7
def book_transactions(request): name = request.param if name and database_exists(name): drop_database(name) # create new book with create_book(uri_conn=name, currency="EUR", keep_foreign_keys=False) as b: # create some accounts curr = b.default_currency other_curr = b.currencies(mnemonic="USD") 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=date(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=date(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=date(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"), ], ) tr_to_foreign = Transaction( post_date=date(2015, 10, 30), description="transfer to foreign asset", currency=curr, splits=[ Split(account=asset, value=(-200, 1)), Split(account=foreign_asset, value=(200, 1), quantity=(135, 1)), ], ) tr_from_foreign = Transaction( post_date=date(2015, 10, 31), description="transfer from foreign asset", currency=other_curr, splits=[ Split(account=asset, value=(135, 1), quantity=(215, 1)), Split(account=foreign_asset, value=(-135, 1)), ], ) Price(commodity=cdty, currency=other_curr, date=date(2015, 11, 1), value=(123, 100)) Price(commodity=cdty, currency=other_curr, date=date(2015, 11, 4), value=(127, 100)) Price(commodity=cdty, currency=curr, date=date(2015, 11, 2), value=(234, 100)) b.save() yield b if name and database_exists(name): drop_database(name)
import sys from piecash import open_book, Transaction, Split from piecash.kvp import Slot # from gnucash import Session, Account, Transaction, Split, GncNumeric # import gnucash from piecash import create_book, Account, Split, Transaction, Commodity, Price from datetime import datetime bookname = "/home/sdementen/Projects/piecash/tests/foozbar.sqlite" bookname = "/home/sdementen/Projects/piecash/gnucash_books/complex_example_piecash.gnucash" with create_book(bookname, currency="EUR", keep_foreign_keys=False, overwrite=True) as b: # create some accounts curr = b.default_currency other_curr = b.currencies(mnemonic="USD") 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),
def test_price_update_on_commodity_no_book(self, book_basic): cdty = Commodity(namespace="AMEX", mnemonic="APPLE", fullname="Apple") with pytest.raises(GncPriceError): cdty.update_prices()
def test_base_currency_commodity_no_book(self, book_basic): cdty = Commodity(namespace="AMEX", mnemonic="APPLE", fullname="Apple") with pytest.raises(GnucashException): cdty.base_currency
def create_currency(symbol: str): """ Creates a currency """ cur = Commodity("CURRENCY", symbol, fullname="Australian Dollar") return cur
# create a book (in memory) with some currency s = create_book(currency="EUR") print(s.commodities) print(s, s.book, s.book.gnc_session) # creating a new ISO currency (if not already available in s.commodities) USD = s.book.create_currency_from_ISO("USD") print(USD) # create a commodity apple = s.book.create_stock_from_symbol("AAPL") print(apple) XBT = Commodity(namespace="CURRENCY", mnemonic="XBT", fullname="Bitcoin", fraction=1000000) print(XBT) cxwcxwcxw # with create_book("empty.gnucash", overwrite=True) as s: # print(list(s.book.iteritems())) # with open_book("test_bitcoin_piecash.gnucash", readonly=False, open_if_lock=True, backup=False) as s: # for tr in s.transactions: # print(tr.ledger_str()) # with create_book("super_empty_piecash.gnucash") as s: # pass # dffdsfds with open_book("super_empty_piecash.gnucash", readonly=False, open_if_lock=True, backup=False) as s: print(s.book.root_account.commodity)