def test_ancestry_should_encode_full(self): ancestry = Ancestry() place_id = 'the_place' place_name = 'The Place' place = Place(place_id, place_name) ancestry.places[place_id] = place person_id = 'the_person' person_family_name = 'Person' person_individual_name = 'The' person = Person(person_id, person_individual_name, person_family_name) ancestry.people[person_id] = person expected = { 'places': { place_id: { 'id': place_id, 'name': place_name, }, }, 'people': { person_id: { 'id': person_id, 'family_name': person_family_name, 'individual_name': person_individual_name, 'parent_ids': [], 'child_ids': [], 'private': None, }, }, } self.assert_encodes(expected, ancestry)
def test_clean_should_not_clean_event_with_presences_with_people( self) -> None: ancestry = Ancestry() source = IdentifiableSource('S1', 'The Source') ancestry.sources[source.id] = source citation = IdentifiableCitation('C1', source) ancestry.citations[citation.id] = citation file = File('F1', __file__) ancestry.files[file.id] = file place = Place('P0', [PlaceName('The Place')]) ancestry.places[place.id] = place person = Person('P0') event = IdentifiableEvent('E0', Birth()) event.citations.append(citation) event.files.append(file) event.place = place ancestry.events[event.id] = event Presence(person, Subject(), event) clean(ancestry) self.assertEqual(event, ancestry.events[event.id]) self.assertIn(event, place.events) self.assertEqual(place, ancestry.places[place.id]) self.assertIn(event, citation.facts) self.assertEqual(citation, ancestry.citations[citation.id]) self.assertIn(event, file.resources) self.assertEqual(file, ancestry.files[file.id])
def test_clean_should_clean_event(self) -> None: ancestry = Ancestry() source = IdentifiableSource('S1', 'The Source') ancestry.sources[source.id] = source citation = IdentifiableCitation('C1', source) ancestry.citations[citation.id] = citation file = File('F1', __file__) ancestry.files[file.id] = file place = Place('P0', [PlaceName('The Place')]) ancestry.places[place.id] = place event = IdentifiableEvent('E0', Birth()) event.citations.append(citation) event.files.append(file) event.place = place ancestry.events[event.id] = event clean(ancestry) self.assertNotIn(event.id, ancestry.events) self.assertIsNone(event.place) self.assertNotIn(event, place.events) self.assertNotIn(place.id, ancestry.places) self.assertNotIn(event, citation.facts) self.assertNotIn(citation.id, ancestry.citations) self.assertNotIn(event, file.resources) self.assertNotIn(file.id, ancestry.files)
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 = IdentifiableCitation('C0', source) citation.files.append(citation_file) event_as_subject = Event(Birth()) event_as_attendee = Event(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.people[person.id] = 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)
def test_ancestry_should_encode_minimal(self): ancestry = Ancestry() expected = { 'places': {}, 'people': {}, } self.assert_encodes(expected, ancestry)
async def test_populate_should_populate_existing_link(self, m_retriever) -> None: sut = Populator(m_retriever) entry_language = 'en' entry_name = 'Amsterdam' entry_title = 'Amsterdam' entry_content = 'Capitol of the Netherlands' entry = Entry(entry_language, entry_name, entry_title, entry_content) m_retriever.get_entry.return_value = entry resource = IdentifiableSource('the_source', 'The Source') link = Link('https://en.wikipedia.org/wiki/Amsterdam') resource.links.add(link) ancestry = Ancestry() ancestry.sources[resource.id] = resource with TemporaryDirectory() as output_directory_path: with TemporaryDirectory() as cache_directory_path: configuration = Configuration(output_directory_path, 'https://example.com') configuration.cache_directory_path = cache_directory_path async with Site(configuration) as site: await sut.populate(ancestry, site) m_retriever.get_entry.assert_called_once_with(entry_language, entry_name) self.assertEqual(1, len(resource.links)) self.assertEqual('Amsterdam', link.label) self.assertEqual('en', link.locale) self.assertEqual('text/html', link.media_type) self.assertIsNotNone(link.description) self.assertEqual('external', link.relationship)
def _parse(self, xml: str) -> Ancestry: with NamedTemporaryFile(mode='r+') as f: f.write(xml.strip()) f.seek(0) ancestry = Ancestry() parse_xml_file(ancestry, f.name) return ancestry
def test_with_private_person_should_anonymize(self, m_anonymize_person) -> None: person = Person('P0') person.private = True ancestry = Ancestry() ancestry.people[person.id] = person anonymize(ancestry) m_anonymize_person.assert_called_once_with(person)
def test_with_public_event_should_not_anonymize(self, m_anonymize_event) -> None: event = IdentifiableEvent('E0', Event.Type.BIRTH) event.private = False ancestry = Ancestry() ancestry.events[event.id] = event anonymize(ancestry) m_anonymize_event.assert_not_called()
def test_with_private_event_should_anonymize(self, m_anonymize_event) -> None: event = IdentifiableEvent('E0', Event.Type.BIRTH) event.private = True ancestry = Ancestry() ancestry.events[event.id] = event anonymize(ancestry) m_anonymize_event.assert_called_once_with(event)
def test_with_public_file_should_not_anonymize(self, m_anonymize_file) -> None: file = File('F0', __file__) file.private = False ancestry = Ancestry() ancestry.files[file.id] = file anonymize(ancestry) m_anonymize_file.assert_not_called()
def test_with_private_file_should_anonymize(self, m_anonymize_file) -> None: file = File('F0', __file__) file.private = True ancestry = Ancestry() ancestry.files[file.id] = file anonymize(ancestry) m_anonymize_file.assert_called_once_with(file)
def test_with_public_source_should_not_anonymize( self, m_anonymize_source) -> None: source = Source('S0', 'The Source') source.private = False ancestry = Ancestry() ancestry.sources[source.id] = source anonymize(ancestry) m_anonymize_source.assert_not_called()
def test_with_private_source_should_anonymize(self, m_anonymize_source) -> None: source = Source('S0', 'The Source') source.private = True ancestry = Ancestry() ancestry.sources[source.id] = source anonymize(ancestry) m_anonymize_source.assert_called_once_with(source)
def test_with_public_person_should_not_anonymize( self, m_anonymize_person) -> None: person = Person('P0') person.private = False ancestry = Ancestry() ancestry.people[person.id] = person anonymize(ancestry) m_anonymize_person.assert_not_called()
def test_clean_should_clean_file(self) -> None: ancestry = Ancestry() file = File('F0', __file__) ancestry.files[file.id] = file clean(ancestry) self.assertNotIn(file.id, ancestry.files)
def test_clean_should_clean_source(self) -> None: ancestry = Ancestry() source = IdentifiableSource('S0', 'The source') ancestry.sources[source.id] = source clean(ancestry) self.assertNotIn(source.id, ancestry.sources)
def test_with_private_citation_should_anonymize( self, m_anonymize_citation) -> None: source = Source('S0', 'The Source') citation = Citation('C0', source) citation.private = True ancestry = Ancestry() ancestry.citations[citation.id] = citation anonymize(ancestry) m_anonymize_citation.assert_called_once_with(citation)
def test_with_public_citation_should_not_anonymize( self, m_anonymize_citation) -> None: source = Source('S0', 'The Source') citation = Citation('C0', source) citation.private = False ancestry = Ancestry() ancestry.citations[citation.id] = citation anonymize(ancestry) m_anonymize_citation.assert_not_called()
def test_privatize_without_relatives(self, expected, private, event: Optional[Event]): person = Person('P0') person.private = private if event is not None: Presence(person, Presence.Role.SUBJECT, event) ancestry = Ancestry() ancestry.people[person.id] = person sut = Privatizer() sut.privatize(ancestry) self.assertEquals(expected, person.private)
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.people[person.id] = person privatize(ancestry) self.assertEquals(expected, person.private)
def test_privatize_source_should_not_privatize_if_public(self): file = File('F0', __file__) source = IdentifiableSource('S0', 'The Source') source.private = False source.files.append(file) ancestry = Ancestry() ancestry.sources[source.id] = source privatize(ancestry) self.assertEqual(False, source.private) self.assertIsNone(file.private)
def test_privatize_source_should_privatize_if_private(self): file = File('F0', __file__) source = IdentifiableSource('S0', 'The Source') source.private = True source.files.append(file) ancestry = Ancestry() ancestry.sources[source.id] = source privatize(ancestry) self.assertTrue(source.private) self.assertTrue(file.private)
def test_clean_should_not_clean_person_if_public(self): ancestry = Ancestry() person = Person('P0') person.private = False ancestry.people[person.id] = person clean(ancestry) self.assertEqual(person, ancestry.people[person.id])
def __init__(self, configuration: Configuration): self._ancestry = Ancestry() self._configuration = configuration self._resources = FileSystem( join(dirname(abspath(__file__)), 'resources')) self._event_dispatcher = EventDispatcher() self._plugins = {} self._init_plugins() if configuration.resources_directory_path: self._resources.paths.appendleft( configuration.resources_directory_path)
def test_privatize_file_should_not_privatize_if_public(self): source = Source('The Source') citation = Citation(source) file = File('F0', __file__) file.private = False file.citations.append(citation) ancestry = Ancestry() ancestry.files[file.id] = file privatize(ancestry) self.assertEqual(False, file.private) self.assertIsNone(citation.private)
def test_privatize_file_should_privatize_if_private(self): source = Source('The Source') citation = Citation(source) file = File('F0', __file__) file.private = True file.citations.append(citation) ancestry = Ancestry() ancestry.files[file.id] = file privatize(ancestry) self.assertTrue(True, file.private) self.assertTrue(citation.private)
def test_anonymize_should_not_anonymize_public_person(self): with NamedTemporaryFile() as file_f: person = Person('P0', 'Janet', 'Dough') presence = Presence(Presence.Role.SUBJECT) presence.event = Event('E0', Event.Type.BIRTH) person.presences.add(presence) person.files.add(File('D0', file_f.name)) ancestry = Ancestry() ancestry.people[person.id] = person anonymize(ancestry) self.assert_not_anonymized(person)
async def test_populate_should_add_translation_links(self, m_retriever) -> None: sut = Populator(m_retriever) entry_language = 'en' entry_name = 'Amsterdam' entry_title = 'Amsterdam' entry_content = 'Capitol of the Netherlands' entry = Entry(entry_language, entry_name, entry_title, entry_content) added_entry_language = 'nl' added_entry_name = 'Amsterdam' added_entry_title = 'Amsterdam' added_entry_content = 'Hoofdstad van Nederland' added_entry = Entry(added_entry_language, added_entry_name, added_entry_title, added_entry_content) m_retriever.get_entry.side_effect = [entry, added_entry] m_retriever.get_translations.return_value = { entry_language: entry_name, added_entry_language: added_entry_name, } resource = IdentifiableSource('the_source', 'The Source') link_en = Link('https://en.wikipedia.org/wiki/Amsterdam') resource.links.add(link_en) ancestry = Ancestry() ancestry.sources[resource.id] = resource with TemporaryDirectory() as output_directory_path: with TemporaryDirectory() as cache_directory_path: configuration = Configuration(output_directory_path, 'https://example.com') configuration.cache_directory_path = cache_directory_path configuration.locales.clear() configuration.locales['en-US'] = LocaleConfiguration( 'en-US', 'en') configuration.locales['nl-NL'] = LocaleConfiguration( 'nl-NL', 'nl') async with Site(configuration) as site: await sut.populate(ancestry, site) m_retriever.get_entry.assert_has_calls([ call(entry_language, entry_name), call(added_entry_language, added_entry_name), ]) m_retriever.get_translations.assert_called_once_with( entry_language, entry_name) self.assertEqual(2, len(resource.links)) link_nl = resource.links.difference({link_en}).pop() self.assertEqual('Amsterdam', link_nl.label) self.assertEqual('nl', link_nl.locale) self.assertEqual('text/html', link_nl.media_type) self.assertIsNotNone(link_nl.description) self.assertEqual('external', link_nl.relationship)
def __init__(self, configuration: Configuration): self._ancestry = Ancestry() self._configuration = configuration self._resources = FileSystem( join(dirname(abspath(__file__)), 'resources')) self._event_dispatcher = EventDispatcher() self._translations = defaultdict(gettext.NullTranslations) self._plugins = OrderedDict() self._init_plugins() self._init_event_listeners() self._init_resources() self._init_translations()