Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing guide #151

Open
AriaMoradi opened this issue Jul 24, 2022 · 2 comments
Open

Testing guide #151

AriaMoradi opened this issue Jul 24, 2022 · 2 comments

Comments

@AriaMoradi
Copy link

AriaMoradi commented Jul 24, 2022

Strawberry provides a guide on testing: https://strawberry.rocks/docs/operations/testing
That guide doesn't work in a django context, for example tests need to use django.test.TestCase for model and database support and mutations and queries might need access to a request object for handling authentication, language and other stuff.

Can someone share some test examples please?
Also I believe a page in docs/references will be helpful to users of this package.

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@nrbnlulu
Copy link
Member

nrbnlulu commented Jul 24, 2022

@AriaMoradi I believe it is fairly simple.
You have two main choices:

  • execute the schema (as exampled at strawberry site) with django request object in the schema context.
  • use django.test.Client and run your queries against the graphql view url.

I prefer the latter because it is more real in the django domain.
In my library I think you can find a good example.

@aodelreal
Copy link

aodelreal commented Aug 17, 2022

Hello, in my app I use this base test class:

from typing import Dict

from django.test import TestCase, Client


class GRAPHQLBaseTestClass(TestCase):

    URL = '/my-graphql-url'

    def setUp(self) -> None:
        self.client = Client()

    def make_query(self, query: str, with_assert: bool = True) -> Dict:
        response = self.client.post(
            self.URL,
            data={'query': query},
            content_type='application/json'
        )
        if with_assert:
            self.assertEqual(response.status_code, 200)
        return response.json()

Then use it like:

class TestGetMe(GRAPHQLBaseTestClass):

    def setUp(self):
        super(TestGetMe, self).setUp()
        ... some setup

    def test_should_get_me(self):

        query = """
        query {
          me {
            name
          }
        }
        """
        response_data = self.make_query(query)
        my_name = response_data['data']['me']['name']
        self.assertEqual(my_name, 'Arturo')

This can be modified to allow queries with operationName and variables.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants