Esempio n. 1
0
    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])
Esempio n. 2
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])
Esempio n. 3
0
 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])
Esempio n. 4
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_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)
Esempio n. 5
0
    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'), ('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'])
Esempio n. 6
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)
Esempio n. 7
0
 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_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_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},
     )
Esempio n. 10
0
 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)
Esempio n. 11
0
 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]],
     )
Esempio n. 12
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]])
Esempio n. 13
0
 def test_download_too_big(self, export, get_cached):
     get_cached.return_value = None
     [factories.CandidateFactory() for _ in range(5)]
     db.session.commit()
     res = self.client.post_json(
         api.url_for(resource.DownloadView, path='candidates'), expect_errors=True,
     )
     assert res.status_code == 403
     assert not export.delay.called
Esempio n. 14
0
 def test_full_text_search_with_whitespace(self):
     candidate = factories.CandidateFactory(name='Josiah Bartlet')
     factories.CandidateSearchFactory(
         id=candidate.candidate_id, fulltxt=sa.func.to_tsvector('Josiah Bartlet'),
     )
     rest.db.session.flush()
     results = self._results(api.url_for(CandidateList, q='bartlet josiah'))
     self.assertEqual(len(results), 1)
     self.assertIn('josiah', results[0]['name'].lower())
Esempio n. 15
0
 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))
Esempio n. 16
0
 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))
Esempio n. 17
0
 def test_candidate_sort_nulls_last(self):
     """
     Nulls will sort last by default when sorting ascending -
     sort_nulls_last forces nulls to the bottom for descending sort
     """
     candidates = [
         factories.CandidateFactory(candidate_id='1'),
         factories.CandidateFactory(candidate_id='2', candidate_status='P'),
         factories.CandidateFactory(candidate_id='3', candidate_status='C'),
     ]
     candidate_ids = [each.candidate_id for each in candidates]
     results = self._results(
         api.url_for(CandidateList, sort='candidate_status', sort_nulls_last=True)
     )
     self.assertEqual(
         [each['candidate_id'] for each in results], candidate_ids[::-1]
     )
     results = self._results(
         api.url_for(CandidateList, sort='-candidate_status', sort_nulls_last=True)
     )
     self.assertEqual([each['candidate_id'] for each in results], ['2', '3', '1'])
Esempio n. 18
0
 def test_committees_by_candidate_count(self):
     committee = factories.CommitteeFactory()
     candidate = factories.CandidateFactory()
     db.session.flush()
     [
         factories.CandidateCommitteeLinkFactory(
             candidate_id=candidate.candidate_id,
             committee_id=committee.committee_id,
         ),
         factories.CandidateCommitteeLinkFactory(
             candidate_id=candidate.candidate_id,
             committee_id=committee.committee_id,
         ),
     ]
     response = self._response(api.url_for(CommitteeView, candidate_id=candidate.candidate_id))
     self.assertEqual(response['pagination']['count'], 1)
     self.assertEqual(len(response['results']), 1)
Esempio n. 19
0
    def test_committees_by_cand_id(self):
        committees = [factories.CommitteeFactory() for _ in range(3)]
        candidate = factories.CandidateFactory()
        db.session.flush()
        [
            factories.CandidateCommitteeLinkFactory(
                candidate_id=candidate.candidate_id,
                committee_id=committee.committee_id,
            )
            for committee in committees
        ]
        results = self._results(api.url_for(CommitteeView, candidate_id=candidate.candidate_id))

        self.assertEqual(
            set((each['committee_id'] for each in results)),
            set((each.committee_id for each in committees)),
        )
Esempio n. 20
0
 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,
     )
Esempio n. 21
0
 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)
Esempio n. 22
0
 def test_per_page_param(self):
     [factories.CandidateFactory() for _ in range(20)]
     results = self._results(api.url_for(CandidateList, per_page=5))
     self.assertEquals(len(results), 5)
Esempio n. 23
0
 def test_per_page_defaults_to_20(self):
     [factories.CandidateFactory() for _ in range(40)]
     results = self._results(api.url_for(CandidateList))
     self.assertEquals(len(results), 20)
Esempio n. 24
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],
                                              cycles=[2016]),
            factories.CandidateHistoryFactory(candidate_id='C5678',
                                              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)
        self.assertEqual(len(query.all()), len(candidates) - 1)

        self.assertTrue(candidates[0].candidate_id in query.all()[0])