diff --git a/ynr/apps/api/tests/test_api_next.py b/ynr/apps/api/tests/test_api_next.py index c78207a68..5d76010f6 100644 --- a/ynr/apps/api/tests/test_api_next.py +++ b/ynr/apps/api/tests/test_api_next.py @@ -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( @@ -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, + }, + ) diff --git a/ynr/apps/elections/api/next/serializers.py b/ynr/apps/elections/api/next/serializers.py index 2bbe92fb4..32ee4f29b 100644 --- a/ynr/apps/elections/api/next/serializers.py +++ b/ynr/apps/elections/api/next/serializers.py @@ -9,6 +9,7 @@ MinimalPostSerializer, ) from rest_framework import serializers +from uk_results.api.next.serializers import MinimalResultSerializer class MinimalElectionSerializer(serializers.HyperlinkedModelSerializer): @@ -102,6 +103,7 @@ class Meta: "replaces", "replaced_by", "uncontested", + "results", ) replaces = serializers.SlugRelatedField( @@ -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", diff --git a/ynr/apps/uk_results/api/next/serializers.py b/ynr/apps/uk_results/api/next/serializers.py index c527e918a..54371d961 100644 --- a/ynr/apps/uk_results/api/next/serializers.py +++ b/ynr/apps/uk_results/api/next/serializers.py @@ -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