[英]How to write test for graphene graphql in django
Please can anyone help in testing graphene and graphql with django请任何人帮助测试石墨烯和 graphql 与 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我尝试使用内置的 django 测试,但没有看到我的文件
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我想了解模块导入问题我希望有人参考高级 python 课程
Here is an example on how to test graphql using django and graphene, graphene-django
:这是一个关于如何使用 django 和石墨烯、 graphene-django
测试 graphql 的示例:
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:这是基于:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.