Exemplo n.º 1
0
    def test_save_divisionset_before_start(self):
        """
        This seems strange on the face of it, but it does legitimately happen.

        For example, the Local Government Act 1972 reformed local
        government in England and Wales on 1 April 1974
        but Elections were held to the new authorities in 1973,
        and they acted as "shadow authorities" until the handover date.

        Similarly, when Northern Ireland was re-organised from 26
        local authorities into 11, they held elections using the new
        electoral divisions in 2014 even though the new local authorities
        weren't officially created until April 2015.
        """
        try:
            OrganisationDivisionSetFactory(organisation=self.org,
                                           start_date=date(2000, 1, 1),
                                           end_date=None)
        except ValidationError:
            self.fail("ValidationError raised unexpectedly!")
Exemplo n.º 2
0
    def test_invalid_division_wrong_subtype(self):
        naw_election_type = ElectionType.objects.get(election_type="naw")
        region_sub_type = ElectionSubType.objects.get(
            election_subtype="r", election_type=naw_election_type
        )
        naw_org = Organisation.objects.create(
            official_identifier="naw",
            organisation_type="naw",
            official_name="naw",
            slug="naw",
            territory_code="WLS",
            election_name="National Assembly for Wales elections",
            start_date=date(2016, 10, 1),
        )
        ElectedRole.objects.create(
            election_type=naw_election_type,
            organisation=naw_org,
            elected_title="Assembly Member",
            elected_role_name="Assembly Member for Foo",
        )

        naw_div_set = OrganisationDivisionSetFactory(organisation=naw_org)
        constituency_div = OrganisationDivisionFactory(
            divisionset=naw_div_set,
            name="Test Div",
            slug="test-div",
            division_election_sub_type="c",
        )
        builder = (
            ElectionBuilder("naw", "2017-06-08")
            .with_organisation(naw_org)
            .with_subtype(region_sub_type)
        )

        # constituency_div is a constituency
        # but this builder object expects a region
        with self.assertRaises(OrganisationDivision.ValidationError):
            builder.with_division(constituency_div)
Exemplo n.º 3
0
    def _create_models(self):
        self.date = date.today()
        self.date_str = self.date.strftime("%Y-%m-%d")

        self.election_type1 = ElectionType.objects.get(election_type="local")
        self.org1 = Organisation.objects.create(
            official_identifier="TEST1",
            organisation_type="local-authority",
            official_name="Test Council",
            slug="test",
            territory_code="ENG",
            election_name="Test Council local elections",
            start_date=date(2016, 10, 1),
        )

        self.elected_role1 = ElectedRole.objects.create(
            election_type=self.election_type1,
            organisation=self.org1,
            elected_title="Local Councillor",
            elected_role_name="Councillor for Test Council",
        )

        self.div_set = OrganisationDivisionSetFactory(organisation=self.org1)
        self.org_div_1 = OrganisationDivisionFactory(divisionset=self.div_set,
                                                     name="Test Div 1",
                                                     slug="test-div",
                                                     seats_total=3)
        self.org_div_2 = OrganisationDivisionFactory(divisionset=self.div_set,
                                                     name="Test Div 2",
                                                     slug="test-div-2")

        self.base_data = {
            "election_organisation": [self.org1],
            "election_type": self.election_type1,
            "date": self.date,
        }
Exemplo n.º 4
0
 def test_organisation_division_set_factory(self):
     ods = OrganisationDivisionSetFactory()
     assert ods.organisation.slug.startswith("org-")
Exemplo n.º 5
0
 def test_save_divisionset_before_start(self):
     with self.assertRaises(ValidationError):
         OrganisationDivisionSetFactory(
             organisation=self.org,
             start_date=date(2000, 1, 1),
         )
Exemplo n.º 6
0
    def test_creates_naw_id(self):
        naw_org = Organisation.objects.create(
            official_identifier="naw",
            organisation_type="naw",
            official_name="naw",
            slug="naw",
            territory_code="WLS",
            election_name="National Assembly for Wales elections",
            start_date=date(2016, 10, 1),
        )
        naw_election_type = ElectionType.objects.get(election_type="naw")
        naw_election_sub_type_c = ElectionSubType.objects.get(
            election_subtype="c", election_type=naw_election_type
        )
        naw_election_sub_type_r = ElectionSubType.objects.get(
            election_subtype="r", election_type=naw_election_type
        )
        ElectedRole.objects.create(
            election_type=naw_election_type,
            organisation=naw_org,
            elected_title="Assembly Member",
            elected_role_name="Assembly Member for Foo",
        )
        naw_div_set = OrganisationDivisionSetFactory(organisation=naw_org)
        org_div_3 = OrganisationDivisionFactory(
            divisionset=naw_div_set,
            name="Test Div 3",
            slug="test-div-3",
            division_election_sub_type="c",
        )
        org_div_4 = OrganisationDivisionFactory(
            divisionset=naw_div_set,
            name="Test Div 4",
            slug="test-div-4",
            division_election_sub_type="c",
        )
        org_div_5 = OrganisationDivisionFactory(
            divisionset=naw_div_set,
            name="Test Div 5",
            slug="test-div-5",
            division_election_sub_type="r",
        )

        all_data = {
            "election_organisation": [naw_org],
            "election_type": naw_election_type,
            "election_subtype": [naw_election_sub_type_c, naw_election_sub_type_r],
            "date": self.date,
        }

        all_data.update(
            {
                self.make_div_id(
                    org=naw_org, div=org_div_3, subtype="c"
                ): "contested",  # contested seat
                self.make_div_id(
                    org=naw_org, div=org_div_4, subtype="c"
                ): "by_election",  # by election
                self.make_div_id(org=naw_org, div=org_div_5, subtype="r"): "contested",
            }
        )

        expected_ids = [
            "naw." + self.date_str,
            "naw.c." + self.date_str,
            "naw.r." + self.date_str,
            "naw.c.test-div-3." + self.date_str,  # no 'by' suffix
            "naw.c.test-div-4.by." + self.date_str,  # 'by' suffix
            "naw.r.test-div-5." + self.date_str,
        ]
        expected_titles = [
            "National Assembly for Wales elections",
            "National Assembly for Wales elections (Constituencies)",
            "National Assembly for Wales elections (Regions)",
            "National Assembly for Wales elections (Constituencies) Test Div 3",
            "National Assembly for Wales elections (Constituencies) Test Div 4 by-election",
            "National Assembly for Wales elections (Regions) Test Div 5",
        ]

        self.run_test_with_data(
            all_data,
            expected_ids,
            expected_titles,
            subtypes=[naw_election_sub_type_c, naw_election_sub_type_r],
        )
    def test_all_expected_fields_returned(self):

        OrganisationFactory.reset_sequence(0)
        OrganisationDivisionFactory.reset_sequence(0)
        org = OrganisationFactory()
        div_set = OrganisationDivisionSetFactory(organisation=org)
        org_div = OrganisationDivisionFactory(divisionset=div_set,
                                              territory_code="ENG")
        ElectionWithStatusFactory(group=None,
                                  organisation=org,
                                  division=org_div)

        self.expected_object = json.loads("""
        {
            "group_type": null,
            "identifier_type": "ballot",
            "current": false,
            "poll_open_date": "2017-03-23",
            "election_id": "local.place-name-0.2017-03-23",
            "group": null,
            "division": {
                "name": "Division 0",
                "slug": "0",
                "divisionset": {
                    "start_date": "2017-05-04",
                    "legislation_url": "https://example.com/the-law",
                    "short_title": "Made up boundary changes",
                    "notes": "This is just for testing.",
                    "end_date": "2025-05-03",
                    "consultation_url": "https://example.com/consultation"
                },
                "seats_total": null,
                "division_election_sub_type": "",
                "division_subtype": "",
                "division_type": "test",
                "official_identifier": "0",
                "territory_code": "ENG"
            },
            "election_type": {
                "name": "Local elections",
                "election_type": "local"
            },
            "explanation": null,
            "voting_system": {
                "slug": "",
                "name": "",
                "uses_party_lists": false
            },
            "children": [],
            "election_subtype": null,
            "organisation": {
                "url": "http://testserver/api/organisations/local-authority/0/2016-10-01/",
                "slug": "org-0",
                "territory_code": "ENG",
                "organisation_subtype": "",
                "common_name": "Organisation 0",
                "official_name": "The Organisation 0 Council",
                "organisation_type": "local-authority",
                "election_name": "",
                "official_identifier": "0",
                "start_date": "2016-10-01",
                "end_date": null
            },
            "election_title": "Election 0",
            "elected_role": "Councillor",
            "seats_contested": 1,
            "tmp_election_id": null,
            "metadata": null,
            "deleted": false,
            "cancelled": false,
            "replaces": null,
            "replaced_by": null
        }
        """)

        resp = self.client.get("/api/elections/")
        data = resp.json()
        self.assertEqual(data["results"][0], self.expected_object)

        resp = self.client.get("/api/elections/local.place-name-0.2017-03-23/")
        data = resp.json()
        self.assertEqual(data, self.expected_object)

        resp = self.client.get(
            "/api/elections/local.place-name-0.2017-03-23/geo/",
            content_type="application/json",
        )
        data = resp.json()
        self.assertEqual(data["properties"], self.expected_object)
Exemplo n.º 8
0
    def test_get_seats_contested(self):
        sp_election_type = ElectionType.objects.get(election_type="sp")
        region_sub_type = ElectionSubType.objects.get(
            election_subtype="r", election_type=sp_election_type
        )
        constituency_sub_type = ElectionSubType.objects.get(
            election_subtype="c", election_type=sp_election_type
        )
        sp_org = Organisation.objects.create(
            official_identifier="sp",
            organisation_type="sp",
            official_name="Scottish Parliament",
            slug="sp",
            election_name="Scottish parliament election",
            territory_code="SCT",
            start_date=date(1999, 5, 6),
        )

        ElectedRole.objects.create(
            election_type=sp_election_type,
            organisation=sp_org,
            elected_title="Member of the Scottish Parliament",
            elected_role_name="Member of the Scottish Parliament",
        )

        sp_div_set = OrganisationDivisionSetFactory(organisation=sp_org)

        sp_r_div = OrganisationDivisionFactory(
            divisionset=sp_div_set,
            name="sp Div 1",
            slug="sp-div-1",
            seats_total=7,
            division_election_sub_type="r",
        )
        sp_c_div = OrganisationDivisionFactory(
            divisionset=sp_div_set,
            name="sp Div 2",
            slug="sp-div-2",
            division_election_sub_type="c",
        )

        builder_1 = (
            ElectionBuilder("sp", "2021-5-06")
            .with_organisation(sp_org)
            .with_division(sp_r_div)
            .with_subtype(region_sub_type)
        )
        builder_2 = (
            ElectionBuilder("sp", "2021-5-06")
            .with_organisation(sp_org)
            .with_division(sp_c_div)
            .with_subtype(constituency_sub_type)
        )

        ballot_1 = builder_1.build_ballot(None)
        ballot_1.save()

        ballot_2 = builder_2.build_ballot(None)
        ballot_2.save()

        self.assertEqual(7, ballot_1.seats_contested)
        self.assertEqual(1, ballot_2.seats_contested)