Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MERGE WITH GIT FLOW] Show 2020 house candidate history #3485

Merged
merged 1 commit into from
Nov 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions tests/test_candidates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 7 additions & 1 deletion webservices/resources/candidates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down