Django test cases: Not able to call execute function from GraphQL Client

102 Views Asked by At

i am creating test cases to validate graphql api calls using graphene-django library and while trying to execute query i am getting Client obj has no attr execute where as the dir method is showing execute method, i am very confused any help is much appreciated:

here is what i have tried:

from django.test import TestCase
from graphene.test import Client

from app.graphql.schema import schema
from app.graphql.tests import constants


def generate_token(user):
    from app.serializers import TwoFactorAuthenticationSerializer
    return TwoFactorAuthenticationSerializer().get_token(user).access_token

class AssetGraphQLTestCase(TestCase):

    client = Client(schema)
    print("outside------------------", type(client))
    print("outside------------------", dir(client))

    @classmethod
    def setUpTestData(cls):
        print("Setting up test data-------------------------------")
        cls.client = Client(schema)
        cls.user = constants.TEST_USER
        cls.organization = constants.TEST_ORGANIZATION
        cls.asset_1 = constants.TEST_ASSET_1
        cls.headers = {
            "Content-Type": "application/json",
            "HTTP_AUTHORIZATION":generate_token(cls.user)
        }

        print("--------------------------------",type(cls.client))
        print("--------------------------------",dir(cls.client))


    def test_all_asset(self):
        print("Testing-------------------------------------------")
        response_data = self.client.execute(query=constants.GRAPHQL_ASSET_QUERY, varaibles=constants.QUERY_VARIABLES, headers=self.headers)
        print(response_data)
        print(response_data['data'])
        self.assertEqual(response_data.status_code, 200)
        self.assertEqual(response_data['data'], 'Expected value')`

here is the output:

Setting up test data-------------------------------
-------------------------------- <class 'graphene.test.Client'>
-------------------------------- ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'execute', 'execute_async', 'execute_options', 'format_error', 'format_result', 'schema']
Testing-------------------------------------------
E
======================================================================
ERROR: test_all_asset (app.graphql.tests.test_assets_graphql_fix.AssetGraphQLTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/app/graphql/tests/test_assets_graphql_fix.py", line 41, in test_all_asset
    response_data = self.client.execute(query=constants.GRAPHQL_ASSET_QUERY, varaibles=constants.QUERY_VARIABLES, headers=self.headers)
AttributeError: 'Client' object has no attribute 'execute'

----------------------------------------------------------------------
Ran 1 test in 0.009s

FAILED (errors=1)
Destroying test database for alias 'default'...
root@b026e124367f:/app#
1

There are 1 best solutions below

0
Ahtisham On

You are inheriting from TestCase which uses its client variable rather than the class variable you have defined and this client variable doesn't have execute method.

You can simply call your client class variable in test_all_asset method like this AssetGraphQLTestCase.client.execute(..)

I would recommend inheriting from GraphQLTestCase than django TestCase here is the example