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/api_views.py b/ynr/apps/elections/api/next/api_views.py index 6a6b730c8..5e87ccbc9 100644 --- a/ynr/apps/elections/api/next/api_views.py +++ b/ynr/apps/elections/api/next/api_views.py @@ -39,7 +39,7 @@ class BallotViewSet(viewsets.ReadOnlyModelViewSet): lookup_value_regex = r"(?!\.json$)[^/]+" queryset = ( extra_models.Ballot.objects.select_related( - "election", "post", "replaces", "replaced_by" + "election", "post", "replaces", "replaced_by", "resultset" ) .prefetch_related( Prefetch( 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/elections/tests/test_ballot_view.py b/ynr/apps/elections/tests/test_ballot_view.py index a3f967972..fc2eb2fd1 100644 --- a/ynr/apps/elections/tests/test_ballot_view.py +++ b/ynr/apps/elections/tests/test_ballot_view.py @@ -131,7 +131,7 @@ def test_ballot_with_candidates_no_sopn(self): self.assertEqual(response.context["candidates"].count(), 9) self.assertDataTimelineCandidateAddingInProgress(response) self.assertInHTML( - "