Ejemplo n.º 1
0
def test_account(bucket):
    name = "test account"
    description = "This is a test account"

    push_is_running_service()

    try:
        account = Account(name, description, bucket=bucket)

        assert (account.name() == name)
        assert (account.description() == description)
        assert (not account.is_null())

        uid = account.uid()
        assert (uid is not None)

        account2 = Account(uid=uid, bucket=bucket)

        assert (account2.name() == name)
        assert (account2.description() == description)

        assert (account.balance() == Balance())
    except:
        pop_is_running_service()
        raise

    pop_is_running_service()
Ejemplo n.º 2
0
    def _refresh(self, force_update=False):
        """Refresh the current status of this account. This fetches
           the latest data, e.g. balance, limits etc. Note that this
           limits you to refreshing at most once every five seconds...

           Args:
                force_update (bool, default=False): Force the refresh
           Returns:
                None
        """
        if self.is_null():
            from Acquire.Accounting import create_decimal as _create_decimal
            from Acquire.Accounting import Balance as _Balance
            self._overdraft_limit = _create_decimal(0)
            self._balance = _Balance()
            return

        if force_update:
            should_refresh = True
        else:
            should_refresh = False

            if self._last_update is None:
                should_refresh = True
            else:
                should_refresh = (_datetime.datetime.now() -
                                  self._last_update).seconds > 5

        if not should_refresh:
            return

        if not self.is_logged_in():
            raise PermissionError(
                "You cannot get information about this account "
                "until after the owner has successfully authenticated.")

        from Acquire.Client import Authorisation as _Authorisation
        from Acquire.Accounting import create_decimal as _create_decimal

        service = self.accounting_service()

        auth = _Authorisation(resource="get_info %s" % self._account_uid,
                              user=self._user)

        args = {
            "authorisation": auth.to_data(),
            "account_name": self.name(),
            "account_uid": self.uid()
        }

        result = service.call_function(function="get_info", args=args)

        from Acquire.Accounting import Balance as _Balance
        self._balance = _Balance.from_data(result["balance"])
        self._overdraft_limit = _create_decimal(result["overdraft_limit"])
        self._description = result["description"]

        self._last_update = _datetime.datetime.now()
Ejemplo n.º 3
0
def account1(bucket):
    push_is_running_service()
    accounts = Accounts(user_guid=account1_user)
    account = Account(name="Testing Account",
                      description="This is the test account",
                      group_name=accounts.name(),
                      bucket=bucket)
    uid = account.uid()
    assert (uid is not None)
    assert (account.balance() == Balance())

    account.set_overdraft_limit(account1_overdraft_limit)
    assert (account.get_overdraft_limit() == account1_overdraft_limit)
    pop_is_running_service()

    return account
Ejemplo n.º 4
0
def account1(bucket):
    if not have_freezetime:
        return None

    with freeze_time(start_time) as _frozen_datetime:
        now = get_datetime_now()
        assert (start_time == now)
        push_is_running_service()
        accounts = Accounts(user_guid=account1_user)
        account = Account(name="Testing Account",
                          description="This is the test account",
                          group_name=accounts.name())
        uid = account.uid()
        assert (uid is not None)
        assert (account.balance() == Balance())

        account.set_overdraft_limit(account1_overdraft_limit)
        assert (account.get_overdraft_limit() == account1_overdraft_limit)
        pop_is_running_service()

    return account
Ejemplo n.º 5
0
def test_temporal_transactions(account1, account2, bucket):
    if not have_freezetime:
        return

    zero = create_decimal(0)

    balance1 = zero
    balance2 = zero
    final_balance1 = zero
    final_balance2 = zero
    liability1 = zero
    liability2 = zero
    receivable1 = zero
    receivable2 = zero

    # generate some random times for the transactions
    random_dates = []
    now = get_datetime_now()
    for i in range(0, 50):
        if i == 0:
            # this is an evil edge-case datetime
            s = "2019-01-20 20:59:59.092627+00:00"
            r = datetime.datetime.fromisoformat(s)
        else:
            r = start_time + random.random() * (now - start_time)

        while (r.minute == 59 and r.second >= 58) or \
              (r.minute == 0 and r.second == 0 and r.microsecond < 10):
            r = r + datetime.timedelta(seconds=1)

        random_dates.append(r)

    random_dates.sort()

    # (which must be applied in time order!)
    random_dates.sort()

    provisionals = []

    for (i, transaction_time) in enumerate(random_dates):
        with freeze_time(transaction_time) as _frozen_datetime:
            now = get_datetime_now()
            assert (transaction_time == now)

            is_provisional = (random.randint(0, 3) <= 2)

            # check search for transaction is not O(n^2) lookup scanning
            # through the keys...

            transaction = Transaction(25 * random.random(),
                                      "test transaction %d" % i)

            if random.randint(0, 1):
                debit_account = account1
                credit_account = account2

                if is_provisional:
                    liability1 += transaction.value()
                    receivable2 += transaction.value()
                else:
                    balance1 -= transaction.value()
                    balance2 += transaction.value()

                final_balance1 -= transaction.value()
                final_balance2 += transaction.value()
            else:
                debit_account = account2
                credit_account = account1

                if is_provisional:
                    receivable1 += transaction.value()
                    liability2 += transaction.value()
                else:
                    balance1 += transaction.value()
                    balance2 -= transaction.value()

                final_balance1 += transaction.value()
                final_balance2 -= transaction.value()

            auth = Authorisation(resource=transaction.fingerprint(),
                                 testing_key=testing_key,
                                 testing_user_guid=debit_account.group_name())

            records = Ledger.perform(transaction=transaction,
                                     debit_account=debit_account,
                                     credit_account=credit_account,
                                     authorisation=auth,
                                     is_provisional=is_provisional,
                                     bucket=bucket)

            for record in records:
                assert (record.datetime() == now)

            if is_provisional:
                for record in records:
                    provisionals.append((credit_account, record))
            elif (random.randint(0, 3) <= 2):
                # receipt pending transactions
                balance1 = Balance(balance=balance1,
                                   liability=liability1,
                                   receivable=receivable1)

                balance2 = Balance(balance=balance2,
                                   liability=liability2,
                                   receivable=receivable2)

                assert (account1.balance() == balance1)
                assert (account2.balance() == balance2)

                for (credit_account, record) in provisionals:
                    credit_note = record.credit_note()
                    auth = Authorisation(
                        resource=credit_note.fingerprint(),
                        testing_key=testing_key,
                        testing_user_guid=credit_account.group_name())

                    receipted_value = create_decimal(
                        random.random() * float(credit_note.value()))

                    delta_value = credit_note.value() - receipted_value

                    Ledger.receipt(Receipt(credit_note=credit_note,
                                           receipted_value=receipted_value,
                                           authorisation=auth),
                                   bucket=bucket)

                    if credit_note.debit_account_uid() == account1.uid():
                        final_balance1 += delta_value
                        final_balance2 -= delta_value
                    else:
                        final_balance2 += delta_value
                        final_balance1 -= delta_value

                assert (account1.balance() == Balance(balance=final_balance1))
                assert (account2.balance() == Balance(balance=final_balance2))

                provisionals = []
                balance1 = final_balance1
                balance2 = final_balance2
                liability1 = zero
                liability2 = zero
                receivable1 = zero
                receivable2 = zero

    balance1 = Balance(balance=balance1,
                       liability=liability1,
                       receivable=receivable1)

    balance2 = Balance(balance=balance2,
                       liability=liability2,
                       receivable=receivable2)

    assert (account1.balance() == balance1)
    assert (account2.balance() == balance2)

    for (credit_account, record) in provisionals:
        credit_note = record.credit_note()
        auth = Authorisation(resource=record.credit_note().fingerprint(),
                             testing_key=testing_key,
                             testing_user_guid=credit_account.group_name())

        receipted_value = create_decimal(random.random() *
                                         float(credit_note.value()))

        delta_value = credit_note.value() - receipted_value

        Ledger.receipt(Receipt(credit_note=credit_note,
                               authorisation=auth,
                               receipted_value=receipted_value),
                       bucket=bucket)

        if credit_note.debit_account_uid() == account1.uid():
            final_balance1 += delta_value
            final_balance2 -= delta_value
        else:
            final_balance2 += delta_value
            final_balance1 -= delta_value

    assert (account1.balance() == Balance(balance=final_balance1))
    assert (account2.balance() == Balance(balance=final_balance2))