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 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 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_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_sell_nonexistent_currency(): with pytest.raises(UsersDALException): UsersDAL.make_operation_with_currency(1, 1, OperationType.SELL, Decimal('1.0'), datetime.now())
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)