def test_valid_resource_with_query_parameters(self):

        TypedAPI().add_route('/resource/{query_parameter}',
                             self.ValidResourceWithQueryParameter1())
        TypedAPI().add_route('/resource/{query_parameter}',
                             self.ValidResourceWithQueryParameter2())
        TypedAPI().add_route('/resource/{query_parameter}',
                             self.ValidResourceWithQueryParameter3())
        TypedAPI().add_route('/resource/{query_parameter}',
                             self.ValidResourceWithQueryParameter4())
        TypedAPI().add_route('/resource/{query_parameter}',
                             self.ValidResourceWithQueryParameter5())
    def test_invalid_resource_with_query_parameters(self):

        with pytest.raises(TypeValidationError):
            TypedAPI().add_route('/resource/{query_parameter}',
                                 self.InvalidResourceWithQueryParameter1())

        with pytest.raises(TypeValidationError):
            TypedAPI().add_route('/resource/{query_parameter}',
                                 self.InvalidResourceWithQueryParameter2())

        with pytest.raises(TypeValidationError):
            TypedAPI().add_route('/resource/{query_parameter}',
                                 self.InvalidResourceWithQueryParameter3())

        with pytest.raises(TypeValidationError):
            TypedAPI().add_route('/resource/{query_parameter}',
                                 self.InvalidResourceWithQueryParameter4())

        with pytest.raises(TypeValidationError):
            TypedAPI().add_route('/resource/{query_parameter}',
                                 self.InvalidResourceWithQueryParameter5())

        with pytest.raises(TypeValidationError):
            TypedAPI().add_route('/resource/{query_parameter}',
                                 self.InvalidResourceWithQueryParameter6())

        with pytest.raises(TypeValidationError):
            TypedAPI().add_route('/resource/{query_parameter}',
                                 self.InvalidResourceWithQueryParameter7())

        with pytest.raises(TypeValidationError):
            TypedAPI().add_route('/resource/{query_parameter}',
                                 self.InvalidResourceWithQueryParameter8())

        with pytest.raises(TypeValidationError):
            TypedAPI().add_route('/resource/{query_parameter}',
                                 self.InvalidResourceWithQueryParameter1())

        with pytest.raises(TypeValidationError):
            TypedAPI().add_route('/resource/{query_parameter}',
                                 self.InvalidResourceWithQueryParameter1())
Example #3
0
    username: str
    balance: float


class UsersResource(TypedResource):
    def on_post(self, request, response,
                user: Union[UserV2, UserV1]) -> Union[UserV2, UserV1]:
        if isinstance(user, UserV2):
            return UserV2(username=user.username, balance=user.balance)

        else:
            return UserV1(username=user.username)


class UserDetailsResource(TypedResource):
    def on_get(self, request, response,
               user_id: int) -> Optional[Union[UserV2, UserV1]]:
        if user_id == 2:
            return UserV2(username='******', balance=0.0)

        if user_id == 1:
            return UserV1(username='******')

        return None


API = TypedAPI()
API.add_route('/users', UsersResource())
API.add_route('/users/{user_id}', UserDetailsResource())
    def API(self):
        _API = TypedAPI()
        _API.add_route('/resource1/{query_parameter}',
                       self.ValidResourceWithQueryParameter1())
        _API.add_route('/resource2/{query_parameter}',
                       self.ValidResourceWithQueryParameter2())
        _API.add_route('/resource3/{query_parameter}',
                       self.ValidResourceWithQueryParameter3())
        _API.add_route('/resource4/{query_parameter}',
                       self.ValidResourceWithQueryParameter4())
        _API.add_route('/resource5/{query_parameter}',
                       self.ValidResourceWithQueryParameter5())
        _API.add_route('/resource6/{query_parameter}',
                       self.ValidResourceWithQueryParameter6())
        _API.add_route('/resource7/{query_parameter}',
                       self.ValidResourceWithQueryParameter7())
        _API.add_route('/resource8/{query_parameter}',
                       self.ValidResourceWithQueryParameter8())
        _API.add_route('/resource9/{query_parameter}',
                       self.ValidResourceWithQueryParameter9())
        _API.add_route('/resource10/{query_parameter}',
                       self.ValidResourceWithQueryParameter10())
        _API.add_route('/resource11/{query_parameter}',
                       self.ValidResourceWithQueryParameter11())
        _API.add_route('/resource12/{query_parameter}',
                       self.ValidResourceWithQueryParameter12())

        return falcon.testing.TestClient(_API)
    def test_invalid_resource_without_query_parameters(self):

        with pytest.raises(TypeValidationError):
            TypedAPI().add_route('/resource',
                                 self.InvalidResourceWithoutQueryParameter1())