Skip to content
This repository has been archived by the owner on Apr 14, 2019. It is now read-only.

Commit

Permalink
Rewrite supporting/opposing with test factories
Browse files Browse the repository at this point in the history
  • Loading branch information
adborden committed Aug 22, 2016
1 parent 9019801 commit f7a3130
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 31 deletions.
77 changes: 77 additions & 0 deletions ballot/tests/factory.py
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)
111 changes: 81 additions & 30 deletions disclosure/tests/test_api_supporting_opposing.py
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)
2 changes: 1 addition & 1 deletion disclosure/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def opposing(self, request, referendum_id):
"""
ballot_item_selection = get_object_or_404(
ReferendumSelection,
in_favor=False,
in_favor=True,
ballot_item__id=referendum_id)
return super(ReferendumViewSet, self).opposing(
request, ballot_item_selection_id=ballot_item_selection.id)
Expand Down
36 changes: 36 additions & 0 deletions finance/tests/factory.py
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'
1 change: 1 addition & 0 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ flake8
coverage
pygraphviz
graphviz
factory-boy==2.7.0

0 comments on commit f7a3130

Please sign in to comment.