0

I am using

from graphene_django.utils.testing import GraphQLTestCase

to implement my tests. However, the result from

class PeopleTests(GraphQLTestCase):
  GRAPHQL_SCHEMA = schema

  def test_all_person_query_admin_token(self):

    response = self.query(
        '''
       query getPersons{
            persons{
            firstName,
            lastName
            }
        }
        ''',
        op_name='persons',
        headers={'HTTP_AUTHORIZATION': f'JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiZXhwIjoxNTY2MTk4Mzg5LCJvcmlnSWF0IjoxNTY2MTk4MDg5fQ.P4u7PNKPLFsc4dvhBLw-EfuN8jg2d-lqdZjWruEDlpc'}
    )

is always returns an empty list of Person. I read in Django Test Database looks empty while test is runnin that this is a result from GraphQLTestCase inheriting from TestCase. This post is very old (2012) but I can not seem to find any documentation on how to use a propper test database.

Sorry if I have missed something, and the answer was obvious.

Thanks

bwright
  • 896
  • 11
  • 29

1 Answers1

0

Please make sure that you are creating persons before fetching data with GraphQL. Maybe you need to add some fixtures or add a setUp method to add some entries. Here is how I tested Graphene-Django API with Pytest.

@pytest.mark.django_db
class TestBlogSchema(TestCase):

    def setUp(self):
       self.client = Client(schema)
       self.blog = mixer.blend(Blog)

    def test_single_blog_query(self):
       response = self.client.execute(single_blog_query, variables={"id": self.blog.id})
       response_blog = response.get("data").get("blog")

       assert response_blog["id"] == str(self.blog.id)

You can check out the full example from here

Ijharul Islam
  • 1,429
  • 11
  • 13