Exemplo n.º 1
0
    def test_create_basictransaction(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")

        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=[
                             Split(account=a, value=100, memo=u"mémo asset"),
                             Split(account=e, value=-10, memo=u"mémo exp"),
                         ])
        # check issue with balance
        with pytest.raises(GncImbalanceError):
            book_basic.flush()
            book_basic.validate()

        # adjust balance
        Split(account=e, value=-90, memo="missing exp", transaction=tr)
        book_basic.flush()

        # check no issue with str
        assert str(tr)
        assert str(tr.splits)
        assert repr(tr)
        assert repr(tr.splits)
        assert tr.notes == u"on St-Eugène day"
Exemplo n.º 2
0
    def test_create_person_taxtabme(self, book_basic, Person):
        if Person is Employee:
            return

        EUR = book_basic.commodities(namespace="CURRENCY")

        # create person detached from book with a specific id
        taxtable = Taxtable(
            name="Local tax",
            entries=[
                TaxtableEntry(type="percentage",
                              amount=Decimal("6.5"),
                              account=Account(name="MyAcc",
                                              parent=book_basic.root_account,
                                              commodity=EUR,
                                              type="ASSET"))
            ])
        te = TaxtableEntry(type="percentage",
                           amount=Decimal("6.5"),
                           account=Account(name="MyOtherAcc",
                                           parent=book_basic.root_account,
                                           commodity=EUR,
                                           type="ASSET"),
                           taxtable=taxtable)

        c = Person(name="John Föo",
                   currency=EUR,
                   taxtable=taxtable,
                   book=book_basic)
        assert len(taxtable.entries) == 2
        assert taxtable.entries[0].account.parent == book_basic.root_account

        book_basic.flush()

        assert book_basic.taxtables == [taxtable]
Exemplo n.º 3
0
    def test_create_person_taxtabme(self, book_basic, Person):
        if Person is Employee:
            return

        EUR = book_basic.commodities(namespace="CURRENCY")

        # create person detached from book with a specific id
        taxtable = Taxtable(name="Local tax", entries=[
            TaxtableEntry(type="percentage",
                          amount=Decimal("6.5"),
                          account=Account(name="MyAcc", parent=book_basic.root_account, commodity=EUR, type="ASSET"))
        ])
        te = TaxtableEntry(type="percentage",
                           amount=Decimal("6.5"),
                           account=Account(name="MyOtherAcc", parent=book_basic.root_account, commodity=EUR,
                                           type="ASSET"),
                           taxtable=taxtable)

        c = Person(name="John Föo", currency=EUR, taxtable=taxtable, book=book_basic)
        assert len(taxtable.entries) == 2
        assert taxtable.entries[0].account.parent == book_basic.root_account

        book_basic.flush()

        assert book_basic.taxtables == [taxtable]
Exemplo n.º 4
0
    def test_create_basictransaction(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")

        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=[
                             Split(account=a, value=100, memo=u"mémo asset"),
                             Split(account=e, value=-10, memo=u"mémo exp"),
                         ])
        # check issue with balance
        with pytest.raises(GncImbalanceError):
            book_basic.flush()

        # adjust balance
        Split(account=e, value=-90, memo="missing exp", transaction=tr)
        book_basic.flush()

        # check no issue with str
        assert str(tr)
        assert str(tr.splits)
        assert repr(tr)
        assert repr(tr.splits)
Exemplo n.º 5
0
 def test_create_basictransaction_splitfirst(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")
     s = Split(account=a)
     assert repr(s)
Exemplo n.º 6
0
    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=datetime(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()
Exemplo n.º 7
0
 def test_create_basictransaction_splitfirst(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")
     s = Split(account=a, value=Decimal(1))
     assert repr(s)
Exemplo n.º 8
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="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"])
Exemplo n.º 9
0
    def test_create_person_noid_inbook(self, book_basic, Person):
        EUR = book_basic.commodities(namespace="CURRENCY")

        # create person attached to book
        c = Person(name="John Föo", currency=EUR, book=book_basic)
        # id should have already been set
        assert c.id == "000001"

        # flushing should not change the id
        book_basic.flush()
        assert c.id == "000001"
Exemplo n.º 10
0
    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")
Exemplo n.º 11
0
    def test_create_person_noid_inbook(self, book_basic, Person):
        EUR = book_basic.commodities(namespace="CURRENCY")

        # create person attached to book
        c = Person(name="John Föo", currency=EUR, book=book_basic)
        # id should have already been set
        assert c.id == "000001"

        # flushing should not change the id
        book_basic.flush()
        assert c.id == "000001"
Exemplo n.º 12
0
    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")
Exemplo n.º 13
0
    def test_create_cdtytransaction_tradingaccount(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")

        book_basic.use_trading_accounts = True
        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=-100,
                                   quantity=-15,
                                   memo=u"mémo brok"),
                         ])
        book_basic.validate()

        assert "{}".format(
            tr) == "Transaction<[EUR] 'buy stock' on 2014-01-02>"
        assert "{}".format(s) == "Account<asset:broker[ïoà]>"
        assert "{}".format(
            tr.splits(account=s)
        ) == "Split<Account<asset:broker[ïoà]> -100 EUR [-15 ïoà]>"
        assert "{}".format(
            tr.splits(account=a)) == "Split<Account<asset[EUR]> 100 EUR>"

        # check sum of quantities are all balanced per commodity as 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 == Decimal(0) for k, v in d.items() if k != "cur"])

        # change existing quantity
        sp = tr.splits(memo=u"mémo brok")
        sp.quantity += 1
        book_basic.validate()

        # check sum of quantities are all balanced per commodity as 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 == Decimal(0) for k, v in d.items() if k != "cur"])
Exemplo n.º 14
0
    def test_create_person_id_inbook(self, book_basic, Person):
        EUR = book_basic.commodities(namespace="CURRENCY")

        # create person attached to book with a specific id
        cust_id = "éyO903"
        c = Person(name="John Föo", currency=EUR, book=book_basic, id=cust_id)
        # id should have already been set
        assert c.id == cust_id

        # flushing should not change the id
        book_basic.flush()
        assert c.id == cust_id
Exemplo n.º 15
0
    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="mémo asset"),
            Split(account=e, value=-10, memo="mémo exp"),
        ]

        with pytest.raises(GncValidationError):
            tr = Transaction(
                currency=EUR,
                description="wire from Hélène",
                notes="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="wire from Hélène",
                notes="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="wire from Hélène",
                notes="on St-Eugène day",
                post_date=date(2014, 1, 1),
                enter_date=date(2014, 1, 1),
                splits=splits,
            )

        tr = Transaction(
            currency=EUR,
            description="wire from Hélène",
            notes="on St-Eugène day",
            post_date=None,
            enter_date=None,
            splits=splits,
        )

        with pytest.raises(GncImbalanceError):
            book_basic.flush()
            book_basic.validate()
Exemplo n.º 16
0
    def test_create_person_id_inbook(self, book_basic, Person):
        EUR = book_basic.commodities(namespace="CURRENCY")

        # create person attached to book with a specific id
        cust_id = "éyO903"
        c = Person(name="John Föo", currency=EUR, book=book_basic, id=cust_id)
        # id should have already been set
        assert c.id == cust_id

        # flushing should not change the id
        book_basic.flush()
        assert c.id == cust_id
Exemplo n.º 17
0
    def test_create_duplicateprice_different_source(self, book_basic):
        EUR = book_basic.commodities(namespace="CURRENCY")
        USD = book_basic.currencies(mnemonic="USD")
        p = Price(commodity=USD, currency=EUR, date=date(2014, 2, 22), value=Decimal('0.54321'), source="user:price")
        p1 = Price(commodity=USD, currency=EUR, date=date(2014, 2, 22), value=Decimal('0.12345'), source="other:price")

        book_basic.flush()
        assert USD.prices.filter_by(value=Decimal('0')).all() == []
        assert USD.prices.filter_by(value=Decimal('0.12345')).one() == p1

        # validation should work as different sources
        book_basic.validate()
Exemplo n.º 18
0
    def test_create_duplicateprice(self, book_basic):
        EUR = book_basic.commodities(namespace="CURRENCY")
        USD = book_basic.currencies(mnemonic="USD")
        p = Price(commodity=USD, currency=EUR, date=date(2014, 2, 22), value=Decimal('0.54321'))
        p1 = Price(commodity=USD, currency=EUR, date=date(2014, 2, 22), value=Decimal('0.12345'))

        book_basic.flush()
        assert USD.prices.filter_by(value=Decimal('0')).all() == []
        assert USD.prices.filter_by(value=Decimal('0.12345')).one() == p1

        with pytest.raises(ValueError):
            book_basic.validate()
Exemplo n.º 19
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"])
Exemplo n.º 20
0
    def test_create_duplicateprice(self, book_basic):
        EUR = book_basic.commodities(namespace="CURRENCY")
        USD = book_basic.currencies(mnemonic="USD")
        p = Price(commodity=USD, currency=EUR, date=datetime(2014, 2, 22), value=Decimal('0.54321'))
        p1 = Price(commodity=USD, currency=EUR, date=datetime(2014, 2, 22), value=Decimal('0.12345'))

        book_basic.flush()
        assert USD.prices.filter_by(value=Decimal('0')).all() == []
        assert USD.prices.filter_by(value=Decimal('0.12345')).one() == p1

        from sqlalchemy.orm.exc import NoResultFound
        with pytest.raises(NoResultFound):
            USD.prices.filter_by(value=Decimal('0.123')).one()
Exemplo n.º 21
0
    def test_create_stock_from_symbol(self, book_basic):
        assert len(book_basic.commodities) == 2

        factories.create_stock_from_symbol("AAPL", book_basic)

        assert len(book_basic.commodities) == 3

        cdty = book_basic.commodities(mnemonic="AAPL")

        assert cdty.namespace == "NMS"
        assert cdty.quote_tz == "EST"
        assert cdty.quote_source == "yahoo"
        assert cdty.mnemonic == "AAPL"
        assert cdty.fullname == "Apple Inc."
Exemplo n.º 22
0
    def test_create_basicprice(self, book_basic):
        EUR = book_basic.commodities(namespace="CURRENCY")
        USD = book_basic.currencies(mnemonic="USD")
        p = Price(commodity=USD, currency=EUR, date=datetime(2014, 2, 22), value=Decimal('0.54321'))

        # check price exist
        np = USD.prices.first()
        assert np is p
        assert repr(p) == "Price<2014-02-22 : 0.54321 EUR/USD>"

        p2 = Price(commodity=USD, currency=EUR, date=datetime(2014, 2, 21), value=Decimal('0.12345'))
        book_basic.flush()
        assert p.value + p2.value == Decimal("0.66666")
        assert len(USD.prices.all()) == 2
Exemplo n.º 23
0
    def test_create_customer_job(self, book_basic, Person):
        EUR = book_basic.commodities(namespace="CURRENCY")

        # create detached person
        c = Person(name="John Föo", currency=EUR)
        book_basic.add(c)

        if Person != Employee:
            j = Job(name="my job", owner=c)

        book_basic.validate()
        book_basic.flush()
        if Person != Employee:
            print(c.jobs)
Exemplo n.º 24
0
    def test_create_basicprice(self, book_basic):
        EUR = book_basic.commodities(namespace="CURRENCY")
        USD = book_basic.currencies(mnemonic="USD")
        p = Price(commodity=USD, currency=EUR, date=date(2014, 2, 22), value=Decimal('0.54321'))

        # check price exist
        np = USD.prices.first()
        assert np is p
        assert repr(p) == "Price<2014-02-22 : 0.54321 EUR/USD>"

        p2 = Price(commodity=USD, currency=EUR, date=date(2014, 2, 21), value=Decimal('0.12345'))
        book_basic.flush()
        assert p.value + p2.value == Decimal("0.66666")
        assert len(USD.prices.all()) == 2
Exemplo n.º 25
0
    def test_create_stock_from_symbol(self, book_basic):
        assert len(book_basic.commodities) == 2

        factories.create_stock_from_symbol("AAPL", book_basic)

        assert len(book_basic.commodities) == 3

        cdty = book_basic.commodities(mnemonic="AAPL")

        assert cdty.namespace == "NMS"
        assert cdty.quote_tz == "EST"
        assert cdty.quote_source == "yahoo"
        assert cdty.mnemonic == "AAPL"
        assert cdty.fullname == "Apple Inc."
Exemplo n.º 26
0
 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=datetime(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()
Exemplo n.º 27
0
    def test_create_cdtytransaction_cdtycurrency(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=s.commodity, 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, quantity=10, memo=u"mémo asset"),
                             Split(account=s, value=-100, quantity=-10, memo=u"mémo brok"),
                         ])
        # raise error as Transaction has a non CURRENCY commodity
        with pytest.raises(GncValidationError):
            book_basic.validate()
Exemplo n.º 28
0
 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=datetime(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),
                          ])
Exemplo n.º 29
0
 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),
                          ])
Exemplo n.º 30
0
    def test_create_cdtytransaction_cdtycurrency(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=s.commodity, 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, quantity=10, memo=u"mémo asset"),
                             Split(account=s, value=-100, quantity=-10, memo=u"mémo brok"),
                         ])
        # raise error as Transaction has a non CURRENCY commodity
        with pytest.raises(GncValidationError):
            book_basic.flush()
Exemplo n.º 31
0
    def test_create_person_id_nobook(self, book_basic, Person):
        EUR = book_basic.commodities(namespace="CURRENCY")

        # create person detached from book with a specific id
        cust_id = "éyO903"
        c = Person(name="John Föo", currency=EUR, id=cust_id)
        # id should have already been set
        assert c.id == cust_id

        # flushing should not change the id (as the person is not yet attached to book)
        book_basic.flush()
        assert c.id == cust_id

        # adding the person to the book and flushing should not change the id
        book_basic.add(c)
        assert c.id == cust_id
        book_basic.flush()
        assert c.id == cust_id
Exemplo n.º 32
0
    def test_create_person_address(self, book_basic, Person):
        EUR = book_basic.commodities(namespace="CURRENCY")

        # create person detached from book with a specific id
        addr = Address(name="Héllo", addr1="kap", email="*****@*****.**")
        c = Person(name="John Föo", currency=EUR, address=addr, book=book_basic)

        assert c.addr_addr1 == "kap"
        assert c.address.addr1 == "kap"

        addr.addr1 = "pok"
        c2 = Person(name="Jané Döo", currency=EUR, address=addr, book=book_basic)
        book_basic.flush()

        assert c.addr_addr1 == "kap"
        assert c.address.addr1 == "kap"
        assert c2.addr_addr1 == "pok"
        assert c2.address.addr1 == "pok"
Exemplo n.º 33
0
    def test_create_person_address(self, book_basic, Person):
        EUR = book_basic.commodities(namespace="CURRENCY")

        # create person detached from book with a specific id
        addr = Address(name="Héllo", addr1="kap", email="*****@*****.**")
        c = Person(name="John Föo", currency=EUR, address=addr, book=book_basic)

        assert c.addr_addr1 == "kap"
        assert c.address.addr1 == "kap"

        addr.addr1 = "pok"
        c2 = Person(name="Jané Döo", currency=EUR, address=addr, book=book_basic)
        book_basic.flush()

        assert c.addr_addr1 == "kap"
        assert c.address.addr1 == "kap"
        assert c2.addr_addr1 == "pok"
        assert c2.address.addr1 == "pok"
Exemplo n.º 34
0
    def test_create_person_id_nobook(self, book_basic, Person):
        EUR = book_basic.commodities(namespace="CURRENCY")

        # create person detached from book with a specific id
        cust_id = "éyO903"
        c = Person(name="John Föo", currency=EUR, id=cust_id)
        # id should have already been set
        assert c.id == cust_id

        # flushing should not change the id (as the person is not yet attached to book)
        book_basic.flush()
        assert c.id == cust_id

        # adding the person to the book and flushing should not change the id
        book_basic.add(c)
        assert c.id == cust_id
        book_basic.flush()
        assert c.id == cust_id
Exemplo n.º 35
0
    def test_create_person_noid_nobook(self, book_basic, Person):
        EUR = book_basic.commodities(namespace="CURRENCY")

        # create detached person
        c = Person(name="John Föo", currency=EUR)
        # id should not be set
        assert c.id is None

        # flushing should not set the id as person not added to book
        book_basic.flush()
        assert c.id is None

        # adding the person to the book does not per se set the id
        book_basic.add(c)
        assert c.id is None
        # but validation sets the id if still to None
        book_basic.validate()
        assert c.id == "000001"
Exemplo n.º 36
0
    def test_create_duplicateprice(self, book_basic):
        EUR = book_basic.commodities(namespace="CURRENCY")
        USD = book_basic.currencies(mnemonic="USD")
        p = Price(commodity=USD,
                  currency=EUR,
                  date=date(2014, 2, 22),
                  value=Decimal('0.54321'))
        p1 = Price(commodity=USD,
                   currency=EUR,
                   date=date(2014, 2, 22),
                   value=Decimal('0.12345'))

        book_basic.flush()
        assert USD.prices.filter_by(value=Decimal('0')).all() == []
        assert USD.prices.filter_by(value=Decimal('0.12345')).one() == p1

        with pytest.raises(ValueError):
            book_basic.validate()
Exemplo n.º 37
0
    def test_create_person_noid_nobook(self, book_basic, Person):
        EUR = book_basic.commodities(namespace="CURRENCY")

        # create detached person
        c = Person(name="John Föo", currency=EUR)
        # id should not be set
        assert c.id is None

        # flushing should not set the id as person not added to book
        book_basic.flush()
        assert c.id is None

        # adding the person to the book does not per se set the id
        book_basic.add(c)
        assert c.id is None
        # but validation sets the id if still to None
        book_basic.validate()
        assert c.id == "000001"
Exemplo n.º 38
0
    def test_create_cdtytransaction_tradingaccount(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")

        book_basic.use_trading_accounts = True
        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=-100, quantity=-15, memo=u"mémo brok"),
                         ])
        book_basic.validate()

        assert "{}".format(tr) == "Transaction<[EUR] 'buy stock' on 2014-01-02>"
        assert "{}".format(s) == "Account<asset:broker[ïoà]>"
        assert "{}".format(tr.splits(account=s)) == "Split<Account<asset:broker[ïoà]> -100 EUR [-15 ïoà]>"
        assert "{}".format(tr.splits(account=a)) == "Split<Account<asset[EUR]> 100 EUR>"

        # check sum of quantities are all balanced per commodity as 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 == Decimal(0) for k, v in d.items() if k != "cur"])

        # change existing quantity
        sp = tr.splits(memo=u"mémo brok")
        sp.quantity += 1
        book_basic.validate()

        # check sum of quantities are all balanced per commodity as 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 == Decimal(0) for k, v in d.items() if k != "cur"])
Exemplo n.º 39
0
    def test_create_basictransaction_neutraltime(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")

        tr = Transaction(currency=EUR, description=u"wire from Hélène", notes=u"on St-Eugène day",
                         post_date=date(2014, 1, 1),
                         splits=[
                             Split(account=a, value=100, memo=u"mémo asset"),
                             Split(account=e, value=-100, memo=u"mémo exp"),
                         ])

        assert isinstance(tr.post_date, date)

        book_basic.flush()
        book_basic.validate()

        assert isinstance(tr.post_date, date)
Exemplo n.º 40
0
    def test_create_cdtytransaction_tradingaccount(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=-100, quantity=-15, memo=u"mémo brok"),
                         ])
        book_basic.book.use_trading_accounts = True
        book_basic.flush()
        assert str(tr)
        assert str(tr.splits)
        assert repr(tr)
        assert repr(tr.splits)

        # check sum of quantities are all balanced per commodity as 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"])

        # change existing quantity
        sp = tr.splits(memo=u"mémo brok")
        sp.quantity += 1
        book_basic.flush()

        # check sum of quantities are all balanced per commodity as 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"])
Exemplo n.º 41
0
    def test_create_duplicateprice_different_source(self, book_basic):
        EUR = book_basic.commodities(namespace="CURRENCY")
        USD = book_basic.currencies(mnemonic="USD")
        p = Price(commodity=USD,
                  currency=EUR,
                  date=date(2014, 2, 22),
                  value=Decimal('0.54321'),
                  source="user:price")
        p1 = Price(commodity=USD,
                   currency=EUR,
                   date=date(2014, 2, 22),
                   value=Decimal('0.12345'),
                   source="other:price")

        book_basic.flush()
        assert USD.prices.filter_by(value=Decimal('0')).all() == []
        assert USD.prices.filter_by(value=Decimal('0.12345')).one() == p1

        # validation should work as different sources
        book_basic.validate()
Exemplo n.º 42
0
    def test_create_simplelot_inconsistentaccounts(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="test mé", account=a, notes="ïlya")
        # raise valueerror as split account not the same as lot account
        tr = Transaction(
            currency=EUR,
            description="trade stock",
            notes="àçö",
            post_date=date(2014, 1, 1),
            enter_date=datetime(2014, 1, 1),
            splits=[
                Split(account=a, value=10, memo="mémo asset"),
                Split(account=s, value=-10, quantity=-2, memo="mémo brok", lot=l),
            ],
        )

        with pytest.raises(ValueError):
            book_basic.validate()