コード例 #1
0
    def test_delete_gql(self, books):
        """Ensure we can delete a book"""
        client = GrapheneClient(schema)

        variables = {"id": 1}

        mutation = """
        mutation deleteMutation($id: ID!) {
                deleteBook(id: $id) {
                    found
                    deletedId
                }
            }
        """

        result = {
            "data":
            OrderedDict([(
                "deleteBook",
                {
                    "found": True,
                    "deletedId": "1"
                },
            )])
        }

        assert client.execute(mutation, variable_values=variables) == result

        # reading the book should fail
        with pytest.raises(Book.DoesNotExist) as excinfo:
            Book.objects.get(pk=1)  # deleted book
            assert excinfo.value == "Book matching query does not exist"
コード例 #2
0
    def test_read_one_gql(self, benchmark, books):
        """Ensure we can retrieve one book"""
        client = GrapheneClient(schema)

        query = """ 
        query {
            book(id: 1) {
                id
                title
                author
                language
                pages
            }
        }"""

        result = {
            "data": {
                "book": {
                    "id": "1",
                    "title": "Moby Dick",
                    "author": "Herman Melville",
                    "language": "english",
                    "pages": 677,
                }
            }
        }

        assert benchmark(client.execute, query) == result
コード例 #3
0
    def test_create_gql(self, benchmark):
        """Ensure we can create a new book"""
        client = GrapheneClient(schema)

        book = {
            "title": "Moby Dick",
            "author": "Herman Melville",
            "language": "EN",
            "pages": 698,
        }

        variables = {"input": book}

        mutation = """
        mutation createMutation($input: CreateBookInput!) {
                createBook(input: $input) {
                    book {
                        title
                        author
                        language
                        pages
                    }
                }
            }
        """

        result = {"data": OrderedDict([("createBook", {"book": book})])}

        assert benchmark(client.execute, mutation,
                         variable_values=variables) == result

        # read the created book
        new_book = Book.objects.get(pk=1)

        assert model_to_dict(new_book) == {"id": 1, **book}
コード例 #4
0
ファイル: tests.py プロジェクト: cageyv/otus_web
 def setUp(self):
     self.teacher = TeacherFactory(username='******', password='******')
     self.course = CourseFactory(
         name='Курс 1',
         started='2017-05-01T15:12:04+03:00',
         teacher=self.teacher,
     )
     self.client = GrapheneClient(schema)
コード例 #5
0
    def setUp(self):
        self._client = Client()
        self.token = None

        from PersonalDataApi.schema import schema
        self._gqlclient = GrapheneClient(schema)

        from django.contrib.auth.models import User
        self.user = User.objects.get(username="******")

        from MetaDataApi.users.models import Profile
コード例 #6
0
    def test_read_all_gql(self, benchmark, books):
        """Ensure we can list all books: GET /books/"""
        client = GrapheneClient(schema)

        query = """ 
        query {
            allBooks {
                    id
                    title
                    author
                    language
                    pages
                }
            }"""

        result = {
            "data": {
                "allBooks": [
                    {
                        "id": "1",
                        "title": "Moby Dick",
                        "author": "Herman Melville",
                        "language": "english",
                        "pages": 677,
                    },
                    {
                        "id": "2",
                        "title": "As Crônicas de Nárnia",
                        "author": "C. S. Lewis",
                        "language": "portuguese",
                        "pages": 752,
                    },
                    {
                        "id": "3",
                        "title": "Harry Potter und Der Stein der Weisen",
                        "author": "J. K. Rowling",
                        "language": "german",
                        "pages": 337,
                    },
                    {
                        "id": "4",
                        "title": "羊をめぐる冒険",
                        "author": "Haruki Murakami",
                        "language": "japanese",
                        "pages": 331,
                    },
                ]
            }
        }

        assert benchmark(client.execute, query) == result
コード例 #7
0
 def __init__(
     self,
     app: Union[ASGI2App, ASGI3App],
     session: sessionmaker,
     gql_schema: GrapheneSchema,
     base_url: str = "http://testserver",
     raise_server_exceptions: bool = True,
     root_path: str = "",
 ) -> None:
     self.raise_on_client_error = True
     self.http_client = StarletteTestClient(app, base_url,
                                            raise_server_exceptions,
                                            root_path)
     self.gql_client = GrapheneClient(gql_schema)
     self.session = session
コード例 #8
0
    def test_update_gql(self, benchmark, books):
        """Ensure we can partial update a book"""
        client = GrapheneClient(schema)

        variables = {"id": 1, "input": {"language": "JP", "pages": 566}}

        mutation = """
        mutation updateMutation($id: ID!, $input: PatchBookInput!) {
                updateBook(id: $id, input: $input) {
                    book {
                        id
                        title
                        author
                        language
                        pages
                    }
                }
            }
        """

        book = {
            "title": "Moby Dick",
            "author": "Herman Melville",
            "language": "JP",
            "pages": 566,
        }

        result = {
            "data": OrderedDict([("updateBook", {
                "book": {
                    "id": "1",
                    **book
                }
            })])
        }

        assert benchmark(client.execute, mutation,
                         variable_values=variables) == result

        # read the updated book
        updated_book = Book.objects.get(pk=1)

        assert model_to_dict(updated_book) == {"id": 1, **book}
コード例 #9
0
 def graphene_post(self, user, query):
     client = GrapheneClient(schema)
     return client.execute(query, context_value=GrapheneMockContext(user))
コード例 #10
0
def graphene_client(test_db):
    return GrapheneClient(schema)
コード例 #11
0
def gql_client():
    """Fixture to provide a Graphene client."""
    client = GrapheneClient(normandy_schema)
    return client
コード例 #12
0
    def test_visualize_schema(self):
        """Ensure the allowed methods are OPTIONS, GET, HEAD, POST, PUT, PATCH, DELETE"""
        client = GrapheneClient(schema)

        query = """
        query {
            __schema {
                queryType {
                name
                fields {
                    name
                    type {
                    name
                    }
                    args {
                    name
                    }
                }
                
                }
                mutationType {
                name
                    fields {
                    name
                    type {
                    name
                    }
                    args {
                    name
                    }
                }
                }
            }
        }
        """

        result = {
            "data": {
                "__schema": {
                    "queryType": {
                        "name":
                        "BookQuery",
                        "fields": [
                            {
                                "name": "book",
                                "type": {
                                    "name": "BookType"
                                },
                                "args": [{
                                    "name": "id"
                                }, {
                                    "name": "title"
                                }],
                            },
                            {
                                "name": "allBooks",
                                "type": {
                                    "name": None
                                },
                                "args": []
                            },
                        ],
                    },
                    "mutationType": {
                        "name":
                        "Mutations",
                        "fields": [
                            {
                                "name": "createBook",
                                "type": {
                                    "name": "CreateBookMutation"
                                },
                                "args": [{
                                    "name": "input"
                                }],
                            },
                            {
                                "name": "updateBook",
                                "type": {
                                    "name": "UpdateBookMutation"
                                },
                                "args": [{
                                    "name": "id"
                                }, {
                                    "name": "input"
                                }],
                            },
                            {
                                "name": "deleteBook",
                                "type": {
                                    "name": "DeleteBookMutation"
                                },
                                "args": [{
                                    "name": "id"
                                }],
                            },
                        ],
                    },
                }
            }
        }

        assert client.execute(query) == result