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_filter_individual_sched_a(self): individuals = [ factories.ScheduleAFactory(receipt_type='15J'), factories.ScheduleAFactory(line_number='12', contribution_receipt_amount=150), ] earmarks = [ factories.ScheduleAFactory(), factories.ScheduleAFactory( line_number='12', contribution_receipt_amount=150, memo_text='earmark', memo_code='X', ), ] is_individual = sa.func.is_individual( ScheduleA.contribution_receipt_amount, ScheduleA.receipt_type, ScheduleA.line_number, ScheduleA.memo_code, ScheduleA.memo_text, ) rows = ScheduleA.query.all() self.assertEqual(rows, individuals + earmarks) rows = ScheduleA.query.filter(is_individual).all() self.assertEqual(rows, individuals)
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_schedule_a_filter_zip(self): [ factories.ScheduleAFactory(contributor_zip=96789), factories.ScheduleAFactory(contributor_zip=9678912), factories.ScheduleAFactory(contributor_zip=967891234), factories.ScheduleAFactory(contributor_zip='M4C 1M7') ] results = self._results( api.url_for(ScheduleAView, contributor_zip=967893405, **self.kwargs)) self.assertEqual(len(results), 3) results = self._results( api.url_for(ScheduleAView, contributor_zip='M4C 1M55', **self.kwargs)) self.assertEqual(len(results), 1) contributor_zips = ['M4C 1M5555', 96789] results = self._results( api.url_for(ScheduleAView, contributor_zip=contributor_zips, **self.kwargs)) self.assertEqual(len(results), 4)
def test_schedule_a_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) # invalid line_number testing for sched_b response = self.app.get( api.url_for(ScheduleBView, line_number='f3x21', **self.kwargs)) self.assertEqual(response.status_code, 400) self.assertIn(b'Invalid line_number', response.data) # invalid line_number testing for sched_a response = self.app.get( api.url_for(ScheduleAView, line_number='f3x16', **self.kwargs)) self.assertEqual(response.status_code, 400) self.assertIn(b'Invalid line_number', response.data)
def test_schedule_a_amount(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, two_year_transaction_period=2016)) self.assertTrue( all(each['contribution_receipt_amount'] >= 100 for each in results)) results = self._results( api.url_for(ScheduleAView, max_amount=150, two_year_transaction_period=2016)) 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, two_year_transaction_period=2016)) self.assertTrue( all(100 <= each['contribution_receipt_amount'] <= 150 for each in results))
def setUp(self): super(TestFilterOther, 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'), ]
def test_schedule_a_multiple_two_year_transaction_period(self): """ testing schedule_a api can take multiple cycles now """ receipts = [ factories.ScheduleAFactory(report_year=2014, contribution_receipt_date=datetime.date( 2014, 1, 1), two_year_transaction_period=2014, committee_id='C001'), factories.ScheduleAFactory(report_year=2016, contribution_receipt_date=datetime.date( 2016, 1, 1), two_year_transaction_period=2016, committee_id='C001'), factories.ScheduleAFactory(report_year=2018, contribution_receipt_date=datetime.date( 2018, 1, 1), two_year_transaction_period=2018, committee_id='C001'), ] response = self._response( api.url_for( ScheduleAView, two_year_transaction_period=[2016, 2018], committee_id='C001', )) self.assertEqual(len(response['results']), 2)
def setUp(self): super(TestFilter, self).setUp() self.receipts = [ factories.ScheduleAFactory(entity_type='IND'), factories.ScheduleAFactory(entity_type='CCM'), factories.ScheduleAFactory(entity_type='COM'), factories.ScheduleAFactory(entity_type='PAC'), ]
def test_sched_a_recipient_committee_type_filter(self): [ factories.ScheduleAFactory(recipient_committee_type='S'), factories.ScheduleAFactory(recipient_committee_type='S'), factories.ScheduleAFactory(recipient_committee_type='P'), ] results = self._results(api.url_for(ScheduleAView, recipient_committee_type='S', **self.kwargs)) self.assertEqual(len(results), 2)
def test_filter_case_insensitive(self): [ factories.ScheduleAFactory(contributor_city='NEW YORK'), factories.ScheduleAFactory(contributor_city='DES MOINES'), ] results = self._results(api.url_for(ScheduleAView, contributor_city='new york', **self.kwargs)) self.assertEqual(len(results), 1) self.assertEqual(results[0]['contributor_city'], 'NEW YORK')
def test_filter(self): [ factories.ScheduleAFactory(contributor_state='NY'), factories.ScheduleAFactory(contributor_state='CA'), ] results = self._results(api.url_for(ScheduleAView, contributor_state='CA', **self.kwargs)) self.assertEqual(len(results), 1) self.assertEqual(results[0]['contributor_state'], 'CA')
def test_filter_zip(self): [ factories.ScheduleAFactory(contributor_zip=96789), factories.ScheduleAFactory(contributor_zip=66111) ] results = self._results(api.url_for(ScheduleAView, contributor_zip=96789, **self.kwargs)) self.assertEqual(len(results), 1) self.assertEqual(results[0]['contributor_zip'], '96789')
def test_image_number(self): image_number = '12345' [ factories.ScheduleAFactory(), factories.ScheduleAFactory(image_number=image_number), ] results = self._results(api.url_for(ScheduleAView, image_number=image_number, **self.kwargs)) self.assertEqual(len(results), 1) self.assertEqual(results[0]['image_number'], image_number)
def test_filter_sched_a_zip(self): [ factories.ScheduleAFactory(contributor_zip=96789), factories.ScheduleAFactory(contributor_zip=9678912), factories.ScheduleAFactory(contributor_zip=967891234) ] results = self._results( api.url_for(ScheduleAView, contributor_zip=96789, **self.kwargs)) self.assertEqual(len(results), 3)
def test_sorting(self): [ factories.ScheduleAFactory(report_year=2014, contributor_receipt_date=datetime.datetime(2014, 1, 1)), factories.ScheduleAFactory(report_year=2012, contributor_receipt_date=datetime.datetime(2012, 1, 1)), factories.ScheduleAFactory(report_year=1986, contributor_receipt_date=datetime.datetime(1986, 1, 1)), ] db.session.flush() response = self._response(api.url_for(ScheduleAView, sort='contributor_receipt_date')) self.assertEqual( [each['report_year'] for each in response['results']], [2012, 2014] )
def test_schedule_a_null_pagination_with_null_sort_column_values_descending( 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[9::-1] reversed_from_bottom_to_middle = filings[-1:14:-1] 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'], last_contribution_receipt_date=page1[-1] ['contribution_receipt_date'], 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[14:9:-1]], ) 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[14:9:-1] ])
def test_amount_sched_a(self): [ factories.ScheduleAFactory(contributor_receipt_amount=50), factories.ScheduleAFactory(contributor_receipt_amount=100), factories.ScheduleAFactory(contributor_receipt_amount=150), factories.ScheduleAFactory(contributor_receipt_amount=200), ] results = self._results(api.url_for(ScheduleAView, min_amount=100)) self.assertTrue(all(each['contributor_receipt_amount'] >= 100 for each in results)) results = self._results(api.url_for(ScheduleAView, max_amount=150)) self.assertTrue(all(each['contributor_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['contributor_receipt_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_two_year_transaction_period_limits_results_per_cycle(self): receipts = [ factories.ScheduleAFactory(report_year=2014, contribution_receipt_date=datetime.date( 2014, 1, 1), two_year_transaction_period=2014), factories.ScheduleAFactory(report_year=2012, contribution_receipt_date=datetime.date( 2012, 1, 1), two_year_transaction_period=2012), ] response = self._response( api.url_for(ScheduleAView, two_year_transaction_period=2014)) self.assertEqual(len(response['results']), 1)
def test_two_year_transaction_period_default_supplied_automatically(self): receipts = [ factories.ScheduleAFactory(report_year=2016, contribution_receipt_date=datetime.date( 2016, 1, 1), two_year_transaction_period=2016), factories.ScheduleAFactory(report_year=2018, contribution_receipt_date=datetime.date( 2018, 1, 1), two_year_transaction_period=2018), ] response = self._response(api.url_for(ScheduleAView)) self.assertEqual(len(response['results']), 1)
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 _check_update_aggregate_create(self, item_key, total_key, total_model, value): filing = factories.ScheduleAFactory(**{ 'report_year': 2015, 'committee_id': 'C12345', 'contribution_receipt_amount': 538, 'receipt_type': '15J', item_key: value, }) db.session.flush() db.session.execute('select update_aggregates()') rows = total_model.query.filter_by(**{ 'cycle': 2016, 'committee_id': 'C12345', total_key: value, }).all() self.assertEqual(len(rows), 1) self.assertEqual(rows[0].total, 538) self.assertEqual(rows[0].count, 1) filing.contribution_receipt_amount = 53 db.session.add(filing) db.session.flush() db.session.execute('select update_aggregates()') db.session.refresh(rows[0]) self.assertEqual(rows[0].total, 53) self.assertEqual(rows[0].count, 1)
def test_schedule_a_filter_fulltext_employer_and(self): employers = ['Test&Test', 'Test & Test', 'Test& Test', 'Test &Test'] [ factories.ScheduleAFactory(contributor_employer=employer) for employer in employers ] results = self._results( api.url_for(ScheduleAView, contributor_employer='Test&Test', **self.kwargs)) self.assertIn(results[0]['contributor_employer'], employers) results = self._results( api.url_for(ScheduleAView, contributor_employer='Test & Test', **self.kwargs)) self.assertIn(results[0]['contributor_employer'], employers) results = self._results( api.url_for(ScheduleAView, contributor_employer='Test& Test', **self.kwargs)) self.assertIn(results[0]['contributor_employer'], employers) results = self._results( api.url_for(ScheduleAView, contributor_employer='Test &Test', **self.kwargs)) self.assertIn(results[0]['contributor_employer'], employers)
def test_update_aggregate_size_create(self): filing = factories.ScheduleAFactory( report_year=2015, committee_id='C12345', contributor_receipt_amount=538, ) db.session.flush() db.session.execute('select update_aggregates()') db.session.execute( 'refresh materialized view ofec_sched_a_aggregate_size_merged_mv') rows = models.ScheduleABySize.query.filter_by( cycle=2016, committee_id='C12345', size=500, ).all() self.assertEqual(len(rows), 1) self.assertEqual(rows[0].total, 538) self.assertEqual(rows[0].count, 1) filing.contributor_receipt_amount = 53 db.session.add(filing) db.session.flush() db.session.execute('select update_aggregates()') db.session.execute( 'refresh materialized view ofec_sched_a_aggregate_size_merged_mv') db.session.refresh(rows[0]) self.assertEqual(rows[0].total, 0) self.assertEqual(rows[0].count, 0)
def test_filter_fulltext(self): names = ['David Koch', 'George Soros'] filings = [ factories.ScheduleAFactory(contributor_name=name) for name in names ] results = self._results(api.url_for(ScheduleAView, contributor_name='soros', **self.kwargs)) self.assertEqual(len(results), 1) self.assertEqual(results[0]['contributor_name'], 'George Soros')
def test_filter_fulltext_occupation(self): occupations = ['Attorney at Law', 'Doctor of Philosophy'] filings = [ factories.ScheduleAFactory(contributor_occupation=occupation) for occupation in occupations ] results = self._results(api.url_for(ScheduleAView, contributor_occupation='doctor', **self.kwargs)) self.assertEqual(len(results), 1) self.assertEqual(results[0]['contributor_occupation'], 'Doctor of Philosophy')
def test_filter_fulltext_employer(self): employers = ['Acme Corporation', 'Vandelay Industries'] filings = [ factories.ScheduleAFactory(contributor_employer=employer) for employer in employers ] results = self._results(api.url_for(ScheduleAView, contributor_employer='vandelay', **self.kwargs)) self.assertEqual(len(results), 1) self.assertEqual(results[0]['contributor_employer'], 'Vandelay Industries')
def test_pagination_with_null_sort_column_values(self): filings = [ factories.ScheduleAFactory(contribution_receipt_date=None) for _ in range(5) ] filings = filings + [ factories.ScheduleAFactory( contribution_receipt_date=datetime.date(2016, 1, 1) ) for _ in range(25) ] page1 = self._results(api.url_for( ScheduleAView, sort='contribution_receipt_date', **self.kwargs )) self.assertEqual(len(page1), 20) self.assertEqual( [int(each['sub_id']) for each in page1], [each.sub_id for each in filings[5:25]], ) 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 filings[5:25]] ) page2 = self._results( api.url_for( ScheduleAView, last_index=page1[-1]['sub_id'], 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[25:]], ) 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[25:]] )
def test_use_actual_counts_under_threshold(self, get_query_plan_mock): 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 ), ] query = db.session.query(models.ScheduleA) # Estimated rows == 200 get_query_plan_mock.return_value = [('Seq Scan on fec_fitem_sched_a (cost=0.00..10.60 rows=200 width=1289)',)] count, estimate = counts.get_count(query, db.session) self.assertEqual(count, 2) self.assertEqual(estimate, False)