Skip to content
This repository was archived by the owner on Feb 6, 2026. It is now read-only.

Commit 1d13de8

Browse files
Merge pull request #28 from ShorensteinCenter/devel
[Feature, N/A] Added unit tests for MailChimpList class
2 parents de83259 + 3de5bf1 commit 1d13de8

3 files changed

Lines changed: 341 additions & 26 deletions

File tree

app/lists.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import asyncio
66
from collections import OrderedDict
77
from datetime import datetime, timedelta, timezone
8-
from billiard import current_process
8+
from billiard import current_process # pylint: disable=no-name-in-module
99
import requests
1010
from requests.exceptions import ConnectionError as ConnError
1111
import pandas as pd
@@ -249,6 +249,12 @@ async def make_async_request(self, url, params, session, retry=0):
249249

250250
exception_type = type(e).__name__
251251

252+
# If we're just catching the exception raised above
253+
# don't need to do anything else
254+
if exception_type == 'MailChimpImportError':
255+
raise
256+
257+
# Otherwise, log what happened as appropriate
252258
if exception_type == 'ClientHttpProxyError':
253259
self.logger.warning('Failed to connect to proxy! Proxy: %s',
254260
self.proxy)
@@ -287,6 +293,7 @@ async def make_async_request(self, url, params, session, retry=0):
287293
# Log the error and raise an exception
288294
self.logger.exception('Error in async request to MailChimp (%s)',
289295
exception_type)
296+
290297
raise MailChimpImportError(
291298
'Error in async request to MailChimp ({})'.format(
292299
exception_type),
@@ -397,6 +404,7 @@ async def import_sub_activity(self): # pylint: disable=too-many-locals
397404
"""
398405
params = (
399406
('fields', 'activity.action,activity.timestamp,email_id'),
407+
('exclude_fields', 'total_items,_links')
400408
)
401409

402410
request_uri = ('https://{}.api.mailchimp.com/3.0/lists/{}/members/'
@@ -516,10 +524,10 @@ def calc_frequency(self, date_created, campaign_count):
516524

517525
def calc_histogram(self):
518526
"""Calculates the distribution for subscriber open rate."""
519-
bin_boundaries = [-0.001, .1, .2, .3, .4, .5, .6, .7, .8, .9, 1.]
527+
bin_boundaries = np.linspace(0, 1, num=11)
520528
bins = (pd.cut(
521529
self.df.loc[self.df['status'] == 'subscribed', 'avg_open_rate'],
522-
bin_boundaries))
530+
bin_boundaries, include_lowest=True))
523531
self.hist_bin_counts = (pd.value_counts(bins, sort=False).tolist())
524532

525533
def calc_high_open_rate_pct(self):

tests/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55
from wtforms import BooleanField
66
from app import app
7+
from app.lists import MailChimpList
78

89
@pytest.fixture
910
def test_app():
@@ -96,3 +97,8 @@ def mocked_mailchimp_list(mocker, fake_calculation_results):
9697
for k, v in fake_calculation_results.items():
9798
setattr(mocked_mailchimp_list.return_value, k, v)
9899
yield mocked_mailchimp_list
100+
101+
@pytest.fixture
102+
def mailchimp_list():
103+
"""Creates a MailChimpList. Used for testing class/instance methiods."""
104+
yield MailChimpList(1, 2, 'foo-bar1', 'bar1')

0 commit comments

Comments
 (0)