4

Please can anyone help in testing graphene and graphql with django

I tried using built-in django test but it didn't see my file I used pytest but it complaining of ModuleNotFoundError when importing my schema I will like someone to show me a course on advanced python

class Query(ObjectType):
    calculate_price = Float(margin=Float(), exchangeRate=String(
    ), saleType=Argument(SaleType, required=True))

    def resolve_calculate_price(self, info, **kwargs):
        margin = kwargs.get('margin')
        exchangeRate = kwargs.get('exchangeRate')
        saleType = kwargs.get('saleType')

        request_from_coindesk = requests.get(
            url='https://api.coindesk.com/v1/bpi/currentprice.json')
        json_result_from_coindesk = json.dumps(request_from_coindesk.text)
        coindesk_result = json.loads(json_result_from_coindesk)
        result = json.loads(coindesk_result)

        rate_to_calculate = result["bpi"]["USD"]["rate_float"]

        if saleType == SaleType.sell:
            calculated_value = (margin/100) * rate_to_calculate
            new_rate = (rate_to_calculate - calculated_value) * 360
            print(18, new_rate)
            return new_rate
        elif saleType == SaleType.buy:
            calculated_value = (margin/100) * rate_to_calculate
            new_rate = (rate_to_calculate - calculated_value) * 360
            print(19, new_rate)
            return new_rate
        else:
            raise GraphQLError('please saleType can either be buy or sell')
#my test file
from graphene.test import Client
from buy_coins.schema import schema



def test_hey():
    client = Client(schema)
    executed = client.execute('''calculatePrice(margin, exchangeRate, saleType)''', context={
                              'margin': '1.2', 'exchangeRate': 'USD', 'saleType': 'sell'})
    assert executed == {
        "data": {
            "calculatePrice": 3624484.7302560005
        }
    }

I want to be able to test all possible cases. I want to understand the module import issue I want someone to refer an advanced python course

Clarity
  • 10,730
  • 2
  • 25
  • 35

1 Answers1

7

Here is an example on how to test graphql using django and graphene, graphene-django:

from buy_coins.schema import Query
from django.test.testcases import TestCase
import graphene

class AnExampleTest(TestCase):

    def setUp(self):
        super().setUp()
        self.query = """
            query {
              reporter {
                id
              }
            }
        """

    def test_an_example(self):
        schema = graphene.Schema(query=Query)
        result = schema.execute(query)
        self.assertIsNone(result.errors)
        self.assertDictEqual({"reporter": {"id": "1"}}, result.data)

This is based on:

jmunsch
  • 22,771
  • 11
  • 93
  • 114
  • In case of someone is using graphql_jwt: https://stackoverflow.com/questions/62360456/how-to-authenticate-with-django-grahql-jwt-in-a-graphene-django-graphqltestcase – Alex Rintt Aug 20 '22 at 02:18