Skip to content

Commit

Permalink
Add result_set to ballot serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
VirginiaDooley committed Sep 11, 2023
1 parent 2ba77e7 commit 9057482
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
40 changes: 40 additions & 0 deletions ynr/apps/api/tests/test_api_next.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from parties.tests.fixtures import DefaultPartyFixtures
from people.models import PersonImage
from people.tests.factories import PersonFactory
from uk_results.models import ResultSet


class TestAPI(
Expand Down Expand Up @@ -287,3 +288,42 @@ def test_sopn_on_ballot(self):
"source_url": "",
},
)

def test_no_results_on_ballot(self):
response = self.app.get(
"/api/next/ballots/{}/".format(
self.edinburgh_east_post_ballot.ballot_paper_id
)
)
result = response.json
self.assertEqual(
result["results"],
None,
)

def test_results_on_ballot(self):
ResultSet.objects.create(
ballot=self.edinburgh_east_post_ballot,
created="2015-05-08T00:00:00Z",
modified="2015-05-08T00:00:00Z",
source="Example ResultSet for testing",
num_spoilt_ballots=100,
num_turnout_reported=900,
total_electorate=1000,
)
response = self.app.get(
"/api/next/ballots/{}/".format(
self.edinburgh_east_post_ballot.ballot_paper_id
)
)
result = response.json
self.assertEqual(
result["results"],
{
"num_turnout_reported": 900,
"turnout_percentage": 90.0,
"num_spoilt_ballots": 100,
"source": "Example ResultSet for testing",
"total_electorate": 1000,
},
)
3 changes: 3 additions & 0 deletions ynr/apps/elections/api/next/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
MinimalPostSerializer,
)
from rest_framework import serializers
from uk_results.api.next.serializers import MinimalResultSerializer


class MinimalElectionSerializer(serializers.HyperlinkedModelSerializer):
Expand Down Expand Up @@ -102,6 +103,7 @@ class Meta:
"replaces",
"replaced_by",
"uncontested",
"results",
)

replaces = serializers.SlugRelatedField(
Expand All @@ -126,6 +128,7 @@ class Meta:
post = MinimalPostSerializer(read_only=True)
sopn = serializers.SerializerMethodField()
candidacies = serializers.SerializerMethodField()
results = MinimalResultSerializer(read_only=True, source="resultset")

results_url = serializers.HyperlinkedIdentityField(
view_name="resultset-detail",
Expand Down
12 changes: 12 additions & 0 deletions ynr/apps/uk_results/api/next/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ def get_url(self, obj):
candidate_results = CandidateResultSerializer(many=True)


class MinimalResultSerializer(serializers.ModelSerializer):
class Meta:
model = ResultSet
fields = (
"num_turnout_reported",
"turnout_percentage",
"num_spoilt_ballots",
"source",
"total_electorate",
)


class ElectedSerializer(CandidacyOnBallotSerializer):
class Meta:
model = Membership
Expand Down

0 comments on commit 9057482

Please sign in to comment.