Skip to content

Commit ecee136

Browse files
committed
Add test for full_clean being called when performing nested creation or resources
1 parent e16d70e commit ecee136

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

Diff for: tests/test_input_mutations.py

+105
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
from unittest.mock import patch
2+
13
import pytest
4+
from django.core.exceptions import ValidationError
25
from strawberry.relay import from_base64, to_base64
36

47
from tests.utils import GraphQLTestClient, assert_num_queries
@@ -748,6 +751,108 @@ def test_input_update_mutation_with_multiple_level_nested_creation(
748751
}
749752

750753

754+
@pytest.mark.parametrize("mock_model", ["Milestone", "Issue", "Tag"])
755+
@pytest.mark.django_db(transaction=True)
756+
def test_input_create_mutation_with_nested_calls_nested_full_clean(
757+
db, gql_client: GraphQLTestClient, mock_model: str
758+
):
759+
query = """
760+
mutation createProjectWithMilestones ($input: ProjectInputPartial!) {
761+
createProjectWithMilestones (input: $input) {
762+
__typename
763+
... on OperationInfo {
764+
messages {
765+
kind
766+
field
767+
message
768+
}
769+
}
770+
... on ProjectType {
771+
id
772+
name
773+
milestones {
774+
id
775+
name
776+
issues {
777+
id
778+
name
779+
tags {
780+
name
781+
}
782+
}
783+
}
784+
}
785+
}
786+
}
787+
"""
788+
789+
shared_tag = TagFactory.create(name="Shared Tag")
790+
shared_tag_id = to_base64("TagType", shared_tag.pk)
791+
792+
with patch(
793+
f"tests.projects.models.{mock_model}.clean",
794+
side_effect=ValidationError({"name": ValidationError("Invalid name")}),
795+
) as mocked_full_clean:
796+
res = gql_client.query(
797+
query,
798+
{
799+
"input": {
800+
"name": "Some Project",
801+
"milestones": [
802+
{
803+
"name": "Some Milestone",
804+
"issues": [
805+
{
806+
"name": "Some Issue",
807+
"tags": [
808+
{"name": "Tag 1"},
809+
{"name": "Tag 2"},
810+
{"name": "Tag 3"},
811+
{"id": shared_tag_id},
812+
],
813+
}
814+
],
815+
},
816+
{
817+
"name": "Another Milestone",
818+
"issues": [
819+
{
820+
"name": "Some Issue",
821+
"tags": [
822+
{"name": "Tag 4"},
823+
{"id": shared_tag_id},
824+
],
825+
},
826+
{
827+
"name": "Another Issue",
828+
"tags": [
829+
{"name": "Tag 5"},
830+
{"id": shared_tag_id},
831+
],
832+
},
833+
{
834+
"name": "Third issue",
835+
"tags": [
836+
{"name": "Tag 6"},
837+
{"id": shared_tag_id},
838+
],
839+
},
840+
],
841+
},
842+
],
843+
},
844+
},
845+
)
846+
847+
assert res.data
848+
assert isinstance(res.data["createProjectWithMilestones"], dict)
849+
assert res.data["createProjectWithMilestones"]["__typename"] == "OperationInfo"
850+
assert mocked_full_clean.call_count == 1
851+
assert res.data["createProjectWithMilestones"]["messages"] == [
852+
{"field": "name", "kind": "VALIDATION", "message": "Invalid name"}
853+
]
854+
855+
751856
@pytest.mark.django_db(transaction=True)
752857
def test_input_update_mutation(db, gql_client: GraphQLTestClient):
753858
query = """

0 commit comments

Comments
 (0)