简体   繁体   English

如何在 django 中为石墨烯 graphql 编写测试

[英]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.

相关问题 如何在 Django REST Framework 身份验证中使用 Graphene GraphQL 框架 - How to use Graphene GraphQL framework with Django REST Framework authentication graphQL 和石墨烯:如何使用石墨烯从 graphQL 查询中提取信息? - graphQL and graphene: How to extract information from a graphQL query using graphene? 使用 Django GraphQL JWT 和 Graphene Relay 进行身份验证和授权 - Authentication and Authorization with Django GraphQL JWT and Graphene Relay GraphQL / Graphene用于Django模板中的后端调用 - GraphQL/Graphene for backend calls in Django's templates Graphene/Django (GraphQL):如何使用查询参数来排除匹配特定过滤器的节点? - Graphene/Django (GraphQL): How to use a query argument in order to exclude nodes matching a specific filter? 如何在python(graphene?)中的GraphQL中指定(导入?)方案? - How to specify (import?) scheme in GraphQL in python (graphene?)? graphene-django - 如何过滤? - graphene-django - How to filter? Django-Graphene:没有名为“graphql_jwt”的模块 - Django-Graphene: No module named 'graphql_jwt' 使用带有石墨烯的 django-graphql-auth 自定义突变的响应 - Customize a response from a mutation using django-graphql-auth with graphene 使用最新的石墨烯和石墨烯-django 版本时导入(无法从“graphql”导入名称“ResolveInfo”)错误 - Import (cannot import name 'ResolveInfo' from 'graphql') error when using newest graphene and graphene-django version
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM