Esempio n. 1
0
def test_update_stored_data_import_error(mocker, fake_list_data, caplog):
    """Tests the update_stored_data function when the list import raises an error."""
    mocked_list_stats = mocker.patch('app.tasks.ListStats')
    mocked_list_to_update = MagicMock(
        **{('api_key' if k == 'key' else k): v
           for k, v in fake_list_data.items()}
    )
    mocked_analysis = MagicMock(
        analysis_timestamp=datetime(2000, 1, 1, tzinfo=timezone.utc),
        list=mocked_list_to_update,
        list_id=fake_list_data['list_id'])
    (mocked_list_stats.query.order_by.return_value.distinct
     .return_value.all.return_value) = [mocked_analysis]
    mocked_requests = mocker.patch('app.tasks.requests')
    mocked_import_analyze_store_list = mocker.patch(
        'app.tasks.import_analyze_store_list')
    mocked_import_analyze_store_list.side_effect = MailChimpImportError(
        'foo', 'bar')
    mocked_requests.get.return_value.json.return_value = {
        'stats': {
            'member_count': 5,
            'unsubscribe_count': 6,
            'cleaned_count': 7,
            'open_rate': 1,
            'campaign_count': 10
        }
    }
    with pytest.raises(MailChimpImportError):
        update_stored_data()
    assert 'Error importing new data for list foo.' in caplog.text
Esempio n. 2
0
def test_update_stored_data_empty_db(mocker, caplog):
    """Tests the update_stored_data function when there are no lists stored in
    the database."""
    mocked_list_stats = mocker.patch('app.tasks.ListStats')
    (mocked_list_stats.query.order_by.return_value.distinct
     .return_value.all.return_value) = None
    update_stored_data()
    assert 'No lists in the database!' in caplog.text
Esempio n. 3
0
def test_update_stored_data_no_old_analyses(mocker, caplog):
    """Tests the update_stored_data function when there are no analyses older
    than 30 days."""
    mocked_list_stats = mocker.patch('app.tasks.ListStats')
    mocked_analysis = MagicMock(analysis_timestamp=datetime.now(timezone.utc))
    (mocked_list_stats.query.order_by.return_value.distinct.return_value.all.
     return_value) = [mocked_analysis]
    caplog.set_level(logging.INFO)
    update_stored_data()
    assert 'No old lists to update' in caplog.text
Esempio n. 4
0
def test_update_stored_data(mocker, fake_list_data):
    """Tests the update_stored_data function."""
    mocked_list_stats = mocker.patch('app.tasks.ListStats')
    mocked_list_to_update = MagicMock(
        **{('api_key' if k == 'key' else k): v
           for k, v in fake_list_data.items()}
    )
    mocked_analysis = MagicMock(
        analysis_timestamp=datetime(2000, 1, 1, tzinfo=timezone.utc),
        list=mocked_list_to_update,
        list_id=fake_list_data['list_id'])
    (mocked_list_stats.query.order_by.return_value.distinct
     .return_value.all.return_value) = [mocked_analysis]
    mocked_requests = mocker.patch('app.tasks.requests')
    mocked_import_analyze_store_list = mocker.patch(
        'app.tasks.import_analyze_store_list')
    mocked_requests.get.return_value.json.return_value = {
        'stats': {
            'member_count': 5,
            'unsubscribe_count': 6,
            'cleaned_count': 7,
            'open_rate': 1,
            'campaign_count': 10
        }
    }
    update_stored_data()
    mocked_requests.get.assert_called_with(
        'https://bar1.api.mailchimp.com/3.0/lists/foo',
        params=(
            ('fields', 'stats.member_count,'
                       'stats.unsubscribe_count,'
                       'stats.cleaned_count,'
                       'stats.open_rate,'
                       'stats.campaign_count'),
        ),
        auth=('shorenstein', 'foo-bar1'))
    mocked_import_analyze_store_list.assert_called_with(
        {'list_id': 'foo',
         'list_name': 'bar',
         'key': 'foo-bar1',
         'data_center': 'bar1',
         'monthly_updates': False,
         'store_aggregates': False,
         'total_count': 18,
         'open_rate': 1,
         'creation_timestamp': 'quux',
         'campaign_count': 10},
        1)
Esempio n. 5
0
def test_update_stored_data_keyerror(mocker, fake_list_data, caplog):
    """Tests the update_stored_data function when the list raises a KeyError."""
    mocked_list_stats = mocker.patch('app.tasks.ListStats')
    mocked_list_to_update = MagicMock(
        **{('api_key' if k == 'key' else k): v
           for k, v in fake_list_data.items()}
    )
    mocked_analysis = MagicMock(
        analysis_timestamp=datetime(2000, 1, 1, tzinfo=timezone.utc),
        list=mocked_list_to_update,
        list_id=fake_list_data['list_id'])
    (mocked_list_stats.query.order_by.return_value.distinct
     .return_value.all.return_value) = [mocked_analysis]
    mocked_requests = mocker.patch('app.tasks.requests')
    mocked_requests.get.return_value.json.return_value = {}
    with pytest.raises(MailChimpImportError):
        update_stored_data()
    assert ('Error updating list foo. API key is no longer valid or list '
            'no longer exists.') in caplog.text