def test_event_identity_equality_and_inequality(self): with example_file(EXAMPLE_EVENT_JSON) as fname: popolo_a = Popolo.from_filename(fname) event_a = popolo_a.events.first with example_file(EXAMPLE_EVENT_JSON) as fname: popolo_b = Popolo.from_filename(fname) event_b = popolo_b.events.first assert event_a == event_b assert not (event_a != event_b)
def test_post_identity_equality_and_inequality(self): with example_file(EXAMPLE_POST_JSON) as fname: popolo_a = Popolo.from_filename(fname) post_a = popolo_a.posts.first with example_file(EXAMPLE_POST_JSON) as fname: popolo_b = Popolo.from_filename(fname) post_b = popolo_b.posts.first assert post_a == post_b assert not (post_a != post_b)
def test_hash_magic_method(self): with example_file(EXAMPLE_MULTIPLE_MEMBERSHIPS) as fname: popolo_a = Popolo.from_filename(fname) popolo_b = Popolo.from_filename(fname) set_of_memberships = set() for m in popolo_a.memberships: set_of_memberships.add(m) for m in popolo_b.memberships: set_of_memberships.add(m) assert len(set_of_memberships) == 4
def test_hash_magic_method(self): with example_file(EXAMPLE_TWO_PEOPLE) as fname: popolo_a = Popolo.from_filename(fname) popolo_b = Popolo.from_filename(fname) set_of_people = set() for p in popolo_a.persons: set_of_people.add(p) for p in popolo_b.persons: set_of_people.add(p) assert len(set_of_people) == 2
def test_person_equality_and_inequality(self): with example_file(EXAMPLE_TWO_PEOPLE) as fname: person_norma_a = Popolo.from_filename(fname).persons[0] person_norma_b = Popolo.from_filename(fname).persons[0] person_harry = Popolo.from_filename(fname).persons[1] assert person_norma_a == person_norma_b assert not (person_norma_a != person_norma_b) assert person_harry == person_harry assert not (person_norma_a == person_harry) assert person_norma_a != person_harry
def test_equality_of_memberships(self): with example_file(EXAMPLE_SINGLE_MEMBERSHIP) as fname: # Create the same membership via two Popolo objects - they # should still be equal. popolo_a = Popolo.from_filename(fname) assert len(popolo_a.memberships) == 1 m_a = popolo_a.memberships[0] popolo_b = Popolo.from_filename(fname) assert len(popolo_b.memberships) == 1 m_b = popolo_b.memberships[0] assert m_a == m_b assert not (m_a != m_b)
def test_membership_has_person_id_and_organisation_id(self): with example_file(EXAMPLE_SINGLE_MEMBERSHIP) as fname: popolo = Popolo.from_filename(fname) assert len(popolo.memberships) == 1 m = popolo.memberships[0] assert m.person_id == 'SP-937-215' assert m.organization_id == 'starfleet'
def test_membership_should_not_have_name(self): with example_file(EXAMPLE_SINGLE_MEMBERSHIP) as fname: popolo = Popolo.from_filename(fname) assert len(popolo.memberships) == 1 m = popolo.memberships[0] with pytest.raises(AttributeError): m.name
def test_person_facebook_and_links_list(self): with example_file(b''' { "persons": [ { "name": "Harry Truman", "links": [ { "note": "facebook", "url": "https://facebook.example.com/harry-s-truman" }, { "note": "wikia", "url": "http://twinpeaks.wikia.com/wiki/Harry_S._Truman" } ] } ] } ''') as fname: popolo = Popolo.from_filename(fname) person = popolo.persons.first assert person.facebook == \ 'https://facebook.example.com/harry-s-truman' assert person.links == [{ "note": "facebook", "url": "https://facebook.example.com/harry-s-truman" }, { "note": "wikia", "url": "http://twinpeaks.wikia.com/wiki/Harry_S._Truman" }]
def test_post_has_organization_id(self): with example_file(EXAMPLE_POST_JSON) as fname: popolo = Popolo.from_filename(fname) assert len(popolo.posts) == 2 post = popolo.posts[0] assert post.organization_id == \ '574eff8e-8171-4f2b-8279-60ed8dec1a2a'
def test_multiple_identfiers(self): with example_file(b''' { "organizations": [ { "id": "starfleet", "name": "Starfleet", "identifiers": [ { "identifier": "Q288523", "scheme": "wikidata" }, { "identifier": "Q288523-also", "scheme": "wikidata" } ] } ] } ''') as fname: popolo = Popolo.from_filename(fname) assert len(popolo.organizations) == 1 o = popolo.organizations[0] with pytest.raises(MultipleObjectsReturned): o.wikidata
def test_membership_current_true(self, mock_date): mock_date.today.return_value = date(1784, 4, 30) mock_date.side_effect = lambda *args, **kw: date(*args, **kw) with example_file(EXAMPLE_MEMBERSHIP_ALL_FIELDS) as fname: popolo = Popolo.from_filename(fname) m = popolo.memberships[0] assert m.current
def test_organization_links_list(self): with example_file( b''' { "organizations": [ { "id": "starfleet", "name": "Starfleet", "links": [ { "url": "https://en.wikipedia.org/wiki/Starfleet", "note": "Wikipedia" }, { "url": "http://memory-alpha.wikia.com/wiki/Starfleet", "note": "Memory Alpha" } ] } ] } ''') as fname: popolo = Popolo.from_filename(fname) assert len(popolo.organizations) == 1 o = popolo.organizations[0] assert o.links == [ { 'url': 'https://en.wikipedia.org/wiki/Starfleet', 'note': 'Wikipedia', }, { 'url': 'http://memory-alpha.wikia.com/wiki/Starfleet', 'note': 'Memory Alpha', }, ]
def test_person_multiple_names_at_one_date(self): with example_file(b''' { "persons": [ { "name": "Bob", "other_names": [ { "name": "Robert", "start_date": "1989-01-01", "end_date": "1999-12-31" }, { "name": "Bobby", "start_date": "1989-01-01", "end_date": "2012-12-31" } ] } ] } ''') as fname: popolo = Popolo.from_filename(fname) person = popolo.persons.first with pytest.raises(Exception) as excinfo: person.name_at(date(1996, 1, 1)) expected_substring = "Multiple names for <Person: Bob> " \ "found at date 1996-01-01" assert str(expected_substring) in \ str(excinfo)
def test_person_identifier(self): with example_file(b''' { "persons": [ { "name": "Bob", "identifiers": [ { "identifier": "161", "scheme": "everypolitician_legacy" }, { "identifier": "Q4766480", "scheme": "wikidata" } ] } ] } ''') as fname: popolo = Popolo.from_filename(fname) person = popolo.persons.first assert person.get_identifier("wikidata") == "Q4766480" assert person.get_identifier("parlparse") == None
def test_single_person_name(self): with example_file(b'{"persons": [{"name": "Harry Truman"}]}') as fname: popolo = Popolo.from_filename(fname) assert len(popolo.persons) == 1 person = popolo.persons[0] assert person.name == 'Harry Truman' assert person.name_at(date(2016, 1, 11)) == 'Harry Truman'
def test_person_images(self): with example_file(u''' { "persons": [ { "name": "Бганба Валерий Рамшухович", "images": [ { "url": "http://www.parlamentra.org/upload/iblock/b85/%D1%80%D0%B0%D0%BC.jpg" }, { "url": "https://upload.wikimedia.org/wikipedia/commons/a/a3/Бганба_Валерий_Рамшухович.jpg" } ] } ] } '''.encode('utf-8')) as fname: popolo = Popolo.from_filename(fname) person = popolo.persons.first assert person.images == [{ 'url': 'http://www.parlamentra.org' '/upload/iblock/b85/%D1%80%D0%B0%D0%BC.jpg' }, { 'url': u'https://upload.wikimedia.org' u'/wikipedia/commons/a/a3/\u0411\u0433\u0430\u043d' u'\u0431\u0430_\u0412\u0430\u043b\u0435\u0440\u0438' u'\u0439_\u0420\u0430\u043c\u0448\u0443\u0445\u043e' u'\u0432\u0438\u0447.jpg' }]
def test_person_other_names(self): with example_file(b''' { "persons": [ { "id": "john-q-public", "name": "Mr. John Q. Public, Esq.", "other_names": [ { "name": "Mr. Ziggy Q. Public, Esq.", "start_date": "1920-01", "end_date": "1949-12-31", "note": "Birth name" }, { "name": "Dragonsbane", "note": "LARP character name" } ] } ] } ''') as fname: popolo = Popolo.from_filename(fname) person = popolo.persons.first assert person.other_names == [{ 'end_date': '1949-12-31', 'name': 'Mr. Ziggy Q. Public, Esq.', 'note': 'Birth name', 'start_date': '1920-01' }, { "name": "Dragonsbane", "note": "LARP character name" }]
def test_person_contact_detail_twitter_and_contact_details_list(self): with example_file(b''' { "persons": [ { "name": "Harry Truman", "contact_details": [ { "type": "twitter", "value": "notarealtwitteraccountforharry" }, { "type": "phone", "value": "555-5555" } ] } ] } ''') as fname: popolo = Popolo.from_filename(fname) person = popolo.persons.first assert person.twitter == \ 'notarealtwitteraccountforharry' assert person.contact_details == [{ "type": "twitter", "value": "notarealtwitteraccountforharry" }, { "type": "phone", "value": "555-5555" }] assert person.twitter_all == ['notarealtwitteraccountforharry']
def test_identifiers_list(self): with example_file( b''' { "organizations": [ { "id": "starfleet", "name": "Starfleet", "identifiers": [ { "identifier": "Q288523", "scheme": "wikidata" }, { "identifier": "123456", "scheme": "made-up-id" } ] } ] } ''') as fname: popolo = Popolo.from_filename(fname) assert len(popolo.organizations) == 1 o = popolo.organizations[0] assert o.identifiers == [ { 'identifier': 'Q288523', 'scheme': 'wikidata', }, { 'identifier': '123456', 'scheme': 'made-up-id', }, ]
def test_get_person_with_image_and_wikidata(self): # n.b. this is actually the Wikidata ID for the actor who # played Harry Truman; I couldn't find one for the character. with example_file(b''' { "persons": [ { "name": "Harry Truman", "image": "http://twin-peaks.example.org/harry.jpg", "identifiers": [ { "scheme": "wikidata", "identifier": "Q1343162" } ] } ] } ''') as fname: popolo = Popolo.from_filename(fname) person = popolo.persons.first assert person.wikidata == 'Q1343162' assert person.image == 'http://twin-peaks.example.org/harry.jpg' assert person.identifiers == [{ 'scheme': 'wikidata', 'identifier': 'Q1343162' }]
def test_term_current_true(self, mock_date): mock_date.today.return_value = date(2013, 1, 1) mock_date.side_effect = lambda *args, **kw: date(*args, **kw) with example_file(EXAMPLE_EVENT_JSON) as fname: popolo = Popolo.from_filename(fname) event = popolo.events[0] assert event.current
def test_get_first_person(self): with example_file(EXAMPLE_TWO_PEOPLE) as fname: popolo = Popolo.from_filename(fname) assert len(popolo.persons) == 2 person = popolo.persons.first assert person.name == 'Norma Jennings' assert person.id == "1"
def test_simple_person_fields(self): with example_file(b''' { "persons": [ { "name": "Harry Truman", "email": "*****@*****.**", "image": "http://twin-peaks.example.org/harry.jpg", "gender": "male", "honorific_prefix": "Sheriff", "honorific_suffix": "Bookhouse Boy", "biography": "Harry S. Truman is the sheriff of Twin Peaks", "summary": "He assists Dale Cooper in the Laura Palmer case", "given_name": "Harry", "family_name": "Truman" } ] } ''') as fname: popolo = Popolo.from_filename(fname) person = popolo.persons.first assert person.name == "Harry Truman" assert person.email == "*****@*****.**" assert person.image == "http://twin-peaks.example.org/harry.jpg" assert person.gender == "male" assert person.honorific_prefix == "Sheriff" assert person.honorific_suffix == "Bookhouse Boy" assert person.biography == \ "Harry S. Truman is the sheriff of Twin Peaks" assert person.summary == \ "He assists Dale Cooper in the Laura Palmer case" assert person.given_name == "Harry" assert person.family_name == "Truman"
def test_legislative_periods(self): with example_file(EXAMPLE_MULTIPLE_EVENTS) as fname: popolo = Popolo.from_filename(fname) legislative_periods = popolo.legislative_periods assert len(legislative_periods) == 2 for lp in legislative_periods: assert lp.classification == 'legislative period' assert popolo.terms.first == legislative_periods.first
def test_person_membership_filtering(self): with example_file(EXAMPLE_MULTIPLE_MEMBERSHIPS) as fname: popolo = Popolo.from_filename(fname) person = popolo.persons.first person_memberships = person.memberships starfleet_memberships = \ person_memberships.filter(organization_id="starfleet") assert len(starfleet_memberships) == 2
def test_area_repr(self): with example_file(EXAMPLE_POST_JSON) as fname: popolo = Popolo.from_filename(fname) post = popolo.posts.first if six.PY2: assert repr(post) == b"<Post: Nominated Representative>" else: assert repr(post) == u"<Post: Nominated Representative>"
def test_event_repr(self): with example_file(EXAMPLE_EVENT_JSON) as fname: popolo = Popolo.from_filename(fname) event = popolo.events.first if six.PY2: assert repr(event) == b"<Event: 12th Riigikogu>" else: assert repr(event) == u"<Event: 12th Riigikogu>"
def test_event_repr_non_ascii(self): with example_file(EXAMPLE_EVENT_NON_ASCII_JSON) as fname: popolo = Popolo.from_filename(fname) event = popolo.events.first if six.PY2: assert repr(event) == b"<Event: 2015\xe2\x80\x94>" else: assert repr(event) == u"<Event: 2015—>"
def test_person_memberships_method(self): with example_file(EXAMPLE_MULTIPLE_MEMBERSHIPS) as fname: popolo = Popolo.from_filename(fname) person = popolo.persons.first person_memberships = person.memberships assert len(person_memberships) == 2 assert popolo.memberships[0] == person_memberships[0] assert popolo.memberships[1] == person_memberships[1]