def test_sell_more_than_have(): user_id = 1 currency_id = 1 amount = Decimal('1') UsersDAL.make_operation_with_currency(user_id, currency_id, OperationType.BUY, amount, datetime.now()) with pytest.raises(UsersDALException): UsersDAL.make_operation_with_currency(user_id, currency_id, OperationType.SELL, Decimal('2'), datetime.now())
def test_sell_currency(): user_id = 1 currency_id = 1 amount = Decimal('1') UsersDAL.make_operation_with_currency(user_id, currency_id, OperationType.BUY, amount, datetime.now()) user_currency: UserCurrencyFields = UsersDAL.make_operation_with_currency( user_id, currency_id, OperationType.SELL, amount, datetime.now()) assert user_currency.amount == Decimal('0') with create_session() as session: user = session.query(User).filter(User.id == user_id).first() assert user.money == Decimal('1000') assert session.query(UserCurrency).filter( UserCurrency.id == 1).first() is None
def get(self) -> Any: try: user_id = request.args.get('user_id') user_name = request.args.get('user_name') if not user_id and not user_name or user_id and user_name: raise ValidationException() if user_id: user_id_in_int = validate_path_parameter(user_id) return ( marshal(UsersDAL.get_user_by_id(user_id_in_int), user_output_fields), HTTPStatus.OK, ) else: return marshal(UsersDAL.get_user_by_name(user_name), user_output_fields), HTTPStatus.OK except (ValidationException, UsersDALException) as e: return marshal({'message': e}, error_fields), HTTPStatus.BAD_REQUEST
def post(self) -> Any: try: validated_json = validate_request_json(request.data, user_input_fields) return ( marshal(UsersDAL.add_user(validated_json['login']), user_output_fields), HTTPStatus.CREATED, ) except (ValidationException, UsersDALException) as e: return marshal({'message': e}, error_fields), HTTPStatus.BAD_REQUEST
def get(self, user_id: str) -> Any: try: user_id_in_int = validate_path_parameter(user_id) return ( marshal(UsersDAL.get_user(user_id_in_int), user_output_fields), HTTPStatus.OK, ) except (ValidationException, UsersDALException) as e: return marshal({'message': e}, error_fields), HTTPStatus.BAD_REQUEST
def test_buy_currency(): user_id = 1 currency_id = 1 amount = Decimal('1') user_currency: UserCurrencyFields = UsersDAL.make_operation_with_currency( user_id, currency_id, OperationType.BUY, amount, datetime.now()) assert user_currency.id == currency_id assert user_currency.user_id == user_id assert user_currency.amount == amount with create_session() as session: user = session.query(User).filter(User.id == 1).first() assert user.money == Decimal('999')
def post(self, user_id: str) -> Any: try: validated_json = validate_request_json(request.data, currency_input_fields) user_id_in_int = validate_path_parameter(user_id) return ( marshal( UsersDAL.make_operation_with_currency( user_id_in_int, **validated_json ), currency_output_fields, ), HTTPStatus.CREATED, ) except (ValidationException, UsersDALException) as e: return marshal({'message': e}, error_fields), HTTPStatus.BAD_REQUEST
def get(self, user_id: str) -> Any: try: validated_params: Dict[str, RequestParam] = validate_request_params( dict( operation_type=RequestParam(OperationType), size=RequestParam(int), page=RequestParam(int), ), request.values, ) user_id_in_int = validate_path_parameter(user_id) return UsersDAL.get_user_operations_history( user_id_in_int, **validated_params # type: ignore ) except (ValidationException, PaginationError, UsersDALException) as e: abort(HTTPStatus.BAD_REQUEST, e)
def test_buy_with_insufficient_funds_raise_error(): with pytest.raises(UsersDALException): UsersDAL.make_operation_with_currency(1, 1, OperationType.BUY, Decimal('1001'), datetime.now())
def test_add_user_to_db_second_time(user_login): with pytest.raises(UsersDALException): UsersDAL.add_user(user_login)
def test_add_user_returns_valid(user_login): res = UsersDAL.add_user(user_login) assert res.money == Decimal(START_MONEY) assert res.login == user_login
def test_buy_currency_with_too_early_time(): with pytest.raises(UsersDALException): time = datetime.now() + timedelta(seconds=1000) UsersDAL.make_operation_with_currency(1, 1, OperationType.BUY, Decimal('1.0'), time)
def test_buy_currency_with_old_price_known(): with pytest.raises(UsersDALException): time = datetime.now() - timedelta(seconds=20) UsersDAL.make_operation_with_currency(1, 1, OperationType.BUY, Decimal('1.0'), time)
def get(self, user_id: int, currency_id: int): try: return marshal(UsersDAL.get_currency(user_id, currency_id), currency_output_fields) except (UsersDALException) as e: return marshal({'message': e}, error_fields), HTTPStatus.BAD_REQUEST
def test_get_user_currencies(): UsersDAL.make_operation_with_currency(1, 1, OperationType.BUY, Decimal('1'), datetime.now()) currencies: List[UserCurrencyFields] = UsersDAL.get_user_currencies(1) assert len(currencies) == 1 assert currencies[0].amount == Decimal('1')
def test_sell_nonexistent_currency(): with pytest.raises(UsersDALException): UsersDAL.make_operation_with_currency(1, 1, OperationType.SELL, Decimal('1.0'), datetime.now())
def test_get_user_currencies_with_invalid_user_id(): with pytest.raises(UsersDALException): UsersDAL.get_user_currencies(228)
def get(self, user_id: str) -> Any: try: user_id_int = validate_path_parameter(user_id) return UsersDAL.get_user_currencies(user_id_int) except (UsersDALException, ValidationException) as e: return marshal({'message': e}, error_fields), HTTPStatus.BAD_REQUEST