def test_committee_date_filters(self): [ factories.CommitteeFactory(first_file_date=datetime.date(2015, 1, 1)), factories.CommitteeFactory(first_file_date=datetime.date(2015, 2, 1)), factories.CommitteeFactory(first_file_date=datetime.date(2015, 3, 1)), factories.CommitteeFactory(first_file_date=datetime.date(2015, 4, 1)), ] results = self._results(api.url_for(CommitteeList, min_first_file_date='02/01/2015')) self.assertTrue(all(each['first_file_date'] >= datetime.date(2015, 2, 1).isoformat() for each in results)) results = self._results(api.url_for(CommitteeList, max_first_file_date='02/03/2015')) self.assertTrue(all(each['first_file_date'] <= datetime.date(2015, 3, 1).isoformat() for each in results)) results = self._results( api.url_for( CommitteeList, min_first_file_date='02/01/2015', max_first_file_date='02/03/2015', ) ) self.assertTrue( all( datetime.date(2015, 2, 1).isoformat() <= each['first_file_date'] <= datetime.date(2015, 3, 1).isoformat() for each in results ) )
def test_page_param(self): [factories.CandidateFactory() for _ in range(20)] page_one_and_two = self._results(api.url_for(CandidateList, per_page=10, page=1)) page_two = self._results(api.url_for(CandidateList, per_page=5, page=2)) self.assertEqual(page_two[0], page_one_and_two[5]) for itm in page_two: self.assertIn(itm, page_one_and_two)
def test_filters(self): factories.CalendarDateFactory(start_date=datetime.datetime(2016, 1, 2)) factories.CalendarDateFactory(location='Mississippi, CA') factories.CalendarDateFactory(event_id=123) factories.CalendarDateFactory(state=['CA']) factories.CalendarDateFactory(category='Public Hearings') factories.CalendarDateFactory(description='a really interesting event') factories.CalendarDateFactory(summary='Meeting that will solve all the problems') factories.CalendarDateFactory(end_date=datetime.datetime(2015, 1, 2)) filter_fields = [ ('min_start_date', '2015-01-01'), ('category', 'Public Hearings'), ('min_end_date', '2014-01-01'), # this is not passing or working :/ #('state', 'CA'), ('description', 'interesting event'), ('summary', 'solve all the problems'), ('event_id', 123), ] orig_response = self._response(api.url_for(CalendarDatesView)) original_count = orig_response['pagination']['count'] for field, example in filter_fields: page = api.url_for(CalendarDatesView, **{field: example}) # returns at least one result results = self._results(page) self.assertEqual(len(results), 1) # doesn't return all results response = self._response(page) self.assertGreater(original_count, response['pagination']['count'])
def test_committee_sort(self): committees = [factories.CommitteeFactory(designation="B"), factories.CommitteeFactory(designation="U")] committee_ids = [each.committee_id for each in committees] results = self._results(api.url_for(CommitteeList, sort="designation")) self.assertEqual([each["committee_id"] for each in results], committee_ids) results = self._results(api.url_for(CommitteeList, sort="-designation")) self.assertEqual([each["committee_id"] for each in results], committee_ids[::-1])
def test_filters(self): [ factories.RadAnalystFactory(telephone_ext=123, committee_id='C0001'), factories.RadAnalystFactory(telephone_ext=456, committee_id='C0002'), factories.RadAnalystFactory(analyst_id=789, committee_id='C0003'), factories.RadAnalystFactory(analyst_id=1011, analyst_short_id=11, committee_id='C0004'), ] filter_fields = ( ('committee_id', 'C0002'), ('telephone_ext', 123), ('analyst_id', 789), ('analyst_short_id', 11), ) # checking one example from each field orig_response = self._response(api.url_for(RadAnalystView)) original_count = orig_response['pagination']['count'] for field, example in filter_fields: page = api.url_for(RadAnalystView, **{field: example}) # returns at least one result results = self._results(page) self.assertGreater(len(results), 0) # doesn't return all results response = self._response(page) self.assertGreater(original_count, response['pagination']['count'])
def test_committee_filters(self): [ factories.CommitteeFactory(state="CA"), factories.CommitteeFactory(name="Obama"), factories.CommitteeFactory(committee_type="S"), factories.CommitteeFactory(designation="P"), factories.CommitteeFactory(party="DEM"), factories.CommitteeFactory(organization_type="C"), factories.CommitteeFactory(committee_id="C01"), ] # checking one example from each field filter_fields = ( ("committee_id", ["C01", "C02"]), ("state", ["CA", "DC"]), ("name", "Obama"), ("committee_type", "S"), ("designation", "P"), ("party", ["REP", "DEM"]), ("organization_type", "C"), ) org_response = self._response(api.url_for(CommitteeList)) original_count = org_response["pagination"]["count"] for field, example in filter_fields: page = api.url_for(CommitteeList, **{field: example}) # returns at least one result results = self._results(page) self.assertGreater(len(results), 0) # doesn't return all results response = self._response(page) self.assertGreater(original_count, response["pagination"]["count"])
def test_cand_filters(self): [ factories.CandidateFactory(office='H'), factories.CandidateFactory(district='00'), factories.CandidateFactory(district='02'), factories.CandidateFactory(state='CA'), factories.CandidateFactory(name='Obama'), factories.CandidateFactory(party='DEM'), factories.CandidateFactory(cycles=[2006]), factories.CandidateFactory(candidate_id='BARLET'), factories.CandidateFactory(candidate_id='RITCHIE'), ] filter_fields = ( ('office', 'H'), ('district', ['00', '02']), ('state', 'CA'), ('name', 'Obama'), ('party', 'DEM'), ('cycle', '2006'), ('candidate_id', ['BARTLET', 'RITCHIE']) ) # checking one example from each field orig_response = self._response(api.url_for(CandidateList)) original_count = orig_response['pagination']['count'] for field, example in filter_fields: page = api.url_for(CandidateList, **{field: example}) # returns at least one result results = self._results(page) self.assertGreater(len(results), 0) # doesn't return all results response = self._response(page) self.assertGreater(original_count, response['pagination']['count'])
def test_filings_filters(self): [ factories.FilingsFactory(committee_id='C0004'), factories.FilingsFactory(committee_id='C0005'), factories.FilingsFactory(beginning_image_number=123456789021234567), factories.FilingsFactory(form_type='3'), factories.FilingsFactory(primary_general_indicator='G'), factories.FilingsFactory(amendment_indicator='A'), factories.FilingsFactory(report_type='Post General'), factories.FilingsFactory(report_year=1999), ] filter_fields = ( ('beginning_image_number', 123456789021234567), ('form_type', '3'), ('primary_general_indicator', 'G'), ('amendment_indicator', 'A'), ('report_type', 'Post General'), ('report_year', 1999), ) # checking one example from each field orig_response = self._response(api.url_for(FilingsList)) original_count = orig_response['pagination']['count'] for field, example in filter_fields: page = api.url_for(FilingsList, **{field: example}) # returns at least one result results = self._results(page) self.assertGreater(len(results), 0) # doesn't return all results response = self._response(page) self.assertGreater(original_count, response['pagination']['count'])
def test_invalid_per_page_param(self): results = self.app.get(api.url_for(CandidateList, per_page=-10)) self.assertEquals(results.status_code, 422) results = self.app.get(api.url_for(CandidateList, per_page=34.2)) self.assertEquals(results.status_code, 422) results = self.app.get(api.url_for(CandidateList, per_page='dynamic-wombats')) self.assertEquals(results.status_code, 422)
def test_reports_by_committee_type_and_year(self): presidential_report_2012 = factories.ReportsPresidentialFactory(report_year=2012) presidential_report_2016 = factories.ReportsPresidentialFactory(report_year=2016) house_report_2016 = factories.ReportsHouseSenateFactory(report_year=2016) results = self._results( api.url_for( ReportsView, committee_type='presidential', year=2016, ) ) self._check_committee_ids( results, [presidential_report_2016], [presidential_report_2012, house_report_2016], ) # Test repeated cycle parameter results = self._results( api.url_for( ReportsView, committee_type='presidential', year=[2016, 2018], ) ) self._check_committee_ids( results, [presidential_report_2016], [presidential_report_2012, house_report_2016], )
def test_committee_filters(self): [ factories.CommitteeFactory(state='CA'), factories.CommitteeFactory(name='Obama'), factories.CommitteeFactory(committee_type='S'), factories.CommitteeFactory(designation='P'), factories.CommitteeFactory(party='DEM'), factories.CommitteeFactory(organization_type='C'), factories.CommitteeFactory(committee_id='C01'), ] # checking one example from each field filter_fields = ( ('committee_id', ['C01', 'C02']), ('state', ['CA', 'DC']), ('name', 'Obama'), ('committee_type', 'S'), ('designation', 'P'), ('party', ['REP', 'DEM']), ('organization_type', 'C'), ) org_response = self._response(api.url_for(CommitteeList)) original_count = org_response['pagination']['count'] for field, example in filter_fields: page = api.url_for(CommitteeList, **{field: example}) # returns at least one result results = self._results(page) self.assertGreater(len(results), 0) # doesn't return all results response = self._response(page) self.assertGreater(original_count, response['pagination']['count'])
def test_committee_sort(self): committees = [ factories.CommitteeFactory(designation='B'), factories.CommitteeFactory(designation='U'), ] committee_ids = [each.committee_id for each in committees] results = self._results(api.url_for(CommitteeList, sort='designation')) self.assertEqual([each['committee_id'] for each in results], committee_ids) results = self._results(api.url_for(CommitteeList, sort='-designation')) self.assertEqual([each['committee_id'] for each in results], committee_ids[::-1])
def test_null_pagination_with_null_sort_column_values_ascending_with_sort_expression(self): # NOTE: Schedule B is sorted by disbursement date with the expression # sa.func.coalesce(self.disbursement_date, sa.cast('9999-12-31', sa.Date)) # by default (in descending order), so we must account for that with the # results and slice the baseline list of objects accordingly! filings = [ factories.ScheduleBFactory(disbursement_date=None) # this range should ensure the page has a null transition for _ in range(10) ] filings = filings + [ factories.ScheduleBFactory( disbursement_date=datetime.date(2016, 1, 1) ) for _ in range(15) ] page1 = self._results(api.url_for( ScheduleBView, sort='disbursement_date', sort_reverse_nulls='true', **self.kwargs )) self.assertEqual(len(page1), 20) top_reversed_from_middle = filings[10::] reversed_from_bottom_to_middle = filings[0:5:] top_reversed_from_middle.extend(reversed_from_bottom_to_middle) self.assertEqual( [int(each['sub_id']) for each in page1], [each.sub_id for each in top_reversed_from_middle], ) self.assertEqual( [each['disbursement_date'] for each in page1], [each.disbursement_date.strftime('%Y-%m-%d') if each.disbursement_date else None for each in top_reversed_from_middle] ) page2 = self._results(api.url_for( ScheduleBView, last_index=page1[-1]['sub_id'], sort_null_only=True, sort='disbursement_date', **self.kwargs )) self.assertEqual(len(page2), 5) self.assertEqual( [int(each['sub_id']) for each in page2], [each.sub_id for each in filings[5:10:]], ) self.assertEqual( [each['disbursement_date'] for each in page2], [each.disbursement_date.strftime('%Y-%m-%d') if each.disbursement_date else None for each in filings[5:10:]] )
def test_conditional_missing_params(self): response = self.app.get(api.url_for(ElectionView, office='president', cycle=2012)) self.assertEquals(response.status_code, 200) response = self.app.get(api.url_for(ElectionView, office='senate', cycle=2012)) self.assertEquals(response.status_code, 422) response = self.app.get(api.url_for(ElectionView, office='senate', cycle=2012, state='NY')) self.assertEquals(response.status_code, 200) response = self.app.get(api.url_for(ElectionView, office='house', cycle=2012, state='NY')) self.assertEquals(response.status_code, 422) response = self.app.get(api.url_for(ElectionView, office='house', cycle=2012, state='NY', district='01')) self.assertEquals(response.status_code, 200)
def test_cycle_filter(self): factories.CandidateFactory(cycles=[1986, 1988]) factories.CandidateFactory(cycles=[2000, 2002]) results = self._results(api.url_for(CandidateList, cycle=1988)) self.assertEqual(len(results), 1) for result in results: self.assertIn(1988, result["cycles"]) results = self._results(api.url_for(CandidateList, cycle=[1986, 2002])) self.assertEqual(len(results), 2) cycles = set([1986, 2002]) for result in results: self.assertTrue(cycles.intersection(result["cycles"]))
def test_amount_sched_e(self): [ factories.ScheduleEFactory(expenditure_amount=50), factories.ScheduleEFactory(expenditure_amount=100), factories.ScheduleEFactory(expenditure_amount=150), factories.ScheduleEFactory(expenditure_amount=200), ] results = self._results(api.url_for(ScheduleEView, min_amount=100)) self.assertTrue(all(each['expenditure_amount'] >= 100 for each in results)) results = self._results(api.url_for(ScheduleEView, max_amount=150)) self.assertTrue(all(each['expenditure_amount'] <= 150 for each in results)) results = self._results(api.url_for(ScheduleEView, min_amount=100, max_amount=150)) self.assertTrue(all(100 <= each['expenditure_amount'] <= 150 for each in results))
def test_image_number_range(self): [ factories.ScheduleAFactory(image_number='1'), factories.ScheduleAFactory(image_number='2'), factories.ScheduleAFactory(image_number='3'), factories.ScheduleAFactory(image_number='4'), ] results = self._results(api.url_for(ScheduleAView, min_image_number='2')) self.assertTrue(all(each['image_number'] >= '2' for each in results)) results = self._results(api.url_for(ScheduleAView, max_image_number='3')) self.assertTrue(all(each['image_number'] <= '3' for each in results)) results = self._results(api.url_for(ScheduleAView, min_image_number='2', max_image_number='3')) self.assertTrue(all('2' <= each['image_number'] <= '3' for each in results))
def test_amount_sched_b(self): [ factories.ScheduleBFactory(disbursement_amount=50), factories.ScheduleBFactory(disbursement_amount=100), factories.ScheduleBFactory(disbursement_amount=150), factories.ScheduleBFactory(disbursement_amount=200), ] results = self._results(api.url_for(ScheduleBView, min_amount=100)) self.assertTrue(all(each['disbursement_amount'] >= 100 for each in results)) results = self._results(api.url_for(ScheduleBView, max_amount=150)) self.assertTrue(all(each['disbursement_amount'] <= 150 for each in results)) results = self._results(api.url_for(ScheduleBView, min_amount=100, max_amount=150)) self.assertTrue(all(100 <= each['disbursement_amount'] <= 150 for each in results))
def test_amount_sched_a(self): [ factories.ScheduleAFactory(contribution_receipt_amount=50), factories.ScheduleAFactory(contribution_receipt_amount=100), factories.ScheduleAFactory(contribution_receipt_amount=150), factories.ScheduleAFactory(contribution_receipt_amount=200), ] results = self._results(api.url_for(ScheduleAView, min_amount=100)) self.assertTrue(all(each['contribution_receipt_amount'] >= 100 for each in results)) results = self._results(api.url_for(ScheduleAView, max_amount=150)) self.assertTrue(all(each['contribution_receipt_amount'] <= 150 for each in results)) results = self._results(api.url_for(ScheduleAView, min_amount=100, max_amount=150)) self.assertTrue(all(100 <= each['contribution_receipt_amount'] <= 150 for each in results))
def test_null_pagination_with_null_sort_column_values_ascending(self): filings = [ factories.ScheduleAFactory(contribution_receipt_date=None) # this range should ensure the page has a null transition for _ in range(10) ] filings = filings + [ factories.ScheduleAFactory( contribution_receipt_date=datetime.date(2016, 1, 1) ) for _ in range(15) ] page1 = self._results(api.url_for( ScheduleAView, sort='contribution_receipt_date', sort_reverse_nulls='true', **self.kwargs )) self.assertEqual(len(page1), 20) top_reversed_from_middle = filings[10::] reversed_from_bottom_to_middle = filings[0:5:] top_reversed_from_middle.extend(reversed_from_bottom_to_middle) self.assertEqual( [int(each['sub_id']) for each in page1], [each.sub_id for each in top_reversed_from_middle], ) self.assertEqual( [each['contribution_receipt_date'] for each in page1], [each.contribution_receipt_date.strftime('%Y-%m-%d') if each.contribution_receipt_date else None for each in top_reversed_from_middle] ) page2 = self._results(api.url_for( ScheduleAView, last_index=page1[-1]['sub_id'], sort_null_only=True, sort='contribution_receipt_date', **self.kwargs )) self.assertEqual(len(page2), 5) self.assertEqual( [int(each['sub_id']) for each in page2], [each.sub_id for each in filings[5:10:]], ) self.assertEqual( [each['contribution_receipt_date'] for each in page2], [each.contribution_receipt_date.strftime('%Y-%m-%d') if each.contribution_receipt_date else None for each in filings[5:10:]] )
def test_candidate_sort(self): candidates = [ factories.CandidateFactory(candidate_status='P'), factories.CandidateFactory(candidate_status='C'), ] candidate_ids = [each.candidate_id for each in candidates] results = self._results(api.url_for(CandidateList, sort='candidate_status')) self.assertEqual([each['candidate_id'] for each in results], candidate_ids[::-1]) results = self._results(api.url_for(CandidateSearch, sort='candidate_status')) self.assertEqual([each['candidate_id'] for each in results], candidate_ids[::-1]) results = self._results(api.url_for(CandidateList, sort='-candidate_status')) self.assertEqual([each['candidate_id'] for each in results], candidate_ids) results = self._results(api.url_for(CandidateSearch, sort='-candidate_status')) self.assertEqual([each['candidate_id'] for each in results], candidate_ids)
def test_candidate_multi_sort(self): candidates = [ factories.CandidateFactory(candidate_status='C', party='DFL'), factories.CandidateFactory(candidate_status='P', party='FLP'), factories.CandidateFactory(candidate_status='P', party='REF'), ] candidate_ids = [each.candidate_id for each in candidates] results = self._results(api.url_for(CandidateList, sort=['candidate_status', 'party'])) self.assertEqual([each['candidate_id'] for each in results], candidate_ids) results = self._results(api.url_for(CandidateList, sort=['candidate_status', '-party'])) self.assertEqual( [each['candidate_id'] for each in results], [candidate_ids[0], candidate_ids[2], candidate_ids[1]], )
def test_fulltext_match(self): danielle = factories.CandidateFactory(name='Danielle') factories.CandidateSearchFactory(id=danielle.candidate_id, fulltxt=sa.func.to_tsvector('Danielle')) dana = factories.CandidateFactory(name='Dana') factories.CandidateSearchFactory(id=dana.candidate_id, fulltxt=sa.func.to_tsvector('Dana')) rest.db.session.flush() results = self._results(api.url_for(CandidateList, q='danielle')) self.assertEqual(len(results), 1) self.assertEqual(results[0]['candidate_id'], danielle.candidate_id) results = self._results(api.url_for(CandidateList, q='dan')) self.assertEqual(len(results), 2) self.assertEqual( set(each['candidate_id'] for each in results), {danielle.candidate_id, dana.candidate_id}, )
def test_filter_line_number(self): [ factories.ScheduleAFactory(line_number='16', filing_form='F3X'), factories.ScheduleAFactory(line_number='17', filing_form='F3X') ] results = self._results(api.url_for(ScheduleAView, line_number='f3X-16', **self.kwargs)) self.assertEqual(len(results), 1) [ factories.ScheduleBFactory(line_number='21', filing_form='F3X'), factories.ScheduleBFactory(line_number='22', filing_form='F3X') ] results = self._results(api.url_for(ScheduleBView, line_number='f3X-21', **self.kwargs)) self.assertEqual(len(results), 1)
def test_candidate_aggregates_by_committee_full(self): """For each aggregate type, create a two-year aggregate in the target election year and a two-year aggregate in the previous two-year period. Assert that both aggregates are summed when the `election_full` flag is passed. """ for factory, resource, schema in self.cases: aggregates = self.make_aggregates(factory) results = self._results( api.url_for( resource, candidate_id=self.candidate.candidate_id, committee_id=self.committee.committee_id, cycle=2012, election_full='true', ) ) assert len(results) == 1 serialized = schema().dump(aggregates[0]).data serialized.update({ 'committee_name': self.committee.name, 'candidate_name': self.candidate.name, 'total': sum(each.total for each in aggregates), 'count': sum(each.count for each in aggregates), }) assert results[0] == serialized
def test_filings(self): """ Check filings returns in general endpoint""" factories.FilingsFactory(committee_id='C001') factories.FilingsFactory(committee_id='C002') results = self._results(api.url_for(FilingsList)) self.assertEqual(len(results), 2)
def test_committee_filings(self): """ Check filing returns with a specified committee id""" committee_id = 'C8675309' factories.FilingsFactory(committee_id=committee_id) results = self._results(api.url_for(FilingsView, committee_id=committee_id)) self.assertEqual(results[0]['committee_id'], committee_id)
def test_sorting(self): receipts = [ factories.ScheduleAFactory( report_year=2016, contribution_receipt_date=datetime.date(2016, 1, 1), two_year_transaction_period=2016 ), factories.ScheduleAFactory( report_year=2015, contribution_receipt_date=datetime.date(2015, 1, 1), two_year_transaction_period=2016 ), ] response = self._response(api.url_for(ScheduleAView, sort='contribution_receipt_date', **self.kwargs)) self.assertEqual( [each['report_year'] for each in response['results']], [2015, 2016] ) self.assertEqual( response['pagination']['last_indexes'], { 'last_index': str(receipts[0].sub_id), 'last_contribution_receipt_date': receipts[0].contribution_receipt_date.isoformat(), } )
def test_hide_bad_data(self): factories.ElectionDateFactory(election_status_id=1) factories.ElectionDateFactory(election_status_id=2) page = api.url_for(ElectionDatesView) results = self._results(page) assert len(results) == 1
def test_by_state(self): [ factories.ScheduleAByStateFactory( committee_id=self.committees[0].committee_id, cycle=2012, total=50, state='NY', state_full='New York', ), factories.ScheduleAByStateFactory( committee_id=self.committees[1].committee_id, cycle=2012, total=150, state='NY', state_full='New York', ), ] results = self._results( api.url_for( ScheduleAByStateCandidateView, candidate_id=self.candidate.candidate_id, cycle=2012, ) ) self.assertEqual(len(results), 1) expected = { 'candidate_id': self.candidate.candidate_id, 'cycle': 2012, 'total': 200, 'state': 'NY', 'state_full': 'New York', } self.assertEqual(results[0], expected)
def test_disbursement_purpose(self): committee = factories.CommitteeHistoryFactory(cycle=2012) aggregate = factories.ScheduleBByPurposeFactory( committee_id=committee.committee_id, cycle=committee.cycle, purpose='ADMINISTRATIVE EXPENSES') results = self._results( api.url_for(ScheduleBByPurposeView, committee_id=committee.committee_id, cycle=2012, purpose='Administrative')) self.assertEqual(len(results), 1) expected = { 'committee_id': committee.committee_id, 'purpose': 'ADMINISTRATIVE EXPENSES', 'cycle': 2012, 'total': aggregate.total, 'count': aggregate.count, 'memo_total': aggregate.memo_total, 'memo_count': aggregate.memo_count, } self.assertEqual(results[0], expected)
def _check_reports(self, committee_type, factory, schema): committee = factories.CommitteeFactory(committee_type=committee_type) factories.CommitteeHistoryFactory( committee_id=committee.committee_id, committee_type=committee_type, cycle=2012, ) end_dates = [datetime.datetime(2012, 1, 1), datetime.datetime(2008, 1, 1)] committee_id = committee.committee_id db.session.flush() [ factory( committee_id=committee_id, coverage_end_date=end_date, report_year=2011, ) for end_date in end_dates ] response = self._results( api.url_for(CommitteeReportsView, committee_id=committee_id) ) self.assertEqual(len(response), 2) self.assertEqual(response[0]['coverage_end_date'], isoformat(end_dates[0])) self.assertEqual(response[1]['coverage_end_date'], isoformat(end_dates[1])) assert response[0].keys() == schema().fields.keys()
def test_efile_presidential_reports(self): committee_id = 'C86753090' other_id = 'C2222222' committee_efile = factories.EfileReportsPresidentialFactory( committee_id=committee_id, cash_on_hand_end_period=20 ) other_efile = factories.EfileReportsPresidentialFactory( committee_id=other_id, cash_on_hand_end_period=40 ) results = self._results( api.url_for( EFilingPresidentialSummaryView, committee_type='presidential', committee_id=committee_id, ) ) self.assertEqual(results[0]['committee_id'], committee_id) self.assertEqual(results[0]['cash_on_hand_end_period'], 20) self._check_committee_ids(results, [committee_efile], [other_efile])
def test_fulltext_search(self): committee = factories.CommitteeFactory( name='Americans for a Better Tomorrow, Tomorrow' ) decoy_committee = factories.CommitteeFactory() factories.CommitteeSearchFactory( id=committee.committee_id, fulltxt=sa.func.to_tsvector(committee.name), is_active=True, ) queries = [ 'america', 'tomorrow', 'america tomorrow', 'america & tomorrow', ] for query in queries: results = self._results(api.url_for(CommitteeList, q=query)) self.assertEqual(len(results), 1) self.assertEqual(results[0]['committee_id'], committee.committee_id) self.assertNotEqual( results[0]['committee_id'], decoy_committee.committee_id )
def test_pac_total_by_committee_type(self): first_pac_total = { 'committee_id': 'C00001', 'committee_type': 'O', 'cycle': 2018, 'all_loans_received': 1, 'allocated_federal_election_levin_share': 2, } second_pac_total = { 'committee_id': 'C00002', 'committee_type': 'N', 'cycle': 2016, 'all_loans_received': 10, 'allocated_federal_election_levin_share': 20, } factories.TotalsPacFactory(**first_pac_total) factories.TotalsPacFactory(**second_pac_total) results = self._results( api.url_for(TotalsByCommitteeTypeView, committee_type='pac')) assert len(results) == 2 assert results[0]['committee_id'] == 'C00001' assert results[1]['committee_id'] == 'C00002'
def test_disbursement_recipient(self): committee = factories.CommitteeHistoryFactory(cycle=2012) aggregate = factories.ScheduleBByRecipientFactory( committee_id=committee.committee_id, cycle=committee.cycle, recipient_name='STARBOARD STRATEGIES, INC.') results = self._results( api.url_for(ScheduleBByRecipientView, committee_id=committee.committee_id, cycle=2012, recipient_name='Starboard Strategies')) self.assertEqual(len(results), 1) expected = { 'committee_id': committee.committee_id, 'recipient_name': 'STARBOARD STRATEGIES, INC.', 'cycle': 2012, 'total': aggregate.total, 'count': aggregate.count, 'memo_total': aggregate.memo_total, 'memo_count': aggregate.memo_count, } self.assertEqual(results[0], expected)
def test_candidates_search(self): principal_committee = factories.CommitteeFactory(designation='P') joint_committee = factories.CommitteeFactory(designation='J') candidate = factories.CandidateFactory() db.session.flush() [ factories.CandidateCommitteeLinkFactory( candidate_key=candidate.candidate_key, committee_key=principal_committee.committee_key, ), factories.CandidateCommitteeLinkFactory( candidate_key=candidate.candidate_key, committee_key=joint_committee.committee_key, ), ] results = self._results(api.url_for(CandidateSearch)) self.assertEqual(len(results), 1) self.assertIn('principal_committees', results[0]) self.assertEqual(len(results[0]['principal_committees']), 1) self.assertEqual( results[0]['principal_committees'][0]['committee_id'], principal_committee.committee_id, )
def test_filters_sched_e(self): filters = [ ('image_number', ScheduleE.image_number, ['123', '456']), ('committee_id', ScheduleE.committee_id, ['C01', 'C02']), ('support_oppose_indicator', ScheduleE.support_oppose_indicator, ['S', 'O']), ('is_notice', ScheduleE.is_notice, [True, False]), ('candidate_office_state', ScheduleE.candidate_office_state, ['AZ', 'AK']), ('candidate_office_district', ScheduleE.candidate_office_district, ['00', '01']), ('candidate_party', ScheduleE.candidate_party, ['DEM', 'REP']), ('candidate_office', ScheduleE.candidate_office, ['H', 'S', 'P']), ] for label, column, values in filters: [ factories.ScheduleEFactory(**{column.key: value}) for value in values ] results = self._results( api.url_for(ScheduleEView, **{label: values[0]})) assert len(results) == 1 assert results[0][column.key] == values[0]
def test_reports_sort_default(self): committee = factories.CommitteeFactory(committee_type='H') committee_id = committee.committee_id factories.CommitteeHistoryFactory( committee_id=committee_id, committee_type='H', ) dates = [ datetime.datetime(2015, 7, 4), datetime.datetime(2015, 7, 5), ] dates_formatted = [isoformat(each) for each in dates] factories.ReportsHouseSenateFactory( committee_id=committee_id, coverage_end_date=dates[0] ) factories.ReportsHouseSenateFactory( committee_id=committee_id, coverage_end_date=dates[1] ) results = self._results( api.url_for(CommitteeReportsView, committee_id=committee_id) ) self.assertEqual( [each['coverage_end_date'] for each in results], dates_formatted[::-1] )
def test_ie_committee(self): committee = factories.CommitteeFactory(committee_type='I') committee_id = committee.committee_id factories.CommitteeHistoryFactory( committee_id=committee_id, committee_type='I', ) report = factories.ReportsIEOnlyFactory( committee_id=committee_id, independent_contributions_period=200, independent_expenditures_period=100, ) results = self._results( api.url_for( CommitteeReportsView, committee_id=committee_id, )) result = results[0] for key in [ 'report_form', 'independent_contributions_period', 'independent_expenditures_period' ]: self.assertEqual(result[key], getattr(report, key))
def test_sorting(self): receipts = [ factories.ScheduleAFactory(report_year=2014, contribution_receipt_date=datetime.date( 2014, 1, 1)), factories.ScheduleAFactory(report_year=2012, contribution_receipt_date=datetime.date( 2012, 1, 1)), factories.ScheduleAFactory(report_year=1986, contribution_receipt_date=datetime.date( 1986, 1, 1)), ] response = self._response( api.url_for(ScheduleAView, sort='contribution_receipt_date')) self.assertEqual([each['report_year'] for each in response['results']], [2012, 2014]) self.assertEqual( response['pagination']['last_indexes'], { 'last_index': receipts[0].sched_a_sk, 'last_contribution_receipt_date': receipts[0].contribution_receipt_date.isoformat(), })
def test_committee_detail_fields(self): committee = factories.CommitteeDetailFactory( first_file_date=datetime.date(1982, 12, 31), committee_type='P', treasurer_name='Robert J. Lipshutz', party='DEM', form_type='F1Z', street_1='1795 Peachtree Road', zip='30309', ) response = self._response( api.url_for(CommitteeView, committee_id=committee.committee_id)) result = response['results'][0] # main fields self.assertEqual(result['first_file_date'], committee.first_file_date.isoformat()) self.assertEqual(result['committee_type'], committee.committee_type) self.assertEqual(result['treasurer_name'], committee.treasurer_name) self.assertEqual(result['party'], committee.party) # Things on the detailed view self.assertEqual(result['form_type'], committee.form_type) self.assertEqual(result['street_1'], committee.street_1) self.assertEqual(result['zip'], committee.zip)
def test_reports_sort(self): committee = factories.CommitteeFactory(committee_type='H') committee_id = committee.committee_id factories.CommitteeHistoryFactory( committee_id=committee_id, committee_type='H', ) contributions = [0, 100] factories.ReportsHouseSenateFactory( committee_id=committee_id, net_contributions_period=contributions[0]) factories.ReportsHouseSenateFactory( committee_id=committee_id, net_contributions_period=contributions[1]) results = self._results( api.url_for( CommitteeReportsView, committee_id=committee_id, sort=['-net_contributions_period'], )) self.assertEqual( [each['net_contributions_period'] for each in results], contributions[::-1])
def test_Presidential_totals(self): committee_id = 'C8675309' transaction_coverage = factories.TransactionCoverageFactory( committee_id=committee_id, fec_election_year=2016) history = factories.CommitteeHistoryFactory( committee_id=committee_id, committee_type='P', ) presidential_fields = { 'committee_id': 'C8675309', 'cycle': 2016, 'candidate_contribution': 1, 'exempt_legal_accounting_disbursement': 2, 'federal_funds': 3, 'fundraising_disbursements': 4, 'loan_repayments_made': 16, 'loans_received': 5, 'loans_received_from_candidate': 6, 'offsets_to_fundraising_expenditures': 7, 'offsets_to_legal_accounting': 8, 'total_offsets_to_operating_expenditures': 9, 'other_loans_received': 10, 'other_receipts': 11, 'repayments_loans_made_by_candidate': 12, 'repayments_other_loans': 13, 'transfers_from_affiliated_committee': 14, 'transfers_to_other_authorized_committee': 15, } fields = utils.extend(shared_fields, presidential_fields) committee_total = factories.CommitteeTotalsPerCycleFactory(**fields) fields = utils.extend(fields, transaction_coverage_fields) results = self._results( api.url_for(TotalsCommitteeView, committee_id=committee_id)) self.assertEqual(results[0], fields)
def test_candidate(self): """Compare results to expected fields.""" candidate = factories.CandidateDetailFactory(**fields) response = self._response( api.url_for(CandidateView, candidate_id=candidate.candidate_id) ) assert response['pagination'] == { 'count': 1, 'page': 1, 'pages': 1, 'per_page': 20, } # we are showing the full history rather than one result assert len(response['results']) == 1 result = response['results'][0] assert result['candidate_id'] == candidate.candidate_id # # most recent record should be first assert result['name'] == candidate.name # #address assert result['address_city'] == candidate.address_city assert result['address_state'] == candidate.address_state assert result['address_street_1'] == candidate.address_street_1 assert result['address_zip'] == candidate.address_zip # # office assert result['office'] == candidate.office assert result['district'] == candidate.district assert result['state'] == candidate.state assert result['office_full'] == candidate.office_full # # From party_mapping assert result['party'] == candidate.party assert result['party_full'] == candidate.party_full # From status_mapping assert result['active_through'] == candidate.active_through assert result['candidate_inactive'] == candidate.candidate_inactive assert result['candidate_status'] == candidate.candidate_status assert result['incumbent_challenge'] == candidate.incumbent_challenge
def test_basic_receipts_test(self): [ factories.EntityReceiptDisbursementTotalsFactory( cycle=2000, cumulative_candidate_receipts=50000, month=3, year=1999), factories.EntityReceiptDisbursementTotalsFactory( cycle=2020, cumulative_party_receipts=90000, month=3, year=2019), factories.EntityReceiptDisbursementTotalsFactory( cycle=1998, cumulative_pac_disbursements=50000, month=3, year=1997), factories.EntityReceiptDisbursementTotalsFactory( cycle=2008, cumulative_candidate_disbursements=90000, month=3, year=2008) ] filter_fields = ( ('cycle', int(2020)), ('cycle', int(2000)), ('cycle', int(1998)), ('cycle', int(2008)), ) for field, example in filter_fields: page = api.url_for(EntityReceiptDisbursementTotalsView, **{field: example}) # returns one result results = self._results(page) self.assertEqual(len(results), 1)
def test_aggregates_by_election(self): params = [ (factories.CommunicationCostByCandidateFactory, CommunicationCostByCandidateView), (factories.ElectioneeringByCandidateFactory, ElectioneeringByCandidateView), ] candidate = factories.CandidateFactory( election_years=[2012], office='P', ) factories.CandidateHistoryFactory( candidate_id=candidate.candidate_id, candidate_key=candidate.candidate_key, two_year_period=2012, election_years=[2012], office='P', ) for factory, resource in params: [ factory( committee_id=self.committee.committee_id, candidate_id=candidate.candidate_id, cycle=self.committee.cycle, ), factory(cycle=self.committee.cycle, ), ] results = self._results( api.url_for( resource, office='president', cycle=2012, )) self.assertEqual(len(results), 1) self.assertEqual(results[0]['candidate']['candidate_id'], candidate.candidate_id)
def test_schedule_a_sorting(self): receipts = [ factories.ScheduleAFactory(report_year=2016, contribution_receipt_date=datetime.date( 2016, 1, 1), two_year_transaction_period=2016), factories.ScheduleAFactory(report_year=2015, contribution_receipt_date=datetime.date( 2015, 1, 1), two_year_transaction_period=2016), ] response = self._response( api.url_for(ScheduleAView, sort='contribution_receipt_date', **self.kwargs)) self.assertEqual([each['report_year'] for each in response['results']], [2015, 2016]) self.assertEqual( response['pagination']['last_indexes'], { 'last_index': str(receipts[0].sub_id), 'last_contribution_receipt_date': receipts[0].contribution_receipt_date.isoformat(), })
def test_sched_e_filter_match(self): factories.CandidateElectionFactory(candidate_id='S002', cand_election_year=2014) factories.ScheduleEByCandidateFactory( total=50000, count=10, cycle=2008, candidate_id='S001', support_oppose_indicator='O', ), factories.ScheduleEByCandidateFactory( total=10000, count=5, cycle=2010, candidate_id='S002', support_oppose_indicator='O', ), results = self._results( api.url_for(ScheduleEByCandidateView, candidate_id='S002', cycle=2014, support_oppose_indicator='S')) self.assertEqual(len(results), 1)
def test_elections(self): results = self._results( api.url_for(ElectionView, office='senate', cycle=2012, state='NY')) self.assertEqual(len(results), 1) totals = [each for each in self.totals if each.cycle == 2012] expected = { 'candidate_id': self.candidate.candidate_id, 'candidate_name': self.candidate.name, 'incumbent_challenge_full': self.candidate.incumbent_challenge_full, 'party_full': self.candidate.party_full, 'total_receipts': sum(each.receipts for each in totals), 'total_disbursements': sum(each.disbursements for each in totals), 'cash_on_hand_end_period': sum(each.last_cash_on_hand_end_period for each in totals), } assert_dicts_subset(results[0], expected) assert set(each.committee_id for each in self.committees) == set( results[0]['committee_ids'])
def test_aggregates_by_committee(self): params = [ ( factories.ScheduleBByPurposeFactory, ScheduleBByPurposeView, schemas.ScheduleBByPurposeSchema, ), ( factories.ScheduleEByCandidateFactory, ScheduleEByCandidateView, schemas.ScheduleEByCandidateSchema, ), ( factories.CommunicationCostByCandidateFactory, CommunicationCostByCandidateView, schemas.CommunicationCostByCandidateSchema, ), ( factories.ElectioneeringByCandidateFactory, ElectioneeringByCandidateView, schemas.ElectioneeringByCandidateSchema, ), ] for factory, resource, schema in params: aggregate = factory( committee_id=self.committee.committee_id, cycle=self.committee.cycle, ) results = self._results( api.url_for( resource, committee_id=self.committee.committee_id, cycle=2012, )) self.assertEqual(len(results), 1) self.assertEqual(results[0], schema().dump(aggregate).data)
def test_elections_winner(self): [ factories.ElectionResultFactory( cand_office='H', election_yr=2012, cand_office_st='NY', cand_office_district='07', cand_id=self.candidate.candidate_id, cand_name=self.candidate.name, ) ] results = self._results( api.url_for(ElectionView, office='house', cycle=2012, state='NY', district='07')) self.assertEqual(len(results), 1) expected = { 'candidate_id': self.candidate.candidate_id, 'candidate_name': self.candidate.name, 'won': True, } self.assertDictsSubset(results[0], expected)
def test_schedule_a_pagination_bad_per_page(self): response = self.app.get( api.url_for(ScheduleAView, two_year_transaction_period=2018, per_page=999)) self.assertEqual(response.status_code, 422)
def test_schedule_a_pagination_with_null_sort_column_parameter(self): response = self.app.get( api.url_for(ScheduleAView, sort='contribution_receipt_date', last_contribution_receipt_date='null')) self.assertEqual(response.status_code, 422)
def test_schedule_a_filter_multi_start_with(self): [factories.ScheduleAFactory(contributor_zip=1296789)] results = self._results( api.url_for(ScheduleAView, contributor_zip=96789, **self.kwargs)) self.assertEqual(len(results), 0)
def test_schedule_a_invalid_zip(self): response = self.app.get( api.url_for(ScheduleAView, contributor_zip='96%', cycle=2018)) self.assertEqual(response.status_code, 400)
def test_pagination_bad_per_page(self): response = self.app.get(api.url_for(ScheduleAView, per_page=999)) self.assertEqual(response.status_code, 422)
def test_sort_bad_column(self): response = self.app.get(api.url_for(FilingsList, sort='request_type')) self.assertEqual(response.status_code, 422)
def test_schedule_a_sorting_bad_column(self): response = self.app.get(api.url_for(ScheduleAView, sort='bad_column')) self.assertEqual(response.status_code, 422) self.assertIn(b'Cannot sort on value', response.data)
def test_committee_search_double_committee_id(self): committees = [factories.CommitteeFactory() for _ in range(2)] ids = [each.committee_id for each in committees] response = self._response(api.url_for(CommitteeList, committee_id=ids)) results = response['results'] self.assertEqual(len(results), 2)