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_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)
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_with_witnesses(self): event = Event(None, Birth()) Presence(Person('P0'), Witness(), event) expected = 'Birth' async with self._render(data={ 'event': event, }) as (actual, _): self.assertEqual(expected, actual)
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)
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: 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)
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)
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)
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 _load_eventref(loader: _Loader, person_id: str, eventref: ElementTree.Element) -> None: event_handle = eventref.get('hlink') gramps_presence_role = eventref.get('role') role = _PRESENCE_ROLE_MAP[ gramps_presence_role] if gramps_presence_role in _PRESENCE_ROLE_MAP else Attendee( ) presence = Presence(None, role, None) identifiable_presence = FlattenedEntity(presence) loader.add_entity(identifiable_presence) loader.add_association(Presence, identifiable_presence.id, 'person', Person, person_id) loader.add_association(Presence, identifiable_presence.id, 'event', Event, event_handle)
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))
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_role(self) -> None: event = Mock(Event) sut = Presence(Mock(Person), Mock(PresenceRole), event) self.assertEquals(event, sut.event)
def test_event(self) -> None: role = Mock(PresenceRole) sut = Presence(Mock(Person), role, Mock(Event)) self.assertEquals(role, sut.role)
def test_person(self) -> None: person = Mock(Person) sut = Presence(person, Mock(PresenceRole), Mock(Event)) self.assertEquals(person, sut.person)
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)