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_multiple_independent_candidacies_intro(self): election_one = ElectionFactory() election_two = ElectionFactoryLazySlug() party = PartyFactory(party_name="Independent", party_id="ynmp-party:2") PersonPostFactory(person=self.person, election=election_one, party=party) PersonPostFactory(person=self.person, election=election_two, party=party) response = self.client.get(self.person_url, follow=True) self.assertContains( response, "is an Independent candidate in the following elections:")
def test_more_than_one_election(self): person = PersonFactory() election1 = ElectionFactory() election2 = ElectionFactory(slug="parl.2010") post2 = PostFactory(ynr_id="WMC:E14000645", label="Southwark", elections=election2) person.elections.add(election1) person.elections.add(election2) PersonPostFactory(person=person, election=election1) PersonPostFactory(person=person, elections=election2, post=post2) assert person.posts.all().count() == 2
def test_multiple_candidacies_intro(self): election_one = ElectionFactory() election_two = ElectionFactoryLazySlug() party = PartyFactory(party_name="Liberal Democrat", party_id="foo") PersonPostFactory(person=self.person, election=election_one, party=party) PersonPostFactory(person=self.person, election=election_two, party=party) response = self.client.get(self.person_url, follow=True) self.assertContains( response, "is a Liberal Democrat candidate in the following elections:", )
def test_statement_to_voters(self): self.person.statement_to_voters = "I believe in equal rights." self.person.save() PersonPostFactory(person=self.person, election=ElectionFactory()) response = self.client.get(self.person_url, follow=True) self.assertEqual(response.template_name, ["people/person_detail.html"]) self.assertContains(response, "Statement to voters")
def test_TWFY(self): self.person.twfy_id = 123 self.person.save() PersonPostFactory(person=self.person, election=ElectionFactory()) response = self.client.get(self.person_url, follow=True) self.assertEqual(response.template_name, ["people/person_detail.html"]) self.assertContains(response, "Record in office")
def test_instagram(self): self.person.instagram_url = "https://www.instagram.com/yo" self.person.save() PersonPostFactory(person=self.person, election=ElectionFactory()) response = self.client.get(self.person_url, follow=True) self.assertEqual(response.template_name, ["people/person_detail.html"]) self.assertContains(response, "Instagram")
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_candidates_for_postcode_view(self): person = PersonFactory() # Make a person PersonPostFactory(election=self.election, person=person, post=self.post, party=PartyFactory()) url = reverse('api:candidates-for-postcode-list') with self.assertNumQueries(2): req = self.client.get("{}?postcode=EC1A4EU".format(url)) assert req.status_code == 200 assert req.data == [{ 'election_date': datetime.date(2017, 6, 8), 'election_id': 'parl.2017-06-08', 'election_name': '2017 General Election', 'post': { 'post_name': 'Cities of London and Westminster', 'post_slug': 'WMC:E14000639' }, 'candidates': [{ 'list_position': None, 'party': { 'party_id': 'PP01', 'party_name': 'Test Party' }, 'person': { 'ynr_id': '0', 'name': 'Candidate 0' } }], }]
def test_youtube(self): self.person.youtube_profile = "Mary123" self.person.save() PersonPostFactory(person=self.person, election=ElectionFactory()) response = self.client.get(self.person_url, follow=True) self.assertEqual(response.template_name, ["people/person_detail.html"]) self.assertContains(response, "YouTube")
def test_email(self): self.person.email = "*****@*****.**" self.person.save() PersonPostFactory(person=self.person, election=ElectionFactory()) response = self.client.get(self.person_url, follow=True) self.assertEqual(response.template_name, ["people/person_detail.html"]) self.assertContains(response, "Email")
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 test_display_deceased(self): self.party = PartyFactory() self.election = ElectionFactory() self.personpost = PersonPostFactory(person=self.person, party=self.party, election=self.election) self.person.death_date = "01/01/2021" assert self.person.display_deceased is True
def test_current_person_view(self): self.personpost = PersonPostFactory(person=self.person, election=ElectionFactory()) response = self.client.get(self.person_url, follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "people/person_detail.html") self.assertNotContains(response, '<meta name="robots" content="noindex">')
def test_party_page(self): self.person.party_ppc_page_url = "https://www.voteforme.com/bob" self.person.save() PersonPostFactory(person=self.person, election=ElectionFactory()) response = self.client.get(self.person_url, follow=True) self.assertEqual(response.template_name, ["people/person_detail.html"]) self.assertContains(response, "The party's candidate page for this person")
def test_wikipedia(self): self.person.wikipedia_bio = "yo" self.person.wikipedia_url = "https//www.wikipedia.com/yo" self.person.save() PersonPostFactory(person=self.person, election=ElectionFactory()) response = self.client.get(self.person_url, follow=True) self.assertEqual(response.template_name, ["people/person_detail.html"]) self.assertContains(response, "Wikipedia")
def test_facebook(self): self.person.facebook_personal_url = "https//www.facebook.com/yo" self.person.facebook_page_url = "https//www.facebook.com/yo" self.person.save() PersonPostFactory(person=self.person, election=ElectionFactory()) response = self.client.get(self.person_url, follow=True) self.assertEqual(response.template_name, ["people/person_detail.html"]) self.assertContains(response, "yo")
def setUp(self): self.party = PartyFactory() self.election = ElectionFactory() self.post = PostFactory() PersonPostFactory( party=self.party, election=self.election, post=self.post, )
def setUp(self): self.election = ElectionFactory() self.post = PostFactory() people = [PersonFactory() for p in range(5)] for person in people: PersonPostFactory( election=self.election, post=self.post, person=person, )
def setUp(self): # create some Election objects with different dates future_current_election = ElectionFactoryLazySlug( election_date=timezone.datetime(2021, 5, 6), current=True) future_not_current_election = ElectionFactoryLazySlug( election_date=timezone.datetime(2021, 5, 6), current=False) past_current_election = ElectionFactoryLazySlug( current=True, election_date=timezone.datetime(2021, 1, 1)) past_not_current = ElectionFactoryLazySlug( current=False, election_date=timezone.datetime(2017, 6, 8)) # create the 'candidacy' like objects self.in_future_current = PersonPostFactory( election=future_current_election, ) self.in_future_not_current = PersonPostFactory( election=future_not_current_election, ) self.in_past_current = PersonPostFactory( election=past_current_election) self.in_past_not_current = PersonPostFactory(election=past_not_current)
def test_one_candidacy_intro(self): election = ElectionFactory() party = PartyFactory(party_name="Conservative and Unionist Party", party_id="ConUnion") person_post = PersonPostFactory(person=self.person, election=election, party=party) response = self.client.get(self.person_url, follow=True) self.assertContains( response, f"{self.person.name} is a {party.party_name} candidate in {person_post.post.label} in the {election.name}.", )
def test_more_than_one_election(self): person = PersonFactory() election1 = ElectionFactory() election2 = ElectionFactory(slug="parl.2010") post1 = PostFactory() post2 = PostFactory(ynr_id="WMC:E14000645", label="Southwark") 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 test_local_party_for_non_local_election(self): party = PartyFactory(party_name="Labour Party", party_id="party:53") local_party = LocalPartyFactory(name="Welsh Labour | Llafur Cymru", is_local=False, parent=party) PersonPostFactory( person=self.person, election=ElectionFactory(), party=party, ) response = self.client.get(self.person_url, follow=True) expected = f"{self.person.name} is a {local_party.label} candidate." self.assertContains(response, expected)
def test_local_party_for_local_election(self): party = PartyFactory(party_name="Labour Party", party_id="party:53") local_party = LocalPartyFactory(name="Derbyshire Labour", is_local=True, parent=party) PersonPostFactory( person=self.person, election=ElectionFactory(), party=party, ) response = self.client.get(self.person_url, follow=True) expected = f"{self.person.name}'s local party is {local_party.label}." self.assertContains(response, expected)
def test_previous_elections(self): past_election = ElectionFactoryLazySlug(election_date="2019-05-02", current=False) party = PartyFactory(party_name="Liberal Democrat", party_id="foo") PersonPostFactory( person=self.person, post_election__election=past_election, election=past_election, party=party, votes_cast=1000, ) response = self.client.get(self.person_url, follow=True) self.assertContains(response, "Previous Elections")
def test__correct_elections_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=True, slug="foobar") PersonPostFactory(election=election, person=self.person) response = self.client.get(self.person_url, follow=True) self.assertContains(response, election_name)
def setUp(self): self.election = ElectionFactory( name="2017 General Election", election_date="2017-06-08", slug="parl.2017-06-08", ) winners = [ { 'person_id': 2809, 'post_id': 'WMC:E14000803', 'post_label': 'Maidenhead', }, { 'person_id': 357, 'post_id': 'WMC:E14000608', 'post_label': 'Buckingham', }, { 'person_id': 2811, 'post_id': 'WMC:E14000803', 'post_label': 'Maidenhead', }, { 'person_id': 34291, 'post_id': 'WMC:E14000673', 'post_label': 'Dulwich and West Norwood', }, ] for person_info in winners: person = PersonFactory(pk=person_info['person_id']) post = PostFactory( ynr_id=person_info['post_id'], label=person_info['post_label'] ) PostElectionFactory( post=post, election=self.election ) PersonPostFactory( person=person, election=self.election, post=post )
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=True, election_date="2017-01-01", slug="foobar") PersonPostFactory( 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 the")
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 setUp(self): self.election = ElectionFactory( name="2017 General Election", election_date="2017-06-08", slug="parl.2017-06-08", ) winners = [ { "person_id": 2809, "post_id": "WMC:E14000803", "post_label": "Maidenhead", }, { "person_id": 357, "post_id": "WMC:E14000608", "post_label": "Buckingham", }, { "person_id": 2811, "post_id": "WMC:E14000803", "post_label": "Maidenhead", }, { "person_id": 34291, "post_id": "WMC:E14000673", "post_label": "Dulwich and West Norwood", }, ] for person_info in winners: person = PersonFactory(pk=person_info["person_id"]) post = PostFactory(ynr_id=person_info["post_id"], label=person_info["post_label"]) PostElectionFactory(post=post, election=self.election) PersonPostFactory(person=person, election=self.election, post=post)