def test_import_analyze_store_list_maichimpimporterror(mocker, user_email): """Tests that the import_analyze_store_list function fails gracefully when a MailChimpImportError occurs.""" mocker.patch('app.tasks.MailChimpList') mocked_do_async_import = mocker.patch('app.tasks.do_async_import') mocked_do_async_import.side_effect = MailChimpImportError('foo', 'bar') mocked_send_email = mocker.patch('app.tasks.send_email') mocked_os = mocker.patch('app.tasks.os') mocked_os.environ.get.side_effect = ['*****@*****.**'] import_analyze_store_list( { 'list_id': 'foo', 'total_count': 'bar', 'key': 'foo-bar1', 'data_center': 'bar1' }, 1, user_email=user_email) if user_email: mocked_send_email.assert_called_with(ANY, ['*****@*****.**', '*****@*****.**'], 'error-email.html', { 'title': ANY, 'error_details': 'bar' }) else: mocked_send_email.assert_not_called()
def test_import_analyze_store_list_store_results_in_db_exception( # pylint: disable=unused-argument mocker, fake_list_data, mocked_mailchimp_list): """Tests the import_analyze_store_list function when data is stored in the db and an exception occurs.""" mocker.patch('app.tasks.do_async_import') mocker.patch('app.tasks.ListStats') mocker.patch('app.tasks.EmailList') mocked_db = mocker.patch('app.tasks.db') mocked_db.session.commit.side_effect = Exception() fake_list_data['monthly_updates'] = True with pytest.raises(Exception): import_analyze_store_list(fake_list_data, 'foo') mocked_db.session.rollback.assert_called()
def test_import_analyze_store_list( mocker, fake_list_data, fake_calculation_results, mocked_mailchimp_list): """Tests the import_analyze_store_list method.""" mocked_mailchimp_list_instance = mocked_mailchimp_list.return_value mocked_do_async_import = mocker.patch('app.tasks.do_async_import') mocked_list_stats = mocker.patch('app.tasks.ListStats', spec=ListStats) list_stats = import_analyze_store_list( fake_list_data, fake_list_data['org_id']) mocked_mailchimp_list.assert_called_with( fake_list_data['list_id'], fake_list_data['total_count'], fake_list_data['key'], fake_list_data['data_center']) mocked_do_async_import.assert_has_calls( mocked_mailchimp_list_instance.import_list_members.return_value, mocked_mailchimp_list_instance.import_sub_activity.return_value) mocked_mailchimp_list_instance.flatten.assert_called() mocked_mailchimp_list_instance.calc_list_breakdown.assert_called() mocked_mailchimp_list_instance.calc_open_rate.assert_called_with( fake_list_data['open_rate']) mocked_mailchimp_list_instance.calc_frequency.assert_called_with( fake_list_data['creation_timestamp'], fake_list_data['campaign_count']) mocked_mailchimp_list_instance.calc_histogram.assert_called() mocked_mailchimp_list_instance.calc_high_open_rate_pct.assert_called() mocked_mailchimp_list_instance.calc_cur_yr_stats.assert_called() assert isinstance(list_stats, ListStats) mocked_list_stats.assert_called_with( **{k: (v if k != 'hist_bin_counts' else json.dumps(v)) for k, v in fake_calculation_results.items()}, list_id=fake_list_data['list_id'])
def test_import_analyze_store_list_store_results_in_db(mocker, fake_list_data, mocked_mailchimp_list): """Tests the import_analyze_store_list function when data is stored in the db.""" mocker.patch('app.tasks.do_async_import') mocked_list_stats = mocker.patch('app.tasks.ListStats') mocked_email_list = mocker.patch('app.tasks.EmailList') mocked_db = mocker.patch('app.tasks.db') fake_list_data['monthly_updates'] = True import_analyze_store_list(fake_list_data, 'foo') mocked_email_list.assert_called_with( list_id=fake_list_data['list_id'], list_name=fake_list_data['list_name'], api_key=fake_list_data['key'], data_center=fake_list_data['data_center'], store_aggregates=fake_list_data['store_aggregates'], monthly_updates=fake_list_data['monthly_updates'], org_id='foo') mocked_db.session.merge.assert_called_with(mocked_email_list.return_value) mocked_db.session.add.assert_called_with(mocked_list_stats.return_value) mocked_db.session.commit.assert_called()