Skip to content

Commit

Permalink
Add util to get current cycle, make future cycles dynamic for test
Browse files Browse the repository at this point in the history
  • Loading branch information
lbeaufort committed Jun 20, 2018
1 parent 3dd3293 commit 82b0259
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
49 changes: 27 additions & 22 deletions tests/test_aggregates.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from tests import factories
from tests.common import ApiBaseTest, assert_dicts_subset

from webservices.utils import get_current_cycle

from webservices import schemas
from webservices.rest import db, api
from webservices.resources.aggregates import (
Expand Down Expand Up @@ -168,6 +170,9 @@ def test_candidate_aggregates_by_election(self):

class TestCandidateAggregates(ApiBaseTest):

current_cycle = get_current_cycle()
next_cycle = current_cycle + 2

def setUp(self):
super().setUp()
self.candidate = factories.CandidateHistoryFutureFactory(
Expand Down Expand Up @@ -357,50 +362,50 @@ def setUp(self):
fec_election_year=2018,
)

# Create data for future presidential - 2020. Use formula for future
# Create data for future presidential - next_cycle. Use formula for future

# Test full 2020 and 2018 totals
# Test full next_cycle and current_cycle 2-year totals

self.candidate_20 = factories.CandidateHistoryFutureFactory(
candidate_id='P456',
two_year_period=2018,
candidate_election_year=2020,
two_year_period=self.current_cycle,
candidate_election_year=self.next_cycle,
)
self.candidate_20 = factories.CandidateHistoryFutureFactory(
candidate_id='P456',
two_year_period=2020,
candidate_election_year=2020,
two_year_period=self.next_cycle,
candidate_election_year=self.next_cycle,
)
#Candidate history won't have 2020 yet
#Candidate history won't have next_cycle yet
self.committees_20 = [
factories.CommitteeHistoryFactory(cycle=2018, designation='P'),
factories.CommitteeHistoryFactory(cycle=self.current_cycle, designation='P'),
]
factories.CandidateDetailFactory(
candidate_id=self.candidate_20.candidate_id,
election_years=[2020],
election_years=[self.next_cycle],
)
[
factories.CandidateElectionFactory(
candidate_id=self.candidate_20.candidate_id,
cand_election_year=election_year
)
for election_year in [2016, 2020]
for election_year in [self.next_cycle - 4, self.next_cycle]
]
[
factories.CommitteeDetailFactory(committee_id=each.committee_id)
for each in self.committees_20
]
#Full 2020
#Full next_cycle
factories.CandidateTotalFactory(
candidate_id=self.candidate_20.candidate_id,
cycle=2020,
cycle=self.next_cycle,
is_election=True,
receipts=55000,
)
#2018 2-year
#current_cycle 2-year
factories.CandidateTotalFactory(
candidate_id=self.candidate_20.candidate_id,
cycle=2018,
cycle=self.current_cycle,
is_election=False,
receipts=25000,
)
Expand All @@ -414,17 +419,17 @@ def setUp(self):
committee_id=self.committees_20[0].committee_id,
committee_designation='P',
committee_type='P',
cand_election_year=2020,
fec_election_year=2018,
cand_election_year=self.next_cycle,
fec_election_year=self.current_cycle,
)

factories.CandidateCommitteeLinkFactory(
candidate_id=self.candidate_20.candidate_id,
committee_id=self.committees_20[0].committee_id,
committee_designation='P',
committee_type='P',
cand_election_year=2020,
fec_election_year=2020,
cand_election_year=self.next_cycle,
fec_election_year=self.next_cycle,
)

def test_by_size(self):
Expand Down Expand Up @@ -541,11 +546,11 @@ def test_totals(self):
api.url_for(
TotalsCandidateView,
candidate_id=self.candidate_20.candidate_id,
cycle=2018,
cycle=self.current_cycle,
)
)
assert len(results) == 1
assert_dicts_subset(results[0], {'cycle': 2018, 'receipts': 25000})
assert_dicts_subset(results[0], {'cycle': self.current_cycle, 'receipts': 25000})

def test_totals_full(self):
results = self._results(
Expand All @@ -564,9 +569,9 @@ def test_totals_full(self):
api.url_for(
TotalsCandidateView,
candidate_id=self.candidate_20.candidate_id,
cycle=2020,
cycle=self.next_cycle,
election_full='true',
)
)
assert len(results) == 1
assert_dicts_subset(results[0], {'cycle': 2020, 'receipts': 55000})
assert_dicts_subset(results[0], {'cycle': self.next_cycle, 'receipts': 55000})
5 changes: 5 additions & 0 deletions webservices/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,11 @@ def get_election_duration(column):
else_=2,
)

def get_current_cycle():
year = date.today().year
return year + year % 2


def get_elasticsearch_connection():
es_conn = env.get_service(name='fec-api-search56')
if es_conn:
Expand Down

0 comments on commit 82b0259

Please sign in to comment.