From 2a04e438fa29b9b841bfc5f3582d8bda4ce808fe Mon Sep 17 00:00:00 2001 From: Laura Beaufort <31420082+lbeaufort@users.noreply.github.com> Date: Thu, 8 Nov 2018 10:11:18 -0500 Subject: [PATCH] Show history for first-time, future-cycle house candidates Resolves https://github.com/fecgov/fec-cms/issues/2463 For new house candidates that file for a future election, the 2-year period will equal `prev_election_yr` until we reach the 2-year period for that future election. This `>=` (rather than `>`) guarantees results for candidate pages. A `SELECT DISTINCT` on `candidate_id` prevents duplicate rows. --- tests/test_candidates.py | 36 +++++++++++++++++++++++++++++ webservices/resources/candidates.py | 8 ++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/tests/test_candidates.py b/tests/test_candidates.py index 78c331de0..f205b3bf2 100644 --- a/tests/test_candidates.py +++ b/tests/test_candidates.py @@ -233,6 +233,42 @@ def test_history(self): assert results[0]['two_year_period'] == history_2012.two_year_period assert results[1]['two_year_period'] == history_2008.two_year_period + 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 + def test_committee_cycle(self): results = self._results( api.url_for( diff --git a/webservices/resources/candidates.py b/webservices/resources/candidates.py index 755a69e22..b43f22b94 100644 --- a/webservices/resources/candidates.py +++ b/webservices/resources/candidates.py @@ -229,7 +229,13 @@ def _filter_elections(self, query, cycle): sa.and_( models.CandidateHistory.candidate_id == models.CandidateElection.candidate_id, models.CandidateHistory.two_year_period <= models.CandidateElection.cand_election_year, - models.CandidateHistory.two_year_period > models.CandidateElection.prev_election_year, + # For new house candidates that file for a future election, + # the 2-year period will equal `prev_election_yr` + # until we reach the 2-year period for that future election. + # This `>=` (rather than `>`) + # guarantees results for candidate pages. + # A `SELECT DISTINCT` on `candidate_id` prevents duplicate rows. + models.CandidateHistory.two_year_period >= models.CandidateElection.prev_election_year, ), ).filter( cycle <= models.CandidateElection.cand_election_year,