2020-04-19 23:11:33 +03:00
|
|
|
import json
|
|
|
|
|
|
|
|
import pytest
|
2019-06-25 11:40:29 +03:00
|
|
|
from django.utils.translation import gettext_lazy
|
2022-10-19 17:10:30 +03:00
|
|
|
from unittest.mock import patch
|
2019-06-25 11:40:29 +03:00
|
|
|
|
2020-04-19 23:11:33 +03:00
|
|
|
from ..utils import camelize, get_model_fields, GraphQLTestCase
|
2017-05-23 01:20:56 +03:00
|
|
|
from .models import Film, Reporter
|
2020-08-05 22:24:16 +03:00
|
|
|
from ..utils.testing import graphql_query
|
2017-05-23 01:20:56 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_model_fields_no_duplication():
|
|
|
|
reporter_fields = get_model_fields(Reporter)
|
2022-10-19 17:10:30 +03:00
|
|
|
reporter_name_set = {field[0] for field in reporter_fields}
|
2017-05-23 01:20:56 +03:00
|
|
|
assert len(reporter_fields) == len(reporter_name_set)
|
|
|
|
|
|
|
|
film_fields = get_model_fields(Film)
|
2022-10-19 17:10:30 +03:00
|
|
|
film_name_set = {field[0] for field in film_fields}
|
2017-05-23 01:20:56 +03:00
|
|
|
assert len(film_fields) == len(film_name_set)
|
2019-06-25 11:40:29 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_camelize():
|
|
|
|
assert camelize({}) == {}
|
|
|
|
assert camelize("value_a") == "value_a"
|
|
|
|
assert camelize({"value_a": "value_b"}) == {"valueA": "value_b"}
|
|
|
|
assert camelize({"value_a": ["value_b"]}) == {"valueA": ["value_b"]}
|
|
|
|
assert camelize({"value_a": ["value_b"]}) == {"valueA": ["value_b"]}
|
|
|
|
assert camelize({"nested_field": {"value_a": ["error"], "value_b": ["error"]}}) == {
|
|
|
|
"nestedField": {"valueA": ["error"], "valueB": ["error"]}
|
|
|
|
}
|
|
|
|
assert camelize({"value_a": gettext_lazy("value_b")}) == {"valueA": "value_b"}
|
|
|
|
assert camelize({"value_a": [gettext_lazy("value_b")]}) == {"valueA": ["value_b"]}
|
|
|
|
assert camelize(gettext_lazy("value_a")) == "value_a"
|
|
|
|
assert camelize({gettext_lazy("value_a"): gettext_lazy("value_b")}) == {
|
|
|
|
"valueA": "value_b"
|
|
|
|
}
|
|
|
|
assert camelize({0: {"field_a": ["errors"]}}) == {0: {"fieldA": ["errors"]}}
|
2020-04-19 23:11:33 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
|
|
|
@patch("graphene_django.utils.testing.Client.post")
|
2020-08-06 22:44:33 +03:00
|
|
|
def test_graphql_test_case_operation_name(post_mock):
|
2020-04-19 23:11:33 +03:00
|
|
|
"""
|
2020-05-09 14:25:24 +03:00
|
|
|
Test that `GraphQLTestCase.query()`'s `operation_name` argument produces an `operationName` field.
|
2020-04-19 23:11:33 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
class TestClass(GraphQLTestCase):
|
|
|
|
GRAPHQL_SCHEMA = True
|
|
|
|
|
|
|
|
def runTest(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
tc = TestClass()
|
2020-12-31 08:12:24 +03:00
|
|
|
tc._pre_setup()
|
2020-04-19 23:11:33 +03:00
|
|
|
tc.setUpClass()
|
2020-05-09 14:25:24 +03:00
|
|
|
tc.query("query { }", operation_name="QueryName")
|
2022-10-19 17:10:30 +03:00
|
|
|
body = json.loads(post_mock.call_args[0][1])
|
2020-04-19 23:11:33 +03:00
|
|
|
# `operationName` field from https://graphql.org/learn/serving-over-http/#post-request
|
|
|
|
assert (
|
|
|
|
"operationName",
|
|
|
|
"QueryName",
|
|
|
|
) in body.items(), "Field 'operationName' is not present in the final request."
|
2020-08-05 22:24:16 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
|
|
|
@patch("graphene_django.utils.testing.Client.post")
|
2020-08-06 22:44:33 +03:00
|
|
|
def test_graphql_query_case_operation_name(post_mock):
|
|
|
|
graphql_query("query { }", operation_name="QueryName")
|
2022-10-19 17:10:30 +03:00
|
|
|
body = json.loads(post_mock.call_args[0][1])
|
2020-08-05 22:24:16 +03:00
|
|
|
# `operationName` field from https://graphql.org/learn/serving-over-http/#post-request
|
|
|
|
assert (
|
|
|
|
"operationName",
|
|
|
|
"QueryName",
|
|
|
|
) in body.items(), "Field 'operationName' is not present in the final request."
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def client_query(client):
|
|
|
|
def func(*args, **kwargs):
|
|
|
|
return graphql_query(*args, client=client, **kwargs)
|
|
|
|
|
|
|
|
return func
|
|
|
|
|
|
|
|
|
|
|
|
def test_pytest_fixture_usage(client_query):
|
2021-02-23 07:24:02 +03:00
|
|
|
response = client_query("query { test }")
|
2020-08-05 22:24:16 +03:00
|
|
|
content = json.loads(response.content)
|
|
|
|
assert content == {"data": {"test": "Hello World"}}
|