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_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_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 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>.")
示例#7
0
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)
    def test_next_election_displayed(self, client):
        post = PostFactory()
        past = PostElectionFactory(
            post=post,
            election=ElectionFactoryLazySlug(
                election_date="2019-5-2",
                current=False,
            ),
        )
        # create a future election expected to be displayed
        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,
            "due to take place <strong>on Thursday 6 May 2021</strong>.",
        )
示例#9
0
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",
        )
        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",
        )

    @freeze_time("2017-3-22")
    @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)

    @freeze_time("2017-3-22")
    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)

    @freeze_time("2021-4-1")
    def test_displayable_in_past_not_returned(self):
        result = Husting.objects.displayable()
        assert self.hust not in result
        assert result.count() == 0

    @freeze_time("2021-4-1")
    def test_displayable_in_past_with_postevent_url_is_returned(self):
        self.hust.starts = datetime(2021, 3, 31, tzinfo=utc)
        self.hust.postevent_url = "http://example.com/"
        self.hust.save()
        result = Husting.objects.displayable()
        assert self.hust in result
        assert result.count() == 1

    @freeze_time("2021-4-1")
    def test_displayable_in_future_always_returned(self):
        self.hust.starts = datetime(2021, 4, 2, tzinfo=utc)
        self.hust.save()
        result = Husting.objects.displayable()
        assert self.hust in result
        assert result.count() == 1

    @freeze_time("2021-4-1")
    def test_displayable_on_day_always_returned(self):
        self.hust.starts = datetime(2021, 4, 1, tzinfo=utc)
        self.hust.save()
        result = Husting.objects.displayable()
        assert self.hust in result
        assert result.count() == 1

    @freeze_time("2021-4-1")
    def test_future(self):
        past = Husting.objects.get(pk=self.hust.pk)

        today = Husting.objects.get(pk=self.hust.pk)
        today.pk = None
        today.starts = datetime(2021, 4, 1, tzinfo=utc)
        today.save()

        future = Husting.objects.get(pk=self.hust.pk)
        future.pk = None
        future.starts = datetime(2021, 4, 2, tzinfo=utc)
        future.save()

        result = Husting.objects.future()
        assert past not in result
        assert today in result
        assert future in result
        assert result.count() == 2