def test_init_list_analysis_existing_list_update_privacy_options( mocker, fake_list_data): """Tests the init_list_analysis function when the list exists in the database. Also tests that monthly_updates and store_aggregates are updated if they differ from that stored in the database.""" mocked_list_stats = mocker.patch('app.tasks.ListStats') mocked_recent_analyses = ( mocked_list_stats.query.filter_by.return_value.order_by .return_value.limit.return_value.all.return_value) mocked_desc = mocker.patch('app.tasks.desc') mocked_email_list = mocker.patch('app.tasks.EmailList') mocked_list_object = ( mocked_email_list.query.filter_by.return_value.first.return_value) mocked_list_object.monthly_updates = True mocked_list_object.store_aggregates = False mocked_db = mocker.patch('app.tasks.db') mocked_generate_summary_stats = mocker.patch( 'app.tasks.generate_summary_stats') mocked_generate_summary_stats.return_value = 'foo', 'bar' mocked_send_report = mocker.patch('app.tasks.send_report') init_list_analysis({'email': '*****@*****.**'}, fake_list_data, 1) mocked_list_stats.query.filter_by.assert_called_with( list_id=fake_list_data['list_id']) mocked_list_stats.query.filter_by.return_value.order_by.assert_called_with( mocked_desc.return_value) mocked_email_list.query.filter_by.assert_called_with( list_id=fake_list_data['list_id']) mocked_db.session.merge.assert_called_with(mocked_list_object) mocked_db.session.commit.assert_called() mocked_generate_summary_stats.assert_called_with(mocked_recent_analyses) mocked_send_report.assert_called_with( 'foo', 'bar', fake_list_data['list_id'], fake_list_data['list_name'], ['*****@*****.**'])
def test_init_analysis_existing_list_db_error(mocker, fake_list_data): """Tests the init_list_analysis function when the list exists in the database and a database error occurs.""" mocker.patch('app.tasks.ListStats') mocked_email_list = mocker.patch('app.tasks.EmailList') mocked_list_object = ( mocked_email_list.query.filter_by.return_value.first.return_value) mocked_list_object.monthly_updates = True mocked_list_object.store_aggregates = False mocked_db = mocker.patch('app.tasks.db') mocked_db.session.commit.side_effect = Exception() with pytest.raises(Exception): init_list_analysis({'email': '*****@*****.**'}, fake_list_data, 1) mocked_db.session.rollback.assert_called()
def test_init_list_analysis_new_list_no_store(mocker, fake_list_data): """Tests the init_list_analysis function when the list does not exist in the database and the user chose not to store their data.""" mocked_list_stats = mocker.patch('app.tasks.ListStats') (mocked_list_stats.query.filter_by.return_value.order_by.return_value. first.return_value) = None mocked_import_analyze_store_list = mocker.patch( 'app.tasks.import_analyze_store_list') mocked_email_list = mocker.patch('app.tasks.EmailList') mocked_email_list.query.filter_by.return_value.first.return_value = None mocker.patch('app.tasks.extract_stats') mocker.patch('app.tasks.send_report') init_list_analysis({'email': '*****@*****.**'}, fake_list_data, 1) mocked_import_analyze_store_list.assert_called_with( fake_list_data, 1, '*****@*****.**')
def test_init_list_analysis_new_list_monthly_updates(mocker, fake_list_data): """Tests the init_list_analysis function when the list does not exist in the database and the user chose to store their data and requested monthly updates.""" mocked_list_stats = mocker.patch('app.tasks.ListStats') (mocked_list_stats.query.filter_by.return_value.order_by .return_value.limit.return_value.all.return_value) = None mocker.patch('app.tasks.import_analyze_store_list') mocked_email_list = mocker.patch('app.tasks.EmailList') mocked_list_object = ( mocked_email_list.query.filter_by.return_value.first.return_value) mocked_list_object.monthly_updates = True mocked_list_object.store_aggregates = False mocked_associate_user_with_list = mocker.patch( 'app.tasks.associate_user_with_list') mocker.patch('app.tasks.generate_summary_stats', return_value=( 'foo', 'bar')) mocker.patch('app.tasks.send_report') fake_list_data['monthly_updates'] = True init_list_analysis( {'email': '*****@*****.**', 'user_id': 2}, fake_list_data, 1) mocked_associate_user_with_list.assert_called_with(2, mocked_list_object)