def create(): """ Used to create Footprint objects Returns: Footprint: Footprint object """ from Acquire.ObjectStore import get_datetime_now footprint = Footprint() footprint._creation_datetime = get_datetime_now() return footprint
def __init__(self): from Acquire.ObjectStore import get_datetime_now from collections import defaultdict self._creation_datetime = get_datetime_now() self._stored = False # Keyed by name - allows retrieval of UUID from name self._datasource_names = {} # Keyed by UUID - allows retrieval of name by UUID self._datasource_uuids = {} # Hashes of previously uploaded files self._file_hashes = {} # Store the ranking data for all CRDS measurements # Keyed by UUID self._rank_data = defaultdict(dict)
def test_datetime(): dt = get_datetime_now() assert (dt.tzinfo == datetime.timezone.utc) d = dt.date() t = dt.timetz() sdt = datetime_to_string(dt) assert (dt == string_to_datetime(sdt)) sd = date_to_string(d) assert (d == string_to_date(sd)) assert (d == string_to_date(date_to_string(dt))) st = time_to_string(t) assert (t == string_to_time(st)) assert (t == string_to_time(time_to_string(dt)))
def __init__(self): from Acquire.ObjectStore import get_datetime_now self._creation_datetime = get_datetime_now() self._stored = False # self._datasources = [] # Keyed by name - allows retrieval of UUID from name self._datasource_names = {} # Keyed by UUID - allows retrieval of name by UUID self._datasource_uuids = {} # Hashes of previously uploaded files self._file_hashes = {} # Holds parameters used for writing attributes to Datasets self._template_params = {} # Sampling period of TEMPLATE data in seconds self._sampling_period = 60 # Store the ranking data for all CRDS measurements # Keyed by UUID self._rank_data = defaultdict(dict)
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
def __init__(self, name=None): from Acquire.ObjectStore import create_uuid, get_datetime_now from collections import defaultdict self._uuid = create_uuid() self._name = name self._creation_datetime = get_datetime_now() self._metadata = {} # Dictionary keyed by daterange of data in each Dataset self._data = {} self._start_datetime = None self._end_datetime = None self._stored = False # This dictionary stored the keys for each version of data uploaded self._data_keys = defaultdict(dict) self._data_type = None # Hold information regarding the versions of the data # Currently unused self._latest_version = None self._versions = {} # A rank of -1 is unset, 1 is a primary source, 2 secondary self._rank = defaultdict(list)
def test_create_uid(): validate_is_uid('2019-06-13T15:40:15.809684/a499a5b6') validate_is_uid( '2019-06-13T15:40:42.784734/dc396049-ab04-4564-86d0-c78a3f43e22d') validate_is_uid('fc22b2fe-db60-4d80-b652-01b08fe36e27') validate_is_uid('03a07bcf') now = get_datetime_now() for _i in range(0, 100): assert (create_uid(short_uid=True) != create_uid(short_uid=True)) assert (create_uid(short_uid=False) != create_uid(short_uid=False)) validate_is_uid(create_uid()) validate_is_uid(create_uid(short_uid=True)) validate_is_uid(create_uid(short_uid=False)) validate_is_uid(create_uid(short_uid=True, include_date=now)) validate_is_uid(create_uid(short_uid=False, include_date=now)) with pytest.raises(TypeError): validate_is_uid(None) with pytest.raises(TypeError): validate_is_uid("ABCDEFGH")
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))
try: from freezegun import freeze_time have_freezetime = True except: have_freezetime = False account1_user = "******" account2_user = "******" account1_overdraft_limit = 1500000 account2_overdraft_limit = 2500000 testing_key = get_private_key("testing") start_time = get_datetime_now() - datetime.timedelta(days=365) @pytest.fixture(scope="session") def bucket(tmpdir_factory): try: return get_service_account_bucket() except: d = tmpdir_factory.mktemp("objstore") push_is_running_service() bucket = get_service_account_bucket(str(d)) while is_running_service(): pop_is_running_service() return bucket
def test_pending_transactions(random_transaction): (transaction, account1, account2) = random_transaction starting_balance1 = account1.balance() starting_balance2 = account2.balance() authorisation = Authorisation(resource=transaction.fingerprint(), testing_key=testing_key, testing_user_guid=account1.group_name()) records = Ledger.perform(transactions=transaction, debit_account=account1, credit_account=account2, authorisation=authorisation, is_provisional=True) assert (len(records) == 1) record = records[0] ending_balance1 = account1.balance() ending_balance2 = account2.balance() assert (ending_balance1.liability() == starting_balance1.liability() + transaction.value()) assert (ending_balance2.receivable() == starting_balance2.receivable() + transaction.value()) assert (starting_balance1.balance() == ending_balance1.balance()) assert (starting_balance2.balance() == ending_balance2.balance()) assert (starting_balance2.liability() == ending_balance2.liability()) assert (starting_balance1.receivable() == ending_balance1.receivable()) assert (record.debit_account_uid() == account1.uid()) assert (record.credit_account_uid() == account2.uid()) debit_note = record.debit_note() credit_note = record.credit_note() assert (not debit_note.is_null()) assert (not credit_note.is_null()) assert (debit_note.account_uid() == account1.uid()) assert (credit_note.account_uid() == account2.uid()) assert (debit_note.is_provisional()) assert (credit_note.is_provisional()) assert (debit_note.value() == transaction.value()) assert (credit_note.value() == transaction.value()) now = get_datetime_now() assert (debit_note.datetime() < now) assert (credit_note.datetime() < now) assert (debit_note.datetime() <= credit_note.datetime()) assert_packable(debit_note) assert_packable(credit_note) # now receipt a random amount of the transaction authorisation = Authorisation(resource=credit_note.fingerprint(), testing_key=testing_key, testing_user_guid=account2.group_name()) with pytest.raises(ValueError): receipt = Receipt( credit_note, authorisation, create_decimal(random.random()) + credit_note.value()) if random.randint(0, 1): value = credit_note.value() receipt = Receipt(credit_note, authorisation) else: value = create_decimal( create_decimal(random.random()) * credit_note.value()) receipt = Receipt(credit_note, authorisation, value) assert (not receipt.is_null()) assert (receipt.authorisation() == authorisation) assert (receipt.receipted_value() == value) assert (receipt.credit_note() == credit_note) assert_packable(receipt) rrecords = Ledger.receipt(receipt) assert (len(rrecords) == 1) rrecord = rrecords[0] assert (not rrecord.is_null()) assert_packable(rrecord) assert (not rrecord.is_provisional()) assert (rrecord.is_direct()) assert (rrecord.get_receipt_info() == receipt) assert (rrecord.is_receipt()) assert (rrecord.original_transaction() == transaction) # the original transaction record has now been updated to # say that it has been receipted... assert (record.is_provisional()) record.reload() assert (record.is_receipted()) assert (rrecord.original_transaction_record() == record) ending_balance1 = account1.balance() ending_balance2 = account2.balance() assert (ending_balance1.liability() == starting_balance1.liability()) assert (ending_balance2.receivable() == starting_balance2.receivable()) assert (starting_balance1.balance() - value == ending_balance1.balance()) assert (starting_balance2.balance() + value == ending_balance2.balance()) assert (starting_balance2.liability() == ending_balance2.liability()) assert (starting_balance1.receivable() == ending_balance1.receivable())
def test_transactions(random_transaction, bucket): (transaction, account1, account2) = random_transaction starting_balance1 = account1.balance() starting_balance2 = account2.balance() authorisation = Authorisation(resource=transaction.fingerprint(), testing_key=testing_key, testing_user_guid=account1.group_name()) records = Ledger.perform(transaction=transaction, debit_account=account1, credit_account=account2, authorisation=authorisation, is_provisional=False, bucket=bucket) assert (len(records) == 1) record = records[0] ending_balance1 = account1.balance() ending_balance2 = account2.balance() assert (ending_balance1 == starting_balance1 - transaction) assert (ending_balance2 == starting_balance2 + transaction) assert (record.debit_account_uid() == account1.uid()) assert (record.credit_account_uid() == account2.uid()) debit_note = record.debit_note() credit_note = record.credit_note() assert (debit_note.account_uid() == account1.uid()) assert (credit_note.account_uid() == account2.uid()) assert (not debit_note.is_provisional()) assert (not credit_note.is_provisional()) assert (debit_note.value() == transaction.value()) assert (credit_note.value() == transaction.value()) now = get_datetime_now() assert (debit_note.datetime() < now) assert (credit_note.datetime() < now) assert (debit_note.datetime() <= credit_note.datetime()) assert_packable(debit_note) assert_packable(credit_note) # now test refunding this transaction # now receipt a random amount of the transaction authorisation = Authorisation(resource=credit_note.fingerprint(), testing_key=testing_key, testing_user_guid=account2.group_name()) refund = Refund(credit_note, authorisation) assert (not refund.is_null()) assert (refund.authorisation() == authorisation) assert (refund.value() == transaction.value()) assert (refund.credit_note() == credit_note) assert_packable(refund) rrecords = Ledger.refund(refund) assert (len(rrecords) == 1) rrecord = rrecords[0] assert (not rrecord.is_null()) assert_packable(rrecord) assert (not rrecord.is_provisional()) assert (rrecord.is_direct()) assert (rrecord.get_refund_info() == refund) assert (rrecord.is_refund()) assert (rrecord.original_transaction() == transaction) # the original transaction record has now been updated to # say that it has been receipted... assert (record.is_direct()) record.reload() assert (record.is_refunded()) assert (rrecord.original_transaction_record() == record) ending_balance1 = account1.balance() ending_balance2 = account2.balance() assert (ending_balance1.liability() == starting_balance1.liability()) assert (ending_balance2.receivable() == starting_balance2.receivable()) assert (starting_balance1.balance() == ending_balance1.balance()) assert (starting_balance2.balance() == ending_balance2.balance()) assert (starting_balance2.liability() == ending_balance2.liability()) assert (starting_balance1.receivable() == ending_balance1.receivable())