def test_success_does_not_perform_retry(): dm_mailchimp_client = DMMailChimpClient('username', 'api key', mock.MagicMock(), retries=2) with mock.patch.object(dm_mailchimp_client._client.lists.members, 'all', autospec=True) as all_members: all_members.side_effect = [ { "members": [ { "email_address": "*****@*****.**" }, { "email_address": "*****@*****.**" }, ] }, { "members": [] }, ] dm_mailchimp_client.get_email_addresses_from_list('a_list_id') assert all_members.mock_calls == [ mock.call('a_list_id', count=1000, offset=0), mock.call('a_list_id', count=1000, offset=1000), ]
def test_default_timeout_retry_performs_no_retries(): dm_mailchimp_client = DMMailChimpClient('username', 'api key', mock.MagicMock()) with mock.patch.object(dm_mailchimp_client._client.lists.members, 'all', autospec=True) as all_members: all_members.side_effect = HTTPError(response=mock.Mock( status_code=504)) with pytest.raises(HTTPError): dm_mailchimp_client.get_email_addresses_from_list('a_list_id') assert all_members.call_args_list == [ mock.call('a_list_id', count=1000, offset=0), ]
def test_get_email_addresses_from_list_generates_emails(self): dm_mailchimp_client = DMMailChimpClient('username', DUMMY_MAILCHIMP_API_KEY, logging.getLogger('mailchimp')) with mock.patch.object(dm_mailchimp_client._client.lists.members, 'all', autospec=True) as all_members: all_members.side_effect = [ { "members": [ {"email_address": "*****@*****.**"}, {"email_address": "*****@*****.**"}, ] }, { "members": [] }, ] res = dm_mailchimp_client.get_email_addresses_from_list('list_id') assert isinstance(res, types.GeneratorType) assert all_members.call_args_list == [] with assert_external_service_log_entry(extra_modules=['mailchimp'], count=2): assert list(res) == ["*****@*****.**", "*****@*****.**"] assert all_members.call_args_list == [ mock.call('list_id', count=100, offset=0), mock.call('list_id', count=100, offset=100), ]
def test_get_email_addresses_from_list(): dm_mailchimp_client = DMMailChimpClient('username', 'api key', mock.MagicMock()) with mock.patch.object(dm_mailchimp_client._client.lists.members, 'all', autospec=True) as all_members: all_members.side_effect = [ { "members": [ { "email_address": "*****@*****.**" }, { "email_address": "*****@*****.**" }, ] }, { "members": [] }, ] res = dm_mailchimp_client.get_email_addresses_from_list('list_id') assert res == ["*****@*****.**", "*****@*****.**"] assert all_members.call_args_list == [ mock.call('list_id', count=1000, offset=0), mock.call('list_id', count=1000, offset=1000), ]
def test_default_timeout_retry_performs_no_retries(self): dm_mailchimp_client = DMMailChimpClient('username', DUMMY_MAILCHIMP_API_KEY, logging.getLogger('mailchimp')) with mock.patch.object(dm_mailchimp_client._client.lists.members, 'all', autospec=True) as all_members: all_members.side_effect = HTTPError(response=mock.Mock(status_code=504)) with pytest.raises(HTTPError): with assert_external_service_log_entry(successful_call=False, extra_modules=['mailchimp']): list(dm_mailchimp_client.get_email_addresses_from_list('a_list_id')) assert all_members.call_args_list == [ mock.call('a_list_id', count=100, offset=0), ]
def test_get_email_addresses_from_list(): dm_mailchimp_client = DMMailChimpClient('username', 'api key', mock.MagicMock()) with mock.patch.object(dm_mailchimp_client._client.lists.members, 'all', autospec=True) as all_members: all_members.return_value = { "members": [{ "email_address": "*****@*****.**" }, { "email_address": "*****@*****.**" }] } res = dm_mailchimp_client.get_email_addresses_from_list('list_id') assert res == ["*****@*****.**", "*****@*****.**"] all_members.assert_called_once_with('list_id', get_all=True)
def test_success_does_not_perform_retry(self): dm_mailchimp_client = DMMailChimpClient( 'username', DUMMY_MAILCHIMP_API_KEY, logging.getLogger('mailchimp'), retries=2 ) with mock.patch.object(dm_mailchimp_client._client.lists.members, 'all', autospec=True) as all_members: all_members.side_effect = [ { "members": [ {"email_address": "*****@*****.**"}, {"email_address": "*****@*****.**"}, ] }, { "members": [] }, ] with assert_external_service_log_entry(extra_modules=['mailchimp'], count=2): list(dm_mailchimp_client.get_email_addresses_from_list('a_list_id')) assert all_members.mock_calls == [ mock.call('a_list_id', count=100, offset=0), mock.call('a_list_id', count=100, offset=100), ]
def test_offset_increments_until_no_members(self): dm_mailchimp_client = DMMailChimpClient('username', DUMMY_MAILCHIMP_API_KEY, logging.getLogger('mailchimp')) with mock.patch.object(dm_mailchimp_client._client.lists.members, 'all', autospec=True) as all_members: all_members.side_effect = [ {"members": [{"email_address": "*****@*****.**"}]}, {"members": [{"email_address": "*****@*****.**"}]}, {"members": [{"email_address": "*****@*****.**"}]}, {"members": [{"email_address": "*****@*****.**"}]}, {"members": [{"email_address": "*****@*****.**"}]}, {"members": [{"email_address": "*****@*****.**"}]}, {"members": [{"email_address": "*****@*****.**"}]}, {"members": []}, ] res = dm_mailchimp_client.get_email_addresses_from_list('a_list_id') with assert_external_service_log_entry(extra_modules=['mailchimp'], count=8): assert list(res) == [ "*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**", ] assert all_members.call_args_list == [ mock.call('a_list_id', count=100, offset=0), mock.call('a_list_id', count=100, offset=100), mock.call('a_list_id', count=100, offset=200), mock.call('a_list_id', count=100, offset=300), mock.call('a_list_id', count=100, offset=400), mock.call('a_list_id', count=100, offset=500), mock.call('a_list_id', count=100, offset=600), mock.call('a_list_id', count=100, offset=700), ]