Skip to content

Commit 6517174

Browse files
committed
Add tests of removing unset values from payload
1 parent 2690d37 commit 6517174

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed

Diff for: tests/client_generators/dependencies/test_async_base_client.py

+74
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
from typing import Optional
23

34
import httpx
45
import pytest
@@ -7,6 +8,7 @@
78
from ariadne_codegen.client_generators.dependencies.async_base_client import (
89
AsyncBaseClient,
910
)
11+
from ariadne_codegen.client_generators.dependencies.base_model import UNSET
1012
from ariadne_codegen.client_generators.dependencies.exceptions import (
1113
GraphQLClientGraphQLMultiError,
1214
GraphQLClientHttpError,
@@ -102,6 +104,78 @@ class TestModel1(BaseModel):
102104
)
103105

104106

107+
@pytest.mark.asyncio
108+
async def test_execute_sends_payload_without_unset_arguments(mocker):
109+
fake_client = mocker.AsyncMock()
110+
client = AsyncBaseClient(url="url", http_client=fake_client)
111+
query_str = """
112+
query Abc($arg1: TestInputA, $arg2: String, $arg3: Float, $arg4: Int!) {
113+
abc(arg1: $arg1, arg2: $arg2, arg3: $arg3, arg4: $arg4){
114+
field1
115+
}
116+
}
117+
"""
118+
119+
await client.execute(
120+
query_str, {"arg1": UNSET, "arg2": UNSET, "arg3": None, "arg4": 2}
121+
)
122+
123+
assert fake_client.post.called
124+
assert len(fake_client.post.mock_calls) == 1
125+
call_kwargs = fake_client.post.mock_calls[0].kwargs
126+
assert call_kwargs["json"] == {
127+
"query": query_str,
128+
"variables": {"arg3": None, "arg4": 2},
129+
}
130+
131+
132+
@pytest.mark.asyncio
133+
async def test_execute_sends_payload_without_unset_input_fields(mocker):
134+
class TestInputB(BaseModel):
135+
required_b: str
136+
optional_b: Optional[str]
137+
138+
class TestInputA(BaseModel):
139+
required_a: str
140+
optional_a: Optional[str]
141+
input_b1: Optional[TestInputB]
142+
input_b2: Optional[TestInputB]
143+
input_b3: Optional[TestInputB]
144+
145+
fake_client = mocker.AsyncMock()
146+
client = AsyncBaseClient(url="url", http_client=fake_client)
147+
query_str = """
148+
query Abc($arg: TestInputB) {
149+
abc(arg: $arg){
150+
field1
151+
}
152+
}
153+
"""
154+
155+
await client.execute(
156+
query_str,
157+
{
158+
"arg": TestInputA(
159+
required_a="a", input_b1=TestInputB(required_b="b"), input_b3=None
160+
)
161+
},
162+
)
163+
164+
assert fake_client.post.called
165+
assert len(fake_client.post.mock_calls) == 1
166+
call_kwargs = fake_client.post.mock_calls[0].kwargs
167+
assert call_kwargs["json"] == {
168+
"query": query_str,
169+
"variables": {
170+
"arg": {
171+
"required_a": "a",
172+
"input_b1": {"required_b": "b"},
173+
"input_b3": None,
174+
}
175+
},
176+
}
177+
178+
105179
@pytest.mark.parametrize(
106180
"status_code, response_content",
107181
[

Diff for: tests/client_generators/dependencies/test_base_client.py

+70
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import json
2+
from typing import Optional
23

34
import httpx
45
import pytest
56
from pydantic import BaseModel
67

78
from ariadne_codegen.client_generators.dependencies.base_client import BaseClient
9+
from ariadne_codegen.client_generators.dependencies.base_model import UNSET
810
from ariadne_codegen.client_generators.dependencies.exceptions import (
911
GraphQLClientGraphQLMultiError,
1012
GraphQLClientHttpError,
@@ -97,6 +99,74 @@ class TestModel1(BaseModel):
9799
)
98100

99101

102+
def test_execute_sends_payload_without_unset_arguments(mocker):
103+
fake_client = mocker.MagicMock()
104+
client = BaseClient(url="url", http_client=fake_client)
105+
query_str = """
106+
query Abc($arg1: TestInputA, $arg2: String, $arg3: Float, $arg4: Int!) {
107+
abc(arg1: $arg1, arg2: $arg2, arg3: $arg3, arg4: $arg4){
108+
field1
109+
}
110+
}
111+
"""
112+
113+
client.execute(query_str, {"arg1": UNSET, "arg2": UNSET, "arg3": None, "arg4": 2})
114+
115+
assert fake_client.post.called
116+
assert len(fake_client.post.mock_calls) == 1
117+
call_kwargs = fake_client.post.mock_calls[0].kwargs
118+
assert call_kwargs["json"] == {
119+
"query": query_str,
120+
"variables": {"arg3": None, "arg4": 2},
121+
}
122+
123+
124+
def test_execute_sends_payload_without_unset_input_fields(mocker):
125+
class TestInputB(BaseModel):
126+
required_b: str
127+
optional_b: Optional[str]
128+
129+
class TestInputA(BaseModel):
130+
required_a: str
131+
optional_a: Optional[str]
132+
input_b1: Optional[TestInputB]
133+
input_b2: Optional[TestInputB]
134+
input_b3: Optional[TestInputB]
135+
136+
fake_client = mocker.MagicMock()
137+
client = BaseClient(url="url", http_client=fake_client)
138+
query_str = """
139+
query Abc($arg: TestInputB) {
140+
abc(arg: $arg){
141+
field1
142+
}
143+
}
144+
"""
145+
146+
client.execute(
147+
query_str,
148+
{
149+
"arg": TestInputA(
150+
required_a="a", input_b1=TestInputB(required_b="b"), input_b3=None
151+
)
152+
},
153+
)
154+
155+
assert fake_client.post.called
156+
assert len(fake_client.post.mock_calls) == 1
157+
call_kwargs = fake_client.post.mock_calls[0].kwargs
158+
assert call_kwargs["json"] == {
159+
"query": query_str,
160+
"variables": {
161+
"arg": {
162+
"required_a": "a",
163+
"input_b1": {"required_b": "b"},
164+
"input_b3": None,
165+
}
166+
},
167+
}
168+
169+
100170
@pytest.mark.parametrize(
101171
"status_code, response_content",
102172
[

0 commit comments

Comments
 (0)