def test_multiple_elections_london(self, mock_response, client): local_london = PostElectionFactory( ballot_paper_id="local.city-of-london.aldgate.2021-05-06", election__slug="local.city-of-london.2021-05-06", election__election_date="2021-05-06", ) parl_london = PostElectionFactory( ballot_paper_id= "parl.cities-of-london-and-westminster.by.2021-05-06", election__slug="parl.2021-05-06", election__election_date="2021-05-06", ) mock_response.json.return_value["results"] = [ { "election_id": local_london.ballot_paper_id }, { "election_id": parl_london.ballot_paper_id }, ] response = client.get(reverse("postcode_view", kwargs={"postcode": "TE11ST"}), follow=True) asserts.assertNotContains( response, "Polling stations are open from 7a.m. till 10p.m. today") asserts.assertNotContains( response, "Polling stations are open from 8a.m. till 8p.m. today")
def test_multiple_elections_not_london(self, mock_response, client): local = PostElectionFactory( ballot_paper_id="local.sheffield.ecclesall.2021-05-06", election__slug="local.sheffield.2021-05-06", election__election_date="2021-05-06", ) pcc = PostElectionFactory( ballot_paper_id="pcc.south-yorkshire.2021-05-06", election__slug="pcc.south-yorkshire.2021-05-06", election__election_date="2021-05-06", ) mock_response.json.return_value["results"] = [ { "election_id": local.ballot_paper_id }, { "election_id": pcc.ballot_paper_id }, ] response = client.get(reverse("postcode_view", kwargs={"postcode": "TE11ST"}), follow=True) assert response.status_code == 200 asserts.assertContains( response, "Polling stations are open from 7a.m. till 10p.m. today") asserts.assertNotContains( response, "Polling stations are open from 8a.m. till 8p.m. today")
def setUp(self): cache.clear() self.election = ElectionFactory( name="2017 General Election", election_date="2017-06-08", slug="parl.2017-06-08", any_non_by_elections=True, ) self.post = PostFactory(ynr_id="WMC:E14000639", label="Cities of London and Westminster") self.post_election = PostElectionFactory( ballot_paper_id="parl.cities-of-london-and-westminster.2017-06-08", post=self.post, election=self.election, ) PersonFactory.reset_sequence() person = PersonFactory() pe = PostElectionFactory(election=self.election, post=self.post) PersonPostFactory( post_election=pe, election=self.election, person=person, post=self.post, party=PartyFactory(), ) self.expected_response = [{ "ballot_paper_id": "parl.cities-of-london-and-westminster.2017-06-08", "absolute_url": "http://testserver/elections/parl.cities-of-london-and-westminster.2017-06-08/cities-of-london-and-westminster/", "election_date": "2017-06-08", "election_id": "parl.2017-06-08", "election_name": "2017 General Election", "post": { "post_name": "Cities of London and Westminster", "post_slug": "WMC:E14000639", }, "cancelled": False, "replaced_by": None, "ballot_locked": False, "candidates": [{ "list_position": None, "party": { "party_id": "PP01", "party_name": "Test Party", }, "person": { "absolute_url": "http://testserver/person/0/candidate-0", "ynr_id": 0, "name": "Candidate 0", }, }], }]
def setUp(self): self.election = ElectionFactory( name="Adur local election", election_date="2021-05-06", slug="local.adur.churchill.2021-05-06", ) self.post = PostFactory(label="Adur local election") self.post_election = PostElectionFactory(election=self.election, post=self.post)
def test_by_election(self, client): """ Test for by elections """ post_election = PostElectionFactory( ballot_paper_id="local.by.election.2020") response = client.get(post_election.get_absolute_url(), follow=True) assertContains(response, "by-election") assertContains(response, post_election.friendly_name) assertContains(response, post_election.post.label)
def test_next_election_not_displayed_for_current_election(self, client): post = PostFactory() current = PostElectionFactory( post=post, election=ElectionFactoryLazySlug( election_date="2021-5-6", current=True, ), ) response = client.get(current.get_absolute_url(), follow=True) assertNotContains(response, "<h3>Next election</h3>")
def test_name_correct(self, post_obj, client): """ Test that the correct names for the post and post election objects are displayed """ post_election = PostElectionFactory(post=post_obj) response = client.get( post_election.get_absolute_url(), follow=True, ) assertContains(response, post_election.friendly_name) assertContains(response, post_election.post.full_label)
def test_get_parish_council_election_none(self, view_obj): """ Test if there is no parish council related to views ballots that None is returned """ post_election = PostElectionFactory() post_election.num_parish_councils = 0 view_obj.ballots = PostElection.objects.annotate( num_parish_councils=Count("parish_councils")) result = view_obj.get_parish_council_election() assert result is None assert view_obj.parish_council_election is None
def test_get_todays_ballots(self, view_obj): today = PostElectionFactory( election__slug="election.today", election__election_date="2021-05-06", ) tomorrow = PostElectionFactory( election__slug="election.tomorrow", election__election_date="2021-05-07", ) view_obj.ballots = PostElection.objects.all() ballots = view_obj.get_todays_ballots() assert len(ballots) == 1 assert today in ballots assert tomorrow not in ballots
def test_get_parish_council_election_object_returned(self, view_obj): """ Test if there is a parish council related to views ballots that it is returned """ post_election = PostElectionFactory() post_election.num_parish_councils = 0 parish_council_election = ParishCouncilElection.objects.create() parish_council_election.ballots.add(post_election) view_obj.ballots = PostElection.objects.annotate( num_parish_councils=Count("parish_councils")) result = view_obj.get_parish_council_election() assert result == parish_council_election assert view_obj.parish_council_election == parish_council_election
def test_party_detail_candidate_count_view(self): # Make a 2nd candidate pe = PostElectionFactory( election=ElectionFactory(slug="2010", name="2010 GE"), post=self.post, ) PersonPostFactory( post_election=pe, party=self.party, election=pe.election, post=self.post, ) p2 = PersonFactory(name="Test 3") PersonPostFactory( post_election=self.pe, person=p2, post=self.post, election=self.election, party=self.party, ) # TODO Use reverse here response = self.client.get("/parties/{}/london".format( self.party.party_id), follow=True) self.assertEqual(response.status_code, 200) self.assertContains(response, "3 candidates") x = response.context_data["object"] assert len(x.personpost_set.all().counts_by_post()) == 2
def test_election_in_past_listed(self): response = self.client.get(self.person_url, follow=True) election_name = "FooBar Election 2017" self.assertNotContains(response, election_name) election = ElectionFactory( name=election_name, current=False, election_date="2017-01-01", slug="local.foobar.2017-01-01", ) post = PostFactory() pe = PostElectionFactory( election=election, post=post, ballot_paper_id="local.foo.bar.2017-01-01", ) PersonPostFactory( post_election=pe, election=election, person=self.person, party=self.party, ) response = self.client.get(self.person_url, follow=True) self.assertContains(response, election_name) self.assertContains(response, "was a")
def ballot(self, mocker): """ Returns an unsaved Ballot object with save method on post mocked """ ballot = PostElectionFactory.build(post__division_type="DIW") mocker.patch.object(ballot.post, "save") return ballot
def test_multiple_non_london_elections_same_day(self, view_obj, mocker): PostElectionFactory( election__slug="local.sheffield.2021-05-06", election__election_date="2021-05-06", ) PostElectionFactory( election__slug="another.sheffield.2021-05-06", election__election_date="2021-05-06", ) mocker.patch.object( view_obj, "get_todays_ballots", return_value=list(PostElection.objects.all()), ) assert view_obj.multiple_city_of_london_elections_today() is False
def test_ical_view(self): election = ElectionFactory(slug="local.cambridgeshire.2017-05-04") post = PostFactory(ynr_id="CED:romsey", label="Romsey") PostElectionFactory(post=post, election=election) response = self.client.get("/elections/CB13HU.ics", follow=True) self.assertEqual(response.status_code, 200)
def test_show_polling_card(self, view_obj, post_elections): post_elections = [ PostElectionFactory( election__slug="local.city-of-london.2020-05-06", election__election_date="2020-05-06", contested=True, cancelled=False, ), PostElectionFactory( election__slug="local.city-of-london.2020-05-06", election__election_date="2020-05-06", contested=False, cancelled=True, ), ] assert view_obj.show_polling_card(post_elections) is True
def setUp(self): cache.clear() self.election = ElectionFactory( name="2017 General Election", election_date="2017-06-08", slug="parl.2017-06-08", any_non_by_elections=True, ) self.post = PostFactory( ynr_id="WMC:E14000639", label="Cities of London and Westminster" ) self.post_election = PostElectionFactory( ballot_paper_id="parl.cities-of-london-and-westminster.2017-06-08", post=self.post, election=self.election, ) PersonFactory.reset_sequence() person = PersonFactory() pe = PostElectionFactory(election=self.election, post=self.post) PersonPostFactory( post_election=pe, election=self.election, person=person, post=self.post, party=PartyFactory(), ) self.expected_response = [ { "ballot_paper_id": "parl.cities-of-london-and-westminster.2017-06-08", "absolute_url": "http://testserver/elections/parl.cities-of-london-and-westminster.2017-06-08/cities-of-london-and-westminster/", "election_date": "2017-06-08", "election_id": "parl.2017-06-08", "election_name": "2017 General Election", "post": { "post_name": "Cities of London and Westminster", "post_slug": "WMC:E14000639", }, "cancelled": False, "replaced_by": None, "ballot_locked": False, "candidates": [ { "list_position": None, "party": { "party_id": "PP01", "party_name": "Test Party", }, "person": { "absolute_url": "http://testserver/person/0/candidate-0", "ynr_id": 0, "name": "Candidate 0", }, } ], } ]
def test_past_registration_deadline(self, post_election): post = PostFactory(territory="ENG") oldest = PostElectionFactory( ballot_paper_id="parl.cities-of-london-and-westminster.2019-05-06", post=post, election=ElectionFactoryLazySlug(election_date="2019-5-6", current=False, election_type="parl"), ) future = PostElectionFactory( ballot_paper_id="parl.cities-of-london-and-westminster.2021-05-06", post=post, election=ElectionFactoryLazySlug(election_date="2021-5-6", current=True, election_type="parl"), ) assert oldest.past_registration_deadline is True assert future.past_registration_deadline is False
class ElectionPostViewTests(TestCase): def setUp(self): self.election = ElectionFactory( name="Adur local election", election_date="2021-05-06", slug="local.adur.churchill.2021-05-06", ) self.post = PostFactory(label="Adur local election") self.post_election = PostElectionFactory(election=self.election, post=self.post) def test_zero_candidates(self): response = self.client.get(self.post_election.get_absolute_url(), follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "elections/post_view.html") self.assertTemplateUsed(response, "elections/includes/_post_meta_title.html") self.assertTemplateUsed( response, "elections/includes/_post_meta_description.html") self.assertContains(response, "No candidates known yet.") def test_num_candidates(self): people = [PersonFactory() for p in range(5)] for person in people: PersonPostFactory( post_election=self.post_election, election=self.election, post=self.post, person=person, ) response = self.client.get(self.post_election.get_absolute_url(), follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "elections/post_view.html") self.assertTemplateUsed(response, "elections/includes/_post_meta_title.html") self.assertTemplateUsed( response, "elections/includes/_post_meta_description.html") self.assertContains(response, f"The 5 candidates in {self.post.label}") self.assertContains(response, f"See all 5 candidates in the {self.post.label}")
def test_next_election_not_displayed_in_past(self, client): post = PostFactory() past = PostElectionFactory( post=post, election=ElectionFactoryLazySlug( election_date="2019-5-2", current=False, ), ) # create an election that just passed PostElectionFactory( post=post, election=ElectionFactoryLazySlug( election_date="2021-5-6", current=True, ), ) response = client.get(past.get_absolute_url(), follow=True) assertNotContains(response, "<h3>Next election</h3>")
def setUp(self): self.election = ElectionFactory( name="2017 General Election", election_date="2017-06-08", slug="parl.2017-06-08", ) self.post = PostFactory(ynr_id="WMC:E14000639", label="Cities of London and Westminster") self.post_election = PostElectionFactory(post=self.post, election=self.election)
def setUp(self): self.party = PartyFactory() self.election = ElectionFactory() self.post = PostFactory() self.pe = PostElectionFactory(election=self.election, post=self.post) PersonPostFactory( post_election=self.pe, party=self.party, election=self.election, post=self.post, )
def test_multiple_london_elections_same_day(self, view_obj, mocker): PostElectionFactory( ballot_paper_id="local.city-of-london.aldgate.2021-05-06", election__slug="local.city-of-london.2021-05-06", election__election_date="2021-05-06", election__election_type="local", ) PostElectionFactory( ballot_paper_id="parl.cities-of-london-and-westminster.2021-05-06", election__slug="parl.2021-05-06", election__election_date="2021-05-06", election__election_type="parl", ) mocker.patch.object( view_obj, "get_todays_ballots", return_value=list(PostElection.objects.all()), ) assert view_obj.multiple_city_of_london_elections_today() is True
def test_next_election_is_today(self, client): post = PostFactory() past = PostElectionFactory( post=post, election=ElectionFactoryLazySlug( election_date="2019-5-2", current=False, ), ) # create an election taking place today PostElectionFactory( post=post, election=ElectionFactoryLazySlug( election_date="2021-5-6", current=True, ), ) response = client.get(past.get_absolute_url(), follow=True) assertContains(response, "<h3>Next election</h3>") assertContains(response, "<strong>being held today</strong>.")
def test_mayor_election_postcode_lookup(self): election = ElectionFactory(slug="mayor.tower-hamlets.2018-05-03") post = PostFactory(ynr_id="tower-hamlets", label="Tower Hamlets", elections=election) PostElectionFactory(post=post, election=election) response = self.client.get("/elections/e32nx/", follow=True) self.assertEqual(response.status_code, 200) self.assertEqual(response.context["postelections"].count(), 1) self.assertContains(response, "Tower Hamlets")
def test_more_than_one_election(self): person = PersonFactory() election1 = ElectionFactory() election2 = ElectionFactory(slug="parl.2010") post1 = PostFactory(elections=election2) post2 = PostFactory( ynr_id="WMC:E14000645", label="Southwark", elections=election2 ) pe1 = PostElectionFactory(election=election1, post=post1) pe2 = PostElectionFactory(election=election2, post=post2) PersonPostFactory( person=person, post_election=pe1, post=post1, election=election1 ) PersonPostFactory( post_election=pe2, person=person, election=election2, post=post2 ) assert person.personpost_set.all().count() == 2
def setUp(self): self.election = ElectionFactory(slug="mayor.tower-hamlets.2018-05-03") self.post = PostFactory( ynr_id="tower-hamlets", label="Tower Hamlets", ) self.ballot = PostElectionFactory( post=self.post, election=self.election, ballot_paper_id="mayor.tower-hamlets.2018-05-03", ) self.hust = Husting.objects.create( post_election=self.ballot, title="Local Election Hustings", url="https://example.com/hustings", starts=datetime(2017, 3, 23, 19, 00, tzinfo=utc), ends=datetime(2017, 3, 23, 21, 00, tzinfo=utc), location="St George's Church", postcode="BN2 1DW", )
def setUp(self): self.election = ElectionFactory() self.post = PostFactory() self.pe = PostElectionFactory(election=self.election, post=self.post) people = [PersonFactory() for p in range(5)] for person in people: PersonPostFactory( post_election=self.pe, election=self.election, post=self.post, person=person, )
def test_next_ballot_different_election_type(self): post = PostFactory() local = PostElectionFactory( post=post, election=ElectionFactoryLazySlug(election_date="2019-5-6", current=False, election_type="local"), ) mayoral = PostElectionFactory( post=post, election=ElectionFactoryLazySlug(election_date="2020-5-6", current=False, election_type="mayor"), ) next_local = PostElectionFactory( post=post, election=ElectionFactoryLazySlug(election_date="2021-5-6", current=True, election_type="local"), ) assert local.next_ballot == next_local assert mayoral.next_ballot is None
def test_not_today(self, mock_response, client): post_election = PostElectionFactory( election__election_date="2021-05-07", ) mock_response.json.return_value["results"].append( {"election_id": post_election.ballot_paper_id}) response = client.get(reverse("postcode_view", kwargs={"postcode": "TE11ST"}), follow=True) asserts.assertNotContains( response, "Polling stations are open from 7a.m. till 10p.m. today") asserts.assertNotContains( response, "Polling stations are open from 8a.m. till 8p.m. today")
def test_city_of_london_today(self, mock_response, client): post_election = PostElectionFactory( ballot_paper_id="local.city-of-london.aldgate.2021-05-06", election__slug="local.city-of-london.2021-05-06", election__election_date="2021-05-06", ) mock_response.json.return_value["results"].append( {"election_id": post_election.ballot_paper_id}) response = client.get(reverse("postcode_view", kwargs={"postcode": "e1 2ax"}), follow=True) asserts.assertContains( response, "Polling stations are open from 8a.m. till 8p.m. today")
def setUp(self): self.election = ElectionFactory(slug="mayor.tower-hamlets.2018-05-03") self.post = PostFactory( ynr_id="tower-hamlets", label="Tower Hamlets", elections=self.election, ) self.ballot = PostElectionFactory( post=self.post, election=self.election, ballot_paper_id="mayor.tower-hamlets.2018-05-03", ) self.hust = Husting.objects.create( post_election=self.ballot, title="Local Election Hustings", url="https://example.com/hustings", starts="2017-03-23 19:00", ends="2017-03-23 21:00", location="St George's Church", postcode="BN2 1DW", )
class TestHustings(TestCase): def setUp(self): self.election = ElectionFactory(slug="mayor.tower-hamlets.2018-05-03") self.post = PostFactory( ynr_id="tower-hamlets", label="Tower Hamlets", elections=self.election, ) self.ballot = PostElectionFactory( post=self.post, election=self.election, ballot_paper_id="mayor.tower-hamlets.2018-05-03", ) self.hust = Husting.objects.create( post_election=self.ballot, title="Local Election Hustings", url="https://example.com/hustings", starts="2017-03-23 19:00", ends="2017-03-23 21:00", location="St George's Church", postcode="BN2 1DW", ) @vcr.use_cassette("fixtures/vcr_cassettes/test_mayor_elections.yaml") def test_hustings_display_on_postcode_page(self): response = self.client.get("/elections/e32nx", follow=True) self.assertEqual(response.status_code, 200) self.assertContains(response, self.hust.title) self.assertContains(response, self.hust.url) self.assertContains(response, self.hust.postcode) def test_hustings_display_on_ballot_page(self): response = self.client.get(self.ballot.get_absolute_url(), follow=True) self.assertEqual(response.status_code, 200) self.assertContains(response, self.hust.title) self.assertContains(response, self.hust.url) self.assertContains(response, self.hust.postcode)
class TestAPISearchViews(APITestCase): def setUp(self): cache.clear() self.election = ElectionFactory( name="2017 General Election", election_date="2017-06-08", slug="parl.2017-06-08", any_non_by_elections=True, ) self.post = PostFactory( ynr_id="WMC:E14000639", label="Cities of London and Westminster" ) self.post_election = PostElectionFactory( ballot_paper_id="parl.cities-of-london-and-westminster.2017-06-08", post=self.post, election=self.election, ) PersonFactory.reset_sequence() person = PersonFactory() pe = PostElectionFactory(election=self.election, post=self.post) PersonPostFactory( post_election=pe, election=self.election, person=person, post=self.post, party=PartyFactory(), ) self.expected_response = [ { "ballot_paper_id": "parl.cities-of-london-and-westminster.2017-06-08", "absolute_url": "http://testserver/elections/parl.cities-of-london-and-westminster.2017-06-08/cities-of-london-and-westminster/", "election_date": "2017-06-08", "election_id": "parl.2017-06-08", "election_name": "2017 General Election", "post": { "post_name": "Cities of London and Westminster", "post_slug": "WMC:E14000639", }, "cancelled": False, "replaced_by": None, "ballot_locked": False, "candidates": [ { "list_position": None, "party": { "party_id": "PP01", "party_name": "Test Party", }, "person": { "absolute_url": "http://testserver/person/0/candidate-0", "ynr_id": 0, "name": "Candidate 0", }, } ], } ] @vcr.use_cassette("fixtures/vcr_cassettes/test_postcode_view.yaml") def test_candidates_for_postcode_view(self): url = reverse("api:candidates-for-postcode-list") with self.assertNumQueries(4): req = self.client.get("{}?postcode=EC1A4EU".format(url)) assert req.status_code == 200 assert req.json() == self.expected_response def test_candidates_for_ballots(self): url = reverse("api:candidates-for-ballots-list") with self.assertNumQueries(4): req = self.client.get( "{}?ballot_ids=parl.cities-of-london-and-westminster.2017-06-08".format( url ) ) assert req.status_code == 200 assert req.json() == self.expected_response @vcr.use_cassette("fixtures/vcr_cassettes/test_postcode_view.yaml") def test_lock_status(self): self.post_election.locked = True self.post_election.save() url = reverse("api:candidates-for-postcode-list") req = self.client.get("{}?postcode=EC1A4EU".format(url)) assert req.status_code == 200 assert req.json()[0]["ballot_locked"] == True