예제 #1
0
파일: test_utils.py 프로젝트: 18F/openFEC
    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])
예제 #2
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]])
예제 #3
0
파일: test_utils.py 프로젝트: 18F/openFEC
 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])
예제 #4
0
파일: test_utils.py 프로젝트: 18F/openFEC
    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)
예제 #5
0
파일: test_utils.py 프로젝트: 18F/openFEC
 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])
예제 #6
0
 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
예제 #7
0
 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)
예제 #8
0
 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
예제 #9
0
파일: utils.py 프로젝트: adborden/openFEC
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'])
예제 #10
0
파일: utils.py 프로젝트: 18F/openFEC
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'])
예제 #11
0
파일: utils.py 프로젝트: EricSchles/openFEC
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)
예제 #12
0
파일: utils.py 프로젝트: 18F/openFEC
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,
    )
예제 #13
0
파일: utils.py 프로젝트: gboone/openFEC
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)
예제 #14
0
파일: utils.py 프로젝트: EricSchles/openFEC
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'])
예제 #15
0
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'])
예제 #16
0
    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])