【问题标题】:How to write test for graphene graphql in django如何在 django 中为石墨烯 graphql 编写测试
【发布时间】:2020-01-20 04:12:14
【问题描述】:

请任何人帮助使用 django 测试石墨烯和 graphql

我尝试使用内置的 django 测试,但它没有看到我的文件 我使用了 pytest,但它在导入我的架构时抱怨 ModuleNotFoundError 我希望有人向我展示有关高级 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
        }
    }

我希望能够测试所有可能的情况。 我想了解模块导入问题 我希望有人参考高级python课程

【问题讨论】:

    标签: python django graphql


    【解决方案1】:

    这是一个关于如何使用 django 和石墨烯测试 graphql 的示例,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)
    

    这是基于:

    【讨论】:

      猜你喜欢
      • 2018-01-11
      • 2018-03-11
      • 2017-05-11
      • 2017-05-13
      • 2020-07-27
      • 2019-12-20
      • 2020-09-19
      • 2020-09-01
      • 2018-03-22
      相关资源
      最近更新 更多