def test_list_wallets(db, client): """List all wallets :param db: database access fixture :param client: Django client fixture """ for i in (1, 2, 3, 4, 5): populate_db_wallet.add_wallet(f'wallet-{i}') _path = apiutils.list_wallets_path() response = apiutils.get(db, client, _path) assert response.status_code == 200 assert len(response.json()) == 5
def test_transaction_delete(db, client): """Test transaction deletion :param db: database access fixture :param client: Django client fixture """ w_pk = populate_db_wallet.add_wallet() w_path = apiutils.get_wallet_path(w_pk) t_pk = populate_db_wallet.add_transaction( w_pk, decimal.Decimal(data_test_transaction.valid_transaction['amount'])) t_path = apiutils.get_transaction_path(w_pk, t_pk) response = apiutils.get(db, client, w_path) assert response.json()['balance'] == \ data_test_transaction.valid_transaction['amount'] response = apiutils.delete(db, client, t_path) assert response.status_code == 204 response = apiutils.get(db, client, t_path) assert response.status_code == 404 response = apiutils.get(db, client, w_path) assert response.json()['balance'] == '0.00'
def test_delete_wallet(db, client): """Test wallet deletion :param db: database access fixture :param client: Django client fixture """ w_pk = populate_db_wallet.add_wallet() for _ in (1, 2, 3): populate_db_wallet.add_transaction(w_pk, decimal.Decimal('123.11')) _path = apiutils.delete_wallet_path(w_pk) response = apiutils.delete(db, client, _path) assert response.status_code == 204 response = apiutils.get(db, client, _path) assert response.status_code == 404
def test_transactions_list_wallet(db, client): """List transactions of specified wallet :param db: database access fixture :param client: Django client fixture """ w_pk = populate_db_wallet.add_wallet() for _ in (1, 2, 3): populate_db_wallet.add_transaction( w_pk, decimal.Decimal(data_test_transaction.valid_transaction['amount'])) _path = apiutils.list_wallet_transactions_path(w_pk) response = apiutils.get(db, client, _path) assert response.status_code == 200 assert len(response.json()) == 3
def test_transaction_retrieve(db, client): """Test transaction retrieval :param db: database access fixture :param client: Django client fixture """ w_pk = populate_db_wallet.add_wallet() t_pk = populate_db_wallet.add_transaction( w_pk, decimal.Decimal(data_test_transaction.valid_transaction['amount'])) _path = apiutils.get_transaction_path(w_pk, t_pk) response = apiutils.get(db, client, _path) assert response.status_code == 200 assert data_test_transaction.validate_transaction(response.json()) assert response.json()['amount'] == \ data_test_transaction.valid_transaction['amount']
def test_retrieve_wallet(db, client): """Try to create wallet with defined balance, retrieve wallet's status :param db: database access fixture :param client: Django client fixture """ _path = apiutils.create_wallet_path() response = apiutils.post(db, client, _path, { 'name': 'wallet with balance', 'balance': '100.00' }) assert response.status_code == 201 w_path = apiutils.get_wallet_path(wallet_pk=1) response = apiutils.get(db, client, w_path) assert response.status_code == 200 assert data_test_wallet.validate_wallet(response.json()) assert response.json()['balance'] == '0.00'
def test_update_wallet_with_balance(db, drf_client): """Try to update wallet via PATCH with defined balance in payload, retrieve wallet's status :param db: database access fixture :param drf_client: Django Rest Framework client fixture """ w_pk = populate_db_wallet.add_wallet('Wallet') _path = apiutils.put_patch_wallet_path(w_pk) response = apiutils.patch(db, drf_client, _path, { 'name': 'wallet with balance 2', 'balance': '100.00' }) assert response.status_code == 200 w_path = apiutils.get_wallet_path(wallet_pk=w_pk) response = apiutils.get(db, drf_client, w_path) assert response.status_code == 200 assert data_test_wallet.validate_wallet(response.json()) assert response.json()['balance'] == '0.00'
def test_transaction_amount_and_wallet_balance(db, client): """Test transaction amount and wallet balance :param db: database access fixture :param client: Django client fixture """ _pk = populate_db_wallet.add_wallet(os.path.basename(__file__)) get_wallet_path = apiutils.get_wallet_path(_pk) post_transaction_path = apiutils.create_transaction_path(_pk) _data = copy.deepcopy(data_test_transaction.valid_transaction) _data.update({'amount': '99999.99'}) response = apiutils.post(db, client, post_transaction_path, _data) assert response.json()['amount'] == '99999.99' response = apiutils.get(db, client, get_wallet_path) logging.info(f'Wallet {_pk} balance: {response.json()["balance"]}') assert response.json()['balance'] == '99999.99' apiutils.post(db, client, post_transaction_path, _data) response = apiutils.get(db, client, get_wallet_path) logging.info(f'Wallet {_pk} balance: {response.json()["balance"]}') assert response.json()['balance'] == '199999.98' _data.update({'amount': '-99999.99'}) response = apiutils.post(db, client, post_transaction_path, _data) assert response.json()['amount'] == '-99999.99' response = apiutils.get(db, client, get_wallet_path) logging.info(f'Wallet {_pk} balance: {response.json()["balance"]}') assert response.json()['balance'] == '99999.99' apiutils.post(db, client, post_transaction_path, _data) response = apiutils.get(db, client, get_wallet_path) logging.info(f'Wallet {_pk} balance: {response.json()["balance"]}') assert response.json()['balance'] == '0.00' _data.update({'amount': '0.01'}) response = apiutils.post(db, client, post_transaction_path, _data) assert response.json()['amount'] == '0.01' response = apiutils.get(db, client, get_wallet_path) logging.info(f'Wallet {_pk} balance: {response.json()["balance"]}') assert response.json()['balance'] == '0.01' _data.update({'amount': '-0.01'}) response = apiutils.post(db, client, post_transaction_path, _data) assert response.json()['amount'] == '-0.01' response = apiutils.get(db, client, get_wallet_path) logging.info(f'Wallet {_pk} balance: {response.json()["balance"]}') assert response.json()['balance'] == '0.00' # TODO: handle decreasing balance below zero response = apiutils.post(db, client, post_transaction_path, _data) # TODO: assert server response response = apiutils.get(db, client, get_wallet_path) logging.info(f'Wallet {_pk} balance: {response.json()["balance"]}') assert response.json()['balance'] == '0.00' _data.update({'amount': '0.00'}) response = apiutils.post(db, client, post_transaction_path, _data) assert response.json()['amount'] == '0.00' response = apiutils.get(db, client, get_wallet_path) logging.info(f'Wallet {_pk} balance: {response.json()["balance"]}') assert response.json()['balance'] == '0.00' _data.update({'amount': '99999.99'}) for _ in range(10): apiutils.post(db, client, post_transaction_path, _data) _data.update({'amount': '0.09'}) apiutils.post(db, client, post_transaction_path, _data) response = apiutils.get(db, client, get_wallet_path) logging.info(f'Wallet {_pk} balance: {response.json()["balance"]}') assert response.json()['balance'] == '999999.99' _data.update({'amount': '0.01'}) # TODO: handle increasing balance above maximum response = apiutils.post(db, client, post_transaction_path, _data) # TODO: assert server response response = apiutils.get(db, client, get_wallet_path) logging.info(f'Wallet {_pk} balance: {response.json()["balance"]}') assert response.json()['balance'] == '999999.99'