mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-04-15 14:42:06 +03:00
Implement mock.patch.dict
This commit is contained in:
parent
26b95100d0
commit
8b961fbc1e
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue
Block a user