Skip to content

Commit b94b3e6

Browse files
committed
add test for upsert_member
1 parent 2fa981e commit b94b3e6

File tree

1 file changed

+78
-2
lines changed

1 file changed

+78
-2
lines changed

src/tests/test_mailchimp.py

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def test_get_subscriber_hash(self):
198198
@patch.object(MailChimpClient, "transform_response")
199199
@patch("requests.Session.post")
200200
def test_update_member_tags_success_active(self, mock_post, mock_transform):
201-
"""Test that update_member_tags correctly handles a 204 No Content Mailchimp success response"""
201+
"""Test that update_member_tags correctly handles a 204 MailChimp success response for adding tags"""
202202
fake_response = MagicMock()
203203
fake_response.status_code = 204
204204
fake_response.content = b""
@@ -235,7 +235,7 @@ def test_update_member_tags_success_active(self, mock_post, mock_transform):
235235
@patch.object(MailChimpClient, "transform_response")
236236
@patch("requests.Session.post")
237237
def test_update_member_tags_success_inactive(self, mock_post, mock_transform):
238-
"""Test that update_member_tags correctly handles a 204 No Content Mailchimp success response"""
238+
"""Test that update_member_tags correctly handles a 204 MailChimp success response for removing tags"""
239239
fake_response = MagicMock()
240240
fake_response.status_code = 204
241241
fake_response.content = b""
@@ -268,3 +268,79 @@ def test_update_member_tags_success_inactive(self, mock_post, mock_transform):
268268

269269
# no body in this call, just the status code
270270
self.assertEqual(result, {"status_code": 204})
271+
272+
# have to include this, as otherwise tests will fail as no datatype mapping
273+
@patch.object(
274+
MailChimpClient,
275+
"get_merge_fields_data_type_map",
276+
return_value={"FNAME": "text", "LNAME": "text"},
277+
)
278+
@patch.object(MailChimpClient, "transform_response")
279+
@patch("requests.Session.put")
280+
def test_upsert_member_success(
281+
self, mock_put, mock_transform, mock_merge_fields_map
282+
):
283+
"""Test that upsert_member sends correct payload and handles MailChimp 200 JSON response"""
284+
285+
fake_response = MagicMock()
286+
fake_response.status_code = 200
287+
288+
mock_put.return_value = fake_response
289+
mock_transform.return_value = {
290+
"id": "8121stac",
291+
"email_address": "[email protected]",
292+
"status_code": 200,
293+
}
294+
295+
list_id = "102930al"
296+
email_address = "[email protected]"
297+
merge_fields = {"FNAME": "Fake", "LNAME": "Dude"}
298+
299+
result = self.test_client.upsert_member(
300+
list_id=list_id, email_address=email_address, merge_fields=merge_fields
301+
)
302+
303+
expected_hash = self.test_client.get_subscriber_hash(email_address)
304+
expected_url = (
305+
f"https://us9.api.mailchimp.com/3.0/lists/{list_id}/members/{expected_hash}"
306+
)
307+
308+
expected_payload = {
309+
"email_address": email_address,
310+
"status_if_new": "subscribed",
311+
"merge_fields": merge_fields,
312+
}
313+
# put called once
314+
mock_put.assert_called_once_with(expected_url, json=expected_payload)
315+
# return is called once
316+
mock_transform.assert_called_once_with(fake_response)
317+
# success
318+
self.assertEqual(result["status_code"], 200)
319+
# compare final email val to expected
320+
self.assertEqual(result["email_address"], "[email protected]")
321+
322+
# have to include this, as otherwise tests will fail as no datatype mapping
323+
@patch.object(
324+
MailChimpClient,
325+
"get_merge_fields_data_type_map",
326+
return_value={"FNAME": "text"},
327+
)
328+
@patch.object(MailChimpClient, "transform_response")
329+
@patch("requests.Session.put")
330+
def test_upsert_member_fail(self, mock_put, mock_transform, mock_merge_fields_map):
331+
"""Test that upsert_member raises error when merge fields contain fake tags"""
332+
list_id = "102930al"
333+
email_address = "[email protected]"
334+
335+
# test KeyError is raised
336+
with self.assertRaises(KeyError):
337+
self.test_client.upsert_member(
338+
list_id=list_id,
339+
email_address=email_address,
340+
merge_fields={"FAKE_MERGE_TAG": "some_val"},
341+
)
342+
343+
# no put call for upsert should have occurred
344+
mock_put.assert_not_called()
345+
# no transform_response call for upsert should have occurred
346+
mock_transform.assert_not_called()

0 commit comments

Comments
 (0)