def create_wallet(self): wallet = Wallet.find_by_user_and_currency(self.user, self.currency) if wallet is None: wallet = Wallet.create(self.user, self.currency) return wallet
def test_creates_wallet_when_all_details_correct(self, database): user_authentication_details = UserCreate.call(self.user_details) user = User.find_by_id(user_authentication_details.user_id) assert Wallet.find_by_user_and_currency(user, self.currency) is None user_authentication_details.generate_token() wallet = WalletCreate.call(user_authentication_details.token, self.details) assert Wallet.find_by_user_and_currency(user, self.currency) == wallet
def test_does_not_create_wallet_with_duplicated_currency(self, database): user = User.create(first_name=self.first_name, last_name=self.last_name, email=self.email) wallet = Wallet.create(user, self.currency) prev_count = Wallet.query.filter_by(user_id=user.id).count() with pytest.raises(InvalidCurrency): wallet = Wallet.create(user, self.currency) assert Wallet.query.filter_by(user_id=user.id).count() == prev_count
def test_find_all_by_user_returns_all_expected_wallets(self, database): user = User.create(first_name=self.first_name, last_name=self.last_name, email=self.email) user2 = User.create(first_name=self.first_name, last_name=self.last_name, email='*****@*****.**') wallet = Wallet.create(user, self.currency) assert Wallet.query.count() == 1 assert Wallet.find_all_by_user(user2) == [] assert Wallet.find_all_by_user(user) == [wallet]
def test_finds_by_id_returns_the_expected_user(self, database): user = User.create(first_name=self.first_name, last_name=self.last_name, email=self.email) user2 = User.create(first_name=self.first_name, last_name=self.last_name, email='*****@*****.**') wallet = Wallet.create(user, self.currency) assert Wallet.query.count() == 1 assert Wallet.find_by_user_and_currency(user2, self.currency) is None assert Wallet.find_by_user_and_currency(user, self.currency) == wallet
def get_receiver_wallet(self): wallet = Wallet.find_by_user_and_currency(self.receiver, self.currency) if wallet is None: raise InvalidWallet return wallet
def test_creates_transaction_with_all_expected_attributes(self, database, transaction_type): user = User.create( first_name = self.first_name, last_name = self.last_name, email = self.email ) wallet = Wallet.create( user, self.currency ) transaction = Transaction.create( wallet, self.amount_cents, transaction_type ) assert Transaction.query.filter_by( wallet_id = wallet.id ).count() == 1 assert Transaction.query.filter_by( wallet_id = wallet.id ).first().id == transaction.id assert transaction.id is not None assert Transaction.query.filter_by( wallet_id = wallet.id ).first().created_at == transaction.created_at assert transaction.created_at is not None assert transaction.type == transaction_type.name assert transaction.amount_cents == self.amount_cents
def wallets(): user_authentication_details = flask_login.current_user user = User.find_by_id(user_authentication_details.user_id) wallets = [wallet.as_dict() for wallet in Wallet.find_all_by_user(user)] return jsonify(wallets), HTTPStatus.OK
def test_does_not_create_wallet_with_non_existing_user(self, database): user = User(id=uuid.uuid4(), first_name=self.first_name, last_name=self.last_name, email=self.email) prev_count = Wallet.query.count() with pytest.raises(InvalidUser): wallet = Wallet.create(user, self.currency) assert Wallet.query.count() == prev_count
def test_does_not_create_transaction_when_wallet_invalid( self, database, details): user_authentication_details = UserCreate.call(self.user_details) user = User.find_by_id(user_authentication_details.user_id) wallet = Wallet(user_id=user.id, currency=self.wallet_details['currency']) prev_count = Transaction.query.count() with pytest.raises(InvalidWallet): transaction = TransactionCreate.call(wallet, details) assert Transaction.query.count() == prev_count
def test_add_operation_modifies_wallet_balance_accordingly( self, database, operation): user = User.create(first_name=self.first_name, last_name=self.last_name, email=self.email) wallet = Wallet.create(user, self.currency) assert wallet.balance_cents == 0 wallet.add_operation(operation[0], operation[1]) assert wallet.balance_cents == operation[2]
def get_wallet(user, currency_name): output = None currency = get_currency(currency_name) if type(currency) is str: output = currency else: wallet = Wallet.find_by_user_and_currency(user, currency) if wallet is None: output = 'Invalid account. Account does not exist for user.' else: output = wallet return output
def test_as_dict_converts_wallet_into_expected_dict(self, database): user = User.create(first_name=self.first_name, last_name=self.last_name, email=self.email) wallet = Wallet.create(user, self.currency) expected_dict = { 'id': wallet.id, 'currency': wallet.currency, 'balance_cents': wallet.balance_cents, 'updated_at': int(wallet.updated_at.timestamp()), 'created_at': int(wallet.created_at.timestamp()) } assert wallet.as_dict() == expected_dict
def test_find_all_transactions_with_valid_wallet(self, database): user = User.create( first_name = self.first_name, last_name = self.last_name, email = self.email ) wallet = Wallet.create( user, self.currency ) assert Transaction.find_all_by_wallet(wallet) == [] transaction = Transaction.create( wallet, self.amount_cents, self.type ) assert Transaction.find_all_by_wallet(wallet) == [transaction]
def test_creates_wallet_with_all_expected_attributes(self, database): user = User.create(first_name=self.first_name, last_name=self.last_name, email=self.email) wallet = Wallet.create(user, self.currency) assert Wallet.query.filter_by(user_id=user.id).count() == 1 assert Wallet.query.filter_by(user_id=user.id).first().id == wallet.id assert wallet.id is not None assert Wallet.query.filter_by( user_id=user.id).first().created_at == wallet.created_at assert wallet.created_at is not None assert Wallet.query.filter_by( user_id=user.id).first().updated_at == wallet.updated_at assert wallet.updated_at is not None assert wallet.currency == self.currency.name assert wallet.balance_cents == 0
def test_does_not_create_transaction_when_wallet_not_valid(self, database): user = User.create( first_name = self.first_name, last_name = self.last_name, email = self.email ) wallet = Wallet( id = uuid.uuid4(), user_id = user.id, currency = self.currency ) prev_count = Transaction.query.count() with pytest.raises(InvalidWallet): transaction = Transaction.create( wallet, self.amount_cents, self.type ) assert Transaction.query.count() == prev_count