def _check_filter(self, field, values, base_url, alt=None, **attrs): # Build fixtures factories.CommitteeFactory(**utils.extend(attrs, {field: alt})) [ factories.CommitteeFactory(**utils.extend(attrs, {field: value})) for value in values ] # Assert that exactly one record is found for each single-valued search # (e.g. field=value1) for value in values: url = '{0}?{1}={2}'.format(base_url, field, value) results = self._results(url) self.assertEqual(len(results), 1) self.assertEqual(results[0][field], value) # Assert that `len(values)` records are found for multi-valued search # (e.g. field=value1,value2...valueN) url = '{0}?{1}'.format( base_url, urllib.parse.urlencode({field: values}, doseq=True)) results = self._results(url) self.assertEqual(len(results), len(values)) for result in results: self.assertIn(result[field], values)
def setUp(self): super(TestFilterMulti, self).setUp() self.dates = [ factories.CalendarDateFactory( event_id=123, calendar_category_id=1, summary='July Quarterly Report Due', ), factories.CalendarDateFactory(event_id=321, calendar_category_id=1, summary='TX Primary Runoff'), factories.CalendarDateFactory(event_id=111, calendar_category_id=2, summary='EC Reporting Period'), factories.CalendarDateFactory(event_id=222, calendar_category_id=2, summary='IE Reporting Period'), factories.CalendarDateFactory(event_id=333, calendar_category_id=3, summary='Executive Session'), factories.CalendarDateFactory(calendar_category_id=3, summary='Missing ID'), ] self.committees = [ factories.CommitteeFactory(name='Candidate 1 for Prez', designation='P'), factories.CommitteeFactory(name='Candidate 2 for Prez', designation='P'), factories.CommitteeFactory(name='B-type committee', designation='B'), factories.CommitteeFactory(name='U-type committee', designation='U'), factories.CommitteeFactory(name='None-type committee'), ]
def setUp(self): super(TestFilter, self).setUp() self.receipts = [ factories.ScheduleAFactory(entity_type=None), factories.ScheduleAFactory(entity_type='IND'), factories.ScheduleAFactory(entity_type='CCM'), factories.ScheduleAFactory(entity_type='COM'), factories.ScheduleAFactory(entity_type='PAC'), ] self.dates = [ factories.CalendarDateFactory(event_id=123, calendar_category_id=1, summary='July Quarterly Report Due'), factories.CalendarDateFactory(event_id=321, calendar_category_id=1, summary='TX Primary Runoff'), factories.CalendarDateFactory(event_id=111, calendar_category_id=2, summary='EC Reporting Period'), factories.CalendarDateFactory(event_id=222, calendar_category_id=2, summary='IE Reporting Period'), factories.CalendarDateFactory(event_id=333, calendar_category_id=3, summary='Executive Session'), ] self.reports = [ factories.ReportsHouseSenateFactory(means_filed='e-file'), factories.ReportsHouseSenateFactory(means_filed='paper'), ] self.committees = [ factories.CommitteeFactory(designation='P'), factories.CommitteeFactory(designation='P'), factories.CommitteeFactory(designation='B'), factories.CommitteeFactory(designation='U'), ]
def test_committee_date_filters(self): [ factories.CommitteeFactory(first_file_date=datetime.date.fromisoformat('2015-01-01')), factories.CommitteeFactory(first_file_date=datetime.date.fromisoformat('2015-02-01')), factories.CommitteeFactory(first_file_date=datetime.date.fromisoformat('2015-03-01')), factories.CommitteeFactory(first_file_date=datetime.date.fromisoformat('2015-04-01')), ] results = self._results( api.url_for(CommitteeList, min_first_file_date=datetime.date.fromisoformat('2015-02-01'))) self.assertTrue( all(each['first_file_date'] >= datetime.date.fromisoformat('2015-02-01').isoformat() for each in results)) results = self._results( api.url_for(CommitteeList, max_first_file_date=datetime.date.fromisoformat('2015-02-03'))) self.assertTrue( all(each['first_file_date'] <= datetime.date.fromisoformat('2015-02-03').isoformat() for each in results)) results = self._results( api.url_for( CommitteeList, min_first_file_date=datetime.date.fromisoformat('2015-02-01'), max_first_file_date=datetime.date.fromisoformat('2015-03-01'), ) ) self.assertTrue( all( datetime.date.fromisoformat('2015-02-01').isoformat() <= each['first_file_date'] <= datetime.date.fromisoformat('2015-03-01').isoformat() for each in results ) )
def test_committee_date_filters(self): [ factories.CommitteeFactory(first_file_date=self.date_2015_01_01), factories.CommitteeFactory(first_file_date=self.date_2015_02_01), factories.CommitteeFactory(first_file_date=self.date_2015_03_01), factories.CommitteeFactory(first_file_date=self.date_2015_04_01), ] results = self._results( api.url_for(CommitteeList, min_first_file_date=self.date_2015_02_01)) self.assertTrue( all(each['first_file_date'] >= self.date_2015_02_01.isoformat() for each in results)) results = self._results( api.url_for(CommitteeList, max_first_file_date=self.date_2015_02_03)) self.assertTrue( all(each['first_file_date'] <= self.date_2015_02_03.isoformat() for each in results)) results = self._results( api.url_for( CommitteeList, min_first_file_date=self.date_2015_02_01, max_first_file_date=self.date_2015_03_01, )) self.assertTrue( all(self.date_2015_02_01.isoformat() <= each['first_file_date'] <= self.date_2015_03_01.isoformat() for each in results))
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_filter_by_candidate_id(self): candidate_id = 'ID0' candidate_committees = [ factories.CommitteeFactory(candidate_ids=[candidate_id]) for _ in range(2) ] other_committees = [factories.CommitteeFactory() for _ in range(3)] # noqa response = self._response(api.url_for(CommitteeList, candidate_id=candidate_id)) self.assertEqual(len(response['results']), len(candidate_committees))
def setUp(self): super(TestFilterOverlap, self).setUp() self.committees = [ factories.CommitteeFactory(committee_id='C001', sponsor_candidate_ids=['S001']), factories.CommitteeFactory(committee_id='C002', sponsor_candidate_ids=['H001']), ]
def test_committee_sort_default(self): committees = [ factories.CommitteeFactory(name='Zartlet for America'), factories.CommitteeFactory(name='Bartlet for America'), ] committee_ids = [each.committee_id for each in committees] results = self._results(api.url_for(CommitteeList)) self.assertEqual([each['committee_id'] for each in results], committee_ids[::-1])
def test_treasurer_filter(self): committees = [ factories.CommitteeFactory(treasurer_text=sa.func.to_tsvector('uncle pennybags')), factories.CommitteeFactory(treasurer_text=sa.func.to_tsvector('eve moneypenny')), ] results = self._results(api.url_for(CommitteeList, treasurer_name='moneypenny')) assert len(results) == 1 assert results[0]['committee_id'] == committees[1].committee_id
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_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), ) results = self._results(api.url_for(CommitteeList, q='america')) 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_committee_year_filter_skips_null_first_file_date(self): # Build fixtures dates = [datetime.date.fromisoformat('2012-01-01'), datetime.date.fromisoformat('2015-01-01')] [ factories.CommitteeFactory(first_file_date=None, last_file_date=None), factories.CommitteeFactory(first_file_date=dates[0], last_file_date=None), factories.CommitteeFactory(first_file_date=None, last_file_date=dates[1]), factories.CommitteeFactory(first_file_date=dates[0], last_file_date=dates[1]), ] # Check committee list results results = self._results(api.url_for(CommitteeList, year=2013)) self.assertEqual(len(results), 2) for each in results: self.assertIsNotNone(each['first_file_date'])
def test_views(self, upload_s3): committee = factories.CommitteeFactory(committee_type='H') committee_id = committee.committee_id factories.CommitteeHistoryFactory(committee_id=committee_id, committee_type='H') filing = factories.FilingsFactory(committee_id=committee_id) efiling = factories.EFilingsFactory(committee_id=committee_id, receipt_date=datetime.datetime( 2012, 1, 1)) basef3pfiling = factories.BaseF3PFilingFactory( committee_id=committee_id, receipt_date=datetime.date(2012, 1, 1)) db.session.commit() for view in tasks.RESOURCE_WHITELIST: if view.endpoint in [ 'reportsview', 'efilingsummaryview', ]: url = api.url_for(view, committee_type=committee.committee_type) elif view.endpoint in [ 'filingsview', 'committeereportsview', ]: url = api.url_for(view, committee_id=committee.committee_id) else: url = api.url_for(view) tasks.export_query(url, b'')
def test_views(self, make_bundle): committee = factories.CommitteeFactory(committee_type='H') committee_id = committee.committee_id factories.CommitteeHistoryFactory(committee_id=committee_id, committee_type='H') filing = factories.FilingsFactory(committee_id=committee_id) # noqa efiling = factories.EFilingsFactory( # noqa committee_id=committee_id, receipt_date=datetime.datetime(2012, 1, 1)) basef3pfiling = factories.BaseF3PFilingFactory( # noqa committee_id=committee_id, receipt_date=datetime.date(2012, 1, 1)) db.session.commit() # these are the major downloadable resources, we may want to add more later RESOURCE_WHITELIST = { aggregates.ScheduleABySizeView, aggregates.ScheduleAByStateView, aggregates.ScheduleAByZipView, aggregates.ScheduleAByEmployerView, aggregates.ScheduleAByOccupationView, aggregates.ScheduleBByRecipientView, aggregates.ScheduleBByRecipientIDView, aggregates.ScheduleBByPurposeView, candidate_aggregates.TotalsCandidateView, candidates.CandidateList, committees.CommitteeList, costs.CommunicationCostView, costs.ElectioneeringView, filings.EFilingsView, filings.FilingsList, filings.FilingsView, reports.ReportsView, reports.CommitteeReportsView, reports.EFilingHouseSenateSummaryView, reports.EFilingPresidentialSummaryView, reports.EFilingPacPartySummaryView, sched_a.ScheduleAView, sched_a.ScheduleAEfileView, sched_b.ScheduleBView, sched_b.ScheduleBEfileView, sched_d.ScheduleDView, sched_e.ScheduleEView, sched_e.ScheduleEEfileView, sched_f.ScheduleFView, } for view in RESOURCE_WHITELIST: if view.endpoint in ['reportsview']: url = api.url_for(view, committee_type=committee.committee_type) elif view.endpoint in [ 'filingsview', 'committeereportsview', ]: url = api.url_for(view, committee_id=committee.committee_id) else: url = api.url_for(view) tasks.export_query(url, base64.b64encode(b'').decode('UTF-8'))
def test_committee_party(self): factories.CommitteeFactory( party='REP', party_full='Republican Party', ) response = self._results(api.url_for(CommitteeList, party='REP')) self.assertEqual(response[0]['party'], 'REP') self.assertEqual(response[0]['party_full'], 'Republican Party')
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_filter_by_candidate_ids(self): candidate_ids = ['id0', 'id1'] candidate1_committees = [ factories.CommitteeFactory(candidate_ids=[candidate_ids[0]]) for _ in range(2) ] candidate2_committees = [ factories.CommitteeFactory(candidate_ids=[candidate_ids[1]]) for _ in range(2) ] other_committees = [factories.CommitteeFactory() for _ in range(3)] # noqa response = self._response( api.url_for(CommitteeList, candidate_id=candidate_ids)) self.assertEqual( len(response['results']), len(candidate1_committees) + len(candidate2_committees))
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_filter_by_sponsor_candidate_ids(self): sponsor_candidate_ids1 = ['H001'] sponsor_candidate_ids2 = ['S001'] factories.CommitteeFactory( sponsor_candidate_ids=sponsor_candidate_ids1) factories.CommitteeFactory( sponsor_candidate_ids=sponsor_candidate_ids2) results = self._results( api.url_for(CommitteeList, sponsor_candidate_id='H001')) assert len(results) == 1 assert results[0]['sponsor_candidate_ids'] == sponsor_candidate_ids1 results = self._results( api.url_for(CommitteeList, sponsor_candidate_id='-H001')) assert len(results) == 1 assert results[0]['sponsor_candidate_ids'] == sponsor_candidate_ids2
def test_candidates_by_com(self): committee = factories.CommitteeFactory() candidate = factories.CandidateFactory() db.session.flush() factories.CandidateCommitteeLinkFactory( candidate_id=candidate.candidate_id, committee_id=committee.committee_id, ) results = self._results(api.url_for(CandidateView, committee_id=committee.committee_id)) self.assertEquals(1, len(results))
def test_committee_by_cand_filter(self): committee = factories.CommitteeFactory(designation='P') candidate = factories.CandidateFactory() db.session.flush() factories.CandidateCommitteeLinkFactory( candidate_id=candidate.candidate_id, committee_id=committee.committee_id, ) results = self._results( api.url_for(CommitteeView, candidate_id=candidate.candidate_id, designation='P') ) self.assertEqual(1, len(results))
def test_totals_house_senate(self): committee = factories.CommitteeFactory(committee_type='H') committee_id = committee.committee_id factories.CommitteeHistoryFactory(committee_id=committee_id, committee_type='H') [ factories.TotalsHouseSenateFactory(committee_id=committee_id, cycle=2008), factories.TotalsHouseSenateFactory(committee_id=committee_id, cycle=2012), ] response = self._results(api.url_for(TotalsView, committee_id=committee_id)) self.assertEqual(len(response), 2) self.assertEqual(response[0]['cycle'], 2012) self.assertEqual(response[1]['cycle'], 2008)
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_reports_by_committee_id(self): committee = factories.CommitteeFactory(committee_type='P') committee_id = committee.committee_id factories.CommitteeHistoryFactory( committee_id=committee_id, committee_type='P', ) committee_report = factories.ReportsPresidentialFactory( committee_id=committee_id) other_report = factories.ReportsPresidentialFactory() results = self._results( api.url_for(CommitteeReportsView, committee_id=committee_id)) self._check_committee_ids(results, [committee_report], [other_report])
def test_filters_generic(self): committee = factories.CommitteeFactory() committee_id = committee.committee_id base_url = api.url_for(CommitteeView, committee_id=committee_id) self._check_filter('designation', ['B', 'P'], base_url, committee_id=committee_id) self._check_filter('organization_type', ['M', 'T'], base_url, committee_id=committee_id) self._check_filter('committee_type', ['H', 'X'], base_url, committee_id=committee_id)
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_committee_list_fields(self): committee = factories.CommitteeFactory( first_file_date=datetime.date(1982, 12, 31), committee_type='P', treasurer_name='Robert J. Lipshutz', party='DEM', ) response = self._response(api.url_for(CommitteeView, committee_id=committee.committee_id)) result = response['results'][0] # main fields # original registration date doesn't make sense in this example, need to look into this more 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)
def _check_filter(self, field, values, alt=None, **attrs): # Build fixtures factories.CommitteeFactory(**utils.extend(attrs, {field: alt})) [ factories.CommitteeFactory(**utils.extend(attrs, {field: value})) for value in values ] # Assert that exactly one record is found for each single-valued search # (e.g. field=value1) for value in values: url = api.url_for(CommitteeList, **{field: value}) results = self._results(url) self.assertEqual(len(results), 1) self.assertEqual(results[0][field], value) # Assert that `len(values)` records are found for multi-valued search # (e.g. field=value1,value2...valueN) url = api.url_for(CommitteeList, **{field: values}) results = self._results(url) self.assertEqual(len(results), len(values)) for result in results: self.assertIn(result[field], values)
def test_case_insensitivity(self): committee = factories.CommitteeFactory() committee_id = committee.committee_id factories.CommitteeHistoryFactory(committee_id=committee_id, ) 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.lower(), )) for result in results: self.assertEqual(result["committee_id"], committee_id)