def test_hide_null_candidate_totals(self): candidates = [ factories.CandidateFactory(candidate_id='C1234'), factories.CandidateFactory(candidate_id='C5678'), ] candidateHistory = [ factories.CandidateHistoryFactory(candidate_id='C1234', two_year_period=2016, election_years=[2016], cycles=[2016],candidate_election_year=2016), factories.CandidateHistoryFactory(candidate_id='C5678', two_year_period=2016, election_years=[2016], cycles=[2016],candidate_election_year=2016) ] candidateTotals = [ factories.CandidateTotalFactory(candidate_id='C1234', is_election=False, cycle=2016), factories.CandidateTotalFactory(candidate_id='C5678', disbursements='9999.99', is_election=False, cycle=2016) ] candidateFlags = [ factories.CandidateFlagsFactory(candidate_id='C1234'), factories.CandidateFlagsFactory(candidate_id='C5678') ] tcv = candidate_aggregates.TotalsCandidateView() query, columns = sorting.sort(tcv.build_query(election_full=False), 'disbursements', model=None) self.assertEqual(len(query.all()), len(candidates)) query, columns = sorting.sort(tcv.build_query(election_full=False), 'disbursements', model=None, hide_null=True) self.assertEqual(len(query.all()), len(candidates) - 1) self.assertTrue(candidates[1].candidate_id in query.all()[0])
def test_nulls_large(self): candidates = [ factories.CandidateFactory(district='01'), factories.CandidateFactory(district='02'), factories.CandidateFactory(), ] query, columns = sorting.sort(models.Candidate.query, 'district', model=models.Candidate, nulls_large=False) self.assertEqual(query.all(), candidates[-1:] + candidates[:2]) query, columns = sorting.sort(models.Candidate.query, '-district', model=models.Candidate, nulls_large=False) self.assertEqual(query.all(), [candidates[1], candidates[0], candidates[2]])
def test_hide_null(self): candidates = [ factories.CandidateFactory(district='01'), factories.CandidateFactory(district='02'), factories.CandidateFactory(), ] query, columns = sorting.sort(models.Candidate.query, 'district', model=models.Candidate) self.assertEqual(query.all(), candidates) query, columns = sorting.sort(models.Candidate.query, 'district', model=models.Candidate, hide_null=True) self.assertEqual(query.all(), candidates[:2])
def test_hide_null_election(self): candidates = [ factories.CandidateFactory(candidate_id='C1234'), factories.CandidateFactory(candidate_id='C5678'), ] cmteFacorty = [ factories.CommitteeDetailFactory(committee_id='H1234'), factories.CommitteeDetailFactory(committee_id='H5678') ] db.session.flush() candidateHistory = [ factories.CandidateHistoryFactory(candidate_id='C1234', two_year_period=2016, state='MO', candidate_election_year=2016, candidate_inactive=False, district='01', office='S', election_years=[2016], cycles=[2016]), factories.CandidateHistoryFactory(candidate_id='C5678', candidate_election_year=2016, two_year_period=2016, state='MO', election_years=[2016], cycles=[2016], candidate_inactive=False, district='02', office='S') ] candidateCmteLinks = [ factories.CandidateCommitteeLinkFactory(committee_id='H1234', candidate_id='C1234', fec_election_year=2016,committee_designation='P'), factories.CandidateCommitteeLinkFactory(committee_id='H5678', candidate_id='C5678', fec_election_year=2016, committee_designation='P') ] cmteTotalsFactory = [ factories.CommitteeTotalsHouseSenateFactory(committee_id='H1234', cycle=2016), factories.CommitteeTotalsHouseSenateFactory(committee_id='H1234', cycle=2016, disbursements='9999.99'), factories.CommitteeTotalsHouseSenateFactory(committee_id='H5678', cycle=2016) ] electionResults = [ factories.ElectionResultFactory(cand_id='C1234', election_yr=2016, cand_office='S', cand_office_st='MO', cand_office_district='01' ), factories.ElectionResultFactory(cand_id='C5678', election_yr=2016, cand_office='S', cand_office_st='MO', cand_office_district='02') ] db.session.flush() arg_map = {} arg_map['office'] = 'senate' arg_map['cycle'] = 2016 arg_map['state'] = 'MO' #arg_map['district'] = '00' electionView = elections.ElectionView() query, columns = sorting.sort(electionView._get_records(arg_map), 'total_disbursements', model=None) #print(str(query.statement.compile(dialect=postgresql.dialect()))) self.assertEqual(len(query.all()), len(candidates)) query, columns = sorting.sort(electionView._get_records(arg_map), 'total_disbursements', model=None, hide_null=True) #Taking this assert statement out because I believe, at least how the FEC interprets null (i.e. none) primary #committees for a candidate is that they have in fact raised/spent 0.0 dollars, this can be shown as true #using the Alabama special election as an example #self.assertEqual(len(query.all()), len(candidates) - 1) self.assertTrue(candidates[1].candidate_id in query.all()[0]) self.assertEqual(query.all()[0].total_disbursements, 0.0)
def test_single_column_reverse(self): candidates = [ factories.CandidateFactory(district='01'), factories.CandidateFactory(district='02'), ] query, columns = sorting.sort(models.Candidate.query, '-district', model=models.Candidate) self.assertEqual(query.all(), candidates[::-1])
def build_committee_query(self, kwargs, committee_id): query = self.build_query(utils.extend(kwargs, {'committee_id': [committee_id]}), join=False) sort, hide_null, nulls_large = kwargs['sort'], kwargs['sort_hide_null'], kwargs['sort_nulls_large'] query, _ = sorting.sort(query, sort, model=models.ScheduleA, hide_null=hide_null, nulls_large=nulls_large) page_query = utils.fetch_seek_page(query, kwargs, self.index_column, count=-1, eager=False).results count = counts.count_estimate(query, models.db.session, threshold=5000) return page_query, count
def test_multi_column(self): candidates = [ factories.CandidateFactory(district='01', party='DEM'), factories.CandidateFactory(district='01', party='REP'), factories.CandidateFactory(district='02', party='DEM'), factories.CandidateFactory(district='02', party='REP'), ] query, columns = sorting.sort(models.Candidate.query, ['district', 'party'], model=models.Candidate) self.assertEqual(query.all(), candidates)
def build_committee_query(self, kwargs, committee_id): """Build a subquery by committee. """ query = self.build_query(_apply_options=False, **utils.extend(kwargs, {'committee_id': [committee_id]})) sort, hide_null, nulls_large = kwargs['sort'], kwargs['sort_hide_null'], kwargs['sort_nulls_large'] query, _ = sorting.sort(query, sort, model=self.model, hide_null=hide_null, nulls_large=nulls_large) page_query = utils.fetch_seek_page(query, kwargs, self.index_column, count=-1, eager=False).results count = counts.count_estimate(query, models.db.session, threshold=5000) return page_query, count
def fetch_page(query, kwargs, model=None, aliases=None, join_columns=None, clear=False, count=None, cap=100, index_column=None): check_cap(kwargs, cap) sort, hide_null, nulls_large = kwargs.get('sort'), kwargs.get('sort_hide_null'), kwargs.get('sort_nulls_large') if sort: query, _ = sorting.sort( query, sort, model=model, aliases=aliases, join_columns=join_columns, clear=clear, hide_null=hide_null, nulls_large=nulls_large, index_column=index_column, ) paginator = paginators.OffsetPaginator(query, kwargs['per_page'], count=count) return paginator.get_page(kwargs['page'])
def fetch_page(query, kwargs, model=None, aliases=None, join_columns=None, clear=False, count=None, cap=100, index_column=None, multi=False): check_cap(kwargs, cap) sort, hide_null, reverse_nulls = kwargs.get('sort'), kwargs.get('sort_hide_null'), kwargs.get('sort_reverse_nulls') if sort and multi: query, _ = sorting.multi_sort( query, sort, model=model, aliases=aliases, join_columns=join_columns, clear=clear, hide_null=hide_null, index_column=index_column ) elif sort: query, _ = sorting.sort( query, sort, model=model, aliases=aliases, join_columns=join_columns, clear=clear, hide_null=hide_null, index_column=index_column ) paginator = paginators.OffsetPaginator(query, kwargs['per_page'], count=count) return paginator.get_page(kwargs['page'])
def fetch_seek_page(query, kwargs, index_column, clear=False, count=None): model = index_column.class_ sort, hide_null = kwargs['sort'], kwargs['sort_hide_null'] query, sort_columns = sorting.sort(query, sort, model=model, clear=clear, hide_null=hide_null) sort_column = sort_columns[0] if sort_columns else None paginator = paging.SqlalchemySeekPaginator( query, kwargs['per_page'], index_column, sort_column=sort_column, count=count, ) if sort_column is not None: sort_index = kwargs['last_{0}'.format(sort_column[0].key)] else: sort_index = None return paginator.get_page(last_index=kwargs['last_index'], sort_index=sort_index)
def fetch_seek_paginator(query, kwargs, index_column, clear=False, count=None, cap=100): check_cap(kwargs, cap) model = index_column.parent.class_ sort, hide_null = kwargs.get('sort'), kwargs.get('sort_hide_null') if sort: query, sort_column = sorting.sort( query, sort, model=model, clear=clear, hide_null=hide_null ) else: sort_column = None return SeekCoalescePaginator( query, kwargs['per_page'], index_column, sort_column=sort_column, count=count, )
def fetch_seek_page(query, kwargs, index_column, clear=False, count=None, cap=100, eager=True): check_cap(kwargs, cap) model = index_column.class_ sort, hide_null, nulls_large = kwargs['sort'], kwargs['sort_hide_null'], kwargs['sort_nulls_large'] query, sort_columns = sorting.sort(query, sort, model=model, clear=clear, hide_null=hide_null, nulls_large=nulls_large) sort_column = sort_columns[0] if sort_columns else None paginator = paginators.SeekPaginator( query, kwargs['per_page'], index_column, sort_column=sort_column, count=count, ) if sort_column is not None: sort_index = kwargs['last_{0}'.format(sort_column[0].key)] else: sort_index = None return paginator.get_page(last_index=kwargs['last_index'], sort_index=sort_index, eager=eager)
def fetch_page(query, kwargs, model, clear=False, count=None): sort, hide_null = kwargs['sort'], kwargs['sort_hide_null'] query, _ = sorting.sort(query, sort, model=model, clear=clear, hide_null=hide_null) paginator = paging.SqlalchemyOffsetPaginator(query, kwargs['per_page'], count=count) return paginator.get_page(kwargs['page'])
def fetch_page(query, kwargs, model=None, clear=False): query = sorting.sort(query, kwargs['sort'], model=model, clear=clear) paginator = paging.SqlalchemyPaginator(query, kwargs['per_page']) return paginator.get_page(kwargs['page'])
def test_hide_null_election(self): candidates = [ factories.CandidateFactory(candidate_id='C1234'), factories.CandidateFactory(candidate_id='C5678'), ] cmteFacorty = [ factories.CommitteeDetailFactory(committee_id='H1234'), factories.CommitteeDetailFactory(committee_id='H5678') ] db.session.flush() candidateHistory = [ factories.CandidateHistoryFactory(candidate_id='C1234', two_year_period=2016, state='MO', candidate_inactive=False, district='01', office='S', election_years=[2016]), factories.CandidateHistoryFactory(candidate_id='C5678', two_year_period=2016, state='MO', election_years=[2016], candidate_inactive=False, district='02', office='S') ] candidateCmteLinks = [ factories.CandidateCommitteeLinkFactory(committee_id='H1234', candidate_id='C1234', fec_election_year=2016, committee_designation='P'), factories.CandidateCommitteeLinkFactory(committee_id='H5678', candidate_id='C5678', fec_election_year=2016, committee_designation='P') ] cmteTotalsFactory = [ factories.CommitteeTotalsHouseSenateFactory(committee_id='H1234', cycle=2016), factories.CommitteeTotalsHouseSenateFactory( committee_id='H1234', cycle=2016, disbursements='9999.99'), factories.CommitteeTotalsHouseSenateFactory(committee_id='H5678', cycle=2016) ] electionResults = [ factories.ElectionResultFactory(cand_id='C1234', election_yr=2016, cand_office='S', cand_office_st='MO', cand_office_district='01'), factories.ElectionResultFactory(cand_id='C5678', election_yr=2016, cand_office='S', cand_office_st='MO', cand_office_district='02') ] db.session.flush() arg_map = {} arg_map['office'] = 'senate' arg_map['cycle'] = 2016 arg_map['state'] = 'MO' #arg_map['district'] = '00' electionView = elections.ElectionView() query, columns = sorting.sort(electionView._get_records(arg_map), 'total_disbursements', model=None) #print(str(query.statement.compile(dialect=postgresql.dialect()))) self.assertEqual(len(query.all()), len(candidates)) query, columns = sorting.sort(electionView._get_records(arg_map), 'total_disbursements', model=None, hide_null=True) self.assertEqual(len(query.all()), len(candidates) - 1) self.assertTrue(candidates[0].candidate_id in query.all()[0])