def test_scotland_local_stv(self): """ Scottish local elections have the type of `local` but unlike the rest of the UK that uses FPTP, it uses STV """ # "Normal" UK local election is FPTP election_id = ElectionBuilder('local', '2017-05-04')\ .build_election_group() assert election_id.voting_system.slug == "FPTP" scot_org = OrganisationFactory(territory_code="SCT", ) ElectedRole.objects.create( election_type=self.election_type1, organisation=scot_org, elected_title="MSP", elected_role_name="MSP for Foo Town", ) # Scotish local elections are STV scot_id = ElectionBuilder('local', '2017-05-04')\ .with_organisation(scot_org)\ .build_organisation_group(None) assert scot_id.voting_system.slug == "STV"
def test_invalid_subtype(self): naw_election_type = ElectionType.objects.get(election_type="naw") invalid_sub_type = ElectionSubType.objects.create( election_subtype="x", election_type=self.election_type1) builder = ElectionBuilder(naw_election_type, "2017-06-08") with self.assertRaises(ElectionSubType.ValidationError): builder.with_subtype(invalid_sub_type)
def test_organisation_date_range_invalid(self): builder = ElectionBuilder("local", "2001-01-01") # delete the relationship between org1 and local elections self.elected_role1.delete() with self.assertRaises(Organisation.ValidationError): builder.with_organisation(self.org1)
def test_seats_contested_local_election(self): builder = ElectionBuilder('local', '2017-06-08')\ .with_organisation(self.org1)\ .with_division(self.org_div_1) election = builder.build_ballot(None) election.save() self.assertIsNone(election.seats_contested) self.assertEqual(3, election.seats_total)
def test_invalid_organisation(self): builder = ElectionBuilder('local', '2017-06-08') # delete the relationship between org1 and local elections self.elected_role1.delete() with self.assertRaises(Organisation.ValidationError): builder.with_organisation(self.org1)
def setUp(self): super().setUp() Election.objects.all().delete() self.parent_election = ElectionBuilder('local', '2017-06-08')\ .build_election_group() self.child_election = ElectionBuilder('local', '2017-06-08')\ .with_organisation(self.org1)\ .build_organisation_group(self.parent_election)
def test_with_metadata(self): snooper = SnoopedElection.objects.create() builder = ElectionBuilder('local', '2017-06-08')\ .with_organisation(self.org1)\ .with_division(self.org_div_1)\ .with_source('foo/bar.baz')\ .with_snooped_election(snooper.id) election = builder.build_ballot(None) election.save() self.assertEqual('foo/bar.baz', election.source) assert isinstance(election.snooped_election, SnoopedElection)
def setUp(self): super().setUp() Election.private_objects.all().delete() self.election_group = ElectionBuilder( "local", "2017-06-08").build_election_group() self.org_group = (ElectionBuilder( "local", "2017-06-08").with_organisation( self.org1).build_organisation_group(self.election_group)) self.ballot = (ElectionBuilder( "local", "2017-06-08").with_organisation(self.org1).with_division( self.org_div_1).build_ballot(self.org_group))
def test_eq(self): eb1 = ElectionBuilder("local", "2017-06-08") eb2 = (ElectionBuilder( "local", "2017-06-08").with_source("foo/bar.baz").with_snooped_election(7)) # these should be 'equal' because only the meta-data differs self.assertEqual(eb1, eb2) eb2 = eb2.with_organisation(self.org1) # now these objects will build funamentally different elections self.assertNotEqual(eb1, eb2)
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", ) constituency_div = OrganisationDivisionFactory( organisation=naw_org, 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)
def test_seats_contested_local_by_election(self): builder = (ElectionBuilder("local", "2017-06-08").with_organisation( self.org1).with_division(self.org_div_1).with_contest_type("by")) election = builder.build_ballot(None) election.save() self.assertEqual(1, election.seats_contested) self.assertEqual(3, election.seats_total)
def setUp(self): super().setUp() self.election = ElectionBuilder("local", "2017-06-08").build_election_group() self.organisation = (ElectionBuilder( "local", "2017-06-08").with_organisation( self.org1).build_organisation_group(None)) self.ballot1 = (ElectionBuilder( "local", "2017-06-08").with_organisation(self.org1).with_division( self.org_div_1).build_ballot(None)) self.ballot2 = (ElectionBuilder( "local", "2017-06-08").with_organisation(self.org1).with_division( self.org_div_2).build_ballot(None))
def test_with_metadata(self): snooper = SnoopedElection.objects.create() builder = (ElectionBuilder("local", "2017-06-08").with_organisation( self.org1).with_division(self.org_div_1).with_source( "foo/bar.baz").with_snooped_election(snooper.id)) election = builder.build_ballot(None) election.save() self.assertEqual("foo/bar.baz", election.source) assert isinstance(election.snooped_election, SnoopedElection)
def test_invalid_division_not_child_of_org(self): org2 = Organisation.objects.create( official_identifier="TEST2", organisation_type="local-authority", official_name="Test Council", slug="test2", territory_code="ENG", election_name="Test2 Council Local Elections", start_date=date(2016, 10, 1), ) ElectedRole.objects.create( election_type=self.election_type1, organisation=org2, elected_title="Local Councillor", elected_role_name="Councillor for Test2 Council", ) builder = ElectionBuilder("local", "2017-06-08").with_organisation(org2) # self.org_div_1 is not a child of org2 # its a child of self.org1 with self.assertRaises(OrganisationDivision.ValidationError): builder.with_division(self.org_div_1)
def test_with_groups(self): builder = (ElectionBuilder("local", "2017-06-08").with_organisation( self.org1).with_division(self.org_div_1)) election_group = builder.build_election_group() org_group = builder.build_organisation_group(election_group) ballot = builder.build_ballot(org_group) ballot.save() # calling save() on the ballot object # should also save its 2x parent groups self.assertEqual(3, Election.private_objects.all().count()) self.assertIsNotNone(election_group.id) self.assertIsNotNone(org_group.id) self.assertIsNotNone(ballot.id)
def setUp(self): super().setUp() self.election = \ ElectionBuilder('local', '2017-06-08')\ .build_election_group() self.organisation =\ ElectionBuilder('local', '2017-06-08')\ .with_organisation(self.org1)\ .build_organisation_group(None) self.ballot1 =\ ElectionBuilder('local', '2017-06-08')\ .with_organisation(self.org1)\ .with_division(self.org_div_1)\ .build_ballot(None) self.ballot2 =\ ElectionBuilder('local', '2017-06-08')\ .with_organisation(self.org1)\ .with_division(self.org_div_2)\ .build_ballot(None)
def test_invalid_division(self): org2 = Organisation.objects.create( official_identifier='TEST2', organisation_type='local-authority', official_name="Test Council", gss="X00000003", slug="test2", territory_code="ENG", election_name="Test2 Council Local Elections", ) ElectedRole.objects.create( election_type=self.election_type1, organisation=org2, elected_title="Local Councillor", elected_role_name="Councillor for Test2 Council", ) builder = ElectionBuilder('local', '2017-06-08')\ .with_organisation(org2) # self.org_div_1 is not a child of org2 # its a child of self.org1 with self.assertRaises(OrganisationDivision.ValidationError): builder.with_division(self.org_div_1)
def test_created_with_status(self): builder = (ElectionBuilder("local", "2017-06-08").with_organisation( self.org1).with_division(self.org_div_1)) election_group = builder.build_election_group() org_group = builder.build_organisation_group(election_group) ballot = builder.build_ballot(org_group) ballot.save() default_status = DEFAULT_STATUS self.assertEqual(default_status, ballot.moderation_status.short_label) self.assertEqual(default_status, org_group.moderation_status.short_label) self.assertEqual(default_status, election_group.moderation_status.short_label)
class TestElectionModel(BaseElectionCreatorMixIn, TestCase): def setUp(self): super().setUp() Election.objects.all().delete() self.parent_election = ElectionBuilder('local', '2017-06-08')\ .build_election_group() self.child_election = ElectionBuilder('local', '2017-06-08')\ .with_organisation(self.org1)\ .build_organisation_group(self.parent_election) def test_recursive_save(self): # table should be empty before we start self.assertEqual(0, Election.objects.count()) # saving the child record should implicitly save the parent record too self.child_election.save() self.assertEqual(2, Election.objects.count()) def test_transaction_rollback_parent(self): # table should be empty before we start self.assertEqual(0, Election.objects.count()) # doing this will cause save() to throw a exception # if we try to save parent_record self.parent_election.organisation_id = "foo" try: self.child_election.save() except ValueError: pass # the exception should have prevented both the # parent and child records from being saved self.assertEqual(0, Election.objects.count()) def test_transaction_rollback_child(self): # table should be empty before we start self.assertEqual(0, Election.objects.count()) # doing this will cause save() to throw a exception # if we try to save child_record self.child_election.organisation_id = "foo" try: self.child_election.save() except ValueError: pass # the exception should have prevented both the # parent and child records from being saved self.assertEqual(0, Election.objects.count())
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)
def _make_ids_for_date(date): x = ElectionBuilder("local", date) x.with_organisation(org) x.with_division(org_div) return x
class TestElectionModel(BaseElectionCreatorMixIn, TestCase): def setUp(self): super().setUp() Election.private_objects.all().delete() self.election_group = ElectionBuilder( "local", "2017-06-08").build_election_group() self.org_group = (ElectionBuilder( "local", "2017-06-08").with_organisation( self.org1).build_organisation_group(self.election_group)) self.ballot = (ElectionBuilder( "local", "2017-06-08").with_organisation(self.org1).with_division( self.org_div_1).build_ballot(self.org_group)) def test_recursive_save_group(self): # table should be empty before we start self.assertEqual(0, Election.private_objects.count()) # saving the child record should implicitly save the parent record too self.org_group.save() self.assertEqual(2, Election.private_objects.count()) def test_recursive_save_ballot(self): # table should be empty before we start self.assertEqual(0, Election.private_objects.count()) # From a performance perspective, saving a ballot and 2 parent groups # is the worst-case scenario for database I/O # we should monitor this and be aware if this number increases with self.assertNumQueries(19): self.ballot.save() # saving the child record should implicitly save the parent records too self.assertEqual(3, Election.private_objects.count()) def test_transaction_rollback_parent(self): # table should be empty before we start self.assertEqual(0, Election.private_objects.count()) # doing this will cause save() to throw a exception # if we try to save parent_record self.election_group.organisation_id = "foo" try: self.org_group.save() except ValueError: pass # the exception should have prevented both the # parent and child records from being saved self.assertEqual(0, Election.private_objects.count()) def test_transaction_rollback_child(self): # table should be empty before we start self.assertEqual(0, Election.private_objects.count()) # doing this will cause save() to throw a exception # if we try to save child_record self.org_group.organisation_id = "foo" try: self.org_group.save() except ValueError: pass # the exception should have prevented both the # parent and child records from being saved self.assertEqual(0, Election.private_objects.count()) def test_related_object_save(self): # table should be empty before we start self.assertEqual(0, ModerationHistory.objects.count()) # the first time we save a record, we should create # a corresponding moderation status event self.election_group.save() self.assertEqual(1, ModerationHistory.objects.count()) # saving the same record again shouldn't though self.election_group.seats_contests = 7 self.election_group.source = "some bloke down the pub told me" self.election_group.save() self.assertEqual(1, ModerationHistory.objects.count()) def test_save_with_status(self): self.election_group.save() self.assertEqual(self.election_group.moderation_status.short_label, DEFAULT_STATUS) self.election_group.save(status=ModerationStatuses.approved.value) self.assertEqual( self.election_group.moderation_status.short_label, ModerationStatuses.approved.value, )