def test_should_remove_contained_by(self) -> None: source = Source('S0', 'The Source') contained_by = Source(None, 'The Source') source.contained_by = contained_by anonymous_source = AnonymousSource() anonymize_source(source, anonymous_source) self.assertIsNone(source.contained_by)
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_contains(self) -> None: source = Source('S0', 'The Source') contains = Source(None, 'The Source') source.contains.append(contains) anonymous_source = AnonymousSource() anonymize_source(source, anonymous_source) self.assertEquals(0, len(source.contains)) self.assertIn(contains, anonymous_source.contains)
def test_replace(self): citations = [Citation(None, Source(None))] contains = [Source(None)] files = [Mock(File)] sut = AnonymousSource() other = AnonymousSource() other.citations = citations other.contains = contains other.files = files sut.replace(other) self.assertEquals(citations, list(sut.citations)) self.assertEquals(contains, list(sut.contains)) self.assertEquals(files, list(sut.files))
def test_clean_should_not_clean_source_with_contains(self) -> None: ancestry = Ancestry() source = Source('S0', 'The Source') ancestry.entities.append(source) contains = Source('S1', 'The Source') contains.contained_by = source ancestry.entities.append(contains) clean(ancestry) self.assertEqual(source, ancestry.entities[Source][source.id]) self.assertEqual(source, contains.contained_by) self.assertEqual(contains, ancestry.entities[Source][contains.id])
async def test_populate_should_populate_existing_link(self, m_retriever) -> None: 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 = Source('the_source', 'The Source') link = Link('https://en.wikipedia.org/wiki/Amsterdam') resource.links.add(link) 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 App(configuration) as app: app.ancestry.entities.append(resource) sut = _Populator(app, m_retriever) await sut.populate() 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(MediaType('text/html'), link.media_type) self.assertIsNotNone(link.description) self.assertEqual('external', link.relationship)
def test_clean_should_clean_event(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) event = Event('E0', Birth()) event.citations.append(citation) event.files.append(file) event.place = place ancestry.entities.append(event) clean(ancestry) self.assertNotIn(event.id, ancestry.entities[Event]) self.assertIsNone(event.place) self.assertNotIn(event, place.events) self.assertNotIn(place.id, ancestry.entities[Place]) self.assertNotIn(event, citation.facts) self.assertNotIn(citation.id, ancestry.entities[Citation]) self.assertNotIn(event, file.entities) self.assertNotIn(file.id, ancestry.entities[File])
def _load_source(loader: _Loader, element: ElementTree.Element) -> None: source_handle = element.get('handle') source = Source(element.get('id'), _xpath1(element, './ns:stitle').text) repository_source_handle = _load_handle('reporef', element) if repository_source_handle is not None: loader.add_association(Source, source_handle, 'contained_by', Source, repository_source_handle) # Load the author. sauthor_element = _xpath1(element, './ns:sauthor') if sauthor_element is not None: source.author = sauthor_element.text # Load the publication info. spubinfo_element = _xpath1(element, './ns:spubinfo') if spubinfo_element is not None: source.publisher = spubinfo_element.text _load_attribute_privacy(source, element, 'srcattribute') flattened_source = FlattenedEntity(source, source_handle) _load_objref(loader, flattened_source, element) loader.add_entity(flattened_source)
def test_should_remove_citations(self) -> None: person = Person('P0') source = Source('The Source') citation = Citation(None, source) person.citations.append(citation) anonymize_person(person) self.assertEquals(0, len(person.citations))
async def test_source_should_encode_minimal(self): source = Source('the_source', 'The Source') expected = { '$schema': '/schema.json#/definitions/source', '@context': { 'name': 'https://schema.org/name', }, '@type': 'https://schema.org/Thing', 'id': 'the_source', 'name': 'The Source', 'contains': [], 'citations': [], 'links': [ { 'url': '/en/source/the_source/index.json', 'relationship': 'canonical', 'mediaType': 'application/json', }, { 'url': '/nl/source/the_source/index.json', 'relationship': 'alternate', 'locale': 'nl-NL', }, { 'url': '/en/source/the_source/index.html', 'relationship': 'alternate', 'mediaType': 'text/html', }, ], } await self.assert_encodes(expected, source, 'source')
def test_should_remove_source(self) -> None: source = Source('The Source') citation = Citation('C0', source) anonymous_source = AnonymousSource() anonymous_citation = AnonymousCitation(anonymous_source) anonymize_citation(citation, anonymous_citation) self.assertIsNone(citation.source)
async def test_citation_should_encode_minimal(self): citation = Citation('the_citation', Source(None, 'The Source')) expected = { '$schema': '/schema.json#/definitions/citation', '@type': 'https://schema.org/Thing', 'id': 'the_citation', 'facts': [], 'links': [ { 'url': '/en/citation/the_citation/index.json', 'relationship': 'canonical', 'mediaType': 'application/json', }, { 'url': '/nl/citation/the_citation/index.json', 'relationship': 'alternate', 'locale': 'nl-NL', }, { 'url': '/en/citation/the_citation/index.html', 'relationship': 'alternate', 'mediaType': 'text/html', }, ], } await self.assert_encodes(expected, citation, 'citation')
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])
def test_should_remove_citations(self) -> None: event = Event(None, Birth()) source = Source(None, 'The Source') citation = Citation(None, source) event.citations.append(citation) anonymize_event(event) self.assertEquals(0, len(event.citations))
async def test_citation_should_encode_full(self): citation = Citation('the_citation', Source('the_source', 'The Source')) citation.description = 'The Source Description' citation.facts.append(Event('the_event', Birth())) expected = { '$schema': '/schema.json#/definitions/citation', '@type': 'https://schema.org/Thing', 'id': 'the_citation', 'source': '/en/source/the_source/index.json', 'facts': ['/en/event/the_event/index.json'], 'links': [ { 'url': '/en/citation/the_citation/index.json', 'relationship': 'canonical', 'mediaType': 'application/json', }, { 'url': '/nl/citation/the_citation/index.json', 'relationship': 'alternate', 'locale': 'nl-NL', }, { 'url': '/en/citation/the_citation/index.html', 'relationship': 'alternate', 'mediaType': 'text/html', }, ], } await self.assert_encodes(expected, citation, 'citation')
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)
def _load_repository(loader: _Loader, element: ElementTree.Element) -> None: repository_source_handle = element.get('handle') source = Source(element.get('id'), _xpath1(element, './ns:rname').text) _load_urls(source, element) loader.add_entity(FlattenedEntity(source, repository_source_handle))
def test_with_public_source_should_not_anonymize( self, m_anonymize_source) -> None: source = Source('S0', 'The Source') source.private = False ancestry = Ancestry() ancestry.entities.append(source) anonymize(ancestry, AnonymousCitation(AnonymousSource())) 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.entities.append(source) anonymize(ancestry, AnonymousCitation(AnonymousSource())) m_anonymize_source.assert_called_once_with(source, ANY)
def test_should_remove_citations(self) -> None: source = Source('S0', 'The Source') citation = Citation(None, source) source.citations.append(citation) anonymous_source = AnonymousSource() anonymize_source(source, anonymous_source) self.assertEquals(0, len(source.citations)) self.assertIn(citation, anonymous_source.citations)
async def test_with_citation(self): event = Event(None, Birth()) event.citations.append(Citation(None, Source(None, 'The Source'))) expected = '<a href="#reference-1" class="citation">[1]</a>' async with self._render(data={ 'event': event, }) as (actual, _): self.assertEqual(expected, actual)
def test_should_remove_files(self) -> None: source = Source('S0', 'The Source') file = File('F0', __file__) source.files.append(file) anonymous_source = AnonymousSource() anonymize_source(source, anonymous_source) self.assertEquals(0, len(source.files)) self.assertIn(file, anonymous_source.files)
def test_with_private_citation_should_anonymize( self, m_anonymize_citation) -> None: source = Source('The Source') citation = Citation('C0', source) citation.private = True ancestry = Ancestry() ancestry.entities.append(citation) anonymize(ancestry, AnonymousCitation(AnonymousSource())) m_anonymize_citation.assert_called_once_with(citation, ANY)
def test_with_public_citation_should_not_anonymize( self, m_anonymize_citation) -> None: source = Source('The Source') citation = Citation('C0', source) citation.private = False ancestry = Ancestry() ancestry.entities.append(citation) anonymize(ancestry, AnonymousCitation(AnonymousSource())) m_anonymize_citation.assert_not_called()
def test_should_remove_names(self) -> None: person = Person('P0') name = PersonName(person, 'Jane', 'Dough') source = Source('The Source') citation = Citation(None, source) name.citations.append(citation) anonymize_person(person) self.assertEquals(0, len(person.names)) self.assertEquals(0, len(citation.facts))
def test_clean_should_clean_source(self) -> None: ancestry = Ancestry() source = Source('S0', 'The source') ancestry.entities.append(source) clean(ancestry) self.assertNotIn(source.id, ancestry.entities[Source])
def test_should_remove_files(self) -> None: source = Source('The Source') citation = Citation('C0', source) file = File('F0', __file__) citation.files.append(file) anonymous_source = AnonymousSource() anonymous_citation = AnonymousCitation(anonymous_source) anonymize_citation(citation, anonymous_citation) self.assertEquals(0, len(citation.files)) self.assertIn(file, anonymous_citation.files)
async def test_with_one_alternative_name(self): person = Person('P0') 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><a href="#reference-1" class="citation">[1]</a></span></div>' async with self._render(data={ 'person': person, }) as (actual, _): self.assertEqual(expected, actual)
def test_privatize_source_should_privatize_if_private(self): file = File('F0', __file__) source = Source('S0', 'The Source') source.private = True source.files.append(file) ancestry = Ancestry() ancestry.entities.append(source) privatize(ancestry) self.assertTrue(source.private) self.assertTrue(file.private)
def test_privatize_source_should_not_privatize_if_public(self): file = File('F0', __file__) source = Source('S0', 'The Source') source.private = False source.files.append(file) ancestry = Ancestry() ancestry.entities.append(source) privatize(ancestry) self.assertEqual(False, source.private) self.assertIsNone(file.private)