def execute(self) -> None:

        try:

            asset_child = Account.create(self.entity,
                                         'Test Asset Child',
                                         self.asset.am_type,
                                         self.asset.denomination,
                                         parent=self.asset)

            self.create_transaction(amount=Decimal(42))
            self.create_transaction(amount=Decimal(18))

            Transaction.create(self.entity, datetime.utcnow(), [
                Entry(Side.debit, Decimal(5), asset_child),
                Entry(Side.credit, Decimal(5), self.liability)
            ], asset_child.denomination)

        except Exception as error:
            self.record_failure(error)
            return

        try:
            balance = RecursiveBalance.retrieve(self.entity, self.asset)
        except Exception as error:
            self.record_failure(error)
            return

        if balance.magnitude != Decimal(65):
            message = 'Unexpected magnitude: ' + str(balance.magnitude)
            self.record_failure(message)
            return

        self.record_success()
        return
Ejemplo n.º 2
0
    def _decode(cls: Type[T], entity: Entity, data: Any) -> T:

        if not isinstance(data, list):
            raise UnexpectedResponseType(data, list)

        if len(data) < 1:
            raise ApiError('Response unexpectedly empty')

        tx_list_data = data[0]

        try:

            if tx_list_data['versions'] is None:
                tx_list_data['versions'] = list()

            tx_list = cls(
                entity=entity,
                transaction_id=tx_list_data['transaction_id'],
                versions=Transaction.decode_many(
                    entity,
                    tx_list_data['versions']
                )
            )

        except KeyError as error:
            raise MissingKey(error.args[0])

        return tx_list
    def execute(self) -> None:

        try:
            asset_child = Account.create(self.entity,
                                         'Test Asset Child',
                                         self.asset.am_type,
                                         self.asset.denomination,
                                         parent=self.asset)

            Transaction.create(self.entity, datetime.utcnow(), [
                Entry(Side.debit, Decimal(5), self.asset),
                Entry(Side.credit, Decimal(5), self.liability)
            ], self.asset.denomination)

            Transaction.create(self.entity, datetime.utcnow(), [
                Entry(Side.debit, Decimal(5), asset_child),
                Entry(Side.credit, Decimal(5), self.liability)
            ], asset_child.denomination)

            ledger = RecursiveLedger.retrieve(self.entity, self.asset)
        except Exception as error:
            self.record_failure(error)
            return

        if not isinstance(ledger, RecursiveLedger):
            return_type = str(type(ledger))
            self.record_failure('Unexpected return type: ' + return_type)

        if len(ledger) != 2:
            self.record_failure('Ledger length unexpected: ' +
                                str(len(ledger)))
            return

        if ledger.account_id != self.asset.id_:
            self.record_failure('Account IDs do not match')
            return

        self.record_success()

        return
Ejemplo n.º 4
0
    def execute(self) -> None:

        try:

            income = self.create_account(amt=AMType.income, name='Test Income')

            income_child = Account.create(self.entity,
                                          'Test income child',
                                          income.am_type,
                                          income.denomination,
                                          parent=income)

            expense = self.create_account(amt=AMType.expense,
                                          name='Test Expense')

            transaction = Transaction.create(self.entity, datetime.utcnow(), [
                Entry(Side.credit, Decimal('400'), income),
                Entry(Side.debit, Decimal('400'), expense)
            ], self.usd, 'Test transaction 1')

            transaction.update(time=datetime.utcnow(),
                               entries=[
                                   Entry(Side.credit, Decimal('250'),
                                         income_child),
                                   Entry(Side.credit, Decimal('250'), income),
                                   Entry(Side.debit, Decimal('500'), expense)
                               ],
                               denomination=self.usd,
                               description='Test transaction v2')

            transaction.update(time=datetime.utcnow() - timedelta(days=4),
                               entries=[
                                   Entry(Side.credit, Decimal('1'), income),
                                   Entry(Side.debit, Decimal('1'), expense)
                               ],
                               denomination=self.usd,
                               description='Test transaction v3')

            tx_version_list = TransactionVersionList.retrieve(
                entity=self.entity, transaction=transaction)

            assert len(tx_version_list) == 3, 'list length 3'
            for version in tx_version_list:
                assert isinstance(version, Transaction), 'full of Transactions'

        except Exception as error:
            self.record_failure(error)
            return

        self.record_success()
Ejemplo n.º 5
0
    def create_transaction(
        self,
        time: Optional[datetime] = None,
        amount: Optional[Decimal] = None
    ) -> Transaction:

        if time is None:
            time = datetime.utcnow()
        if amount is None:
            amount = Decimal(10)

        transaction = Transaction.create(
            self.entity,
            datetime.utcnow(),
            [
                Entry(Side.debit, amount, self.asset),
                Entry(Side.credit, amount, self.liability)
            ],
            self.usd,
            'Test transaction'
        )

        return transaction
    def execute(self) -> None:

        try:

            asset = self.create_account(AMType.asset, 'Test Asset')

            liability = self.create_account(AMType.liability, 'Test Liability')

            liability_child = Account.create(
                entity=self.entity,
                name='Test liability child',
                am_type=AMType.liability,
                denomination=liability.denomination,
                parent=liability)

            Transaction.create(self.entity, datetime.utcnow(), [
                Entry(Side.debit, Decimal('400'), asset),
                Entry(Side.credit, Decimal('400'), liability)
            ], self.usd, 'Test transaction 1')

            Transaction.create(self.entity, datetime.utcnow(), [
                Entry(Side.debit, Decimal('700'), asset),
                Entry(Side.credit, Decimal('200'), liability),
                Entry(Side.credit, Decimal('500'), liability_child)
            ], self.usd, 'Test transaction 2')

            Transaction.create(self.entity,
                               datetime.utcnow() + timedelta(days=4), [
                                   Entry(Side.debit, Decimal('1'), asset),
                                   Entry(Side.credit, Decimal('1'), liability)
                               ], self.usd, 'Test transaction 3')

            position = Position.retrieve(entity=self.entity,
                                         balance_time=datetime.utcnow(),
                                         denomination=self.usd)

            assert isinstance(position, Position)
            assert position.has_assets
            assert position.has_liabilities
            assert not position.has_equities
            assert isinstance(position.denomination, GlobalUnit)
            assert isinstance(position.balance_time, datetime)
            assert isinstance(position.generated_time, datetime)
            assert isinstance(position.entity, Entity)

            asset_root = position.assets[0]
            liability_root = position.liabilities[0]

            assert isinstance(asset_root, TreeNode)
            assert isinstance(liability_root, TreeNode)
            assert liability_root.has_children
            assert not asset_root.has_children

            liability_depth_1 = liability_root.children[0]

            assert isinstance(liability_depth_1, TreeNode)
            assert not liability_depth_1.has_children

            assert asset_root.recursive_balance == Decimal('1100')
            assert asset_root.account_balance == Decimal('1100')
            assert liability_root.account_balance == Decimal('600')
            assert liability_root.recursive_balance == Decimal('1100')
            assert liability_depth_1.account_balance == Decimal('500')
            assert liability_depth_1.recursive_balance == Decimal('500')

        except Exception as error:
            self.record_failure(error)
            return

        self.record_success()
Ejemplo n.º 7
0
    def execute(self) -> None:

        amount = Decimal(10)

        try:
            transaction = self.create_transaction(amount=amount)
        except Exception as error:
            self.record_failure(error)
            return

        assert isinstance(transaction, Transaction)

        try:
            transaction = Transaction.retrieve(
                self.entity,
                transaction.id_,
                self.asset.denomination
            )
            assert isinstance(transaction, Transaction)
            assert isinstance(transaction.time, datetime)
            assert isinstance(transaction.version_time, datetime)
            assert isinstance(transaction.entries, list)
            assert transaction.magnitude == Decimal(10)
            assert len(transaction) > 1
            for entry in transaction:
                assert isinstance(entry, Entry)

        except Exception as error:
            self.record_failure(error)
            return

        updated_description = 'Updated test description goodness'

        try:
            transaction = transaction.update(description=updated_description)
        except Exception as error:
            self.record_failure(error)
            return

        if not transaction.description == updated_description:
            self.record_failure('Transaction description not updated')
            return

        try:
            transaction.delete()
        except Exception as error:
            self.record_failure(error)
            return

        try:
            Transaction.retrieve(
                self.entity,
                transaction.id_,
                self.asset.denomination
            )
        except Exception as error:
            if not isinstance(error, ResourceNotFound):
                self.record_failure(error)
                return

        self.record_success()
        return
    def execute(self) -> None:

        try:

            income = self.create_account(amt=AMType.income, name='Test Income')

            income_child = Account.create(self.entity,
                                          'Test income child',
                                          income.am_type,
                                          income.denomination,
                                          parent=income)

            expense = self.create_account(amt=AMType.expense,
                                          name='Test Expense')

            asset = self.create_account(AMType.asset, 'Test Asset')

            Transaction.create(self.entity, datetime.utcnow(), [
                Entry(Side.credit, Decimal('400'), income),
                Entry(Side.debit, Decimal('400'), asset)
            ], self.usd, 'Test transaction 1')

            Transaction.create(self.entity, datetime.utcnow(), [
                Entry(Side.credit, Decimal('250'), income_child),
                Entry(Side.credit, Decimal('250'), income),
                Entry(Side.debit, Decimal('500'), asset)
            ], self.usd, 'Test transaction 2')

            Transaction.create(self.entity, datetime.utcnow(), [
                Entry(Side.credit, Decimal('300'), asset),
                Entry(Side.debit, Decimal('300'), expense)
            ], self.usd, 'Test transaction 3')

            Transaction.create(self.entity,
                               datetime.utcnow() + timedelta(days=4), [
                                   Entry(Side.debit, Decimal('50'), expense),
                                   Entry(Side.credit, Decimal('50'), asset)
                               ], self.usd, 'Test transaction 4')

            tree = Tree.retrieve(entity=self.entity,
                                 balance_time=datetime.utcnow() +
                                 timedelta(hours=1),
                                 denomination=self.usd)

            assert isinstance(tree, Tree)
            assert tree.has_income
            assert tree.has_expenses
            assert not tree.has_liabilities
            assert not tree.has_equity
            assert tree.global_unit_id == self.usd.id_
            assert tree.custom_unit_id is None
            assert isinstance(tree.denomination, GlobalUnit)
            assert isinstance(tree.balance_time, datetime)
            assert isinstance(tree.generated_time, datetime)
            assert isinstance(tree.entity, Entity)

            income_root = tree.income[0]
            expense_root = tree.expenses[0]

            assert isinstance(income_root, TreeNode)
            assert isinstance(expense_root, TreeNode)
            assert income_root.has_children
            assert not expense_root.has_children
            assert isinstance(income_root.children[0], TreeNode)

            assert income_root.recursive_balance == Decimal('900')
            assert income_root.account_balance == Decimal('650')
            assert income_root.children[0].account_balance == Decimal('250')
            assert expense_root.account_balance == Decimal('300')
            assert expense_root.recursive_balance == Decimal('300')

            assert tree.total_income == Decimal('900')
            assert tree.total_expenses == Decimal('300')
            assert tree.total_assets == Decimal('600')
            assert tree.total_equity == Decimal('0')
            assert tree.total_liabilities == Decimal('0')

        except Exception as error:
            self.record_failure(error)
            return

        self.record_success()