From 8b961fbc1ed8bb9a4dee44bf4272739792f95abf Mon Sep 17 00:00:00 2001 From: Ignacio Orlandini Date: Tue, 24 Nov 2020 10:45:41 -0300 Subject: [PATCH] Implement mock.patch.dict --- graphene_django/tests/test_views.py | 452 ++++++++++++---------------- 1 file changed, 190 insertions(+), 262 deletions(-) diff --git a/graphene_django/tests/test_views.py b/graphene_django/tests/test_views.py index b1332bd..5d85905 100644 --- a/graphene_django/tests/test_views.py +++ b/graphene_django/tests/test_views.py @@ -568,335 +568,263 @@ def test_passes_request_into_context_request(client): assert response_json(response) == {"data": {"request": "testing"}} +@patch("graphene_django.settings.graphene_settings.ATOMIC_MUTATIONS", False) +@patch.dict( + connection.settings_dict, {"ATOMIC_MUTATIONS": False, "ATOMIC_REQUESTS": True} +) def test_form_mutation_multiple_creation_invalid_atomic_request(client): - old_atomic_mutations = connection.settings_dict.get("ATOMIC_MUTATIONS", False) - old_atomic_requests = connection.settings_dict["ATOMIC_REQUESTS"] - old_graphene_atomic_mutations = graphene_settings.ATOMIC_MUTATIONS - try: - connection.settings_dict["ATOMIC_MUTATIONS"] = False - connection.settings_dict["ATOMIC_REQUESTS"] = True - graphene_settings.ATOMIC_MUTATIONS = False - - query = """ - mutation PetMutations { - petFormMutation1: petFormMutation(input: { name: "Mia", age: 99 }) { - errors { - field - messages - } - } - petFormMutation2: petFormMutation(input: { name: "Enzo", age: 0 }) { - errors { - field - messages - } + query = """ + mutation PetMutations { + petFormMutation1: petFormMutation(input: { name: "Mia", age: 99 }) { + errors { + field + messages } } - """ + petFormMutation2: petFormMutation(input: { name: "Enzo", age: 0 }) { + errors { + field + messages + } + } + } + """ - response = client.post(url_string(query=query)) - content = response_json(response) + response = client.post(url_string(query=query)) + content = response_json(response) - assert "errors" not in content + assert "errors" not in content - assert content["data"]["petFormMutation1"]["errors"] == [ - {"field": "age", "messages": ["Too old"]} - ] + assert content["data"]["petFormMutation1"]["errors"] == [ + {"field": "age", "messages": ["Too old"]} + ] - assert content["data"]["petFormMutation2"]["errors"] == [] + assert content["data"]["petFormMutation2"]["errors"] == [] - assert Pet.objects.count() == 0 - - finally: - connection.settings_dict["ATOMIC_MUTATIONS"] = old_atomic_mutations - connection.settings_dict["ATOMIC_REQUESTS"] = old_atomic_requests - graphene_settings.ATOMIC_MUTATIONS = old_graphene_atomic_mutations + assert Pet.objects.count() == 0 +@patch("graphene_django.settings.graphene_settings.ATOMIC_MUTATIONS", False) +@patch.dict( + connection.settings_dict, {"ATOMIC_MUTATIONS": True, "ATOMIC_REQUESTS": False} +) def test_form_mutation_multiple_creation_invalid_atomic_mutation_1(client): - old_atomic_mutations = connection.settings_dict.get("ATOMIC_MUTATIONS", False) - old_atomic_requests = connection.settings_dict["ATOMIC_REQUESTS"] - old_graphene_atomic_mutations = graphene_settings.ATOMIC_MUTATIONS - try: - connection.settings_dict["ATOMIC_MUTATIONS"] = True - connection.settings_dict["ATOMIC_REQUESTS"] = False - graphene_settings.ATOMIC_MUTATIONS = False - - query = """ - mutation PetMutations { - petFormMutation1: petFormMutation(input: { name: "Mia", age: 99 }) { - errors { - field - messages - } - } - petFormMutation2: petFormMutation(input: { name: "Enzo", age: 0 }) { - errors { - field - messages - } + query = """ + mutation PetMutations { + petFormMutation1: petFormMutation(input: { name: "Mia", age: 99 }) { + errors { + field + messages } } - """ + petFormMutation2: petFormMutation(input: { name: "Enzo", age: 0 }) { + errors { + field + messages + } + } + } + """ - response = client.post(url_string(query=query)) - content = response_json(response) + response = client.post(url_string(query=query)) + content = response_json(response) - assert "errors" not in content + assert "errors" not in content - assert content["data"]["petFormMutation1"]["errors"] == [ - {"field": "age", "messages": ["Too old"]} - ] + assert content["data"]["petFormMutation1"]["errors"] == [ + {"field": "age", "messages": ["Too old"]} + ] - assert content["data"]["petFormMutation2"]["errors"] == [] + assert content["data"]["petFormMutation2"]["errors"] == [] - assert Pet.objects.count() == 0 - - finally: - connection.settings_dict["ATOMIC_MUTATIONS"] = old_atomic_mutations - connection.settings_dict["ATOMIC_REQUESTS"] = old_atomic_requests - graphene_settings.ATOMIC_MUTATIONS = old_graphene_atomic_mutations + assert Pet.objects.count() == 0 +@patch("graphene_django.settings.graphene_settings.ATOMIC_MUTATIONS", True) +@patch.dict( + connection.settings_dict, {"ATOMIC_MUTATIONS": False, "ATOMIC_REQUESTS": False} +) def test_form_mutation_multiple_creation_invalid_atomic_mutation_2(client): - old_atomic_mutations = connection.settings_dict.get("ATOMIC_MUTATIONS", False) - old_atomic_requests = connection.settings_dict["ATOMIC_REQUESTS"] - old_graphene_atomic_mutations = graphene_settings.ATOMIC_MUTATIONS - try: - connection.settings_dict["ATOMIC_MUTATIONS"] = False - connection.settings_dict["ATOMIC_REQUESTS"] = False - graphene_settings.ATOMIC_MUTATIONS = True - - query = """ - mutation PetMutations { - petFormMutation1: petFormMutation(input: { name: "Mia", age: 99 }) { - errors { - field - messages - } - } - petFormMutation2: petFormMutation(input: { name: "Enzo", age: 0 }) { - errors { - field - messages - } + query = """ + mutation PetMutations { + petFormMutation1: petFormMutation(input: { name: "Mia", age: 99 }) { + errors { + field + messages } } - """ + petFormMutation2: petFormMutation(input: { name: "Enzo", age: 0 }) { + errors { + field + messages + } + } + } + """ - response = client.post(url_string(query=query)) - content = response_json(response) + response = client.post(url_string(query=query)) + content = response_json(response) - assert "errors" not in content + assert "errors" not in content - assert content["data"]["petFormMutation1"]["errors"] == [ - {"field": "age", "messages": ["Too old"]} - ] + assert content["data"]["petFormMutation1"]["errors"] == [ + {"field": "age", "messages": ["Too old"]} + ] - assert content["data"]["petFormMutation2"]["errors"] == [] + assert content["data"]["petFormMutation2"]["errors"] == [] - assert Pet.objects.count() == 0 - - finally: - connection.settings_dict["ATOMIC_MUTATIONS"] = old_atomic_mutations - connection.settings_dict["ATOMIC_REQUESTS"] = old_atomic_requests - graphene_settings.ATOMIC_MUTATIONS = old_graphene_atomic_mutations + assert Pet.objects.count() == 0 +@patch("graphene_django.settings.graphene_settings.ATOMIC_MUTATIONS", False) +@patch.dict( + connection.settings_dict, {"ATOMIC_MUTATIONS": False, "ATOMIC_REQUESTS": False} +) def test_form_mutation_multiple_creation_invalid_non_atomic(client): - old_atomic_mutations = connection.settings_dict.get("ATOMIC_MUTATIONS", False) - old_atomic_requests = connection.settings_dict["ATOMIC_REQUESTS"] - old_graphene_atomic_mutations = graphene_settings.ATOMIC_MUTATIONS - try: - connection.settings_dict["ATOMIC_MUTATIONS"] = False - connection.settings_dict["ATOMIC_REQUESTS"] = False - graphene_settings.ATOMIC_MUTATIONS = False - - query = """ - mutation PetMutations { - petFormMutation1: petFormMutation(input: { name: "Mia", age: 99 }) { - errors { - field - messages - } - } - petFormMutation2: petFormMutation(input: { name: "Enzo", age: 0 }) { - errors { - field - messages - } + query = """ + mutation PetMutations { + petFormMutation1: petFormMutation(input: { name: "Mia", age: 99 }) { + errors { + field + messages } } - """ + petFormMutation2: petFormMutation(input: { name: "Enzo", age: 0 }) { + errors { + field + messages + } + } + } + """ - response = client.post(url_string(query=query)) - content = response_json(response) + response = client.post(url_string(query=query)) + content = response_json(response) - assert "errors" not in content + assert "errors" not in content - assert content["data"]["petFormMutation1"]["errors"] == [ - {"field": "age", "messages": ["Too old"]} - ] + assert content["data"]["petFormMutation1"]["errors"] == [ + {"field": "age", "messages": ["Too old"]} + ] - assert content["data"]["petFormMutation2"]["errors"] == [] + assert content["data"]["petFormMutation2"]["errors"] == [] - assert Pet.objects.count() == 1 + assert Pet.objects.count() == 1 - pet = Pet.objects.get() - assert pet.name == "Enzo" - assert pet.age == 0 - - finally: - connection.settings_dict["ATOMIC_MUTATIONS"] = old_atomic_mutations - connection.settings_dict["ATOMIC_REQUESTS"] = old_atomic_requests - graphene_settings.ATOMIC_MUTATIONS = old_graphene_atomic_mutations + pet = Pet.objects.get() + assert pet.name == "Enzo" + assert pet.age == 0 +@patch("graphene_django.settings.graphene_settings.ATOMIC_MUTATIONS", False) +@patch.dict( + connection.settings_dict, {"ATOMIC_MUTATIONS": False, "ATOMIC_REQUESTS": True} +) def test_model_form_mutation_multiple_creation_invalid_atomic_request(client): - old_atomic_mutations = connection.settings_dict.get("ATOMIC_MUTATIONS", False) - old_atomic_requests = connection.settings_dict["ATOMIC_REQUESTS"] - old_graphene_atomic_mutations = graphene_settings.ATOMIC_MUTATIONS - try: - connection.settings_dict["ATOMIC_MUTATIONS"] = False - connection.settings_dict["ATOMIC_REQUESTS"] = True - graphene_settings.ATOMIC_MUTATIONS = False - - query = """ - mutation PetMutations { - petMutation1: petMutation(input: { name: "Mia", age: 99 }) { - pet { - name - age - } - errors { - field - messages - } + query = """ + mutation PetMutations { + petMutation1: petMutation(input: { name: "Mia", age: 99 }) { + pet { + name + age } - petMutation2: petMutation(input: { name: "Enzo", age: 0 }) { - pet { - name - age - } - errors { - field - messages - } + errors { + field + messages } } - """ + petMutation2: petMutation(input: { name: "Enzo", age: 0 }) { + pet { + name + age + } + errors { + field + messages + } + } + } + """ - response = client.post(url_string(query=query)) - content = response_json(response) + response = client.post(url_string(query=query)) + content = response_json(response) - assert "errors" not in content + assert "errors" not in content - assert content["data"]["petMutation1"]["pet"] is None - assert content["data"]["petMutation1"]["errors"] == [ - {"field": "age", "messages": ["Too old"]} - ] + assert content["data"]["petMutation1"]["pet"] is None + assert content["data"]["petMutation1"]["errors"] == [ + {"field": "age", "messages": ["Too old"]} + ] - assert content["data"]["petMutation2"]["pet"] == {"name": "Enzo", "age": 0} + assert content["data"]["petMutation2"]["pet"] == {"name": "Enzo", "age": 0} - assert Pet.objects.count() == 0 - - finally: - connection.settings_dict["ATOMIC_MUTATIONS"] = old_atomic_mutations - connection.settings_dict["ATOMIC_REQUESTS"] = old_atomic_requests - graphene_settings.ATOMIC_MUTATIONS = old_graphene_atomic_mutations + assert Pet.objects.count() == 0 +@patch("graphene_django.settings.graphene_settings.ATOMIC_MUTATIONS", False) +@patch.dict( + connection.settings_dict, {"ATOMIC_MUTATIONS": False, "ATOMIC_REQUESTS": False} +) def test_model_form_mutation_multiple_creation_invalid_non_atomic(client): - old_atomic_mutations = connection.settings_dict.get("ATOMIC_MUTATIONS", False) - old_atomic_requests = connection.settings_dict["ATOMIC_REQUESTS"] - old_graphene_atomic_mutations = graphene_settings.ATOMIC_MUTATIONS - try: - connection.settings_dict["ATOMIC_MUTATIONS"] = False - connection.settings_dict["ATOMIC_REQUESTS"] = False - graphene_settings.ATOMIC_MUTATIONS = False - - query = """ - mutation PetMutations { - petMutation1: petMutation(input: { name: "Mia", age: 99 }) { - pet { - name - age - } - errors { - field - messages - } + query = """ + mutation PetMutations { + petMutation1: petMutation(input: { name: "Mia", age: 99 }) { + pet { + name + age } - petMutation2: petMutation(input: { name: "Enzo", age: 0 }) { - pet { - name - age - } - errors { - field - messages - } + errors { + field + messages } } - """ + petMutation2: petMutation(input: { name: "Enzo", age: 0 }) { + pet { + name + age + } + errors { + field + messages + } + } + } + """ - response = client.post(url_string(query=query)) - content = response_json(response) + response = client.post(url_string(query=query)) + content = response_json(response) - assert "errors" not in content + assert "errors" not in content - assert content["data"]["petMutation1"]["pet"] is None - assert content["data"]["petMutation1"]["errors"] == [ - {"field": "age", "messages": ["Too old"]} - ] + assert content["data"]["petMutation1"]["pet"] is None + assert content["data"]["petMutation1"]["errors"] == [ + {"field": "age", "messages": ["Too old"]} + ] - assert content["data"]["petMutation2"]["pet"] == {"name": "Enzo", "age": 0} + assert content["data"]["petMutation2"]["pet"] == {"name": "Enzo", "age": 0} - assert Pet.objects.count() == 1 + assert Pet.objects.count() == 1 - pet = Pet.objects.get() - assert pet.name == "Enzo" - assert pet.age == 0 - - finally: - connection.settings_dict["ATOMIC_MUTATIONS"] = old_atomic_mutations - connection.settings_dict["ATOMIC_REQUESTS"] = old_atomic_requests - graphene_settings.ATOMIC_MUTATIONS = old_graphene_atomic_mutations + pet = Pet.objects.get() + assert pet.name == "Enzo" + assert pet.age == 0 @patch("graphene_django.utils.utils.transaction.set_rollback") +@patch("graphene_django.settings.graphene_settings.ATOMIC_MUTATIONS", False) +@patch.dict( + connection.settings_dict, {"ATOMIC_MUTATIONS": False, "ATOMIC_REQUESTS": True} +) def test_query_errors_atomic_request(set_rollback_mock, client): - old_atomic_mutations = connection.settings_dict.get("ATOMIC_MUTATIONS", False) - old_atomic_requests = connection.settings_dict["ATOMIC_REQUESTS"] - old_graphene_atomic_mutations = graphene_settings.ATOMIC_MUTATIONS - try: - connection.settings_dict["ATOMIC_MUTATIONS"] = False - connection.settings_dict["ATOMIC_REQUESTS"] = True - graphene_settings.ATOMIC_MUTATIONS = False - - client.get(url_string(query="force error")) - set_rollback_mock.assert_called_once_with(True) - - finally: - connection.settings_dict["ATOMIC_MUTATIONS"] = old_atomic_mutations - connection.settings_dict["ATOMIC_REQUESTS"] = old_atomic_requests - graphene_settings.ATOMIC_MUTATIONS = old_graphene_atomic_mutations + client.get(url_string(query="force error")) + set_rollback_mock.assert_called_once_with(True) @patch("graphene_django.utils.utils.transaction.set_rollback") +@patch("graphene_django.settings.graphene_settings.ATOMIC_MUTATIONS", False) +@patch.dict( + connection.settings_dict, {"ATOMIC_MUTATIONS": False, "ATOMIC_REQUESTS": False} +) def test_query_errors_non_atomic(set_rollback_mock, client): - old_atomic_mutations = connection.settings_dict.get("ATOMIC_MUTATIONS", False) - old_atomic_requests = connection.settings_dict["ATOMIC_REQUESTS"] - old_graphene_atomic_mutations = graphene_settings.ATOMIC_MUTATIONS - try: - connection.settings_dict["ATOMIC_MUTATIONS"] = False - connection.settings_dict["ATOMIC_REQUESTS"] = False - graphene_settings.ATOMIC_MUTATIONS = False - - client.get(url_string(query="force error")) - set_rollback_mock.assert_not_called() - - finally: - connection.settings_dict["ATOMIC_MUTATIONS"] = old_atomic_mutations - connection.settings_dict["ATOMIC_REQUESTS"] = old_atomic_requests - graphene_settings.ATOMIC_MUTATIONS = old_graphene_atomic_mutations + client.get(url_string(query="force error")) + set_rollback_mock.assert_not_called()