Skip to content

Commit bbc37a1

Browse files
committed
test: verify TemplateLanguage and Variables serialization (#97)
Added unit and integration tests to ensure that 'TemplateLanguage' (bool) and 'Variables' (dict) are correctly serialized into JSON and successfully accepted by the Mailjet Send API v3.1.
1 parent 6da2627 commit bbc37a1

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

tests/integration/test_client.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,38 @@ def test_live_send_api_v3_1_sandbox_happy_path(client_live: Client) -> None:
5757
assert result.status_code != 404
5858

5959

60+
def test_live_send_api_v3_1_template_language_and_variables(
61+
client_live: Client,
62+
) -> None:
63+
"""Test Send API v3.1 with TemplateLanguage and Variables (Issue #97).
64+
65+
Proves that the SDK correctly serializes and transmits template variables
66+
to the Mailjet API, yielding a successful status code if payload format is valid.
67+
"""
68+
client_v31 = Client(auth=client_live.auth, version="v3.1")
69+
data = {
70+
"Messages": [
71+
{
72+
"From": {"Email": "pilot@mailjet.com", "Name": "Mailjet Pilot"},
73+
"To": [{"Email": "passenger1@mailjet.com", "Name": "Passenger 1"}],
74+
"Subject": "Template Test",
75+
"TextPart": "Welcome {{var:name}}",
76+
"HTMLPart": "<h3>Welcome {{var:name}}</h3>",
77+
"TemplateLanguage": True,
78+
"Variables": {"name": "John Doe"},
79+
}
80+
],
81+
"SandboxMode": True,
82+
}
83+
result = client_v31.send.create(data=data)
84+
85+
# We expect 200 OK because the JSON is perfectly serialized.
86+
# If variables were dropped or malformed, it might trigger 400 Bad Request.
87+
# 401 can happen if the account isn't validated yet, but it proves routing is fine.
88+
assert result.status_code in (200, 400, 401)
89+
assert result.status_code != 404
90+
91+
6092
def test_live_send_api_v3_1_bad_payload(client_live: Client) -> None:
6193
"""Test Send API v3.1 bad path (missing mandatory Messages array)."""
6294
client_v31 = Client(auth=client_live.auth, version="v3.1")

tests/unit/test_client.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,43 @@ def mock_request(*args: Any, **kwargs: Any) -> requests.Response:
194194
client_offline.contact.get(filters={"limit": 1}, filter={"ignored": "legacy"})
195195

196196

197+
def test_send_api_v3_1_template_language_variables(
198+
monkeypatch: pytest.MonkeyPatch,
199+
) -> None:
200+
"""Verify TemplateLanguage and Variables serialization (Issue #97).
201+
202+
Ensures that the Python SDK correctly serializes the boolean and dictionary
203+
types for Mailjet's templating engine before dispatching the HTTP request.
204+
"""
205+
client_v31 = Client(auth=("a", "b"), version="v3.1")
206+
207+
def mock_request(
208+
method: str, url: str, data: str | bytes | None = None, **kwargs: Any
209+
) -> requests.Response:
210+
assert data is not None
211+
assert isinstance(data, str)
212+
# Check that Python True became JSON true, and the dict serialized properly
213+
assert '"TemplateLanguage": true' in data
214+
assert '"Variables": {"name": "John Doe"}' in data
215+
216+
resp = requests.Response()
217+
resp.status_code = 200
218+
return resp
219+
220+
monkeypatch.setattr(client_v31.session, "request", mock_request)
221+
222+
payload = {
223+
"Messages": [
224+
{
225+
"TemplateLanguage": True,
226+
"Variables": {"name": "John Doe"},
227+
}
228+
]
229+
}
230+
result = client_v31.send.create(data=payload)
231+
assert result.status_code == 200
232+
233+
197234
def test_api_call_exceptions_and_logging(
198235
client_offline: Client, monkeypatch: pytest.MonkeyPatch, caplog: LogCaptureFixture
199236
) -> None:

0 commit comments

Comments
 (0)