Ejemplo n.º 1
0
    def test_sort_by_candidate_id(self):

        factories.CandidateHistoryFactory(
            candidate_id='S001',
            name='Robert Ritchie',
            two_year_period=2012,
            office='S',
            state='NY',
        )
        factories.CandidateHistoryFactory(
            candidate_id='S002',
            name='FARLEY Ritchie',
            two_year_period=2012,
            office='S',
            state='NY',
        )
        factories.CandidateHistoryFactory(
            candidate_id='S003',
            name='Robert Ritchie',
            election_years=[2012],
            two_year_period=2012,
            office='S',
            state='NY',
        )

        factories.CandidateElectionFactory(candidate_id='S001',
                                           cand_election_year=2012)
        factories.CandidateElectionFactory(candidate_id='S002',
                                           cand_election_year=2012)
        factories.CandidateElectionFactory(candidate_id='S003',
                                           cand_election_year=2012)

        factories.ElectioneeringByCandidateFactory(
            cycle=2012,
            candidate_id='S001',
            total=700,
            count=5,
        ),
        factories.ElectioneeringByCandidateFactory(
            cycle=2012,
            candidate_id='S002',
            total=500,
            count=3,
        ),
        factories.ElectioneeringByCandidateFactory(
            cycle=2012,
            candidate_id='S003',
            total=100,
            count=1,
        ),
        response = self._results(
            api.url_for(ElectioneeringByCandidateView,
                        sort='-candidate_id',
                        office='senate',
                        state='NY',
                        cycle=2012))
        self.assertEqual(len(response), 3)
        self.assertEqual(response[0]['candidate_id'], 'S003')
        self.assertEqual(response[1]['candidate_id'], 'S002')
        self.assertEqual(response[2]['candidate_id'], 'S001')
Ejemplo n.º 2
0
    def test_election_full(self):
        # When election_full=true, return all two_year_periods with that candidate_election_year
        candidate = factories.CandidateDetailFactory(candidate_id='H001')
        first_two_year_period = factories.CandidateHistoryFactory(
            candidate_id=candidate.candidate_id,
            two_year_period=2018,
            candidate_election_year=2020,
        )
        second_two_year_period = factories.CandidateHistoryFactory(
            candidate_id=candidate.candidate_id,
            two_year_period=2020,
            candidate_election_year=2020,
        )
        db.session.flush()
        # Link
        factories.CandidateCommitteeLinkFactory(
            candidate_id=candidate.candidate_id,
            fec_election_year=2018,
            committee_type='H',
            election_yr_to_be_included=2020,
        )

        # test election_full='false'
        results_false = self._results(
            api.url_for(
                CandidateHistoryView,
                candidate_id=candidate.candidate_id,
                cycle=2018,
                election_full='false',
            ))
        assert len(results_false) == 1
        assert results_false[0][
            'candidate_id'] == first_two_year_period.candidate_id
        assert results_false[0][
            'two_year_period'] == first_two_year_period.two_year_period
        assert results_false[0][
            'candidate_election_year'] == first_two_year_period.candidate_election_year

        # test election_full='true'
        results_true = self._results(
            api.url_for(
                CandidateHistoryView,
                candidate_id=candidate.candidate_id,
                cycle=2020,
                election_full='true',
            ))
        assert len(results_true) == 2
        assert results_true[0][
            'candidate_id'] == second_two_year_period.candidate_id
        assert results_true[0][
            'two_year_period'] == second_two_year_period.two_year_period
        assert results_true[0][
            'candidate_election_year'] == second_two_year_period.candidate_election_year
        # Default sort is two_year_period descending
        assert results_true[1][
            'candidate_id'] == first_two_year_period.candidate_id
        assert results_true[1][
            'two_year_period'] == first_two_year_period.two_year_period
        assert results_true[1][
            'candidate_election_year'] == first_two_year_period.candidate_election_year
Ejemplo n.º 3
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])
Ejemplo n.º 4
0
    def test_fields(self):

        factories.CandidateHistoryFactory(candidate_id='S01',
                                          two_year_period=2018,
                                          candidate_election_year=2024),
        factories.CandidateHistoryFactory(candidate_id='S01',
                                          two_year_period=2020,
                                          candidate_election_year=2024),

        factories.ElectioneeringByCandidateFactory(candidate_id='S01',
                                                   total=300,
                                                   cycle=2018,
                                                   committee_id='C01'),
        factories.ElectioneeringByCandidateFactory(candidate_id='S01',
                                                   total=200,
                                                   cycle=2020,
                                                   committee_id='C02'),

        results = self._results(
            api.url_for(ECTotalsByCandidateView,
                        candidate_id='S01',
                        election_full=False))
        assert len(results) == 2

        results = self._results(
            api.url_for(ECTotalsByCandidateView,
                        candidate_id='S01',
                        election_full=True))
        assert len(results) == 1
        assert results[0]['total'] == 500
Ejemplo n.º 5
0
    def test_fields(self):

        factories.CandidateHistoryFactory(candidate_id='P01',
                                          two_year_period=2014,
                                          candidate_election_year=2016),
        factories.CandidateHistoryFactory(candidate_id='P01',
                                          two_year_period=2016,
                                          candidate_election_year=2016),

        factories.ScheduleEByCandidateFactory(
            candidate_id='P01',
            total=100,
            cycle=2012,
            committee_id='C01',
            support_oppose_indicator='S',
        ),
        factories.ScheduleEByCandidateFactory(
            candidate_id='P01',
            total=200,
            cycle=2014,
            committee_id='C02',
            support_oppose_indicator='S',
        ),
        factories.ScheduleEByCandidateFactory(
            candidate_id='P01',
            total=300,
            cycle=2014,
            committee_id='C02',
            support_oppose_indicator='O',
        ),
        factories.ScheduleEByCandidateFactory(
            candidate_id='P01',
            total=400,
            cycle=2016,
            committee_id='C03',
            support_oppose_indicator='S',
        ),
        factories.ScheduleEByCandidateFactory(
            candidate_id='P01',
            total=500,
            cycle=2016,
            committee_id='C03',
            support_oppose_indicator='O',
        ),

        results = self._results(
            api.url_for(IETotalsByCandidateView,
                        candidate_id='P01',
                        election_full=False))
        assert len(results) == 4

        results = self._results(
            api.url_for(IETotalsByCandidateView,
                        candidate_id='P01',
                        election_full=True))
        assert len(results) == 2
        assert results[0]['total'] == 800
        assert results[1]['total'] == 600
Ejemplo n.º 6
0
    def test_sort_by_committee_name(self):

        factories.CommitteeHistoryFactory(name='Warner for America',
                                          cycle=2010,
                                          committee_id='C005')
        factories.CommitteeHistoryFactory(name='Ritche for America',
                                          cycle=2010,
                                          committee_id='C006')

        factories.CandidateHistoryFactory(
            candidate_id='S005',
            name='WARNER, MARK',
            election_years=[2010],
            two_year_period=2010,
            office='S',
            state='NY',
        )
        factories.CandidateHistoryFactory(
            candidate_id='S006',
            name='BALDWIN, ALISSA',
            election_years=[2010],
            two_year_period=2010,
            office='S',
            state='NY',
        )

        factories.CandidateElectionFactory(candidate_id='S005',
                                           cand_election_year=2010)
        factories.CandidateElectionFactory(candidate_id='S006',
                                           cand_election_year=2010)

        factories.ScheduleEByCandidateFactory(
            total=50000,
            count=10,
            cycle=2010,
            candidate_id='S005',
            support_oppose_indicator='S',
            committee_id='C005',
        ),
        factories.ScheduleEByCandidateFactory(
            total=10000,
            count=5,
            cycle=2010,
            candidate_id='S005',
            support_oppose_indicator='S',
            committee_id='C006',
        ),

        response = self._results(
            api.url_for(ScheduleEByCandidateView,
                        sort='-committee_name',
                        office='senate',
                        cycle=2010,
                        state='NY'))
        self.assertEqual(len(response), 2)
        self.assertEqual(response[0]['committee_name'], 'Warner for America')
        self.assertEqual(response[1]['committee_name'], 'Ritche for America')
Ejemplo n.º 7
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)
    def test_history(self):
        history_2012 = factories.CandidateHistoryFactory(two_year_period=2012)
        history_2008 = factories.CandidateHistoryFactory(two_year_period=2008, candidate_id=history_2012.candidate_id)
        results = self._results(
            api.url_for(CandidateHistoryView, candidate_id=history_2012.candidate_id)
        )

        assert results[0]['candidate_id'] == history_2012.candidate_id
        assert results[1]['candidate_id'] == history_2012.candidate_id
        assert results[0]['two_year_period'] == history_2012.two_year_period
        assert results[1]['two_year_period'] == history_2008.two_year_period
Ejemplo n.º 9
0
 def test_house_cand_history_between_cycles(self):
     # Committee
     factories.CommitteeDetailFactory()
     candidate = factories.CandidateDetailFactory(candidate_id='H001')
     history = factories.CandidateHistoryFactory(
         candidate_id=candidate.candidate_id,
         two_year_period=2018,
         candidate_election_year=2020,
     )
     db.session.flush()
     # Link
     factories.CandidateCommitteeLinkFactory(
         candidate_id=candidate.candidate_id,
         fec_election_year=2018,
         committee_type='H',
     )
     factories.CandidateElectionFactory(
         candidate_id=candidate.candidate_id,
         cand_election_year=2020,
         prev_election_year=2018,
     )
     # Make sure future house candidate returns results
     results = self._results(
         api.url_for(
             CandidateHistoryView,
             candidate_id=candidate.candidate_id,
             cycle=2020,
             # election_full='false' is strictly 2-year period
             election_full='true',
         ))
     assert len(results) == 1
     assert results[0]['candidate_id'] == history.candidate_id
     assert results[0]['two_year_period'] == history.two_year_period
     assert results[0][
         'candidate_election_year'] == history.candidate_election_year
Ejemplo n.º 10
0
 def setUp(self):
     super().setUp()
     self.candidate = factories.CandidateHistoryFactory(
         two_year_period=2012)
     self.committees = [
         factories.CommitteeHistoryFactory(cycle=2012, designation='P'),
         factories.CommitteeHistoryFactory(cycle=2012, designation='A'),
     ]
     factories.CandidateDetailFactory(
         candidate_key=self.candidate.candidate_key)
     [
         factories.CommitteeDetailFactory(committee_key=each.committee_key)
         for each in self.committees
     ]
     db.session.flush()
     factories.CandidateCommitteeLinkFactory(
         candidate_key=self.candidate.candidate_key,
         committee_key=self.committees[0].committee_key,
         election_year=2012,
     )
     factories.CandidateCommitteeLinkFactory(
         candidate_key=self.candidate.candidate_key,
         committee_key=self.committees[1].committee_key,
         election_year=2012,
     )
Ejemplo n.º 11
0
    def test_sort_by_candidate_name_descending(self):

        factories.CandidateHistoryFactory(
            candidate_id='S005',
            name='WARNER, MARK',
            two_year_period=2010,
            office='S',
            state='NY',
        )
        factories.CandidateHistoryFactory(
            candidate_id='S006',
            name='BALDWIN, ALISSA',
            two_year_period=2010,
            office='S',
            state='NY',
        )

        factories.CandidateElectionFactory(candidate_id='S005',
                                           cand_election_year=2010)
        factories.CandidateElectionFactory(candidate_id='S006',
                                           cand_election_year=2010)

        factories.ScheduleEByCandidateFactory(
            total=50000,
            count=10,
            cycle=2010,
            candidate_id='S005',
            support_oppose_indicator='S',
        ),
        factories.ScheduleEByCandidateFactory(
            total=10000,
            count=5,
            cycle=2010,
            candidate_id='S006',
            support_oppose_indicator='S',
        ),

        response = self._results(
            api.url_for(ScheduleEByCandidateView,
                        cycle=2010,
                        office='senate',
                        state='NY',
                        sort='-candidate_name'))
        self.assertEqual(len(response), 2)
        self.assertEqual(response[0]['candidate_name'], 'WARNER, MARK')
        self.assertEqual(response[1]['candidate_name'], 'BALDWIN, ALISSA')
Ejemplo n.º 12
0
 def test_iter_paginator(self):
     records = [factories.CandidateHistoryFactory() for _ in range(10)]
     paginator = paginators.SeekPaginator(
         models.CandidateHistory.query,
         per_page=5,
         index_column=models.CandidateHistory.idx,
     )
     iterator = tasks.iter_paginator(paginator)
     assert [each.idx
             for each in records] == [each.idx for each in iterator]
Ejemplo n.º 13
0
 def setUp(self):
     super().setUp()
     self.committee = factories.CommitteeDetailFactory()
     self.candidates = [
         factories.CandidateDetailFactory(candidate_id='P001'),
         factories.CandidateDetailFactory(candidate_id='P002'),
     ]
     self.histories = [
         factories.CandidateHistoryFactory(
             candidate_id=self.candidates[0].candidate_id,
             two_year_period=2010),
         factories.CandidateHistoryFactory(
             candidate_id=self.candidates[1].candidate_id,
             two_year_period=2012),
     ]
     db.session.flush()
     self.links = [
         factories.CandidateCommitteeLinkFactory(
             candidate_id=self.candidates[0].candidate_id,
             committee_id=self.committee.committee_id,
             fec_election_year=2010,
             committee_type='P',
         ),
         factories.CandidateCommitteeLinkFactory(
             candidate_id=self.candidates[1].candidate_id,
             committee_id=self.committee.committee_id,
             fec_election_year=2012,
             committee_type='P',
         ),
     ]
     self.elections = [
         factories.CandidateElectionFactory(
             candidate_id=self.candidates[0].candidate_id,
             cand_election_year=2012,
             prev_election_year=2008,
         ),
         factories.CandidateElectionFactory(
             candidate_id=self.candidates[1].candidate_id,
             cand_election_year=2012,
             prev_election_year=2008,
         ),
     ]
Ejemplo n.º 14
0
 def setUp(self):
     super().setUp()
     self.candidate = factories.CandidateHistoryFactory(
         state='NY',
         district='07',
         two_year_period=2012,
         election_years=[2010, 2012],
         office='H',
     )
     self.committees = [
         factories.CommitteeHistoryFactory(cycle=2012, designation='P'),
         factories.CommitteeHistoryFactory(cycle=2012, designation='A'),
     ]
     factories.CandidateDetailFactory(
         candidate_key=self.candidate.candidate_key)
     [
         factories.CommitteeDetailFactory(committee_key=each.committee_key)
         for each in self.committees
     ]
     db.session.flush()
     factories.CandidateCommitteeLinkFactory(
         candidate_key=self.candidate.candidate_key,
         committee_key=self.committees[0].committee_key,
         election_year=2012,
     )
     factories.CandidateCommitteeLinkFactory(
         candidate_key=self.candidate.candidate_key,
         committee_key=self.committees[1].committee_key,
         election_year=2011,
     )
     self.totals = [
         factories.TotalsHouseSenateFactory(
             receipts=50,
             disbursements=75,
             committee_id=self.committees[0].committee_id,
             coverage_end_date=datetime.datetime(2012, 9, 30),
             last_beginning_image_number=123,
             last_report_type_full='Quarter Three',
             last_cash_on_hand_end_period=1979,
             last_report_year=2012,
             cycle=2012,
         ),
         factories.TotalsHouseSenateFactory(
             receipts=50,
             disbursements=75,
             committee_id=self.committees[1].committee_id,
             coverage_end_date=datetime.datetime(2012, 12, 31),
             last_beginning_image_number=456,
             last_report_type_full='Quarter Three',
             last_cash_on_hand_end_period=1979,
             last_report_year=2012,
             cycle=2012,
         ),
     ]
Ejemplo n.º 15
0
 def test_write_csv(self):
     records = [factories.CandidateHistoryFactory() for _ in range(5)]
     query = models.CandidateHistory.query
     schema = schemas.CandidateHistorySchema
     sio = io.StringIO()
     tasks.rows_to_csv(query, schema, sio)
     sio.seek(0)
     reader = csv.DictReader(sio)
     assert reader.fieldnames == tasks.create_headers(schema)
     for record, row in zip(records, reader):
         assert record.candidate_id == row['candidate_id']
Ejemplo n.º 16
0
 def setUp(self):
     super().setUp()
     self.candidate = factories.CandidateHistoryFactory(
         candidate_id='S123',
         two_year_period=2012,
     )
     self.committees = [
         factories.CommitteeHistoryFactory(cycle=2012, designation='P'),
         factories.CommitteeHistoryFactory(cycle=2012, designation='A'),
     ]
     factories.CandidateDetailFactory(
         candidate_id=self.candidate.candidate_id,
         election_years=[2008, 2012],
     )
     [
         factories.CandidateElectionFactory(
             candidate_id=self.candidate.candidate_id,
             cand_election_year=election_year)
         for election_year in [2008, 2012]
     ]
     [
         factories.CommitteeDetailFactory(committee_id=each.committee_id)
         for each in self.committees
     ]
     db.session.flush()
     # Create two-year totals for both the target period (2011-2012) and the
     # previous period (2009-2010) for testing the `election_full` flag
     factories.CandidateCommitteeLinkFactory(
         candidate_id=self.candidate.candidate_id,
         committee_id=self.committees[0].committee_id,
         committee_designation='P',
         committee_type='S',
         fec_election_year=2012,
     )
     factories.CandidateCommitteeLinkFactory(
         candidate_id=self.candidate.candidate_id,
         committee_id=self.committees[1].committee_id,
         committee_designation='A',
         committee_type='S',
         fec_election_year=2012,
     )
     factories.CandidateCommitteeLinkFactory(
         candidate_id=self.candidate.candidate_id,
         committee_id=self.committees[1].committee_id,
         committee_designation='A',
         committee_type='S',
         fec_election_year=2010,
     )
Ejemplo n.º 17
0
 def setUp(self):
     super(TestAggregates, self).setUp()
     self.committee = factories.CommitteeHistoryFactory(
         name='Ritchie for America', cycle=2012,
     )
     self.candidate = factories.CandidateDetailFactory(
         candidate_id='P123',
         name='Robert Ritchie',
         election_years=[2012],
         office='P',
     )
     self.candidate_history = factories.CandidateHistoryFactory(
         candidate_id='P123',
         name='Robert Ritchie',
         election_years=[2012],
         two_year_period=2012,
         office='P',
     )
     factories.CandidateElectionFactory(
         candidate_id='P123', cand_election_year=2012,
     )
Ejemplo n.º 18
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)
Ejemplo n.º 19
0
 def setUp(self):
     super().setUp()
     self.candidate = factories.CandidateDetailFactory()
     self.candidates = [
         factories.CandidateHistoryFactory(
             candidate_id=self.candidate.candidate_id,
             state='PR',
             district='00',
             two_year_period=2018,
             election_years=[2020],
             cycles=[2018, 2020],
             office='H',
             candidate_election_year=2020,
         ),
         factories.CandidateHistoryFactory(
             candidate_id=self.candidate.candidate_id,
             state='PR',
             district='00',
             two_year_period=2020,
             election_years=[2020],
             cycles=[2018, 2020],
             office='H',
             candidate_election_year=2020,
         ),
     ]
     self.committees = [
         factories.CommitteeHistoryFactory(cycle=2020, designation='P'),
         factories.CommitteeHistoryFactory(cycle=2020, designation='A'),
     ]
     [
         factories.CandidateElectionFactory(
             candidate_id=self.candidate.candidate_id,
             cand_election_year=year) for year in [2016, 2020]
     ]
     [
         factories.CommitteeDetailFactory(committee_id=each.committee_id)
         for each in self.committees
     ]
     db.session.flush()
     self.candidate_committee_links = [
         factories.CandidateCommitteeLinkFactory(
             candidate_id=self.candidate.candidate_id,
             committee_id=self.committees[0].committee_id,
             committee_designation='P',
             fec_election_year=2018,
             election_yr_to_be_included=2020,
         ),
         factories.CandidateCommitteeLinkFactory(
             candidate_id=self.candidate.candidate_id,
             committee_id=self.committees[1].committee_id,
             committee_designation='A',
             fec_election_year=2018,
             election_yr_to_be_included=2020,
         ),
         factories.CandidateCommitteeLinkFactory(
             candidate_id=self.candidate.candidate_id,
             committee_id=self.committees[0].committee_id,
             committee_designation='P',
             fec_election_year=2020,
             election_yr_to_be_included=2020,
         ),
         factories.CandidateCommitteeLinkFactory(
             candidate_id=self.candidate.candidate_id,
             committee_id=self.committees[1].committee_id,
             committee_designation='A',
             fec_election_year=2020,
             election_yr_to_be_included=2020,
         ),
     ]
     self.totals = [
         factories.TotalsHouseSenateFactory(
             receipts=50,
             disbursements=75,
             committee_id=self.committees[1].committee_id,
             coverage_end_date=datetime.datetime(2018, 12, 31),
             last_cash_on_hand_end_period=100,
             cycle=2018,
         ),
         factories.TotalsHouseSenateFactory(
             receipts=50,
             disbursements=75,
             committee_id=self.committees[1].committee_id,
             coverage_end_date=datetime.datetime(2020, 12, 31),
             last_cash_on_hand_end_period=300,
             cycle=2020,
         ),
     ]
     db.session.flush()
Ejemplo n.º 20
0
    def setUp(self):
        super().setUp()
        self.candidate = factories.CandidateDetailFactory()
        self.candidates = [
            factories.CandidateHistoryFactory(
                candidate_id=self.candidate.candidate_id,
                state='NY',
                two_year_period=2012,
                election_years=[2010, 2012],
                cycles=[2010, 2012],
                office='S',
                candidate_election_year=2012,
            ),
            factories.CandidateHistoryFactory(
                candidate_id=self.candidate.candidate_id,
                state='NY',
                two_year_period=2010,
                election_years=[2010, 2012],
                cycles=[2010, 2012],
                office='S',
                candidate_election_year=2012,
            ),
        ]
        self.committees = [
            factories.CommitteeHistoryFactory(cycle=2012, designation='P'),
            factories.CommitteeHistoryFactory(cycle=2012, designation='A'),
        ]
        [
            factories.CandidateElectionFactory(
                candidate_id=self.candidate.candidate_id,
                cand_election_year=year) for year in [2010, 2012]
        ]
        [
            factories.CommitteeDetailFactory(committee_id=each.committee_id)
            for each in self.committees
        ]
        db.session.flush()
        self.candidate_committee_links = [
            factories.CandidateCommitteeLinkFactory(
                candidate_id=self.candidate.candidate_id,
                committee_id=self.committees[0].committee_id,
                committee_designation='A',
                fec_election_year=2012,
                election_yr_to_be_included=2012,
            ),
            factories.CandidateCommitteeLinkFactory(
                candidate_id=self.candidate.candidate_id,
                committee_id=self.committees[1].committee_id,
                committee_designation='P',
                fec_election_year=2012,
                election_yr_to_be_included=2012,
            ),
            factories.CandidateCommitteeLinkFactory(
                candidate_id=self.candidate.candidate_id,
                committee_id=self.committees[1].committee_id,
                committee_designation='P',
                fec_election_year=2010,
                election_yr_to_be_included=2012,
            ),
            factories.CandidateCommitteeLinkFactory(
                candidate_id=self.candidate.candidate_id,
                committee_id=self.committees[1].committee_id,
                committee_designation='P',
                fec_election_year=2010,
            ),
        ]
        self.totals = [
            factories.TotalsHouseSenateFactory(
                receipts=50,
                disbursements=75,
                committee_id=self.committees[0].committee_id,
                coverage_end_date=datetime.datetime(2012, 9, 30),
                last_cash_on_hand_end_period=100,
                cycle=2012,
            ),
            factories.TotalsHouseSenateFactory(
                receipts=50,
                disbursements=75,
                committee_id=self.committees[1].committee_id,
                coverage_end_date=datetime.datetime(2012, 12, 31),
                last_cash_on_hand_end_period=100,
                cycle=2012,
            ),
            factories.TotalsHouseSenateFactory(
                receipts=50,
                disbursements=75,
                committee_id=self.committees[1].committee_id,
                coverage_end_date=datetime.datetime(2012, 12, 31),
                last_cash_on_hand_end_period=300,
                cycle=2010,
            ),
        ]

        self.president_candidate = factories.CandidateDetailFactory()
        self.president_candidates = [
            factories.CandidateHistoryFactory(
                candidate_id=self.president_candidate.candidate_id,
                state='NY',
                two_year_period=2020,
                office='P',
                candidate_inactive=False,
                candidate_election_year=2020,
            ),
            factories.CandidateHistoryFactory(
                candidate_id=self.president_candidate.candidate_id,
                state='NY',
                two_year_period=2018,
                office='P',
                candidate_inactive=False,
                candidate_election_year=2020,
            ),
        ]
        self.president_committees = [
            factories.CommitteeHistoryFactory(cycle=2020, designation='P'),
            factories.CommitteeHistoryFactory(cycle=2020, designation='J'),
        ]
        [
            factories.CandidateElectionFactory(
                candidate_id=self.president_candidate.candidate_id,
                cand_election_year=year,
            ) for year in [2016, 2020]
        ]
        [
            factories.CommitteeDetailFactory(committee_id=each.committee_id)
            for each in self.president_committees
        ]
        db.session.flush()
        self.president_candidate_committee_links = [
            factories.CandidateCommitteeLinkFactory(
                candidate_id=self.president_candidate.candidate_id,
                committee_id=self.president_committees[0].committee_id,
                committee_designation='P',
                fec_election_year=2020,
                cand_election_year=2020,
                election_yr_to_be_included=2020,
            ),
            factories.CandidateCommitteeLinkFactory(
                candidate_id=self.president_candidate.candidate_id,
                committee_id=self.president_committees[0].committee_id,
                committee_designation='P',
                fec_election_year=2018,
                cand_election_year=2020,
                election_yr_to_be_included=2020,
            ),
            factories.CandidateCommitteeLinkFactory(
                candidate_id=self.president_candidate.candidate_id,
                committee_id=self.president_committees[1].committee_id,
                committee_designation='P',
                fec_election_year=2018,
                cand_election_year=2020,
                election_yr_to_be_included=2020,
            ),
        ]

        self.presidential_totals = [
            factories.TotalsCombinedFactory(
                receipts=50,
                disbursements=75,
                committee_id=self.president_committees[0].committee_id,
                coverage_end_date=datetime.datetime(2019, 9, 30),
                last_cash_on_hand_end_period=0,
                cycle=2020,
            ),
            factories.TotalsCombinedFactory(
                receipts=1,
                disbursements=1,
                committee_id=self.president_committees[1].committee_id,
                coverage_end_date=datetime.datetime(2017, 12, 31),
                last_cash_on_hand_end_period=100,
                cycle=2018,
            ),
            factories.TotalsCombinedFactory(
                receipts=25,
                disbursements=10,
                committee_id=self.president_committees[0].committee_id,
                coverage_end_date=datetime.datetime(2017, 12, 31),
                last_cash_on_hand_end_period=300,
                cycle=2018,
            ),
        ]
Ejemplo n.º 21
0
 def setUp(self):
     super().setUp()
     self.candidate = factories.CandidateDetailFactory()
     self.candidates = [
         factories.CandidateHistoryFactory(
             candidate_id=self.candidate.candidate_id,
             state='NY',
             two_year_period=2012,
             election_years=[2010, 2012],
             cycles=[2010, 2012],
             office='S',
         ),
         factories.CandidateHistoryFactory(
             candidate_id=self.candidate.candidate_id,
             state='NY',
             two_year_period=2010,
             election_years=[2010, 2012],
             cycles=[2010, 2012],
             office='S',
         ),
     ]
     self.committees = [
         factories.CommitteeHistoryFactory(cycle=2012, designation='P'),
         factories.CommitteeHistoryFactory(cycle=2012, designation='A'),
     ]
     [
         factories.CandidateElectionFactory(
             candidate_id=self.candidate.candidate_id,
             cand_election_year=year) for year in [2010, 2012]
     ]
     [
         factories.CommitteeDetailFactory(committee_id=each.committee_id)
         for each in self.committees
     ]
     db.session.flush()
     factories.CandidateCommitteeLinkFactory(
         candidate_id=self.candidate.candidate_id,
         committee_id=self.committees[0].committee_id,
         committee_designation='A',
         fec_election_year=2012,
     )
     factories.CandidateCommitteeLinkFactory(
         candidate_id=self.candidate.candidate_id,
         committee_id=self.committees[1].committee_id,
         committee_designation='P',
         fec_election_year=2012,
     )
     factories.CandidateCommitteeLinkFactory(
         candidate_id=self.candidate.candidate_id,
         committee_id=self.committees[1].committee_id,
         committee_designation='P',
         fec_election_year=2010,
     )
     self.totals = [
         factories.TotalsHouseSenateFactory(
             receipts=50,
             disbursements=75,
             committee_id=self.committees[0].committee_id,
             coverage_end_date=datetime.datetime(2012, 9, 30),
             last_cash_on_hand_end_period=1979,
             cycle=2012,
         ),
         factories.TotalsHouseSenateFactory(
             receipts=50,
             disbursements=75,
             committee_id=self.committees[1].committee_id,
             coverage_end_date=datetime.datetime(2012, 12, 31),
             last_cash_on_hand_end_period=1979,
             cycle=2012,
         ),
         factories.TotalsHouseSenateFactory(
             receipts=50,
             disbursements=75,
             committee_id=self.committees[1].committee_id,
             coverage_end_date=datetime.datetime(2012, 12, 31),
             last_cash_on_hand_end_period=1979,
             cycle=2010,
         ),
     ]
Ejemplo n.º 22
0
    def test_filters_committee_candidate_id_cycle(self):
        factories.CandidateHistoryFactory(candidate_id='P001',
                                          two_year_period=2000,
                                          candidate_election_year=2000,
                                          name='Apple Smith'),
        factories.CandidateHistoryFactory(candidate_id='P002',
                                          two_year_period=2000,
                                          candidate_election_year=2000,
                                          name='Snapple Smith'),
        factories.CandidateHistoryFactory(candidate_id='P002',
                                          two_year_period=2004,
                                          candidate_election_year=2004,
                                          name='Zapple Smith'),

        factories.CommitteeHistoryFactory(committee_id='C001',
                                          cycle=2000,
                                          name='Acme Co'),
        factories.CommitteeHistoryFactory(committee_id='C002',
                                          cycle=2000,
                                          name='Tetris Corp'),
        factories.CommitteeHistoryFactory(committee_id='C002',
                                          cycle=2004,
                                          name='Winner PAC'),

        factories.CommunicationCostByCandidateFactory(committee_id='C001',
                                                      candidate_id='P001',
                                                      cycle=2000)
        factories.CommunicationCostByCandidateFactory(committee_id='C001',
                                                      candidate_id='P002',
                                                      cycle=2000)
        factories.CommunicationCostByCandidateFactory(committee_id='C002',
                                                      candidate_id='P001',
                                                      cycle=2004)
        db.session.flush()

        # assert results filtered by committee_id sorted by candidate name in descending order
        results = self._results(
            api.url_for(CCAggregatesView,
                        committee_id='C001',
                        sort='-candidate_name'))
        self.assertEqual(len(results), 2)
        self.assertEqual(results[0]['candidate_name'], 'Snapple Smith')
        self.assertEqual(results[1]['candidate_name'], 'Apple Smith')

        # assert results filtered by committee_id sorted by candidate name in ascending order
        results = self._results(
            api.url_for(CCAggregatesView,
                        committee_id='C001',
                        sort='candidate_name'))
        self.assertEqual(len(results), 2)
        self.assertEqual(results[0]['candidate_name'], 'Apple Smith')
        self.assertEqual(results[1]['candidate_name'], 'Snapple Smith')

        # assert results filtered by candidate_id sorted by committee name in descending order
        results = self._results(
            api.url_for(CCAggregatesView,
                        candidate_id='P001',
                        sort='-committee_name'))
        self.assertEqual(len(results), 2)
        self.assertEqual(results[0]['committee_name'], 'Winner PAC')
        self.assertEqual(results[1]['committee_name'], 'Acme Co')

        # assert results filtered by candidate_id sorted by committee name in ascending order
        results = self._results(
            api.url_for(CCAggregatesView,
                        candidate_id='P001',
                        sort='committee_name'))
        self.assertEqual(len(results), 2)
        self.assertEqual(results[0]['committee_name'], 'Acme Co')
        self.assertEqual(results[1]['committee_name'], 'Winner PAC')

        # assert results filtered by cycle sorted by committee name in ascending order
        results = self._results(
            api.url_for(CCAggregatesView, cycle=2000, sort='committee_name'))
        self.assertEqual(len(results), 2)
        self.assertEqual(results[0]['committee_name'], 'Acme Co')
        self.assertEqual(results[1]['committee_name'], 'Acme Co')
Ejemplo n.º 23
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])
Ejemplo n.º 24
0
 def setUp(self):
     super().setUp()
     self.candidate = factories.CandidateHistoryFactory(
         candidate_id='S123',
         two_year_period=2012,
         candidate_election_year=2012,
     )
     self.committees = [
         factories.CommitteeHistoryFactory(cycle=2012, designation='P'),
         factories.CommitteeHistoryFactory(cycle=2012, designation='A'),
     ]
     factories.CandidateHistoryLatestFactory(
         candidate_id=self.candidate.candidate_id,
         candidate_election_year=2012,
         two_year_period=2012,
     )
     factories.CandidateDetailFactory(
         candidate_id=self.candidate.candidate_id,
         election_years=[2008, 2012],
     )
     [
         factories.CandidateElectionFactory(
             candidate_id=self.candidate.candidate_id,
             cand_election_year=election_year)
         for election_year in [2008, 2012]
     ]
     [
         factories.CommitteeDetailFactory(committee_id=each.committee_id)
         for each in self.committees
     ]
     factories.CandidateTotalFactory(
         candidate_id=self.candidate.candidate_id,
         cycle=2012,
         is_election=True,
         receipts=100,
     )
     factories.CandidateTotalFactory(
         candidate_id=self.candidate.candidate_id,
         cycle=2012,
         is_election=False,
         receipts=75,
     )
     factories.CandidateFlagsFactory(
         candidate_id=self.candidate.candidate_id)
     db.session.flush()
     # Create two-year totals for both the target period (2011-2012) and the
     # previous period (2009-2010) for testing the `election_full` flag
     factories.CandidateCommitteeLinkFactory(
         candidate_id=self.candidate.candidate_id,
         committee_id=self.committees[0].committee_id,
         committee_designation='P',
         committee_type='S',
         fec_election_year=2012,
     )
     factories.CandidateCommitteeLinkFactory(
         candidate_id=self.candidate.candidate_id,
         committee_id=self.committees[1].committee_id,
         committee_designation='A',
         committee_type='S',
         fec_election_year=2012,
     )
     factories.CandidateCommitteeLinkFactory(
         candidate_id=self.candidate.candidate_id,
         committee_id=self.committees[1].committee_id,
         committee_designation='A',
         committee_type='S',
         fec_election_year=2010,
     )
     # Create a candidate_zero without a committee and $0 in CandidateTotal
     self.candidate_zero = factories.CandidateHistoryFactory(
         candidate_id='H321',
         two_year_period=2018,
         candidate_election_year=2018,
     )
     factories.CandidateDetailFactory(
         candidate_id=self.candidate_zero.candidate_id,
         election_years=[2018],
     )
     factories.CandidateTotalFactory(
         candidate_id=self.candidate_zero.candidate_id,
         cycle=2018,
         is_election=False,
         receipts=0,
     )