def _setup_data(self): one_day = timedelta(days=1) self.future_date = date.today() + one_day london_assembly = ParliamentaryChamberFactory.create( slug="london-assembly", name="London Assembly") election_lac = ElectionFactory.create( slug="gla.c.2016-05-05", organization=london_assembly, name="2016 London Assembly Election (Constituencies)", election_date=self.future_date.isoformat(), ) self.election_gla = ElectionFactory.create( slug="gla.a.2016-05-05", organization=london_assembly, name="2016 London Assembly Election (Additional)", election_date=self.future_date.isoformat(), ) PostFactory.create( elections=(election_lac, ), organization=london_assembly, slug="lambeth-and-southwark", label="Assembly Member for Lambeth and Southwark", ) self.post = PostFactory.create( elections=(self.election_gla, ), organization=london_assembly, slug="london", label="Assembly Member", )
def _setup_extra_posts(self): # Create some extra posts and areas: london_assembly = ParliamentaryChamberFactory.create( slug="london-assembly", name="London Assembly") election_lac = ElectionFactory.create( slug="gla.c.2016-05-05", organization=london_assembly, name="2016 London Assembly Election (Constituencies)", ) election_gla = ElectionFactory.create( slug="gla.a.2016-05-05", organization=london_assembly, name="2016 London Assembly Election (Additional)", ) PostFactory.create( elections=(election_lac, ), organization=london_assembly, slug="lambeth-and-southwark", label="Assembly Member for Lambeth and Southwark", ) PostFactory.create( elections=(election_gla, ), organization=london_assembly, slug="london", label="2016 London Assembly Election (Additional)", )
def setUp(self): gb_parties = PartySetFactory.create(slug="gb", name="Great Britain") commons = ParliamentaryChamberFactory.create() election = ElectionFactory.create( slug="parl.2017-03-23", name="2015 General Election", organization=commons, election_date="2017-03-23", ) PostFactory.create( elections=(election, ), organization=commons, slug="dulwich-and-west-norwood", label="Member of Parliament for Dulwich and West Norwood", party_set=gb_parties, )
def setUp(self): election = ElectionFactory.create(slug="2015", name="2015 General Election") commons = ParliamentaryChamberFactory.create() self.post = PostFactory.create( elections=(election, ), organization=commons, slug="65808", label="Member of Parliament for Dulwich and West Norwood", )
def setUp(self): election = ElectionFactory.create(slug="parl.2015-05-07", name="2015 General Election") commons = ParliamentaryChamberFactory.create() self.post = PostFactory.create( elections=(election, ), organization=commons, slug="dulwich-and-west-norwood", label="Member of Parliament for Dulwich and West Norwood", ) self.ballot = self.post.ballot_set.get()
def setUp(self): gb_parties = PartySetFactory.create(slug="gb", name="Great Britain") self.election = ElectionFactory.create( slug="parl.2015-05-07", name="2015 General Election", current=True ) commons = ParliamentaryChamberFactory.create() self.post = PostFactory.create( elections=(self.election,), organization=commons, slug="dulwich-and-west-norwood", label="Member of Parliament for Dulwich and West Norwood", party_set=gb_parties, ) self.ballot = self.post.ballot_set.get(election=self.election)
def setUp(self): super().setUp() constituencies = {} for slug, cons_name, country in [ ("66090", "Cardiff Central", "Wales"), ("65672", "Doncaster North", "England"), ("65719", "South Shields", "England"), ]: constituencies[cons_name] = PostFactory.create( elections=(self.election, self.earlier_election), organization=self.commons, slug=slug, label="Member of Parliament for {}".format(cons_name), group=country, ) person = PersonFactory.create(id=3056, name="Ed Miliband") MembershipFactory.create( person=person, post=constituencies["Doncaster North"], party=self.labour_party, ballot=self.election.ballot_set.get( post=constituencies["Doncaster North"] ), ) person = PersonFactory.create(id=3814, name="David Miliband") MembershipFactory.create( person=person, post=constituencies["South Shields"], party=self.labour_party, ballot=self.earlier_election.ballot_set.get( post=constituencies["South Shields"] ), ) conservative_opponent = PersonFactory.create( id="6648", name="Mark Fletcher" ) MembershipFactory.create( person=conservative_opponent, post=constituencies["South Shields"], party=self.conservative_party, ballot=self.election.ballot_set.get( post=constituencies["South Shields"] ), )
def test_invalid_merge(self): other_local_post = PostFactory.create( elections=(self.local_election, ), slug="DIW:E05005005", label="Shepway North Ward", party_set=self.gb_parties, organization=self.local_council, ) self.dest_person.memberships.create(post_election=self.local_pee) self.source_person.memberships.create( post_election=other_local_post.postextraelection_set.get()) self.assertEqual(Person.objects.count(), 2) merger = PersonMerger(self.dest_person, self.source_person) with self.assertRaises(InvalidMergeError): merger.merge() # Make sure we still have two people self.assertEqual(Person.objects.count(), 2)
def test_results_for_candidates_for_postcode(self, mock_requests): one_day = timedelta(days=1) self.future_date = date.today() + one_day london_assembly = ParliamentaryChamberFactory.create( slug="london-assembly", name="London Assembly" ) election_lac = ElectionFactory.create( slug="gla.c.2016-05-05", organization=london_assembly, name="2016 London Assembly Election (Constituencies)", election_date=self.future_date.isoformat(), ) self.election_gla = ElectionFactory.create( slug="gla.a.2016-05-05", organization=london_assembly, name="2016 London Assembly Election (Additional)", election_date=self.future_date.isoformat(), ) PostFactory.create( elections=(election_lac,), organization=london_assembly, slug="lambeth-and-southwark", label="Assembly Member for Lambeth and Southwark", ) self.post = PostFactory.create( elections=(self.election_gla,), organization=london_assembly, slug="london", label="Assembly Member", ) self.person.memberships.all().delete() MembershipFactory.create( person=self.person, post=self.post, party=self.labour_party, post_election=self.election_gla.postextraelection_set.get( post=self.post ), ) membership_pk = self.person.memberships.first().pk self.maxDiff = None mock_requests.get.side_effect = fake_requests_for_every_election response = self.app.get( "/api/next/candidates_for_postcode/?postcode=SE24+0AG" ) output = response.json self.assertEqual(len(output), 2) expected = [ { "candidates": [], "election_date": self.future_date.isoformat(), "election_id": "gla.c.2016-05-05", "election_name": "2016 London Assembly Election (Constituencies)", "organization": "London Assembly", "post": { "post_candidates": None, "post_name": "Assembly Member for Lambeth and Southwark", "post_slug": "lambeth-and-southwark", }, }, { "candidates": [ { "birth_date": "", "death_date": "", "email": None, "gender": "", "honorific_prefix": "", "honorific_suffix": "", "id": 2009, "images": [ { "copyright": "example-license", "id": self.person_image.pk, "image_url": "/media/images/images/imported.jpg", "is_primary": True, "md5sum": "md5sum", "notes": "", "source": "Found on the candidate's Flickr feed", "uploading_user": "******", "user_copyright": "", "user_notes": "Here's an image...", } ], "memberships": [ { "elected": None, "election": { "id": "gla.a.2016-05-05", "name": "2016 London Assembly Election (Additional)", "url": "http://testserver/api/next/elections/gla.a.2016-05-05/", }, "end_date": None, "id": membership_pk, "label": "", "party": { "ec_id": "PP53", "legacy_slug": "party:53", "name": "Labour Party", }, "party_list_position": None, "person": { "id": 2009, "name": "Tessa Jowell", "url": "http://testserver/api/next/persons/2009/", }, "post": { "id": "london", "label": "Assembly Member", "slug": "london", "url": "http://testserver/api/next/posts/london/", }, "role": "Candidate", "start_date": None, "url": "http://testserver/api/next/memberships/{}/".format( membership_pk ), } ], "name": "Tessa Jowell", "other_names": [], "sort_name": "", "thumbnail": "http://testserver/media/cache/69/5d/695d95b49b6a6ab3aebe728d2ec5162b.jpg", "url": "http://testserver/api/next/persons/2009/", } ], "election_date": self.future_date.isoformat(), "election_id": "gla.a.2016-05-05", "election_name": "2016 London Assembly Election (Additional)", "organization": "London Assembly", "post": { "post_candidates": None, "post_name": "Assembly Member", "post_slug": "london", }, }, ] self.assertEqual(expected, output)
def test_conflicting_standing_in_values_regression(self): """ https://github.com/DemocracyClub/yournextrepresentative/issues/811 This was both a bug and a problem with the strategy of merging. The bug was that is was possible to have party info against an election in the not-standing list. The problem was when merging two people with a not standing value and a membership in the same election, we would keep both objects, leaving the database in an inconsistent state. The merging logic was changed to keep the membership and discard the not-standing status. This test checks for a regression of both cases. """ self.local_election.election_date = date.today() + timedelta(days=1) self.local_election.save() other_local_post = PostFactory.create( elections=(self.local_election,), slug="LBW:E05000601", label="Hoe Street", party_set=self.gb_parties, organization=self.local_council, ) ballot = other_local_post.postextraelection_set.get() # Create person 1 response = self.app.get(ballot.get_absolute_url(), user=self.user) form = response.forms["new-candidate-form"] form["name"] = "Imaginary Candidate" form[ "party_GB_{}".format(self.local_election.slug) ] = self.green_party.ec_id form[ "constituency_{}".format(self.local_election.slug) ] = ballot.post.slug form["standing_{}".format(self.local_election.slug)] = "standing" form[ "source" ] = "Testing adding a new candidate to a locked constituency" response = form.submit() person_1 = response.context["object"] # Create person 2 response = self.app.get(ballot.get_absolute_url(), user=self.user) form = response.forms["new-candidate-form"] form["name"] = "Imaginary Candidate" form[ "party_GB_{}".format(self.local_election.slug) ] = self.green_party.ec_id form[ "constituency_{}".format(self.local_election.slug) ] = ballot.post.slug form["standing_{}".format(self.local_election.slug)] = "standing" form[ "source" ] = "Testing adding a new candidate to a locked constituency" response = form.submit() person_2 = response.context["object"] # Remove the membership for person 2 response = self.app.get( "/person/{}/update".format(person_2.pk), user=self.user ) form = response.forms["person-details"] form["standing_{}".format(self.local_election.slug)].force_value( "not-standing" ) form["source"] = "Mumsnet" form.submit() # Merge the two people. What do we expect? # We have a standing in and a not-standing record for the same election # so we decide to remove the not-standing and keep the membership merger = PersonMerger(person_1, person_2) merger.merge() person_1.refresh_from_db() version_data = json.loads(person_1.versions)[0]["data"] self.assertEqual( version_data["standing_in"], { "local.maidstone.2016-05-05": { "name": "Hoe Street", "post_id": "LBW:E05000601", } }, ) self.assertEqual( version_data["party_memberships"], { "local.maidstone.2016-05-05": { "id": "party:63", "name": "Green Party", } }, )
def create_post(self, post_label): ps, _ = PartySet.objects.update_or_create(slug="GB") org = OrganizationFactory(name="Baz council") return PostFactory(label=post_label, organization=org, party_set=ps)