async def test_with_subjects(self): event = Event(None, Birth()) Presence(Person('P0'), Subject(), event) Presence(Person('P1'), Subject(), event) expected = 'Birth of <a href="/person/P0/index.html"><span class="nn" title="This person\'s name is unknown.">n.n.</span></a>, <a href="/person/P1/index.html"><span class="nn" title="This person\'s name is unknown.">n.n.</span></a>' async with self._render(data={ 'event': event, }) as (actual, _): self.assertEqual(expected, actual)
async def test_with_person_context_and_other_as_subject(self): event = Event(None, Marriage()) person = Person('P0') other_person = Person('P1') Presence(person, Subject(), event) Presence(other_person, Subject(), event) expected = 'Marriage with <a href="/person/P1/index.html"><span class="nn" title="This person\'s name is unknown.">n.n.</span></a>' async with self._render(data={ 'event': event, 'person_context': person, }) as (actual, _): self.assertEqual(expected, actual)
async def test_derive_update_derivable_event_without_reference_events( self, event_type_type: Type[DerivableEventType]): person = Person('P0') Presence(person, Subject(), Event(None, Ignored())) derivable_event = Event(None, event_type_type()) Presence(person, Subject(), derivable_event) created, updated = derive(person, event_type_type) self.assertEquals(0, created) self.assertEquals(0, updated) self.assertEquals(2, len(person.presences)) self.assertIsNone(derivable_event.date)
def test_clean(self) -> None: ancestry = Ancestry() onymous_event = Event('E0', Birth()) Presence(Person('P0'), Subject(), onymous_event) ancestry.entities.append(onymous_event) anonymous_event = Event('E1', Birth()) ancestry.entities.append(anonymous_event) onymous_place = Place('P0', [PlaceName('Amsterdam')]) onymous_place.events.append(onymous_event) ancestry.entities.append(onymous_place) anonymous_place = Place('P1', [PlaceName('Almelo')]) ancestry.entities.append(anonymous_place) onmyous_place_because_encloses_onmyous_places = Place( 'P3', [PlaceName('Netherlands')]) Enclosure(onymous_place, onmyous_place_because_encloses_onmyous_places) Enclosure(anonymous_place, onmyous_place_because_encloses_onmyous_places) ancestry.entities.append(onmyous_place_because_encloses_onmyous_places) clean(ancestry) self.assertEquals([onymous_event], list(ancestry.entities[Event])) self.assertEquals( [onymous_place, onmyous_place_because_encloses_onmyous_places], list(ancestry.entities[Place])) self.assertNotIn( anonymous_place, onmyous_place_because_encloses_onmyous_places.encloses)
def test_privatize_person_should_privatize_if_private(self): source_file = File('F0', __file__) source = Source('The Source') source.files.append(source_file) citation_file = File('F1', __file__) citation = Citation('C0', source) citation.files.append(citation_file) event_as_subject = Event(None, Birth()) event_as_attendee = Event(None, Marriage()) person_file = File('F2', __file__) person = Person('P0') person.private = True person.citations.append(citation) person.files.append(person_file) Presence(person, Subject(), event_as_subject) Presence(person, Attendee(), event_as_attendee) ancestry = Ancestry() ancestry.entities.append(person) privatize(ancestry) self.assertTrue(person.private) self.assertTrue(citation.private) self.assertTrue(source.private) self.assertTrue(person_file.private) self.assertTrue(citation_file.private) self.assertTrue(source_file.private) self.assertTrue(event_as_subject.private) self.assertIsNone(event_as_attendee.private)
async def test_post_load(self): person = Person('P0') Presence(person, Subject(), Event(None, Birth())) source_file = File('F0', __file__) source = Source('S0', 'The Source') source.private = True source.files.append(source_file) citation_file = File('F0', __file__) citation_source = Source('The Source') citation = Citation('C0', citation_source) citation.private = True citation.files.append(citation_file) with TemporaryDirectory() as output_directory_path: configuration = Configuration(output_directory_path, 'https://example.com') configuration.extensions.add(ExtensionConfiguration(Privatizer)) async with App(configuration) as app: app.ancestry.entities.append(person) app.ancestry.entities.append(source) app.ancestry.entities.append(citation) await load(app) self.assertTrue(person.private) self.assertTrue(source_file.private) self.assertTrue(citation_file.private)
def test_should_remove_presences(self) -> None: person = Person('P0') event = Event(None, Birth()) Presence(person, Subject(), event) anonymize_person(person) self.assertEquals(0, len(person.presences)) self.assertEquals(0, len(event.presences))
def test_clean_should_not_clean_event_with_presences_with_people( self) -> None: ancestry = Ancestry() source = Source('S1', 'The Source') ancestry.entities.append(source) citation = Citation('C1', source) ancestry.entities.append(citation) file = File('F1', __file__) ancestry.entities.append(file) place = Place('P0', [PlaceName('The Place')]) ancestry.entities.append(place) person = Person('P0') event = Event('E0', Birth()) event.citations.append(citation) event.files.append(file) event.place = place ancestry.entities.append(event) Presence(person, Subject(), event) clean(ancestry) self.assertEqual(event, ancestry.entities[Event][event.id]) self.assertIn(event, place.events) self.assertEqual(place, ancestry.entities[Place][place.id]) self.assertIn(event, citation.facts) self.assertEqual(citation, ancestry.entities[Citation][citation.id]) self.assertIn(event, file.entities) self.assertEqual(file, ancestry.entities[File][file.id])
async def test_with_end(self): person = Person('P0') Presence(person, Subject(), Event(None, Death(), Date(1970))) expected = '<div class="meta person-meta"><dl><div><dt>Death</dt><dd>1970</dd></div></dl></div>' async with self._render(data={ 'person': person, }) as (actual, _): self.assertEqual(expected, actual)
async def test_derive_update_comes_after_derivable_event( self, expected_datey: Optional[Datey], after_datey: Optional[Datey], derivable_datey: Optional[Datey]): expected_updates = 0 if expected_datey == derivable_datey else 1 person = Person('P0') Presence(person, Subject(), Event(None, Ignored(), Date(0, 0, 0))) Presence(person, Subject(), Event(None, ComesAfterReference(), after_datey)) derivable_event = Event(None, ComesAfterDerivable(), derivable_datey) Presence(person, Subject(), derivable_event) created, updated = derive(person, ComesAfterDerivable) self.assertEquals(expected_datey, derivable_event.date) self.assertEquals(0, created) self.assertEquals(expected_updates, updated) self.assertEquals(3, len(person.presences))
async def test_embedded_with_identifiable(self): event = Event('E0', Birth()) Presence(Person('P0'), Subject(), event) expected = 'Birth of <span class="nn" title="This person\'s name is unknown.">n.n.</span>' async with self._render(data={ 'event': event, 'embedded': True, }) as (actual, _): self.assertEqual(expected, actual)
def test_presences(self) -> None: event = Event(None, Birth()) sut = Person('1') presence = Presence(sut, Subject(), event) sut.presences.append(presence) self.assertCountEqual([presence], sut.presences) self.assertEquals(sut, presence.person) sut.presences.remove(presence) self.assertCountEqual([], sut.presences) self.assertIsNone(presence.person)
async def test_with_person_context_as_subject(self): event = Event(None, Birth()) person = Person('P0') Presence(person, Subject(), event) expected = 'Birth' async with self._render(data={ 'event': event, 'person_context': person, }) as (actual, _): self.assertEqual(expected, actual)
def test_presences(self) -> None: person = Person('P1') sut = Event(None, Mock(EventType)) presence = Presence(person, Subject(), sut) sut.presences.append(presence) self.assertCountEqual([presence], sut.presences) self.assertEquals(sut, presence.event) sut.presences.remove(presence) self.assertCountEqual([], sut.presences) self.assertIsNone(presence.event)
def test_privatize_person_without_relatives(self, expected, private, event: Optional[Event]): person = Person('P0') person.private = private if event is not None: Presence(person, Subject(), event) ancestry = Ancestry() ancestry.entities.append(person) privatize(ancestry) self.assertEquals(expected, person.private)
async def test_embedded(self): person = Person('P0') Presence(person, Subject(), Event(None, Birth(), Date(1970))) PersonName(person, 'Jane', 'Dough') name = PersonName(person, 'Janet', 'Doughnut') name.citations.append(Citation(None, Source(None, 'The Source'))) expected = '<div class="meta person-meta"><span class="aka">Also known as <span class="person-label" typeof="foaf:Person"><span property="foaf:individualName">Janet</span> <span property="foaf:familyName">Doughnut</span></span></span><dl><div><dt>Birth</dt><dd>1970</dd></div></dl></div>' async with self._render(data={ 'person': person, 'embedded': True, }) as (actual, _): self.assertEqual(expected, actual)
def test_privatize_person_with_child(self, expected, private, event: Optional[Event]): person = Person('P0') person.private = private child = Person('P1') if event is not None: Presence(child, Subject(), event) person.children.append(child) ancestry = Ancestry() ancestry.entities.append(person) privatize(ancestry) self.assertEquals(expected, person.private)
def test_privatize_person_with_grandparent(self, expected, private, event: Optional[Event]): person = Person('P0') person.private = private parent = Person('P1') person.parents.append(parent) grandparent = Person('P2') if event is not None: Presence(grandparent, Subject(), event) parent.parents.append(grandparent) ancestry = Ancestry() ancestry.entities.append(person) privatize(ancestry) self.assertEquals(expected, person.private)
async def test_derive_create_comes_after_derivable_event( self, expected_datey: Optional[Datey], after_datey: Optional[Datey]): expected_creations = 0 if expected_datey is None else 1 person = Person('P0') Presence(person, Subject(), Event(None, Ignored(), Date(0, 0, 0))) Presence(person, Subject(), Event(None, ComesAfterReference(), after_datey)) created, updated = derive(person, ComesAfterCreatableDerivable) derived_presences = [ presence for presence in person.presences if isinstance(presence.event.type, ComesAfterCreatableDerivable) ] self.assertEquals(expected_creations, len(derived_presences)) if expected_creations: derived_presence = derived_presences[0] self.assertIsInstance(derived_presence.role, Subject) self.assertEquals(expected_datey, derived_presence.event.date) self.assertEquals(expected_creations, created) self.assertEquals(0, updated) self.assertEquals(2 + expected_creations, len(person.presences))
class TestSubjectRoleTest(TemplateTestCase): @parameterized.expand([ ('true', Subject()), ('false', Subject), ('false', Attendee()), ('false', 9), ]) @sync async def test(self, expected, data) -> None: template = '{% if data is subject_role %}true{% else %}false{% endif %}' async with self._render(template_string=template, data={ 'data': data, }) as (actual, _): self.assertEquals(expected, actual)
def derive(person: Person, event_type_type: Type[DerivableEventType]) -> Tuple[int, int]: # Gather any existing events that could be derived, or create a new derived event if needed. derivable_events = list(_get_derivable_events(person, event_type_type)) if not derivable_events: if list( filter(lambda x: isinstance(x.event.type, event_type_type), person.presences)): return 0, 0 if issubclass(event_type_type, CreatableDerivableEventType): derivable_events = [DerivedEvent(event_type_type())] else: return 0, 0 # Aggregate event type order from references and backreferences. comes_before_event_type_types = event_type_type.comes_before() comes_after_event_type_types = event_type_type.comes_after() for other_event_type_type in EVENT_TYPE_TYPES: if event_type_type in other_event_type_type.comes_before(): comes_after_event_type_types.add(other_event_type_type) if event_type_type in other_event_type_type.comes_after(): comes_before_event_type_types.add(other_event_type_type) created_derivations = 0 updated_derivations = 0 for derivable_event in derivable_events: dates_derived = False if derivable_event.date is None or derivable_event.date.end is None: dates_derived = dates_derived or _ComesBeforeDateDeriver.derive( person, derivable_event, comes_before_event_type_types) if derivable_event.date is None or derivable_event.date.start is None: dates_derived = dates_derived or _ComesAfterDateDeriver.derive( person, derivable_event, comes_after_event_type_types) if dates_derived: if isinstance(derivable_event, DerivedEvent): created_derivations += 1 Presence(person, Subject(), derivable_event) else: updated_derivations += 1 return created_derivations, updated_derivations
def test_associated_files(self) -> None: file1 = Mock(File) file2 = Mock(File) file3 = Mock(File) file4 = Mock(File) file5 = Mock(File) file6 = Mock(File) sut = Person('1') sut.files = [file1, file2, file1] citation = Mock(Citation) citation.associated_files = [file3, file4, file2] name = PersonName(sut) name.citations = [citation] event = Mock(Event) event.associated_files = [file5, file6, file4] Presence(sut, Subject(), event) self.assertEquals([file1, file2, file3, file4, file5, file6], list(sut.associated_files))
async def test_post_parse(self): person = Person('P0') reference_presence = Presence(person, Subject(), Event(None, Residence())) reference_presence.event.date = Date(1970, 1, 1) with TemporaryDirectory() as output_directory_path: configuration = Configuration(output_directory_path, 'https://example.com') configuration.extensions.add(ExtensionConfiguration(Deriver)) async with App(configuration) as app: app.ancestry.entities.append(person) await load(app) self.assertEquals(3, len(person.presences)) self.assertEquals( DateRange(None, Date(1970, 1, 1), end_is_boundary=True), person.start.date) self.assertEquals(DateRange(Date(1970, 1, 1), start_is_boundary=True), person.end.date)
def test_privatize_event_should_privatize_if_private(self): source_file = File('F0', __file__) source = Source('The Source') source.files.append(source_file) citation_file = File('F1', __file__) citation = Citation('C0', source) citation.files.append(citation_file) event_file = File('F1', __file__) event = Event('E1', Birth()) event.private = True event.citations.append(citation) event.files.append(event_file) person = Person('P0') Presence(person, Subject(), event) ancestry = Ancestry() ancestry.entities.append(event) privatize(ancestry) self.assertTrue(event.private) self.assertTrue(event_file.private) self.assertTrue(citation.private) self.assertTrue(source.private) self.assertTrue(citation_file.private) self.assertTrue(source_file.private) self.assertIsNone(person.private)
def test_label(self) -> None: sut = Subject() with Translations(NullTranslations()): self.assertIsInstance(sut.label, str) self.assertNotEqual('', sut.label)
def test_name(self) -> None: self.assertIsInstance(Subject.name(), str) self.assertNotEqual('', Subject.name)
def test_end(self) -> None: end = Event(None, Burial()) sut = Person('P1') Presence(sut, Subject(), end) self.assertEquals(end, sut.end)
def test_start(self) -> None: start = Event(None, Birth()) sut = Person('P1') Presence(sut, Subject(), start) self.assertEquals(start, sut.start)
async def test_person_should_encode_full(self): parent_id = 'the_parent' parent = Person(parent_id) child_id = 'the_child' child = Person(child_id) sibling_id = 'the_sibling' sibling = Person(sibling_id) sibling.parents.append(parent) person_id = 'the_person' person_affiliation_name = 'Person' person_individual_name = 'The' person = Person(person_id) PersonName(person, person_individual_name, person_affiliation_name) person.parents.append(parent) person.children.append(child) person.private = False link = Link('https://example.com/the-person') link.label = 'The Person Online' person.links.add(link) person.citations.append(Citation('the_citation', Source('The Source'))) Presence(person, Subject(), Event('the_event', Birth())) expected = { '$schema': '/schema.json#/definitions/person', '@context': { 'parents': 'https://schema.org/parent', 'children': 'https://schema.org/child', 'siblings': 'https://schema.org/sibling', }, '@type': 'https://schema.org/Person', 'id': person_id, 'names': [ { '@context': { 'individual': 'https://schema.org/givenName', 'affiliation': 'https://schema.org/familyName', }, 'individual': person_individual_name, 'affiliation': person_affiliation_name, }, ], 'parents': [ '/en/person/the_parent/index.json', ], 'children': [ '/en/person/the_child/index.json', ], 'siblings': [ '/en/person/the_sibling/index.json', ], 'private': False, 'presences': [ { '@context': { 'event': 'https://schema.org/performerIn', }, 'role': 'subject', 'event': '/en/event/the_event/index.json', }, ], 'citations': [ '/en/citation/the_citation/index.json', ], 'links': [ { 'url': '/en/person/the_person/index.json', 'relationship': 'canonical', 'mediaType': 'application/json', }, { 'url': '/nl/person/the_person/index.json', 'relationship': 'alternate', 'locale': 'nl-NL', }, { 'url': '/en/person/the_person/index.html', 'relationship': 'alternate', 'mediaType': 'text/html', }, { 'url': 'https://example.com/the-person', 'label': 'The Person Online', }, ], } await self.assert_encodes(expected, person, 'person')
async def test_event_should_encode_full(self): event = Event('the_event', Birth()) event.date = DateRange(Date(2000, 1, 1), Date(2019, 12, 31)) event.place = Place('the_place', [PlaceName('The Place')]) Presence(Person('the_person'), Subject(), event) event.citations.append(Citation('the_citation', Source('The Source'))) expected = { '$schema': '/schema.json#/definitions/event', '@context': { 'place': 'https://schema.org/location', }, '@type': 'https://schema.org/Event', 'id': 'the_event', 'type': 'birth', 'presences': [ { '@context': { 'person': 'https://schema.org/actor', }, 'role': 'subject', 'person': '/en/person/the_person/index.json', }, ], 'citations': [ '/en/citation/the_citation/index.json', ], 'date': { 'start': { 'year': 2000, 'month': 1, 'day': 1, }, 'end': { 'year': 2019, 'month': 12, 'day': 31, }, }, 'place': '/en/place/the_place/index.json', 'links': [ { 'url': '/en/event/the_event/index.json', 'relationship': 'canonical', 'mediaType': 'application/json', }, { 'url': '/nl/event/the_event/index.json', 'relationship': 'alternate', 'locale': 'nl-NL', }, { 'url': '/en/event/the_event/index.html', 'relationship': 'alternate', 'mediaType': 'text/html', }, ], } await self.assert_encodes(expected, event, 'event')