This repository has been archived by the owner on Apr 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite supporting/opposing with test factories
- Loading branch information
Showing
5 changed files
with
196 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
from __future__ import absolute_import | ||
import factory | ||
|
||
|
||
class StateFactory(factory.django.DjangoModelFactory): | ||
class Meta: | ||
model = 'locality.State' | ||
|
||
name = 'California' | ||
short_name = 'CA' | ||
|
||
|
||
class CountyFactory(factory.django.DjangoModelFactory): | ||
class Meta: | ||
model = 'locality.County' | ||
|
||
state = factory.SubFactory(StateFactory) | ||
|
||
|
||
class CityFactory(factory.django.DjangoModelFactory): | ||
class Meta: | ||
model = 'locality.City' | ||
|
||
county = factory.SubFactory(CountyFactory) | ||
state = factory.SubFactory(StateFactory) | ||
|
||
|
||
class BallotFactory(factory.django.DjangoModelFactory): | ||
class Meta: | ||
model = 'ballot.Ballot' | ||
|
||
locality = factory.SubFactory(CityFactory) | ||
|
||
|
||
class ReferendumFactory(factory.django.DjangoModelFactory): | ||
class Meta: | ||
model = 'ballot.Referendum' | ||
|
||
number = factory.Faker('random_letter') | ||
title = factory.Faker('sentences', nb=1) | ||
ballot = factory.SubFactory(BallotFactory) | ||
contest_type = 'R' | ||
|
||
|
||
class ReferendumSelectionFactory(factory.django.DjangoModelFactory): | ||
class Meta: | ||
model = 'ballot.ReferendumSelection' | ||
|
||
ballot_item = factory.SubFactory(ReferendumFactory) | ||
|
||
|
||
class OfficeFactory(factory.django.DjangoModelFactory): | ||
class Meta: | ||
model = 'ballot.Office' | ||
|
||
name = factory.Faker('random_element', elements=['Mayor', 'Secretary', 'Councilmember']) | ||
description = 'An office' | ||
locality = factory.SubFactory(CityFactory) | ||
|
||
|
||
class OfficeElectionFactory(factory.django.DjangoModelFactory): | ||
class Meta: | ||
model = 'ballot.OfficeElection' | ||
|
||
ballot = factory.SubFactory(BallotFactory) | ||
contest_type = 'O' | ||
office = factory.SubFactory(OfficeFactory) | ||
|
||
|
||
class CandidateFactory(factory.django.DjangoModelFactory): | ||
class Meta: | ||
model = 'ballot.Candidate' | ||
|
||
first_name = factory.Faker('first_name') | ||
middle_name = factory.Faker('first_name') | ||
last_name = factory.Faker('last_name') | ||
ballot_item = factory.SubFactory(OfficeElectionFactory) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,109 @@ | ||
from django.core.urlresolvers import reverse | ||
from rest_framework.test import APITestCase | ||
|
||
from finance.tests.utils import with_form460A_data | ||
from finance.models import Beneficiary | ||
from ballot.tests.factory import CandidateFactory, OfficeElectionFactory, \ | ||
ReferendumFactory, ReferendumSelectionFactory | ||
from finance.tests.factory import BeneficiaryFactory, IndependentMoneyFactory | ||
|
||
|
||
@with_form460A_data(test_agency='COS', test_year='2015') | ||
class OpposingTests(APITestCase): | ||
class SupportingOpposingCandidateTests(APITestCase): | ||
def setUp(self): | ||
self.office_election = OfficeElectionFactory() | ||
self.candidate = CandidateFactory( | ||
ballot_item=self.office_election | ||
) | ||
|
||
def do_the_thing_for_candidates(self, support): | ||
beneficiary = Beneficiary.objects.filter( | ||
ballot_item_selection__ballot_item__contest_type='O')[0] | ||
beneficiary.support = support | ||
beneficiary.save() | ||
candidate = beneficiary.ballot_item_selection.reverse_lookup() | ||
def test_candidate_opposing_list(self): | ||
beneficiary = BeneficiaryFactory( | ||
support=False, | ||
ballot_item_selection=self.candidate, | ||
) | ||
money = IndependentMoneyFactory( | ||
beneficiary=beneficiary, | ||
amount=30.5, | ||
) | ||
|
||
url = reverse('candidate_%s' % ('supporting' if support else 'opposing'), | ||
kwargs={'candidate_id': candidate.id}) | ||
url = reverse('candidate_opposing', | ||
kwargs={'candidate_id': self.candidate.id}) | ||
resp = self.client.get(url) | ||
self.assertEquals(resp.status_code, 200) | ||
self.assertEqual(resp.status_code, 200) | ||
self.assertTrue(len(resp.data) > 0) | ||
|
||
# TODO: replace dummy tests with live data tests. | ||
row = resp.data[0] | ||
self.assertIn('name', row) | ||
self.assertIn('contributions_received', row) | ||
|
||
def test_candidate_opposing_list(self): | ||
return self.do_the_thing_for_candidates(support=False) | ||
self.assertEqual(row.get('contributions_received'), money.amount) | ||
|
||
def test_candidate_supporting_list(self): | ||
return self.do_the_thing_for_candidates(support=True) | ||
beneficiary = BeneficiaryFactory( | ||
support=True, | ||
ballot_item_selection=self.candidate, | ||
) | ||
money = IndependentMoneyFactory( | ||
beneficiary=beneficiary, | ||
amount=15.5, | ||
) | ||
|
||
def do_the_thing_for_referendums(self, support): | ||
beneficiary = Beneficiary.objects.filter( | ||
ballot_item_selection__ballot_item__contest_type='R')[0] | ||
beneficiary.support = support | ||
beneficiary.save() | ||
|
||
referendum = beneficiary.ballot_item_selection.ballot_item.reverse_lookup() | ||
url = reverse('referendum_%s' % ('supporting' if support else 'opposing'), | ||
kwargs={'referendum_id': referendum.id}) | ||
url = reverse('candidate_supporting', | ||
kwargs={'candidate_id': self.candidate.id}) | ||
resp = self.client.get(url) | ||
self.assertEquals(resp.status_code, 200) | ||
self.assertEqual(resp.status_code, 200) | ||
self.assertTrue(len(resp.data) > 0) | ||
print(resp.data) | ||
|
||
# TODO: replace dummy tests with live data tests. | ||
row = resp.data[0] | ||
self.assertIn('name', row) | ||
self.assertIn('contributions_received', row) | ||
self.assertEqual(row.get('contributions_received'), money.amount) | ||
|
||
|
||
class SupportingOpposingReferendumTests(APITestCase): | ||
def setUp(self): | ||
self.referendum = referendum = ReferendumFactory() | ||
self.referendum_selection = ReferendumSelectionFactory( | ||
in_favor=True, | ||
ballot_item=referendum | ||
) | ||
|
||
def test_referendum_opposing_list(self): | ||
return self.do_the_thing_for_referendums(support=False) | ||
beneficiary = BeneficiaryFactory( | ||
support=False, | ||
ballot_item_selection=self.referendum_selection, | ||
) | ||
IndependentMoneyFactory( | ||
beneficiary=beneficiary, | ||
amount=40.5, | ||
) | ||
|
||
url = reverse('referendum_opposing', | ||
kwargs={'referendum_id': self.referendum.id}) | ||
resp = self.client.get(url) | ||
self.assertEqual(resp.status_code, 200) | ||
self.assertTrue(len(resp.data) > 0) | ||
|
||
row = resp.data[0] | ||
self.assertIn('name', row) | ||
self.assertIn('contributions_received', row) | ||
self.assertEqual(row.get('contributions_received'), 40.5) | ||
|
||
def test_referendum_supporting_list(self): | ||
return self.do_the_thing_for_referendums(support=True) | ||
beneficiary = BeneficiaryFactory( | ||
support=True, | ||
ballot_item_selection=self.referendum_selection, | ||
) | ||
IndependentMoneyFactory( | ||
beneficiary=beneficiary, | ||
amount=55.5, | ||
) | ||
|
||
url = reverse('referendum_supporting', | ||
kwargs={'referendum_id': self.referendum.id}) | ||
resp = self.client.get(url) | ||
self.assertEqual(resp.status_code, 200) | ||
self.assertTrue(len(resp.data) > 0) | ||
|
||
row = resp.data[0] | ||
self.assertIn('name', row) | ||
self.assertIn('contributions_received', row) | ||
self.assertEqual(row.get('contributions_received'), 55.5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from __future__ import absolute_import | ||
import factory | ||
from factory.django import DjangoModelFactory | ||
|
||
from ballot.tests.factory import CityFactory | ||
|
||
|
||
class ZipCodeFactory(DjangoModelFactory): | ||
class Meta: | ||
model = 'locality.ZipCode' | ||
|
||
|
||
class BenefactorFactory(DjangoModelFactory): | ||
class Meta: | ||
model = 'finance.Benefactor' | ||
|
||
benefactor_type = 'IF' | ||
benefactor_locality = factory.SubFactory(CityFactory) | ||
|
||
|
||
class BeneficiaryFactory(DjangoModelFactory): | ||
class Meta: | ||
model = 'finance.Beneficiary' | ||
|
||
|
||
class IndependentMoneyFactory(DjangoModelFactory): | ||
class Meta: | ||
model = 'finance.IndependentMoney' | ||
|
||
amount = 55.5 | ||
beneficiary = factory.SubFactory(BeneficiaryFactory) | ||
benefactor = factory.SubFactory(BenefactorFactory) | ||
benefactor_zip = factory.SubFactory(ZipCodeFactory) | ||
report_date = factory.Faker('date_time') | ||
source = 'NF' | ||
source_xact_id = '1234' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ flake8 | |
coverage | ||
pygraphviz | ||
graphviz | ||
factory-boy==2.7.0 |