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_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 _parse_source(ancestry: _IntermediateAncestry, element: Element) -> None: handle = _xpath1(element, './@handle') source = Source(_xpath1(element, './@id'), _xpath1(element, './ns:stitle').text) repository_source_handle = _xpath1(element, './ns:reporef/@hlink') if repository_source_handle is not None: source.contained_by = ancestry.sources[repository_source_handle] _parse_objref(ancestry, source, element) ancestry.sources[handle] = source
def test_replace(self): citations = [Citation(Source())] contains = [Source()] 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.sources[source.id] = source contains = Source('S1', 'The Source') contains.contained_by = source ancestry.sources[contains.id] = contains clean(ancestry) self.assertEqual(source, ancestry.sources[source.id]) self.assertEqual(source, contains.contained_by) self.assertEqual(contains, ancestry.sources[contains.id])
def test_clean_should_not_clean_source_with_contained_by(self) -> None: ancestry = Ancestry() source = Source('S0', 'The Source') ancestry.sources[source.id] = source contained_by = Source('S1', 'The Source') contained_by.contains.append(source) ancestry.sources[contained_by.id] = contained_by clean(ancestry) self.assertEqual(source, ancestry.sources[source.id]) self.assertIn(source, contained_by.contains) self.assertEqual(contained_by, ancestry.sources[contained_by.id])
class DelegatingUrlGeneratorTest(TestCase): @parameterized.expand([ ('/index.html', '/index.html'), ('/index.html', '<front>'), ('/person/index.html', '<person>'), ('/person/P1/index.html', Person('P1')), ('/event/index.html', '<event>'), ('/event/E1/index.html', Event('E1', Event.Type.DEATH)), ('/place/index.html', '<place>'), ('/place/P1/index.html', Place('P1', 'Place 1')), ('/file/index.html', '<file>'), ('/file/F1/index.html', File('F1', '/tmp')), ('/source/index.html', '<source>'), ('/source/S1/index.html', Source('S1', 'Source 1')), ('/citation/index.html', '<citation>'), ('/citation/C1/index.html', Citation('C1')), ]) def test_generate(self, expected: str, resource: Any): configuration = Configuration('/tmp', 'https://example.com') sut = DelegatingUrlGenerator(configuration) self.assertEquals(expected, sut.generate(resource)) def test_generate_with_invalid_value(self): configuration = Configuration('/tmp', 'https://example.com') sut = DelegatingUrlGenerator(configuration) with self.assertRaises(ValueError): sut.generate(9)
def test_should_remove_source(self) -> None: source = Source('The Source') citation = IdentifiableCitation('C0', source) anonymous_source = AnonymousSource() anonymous_citation = AnonymousCitation(anonymous_source) anonymize_citation(citation, anonymous_citation) self.assertIsNone(citation.source)
def test_should_remove_contained_by(self) -> None: source = IdentifiableSource('S0', 'The Source') contained_by = Source('The Source') source.contained_by = contained_by anonymous_source = AnonymousSource() anonymize_source(source, anonymous_source) self.assertIsNone(source.contained_by)
def test_should_remove_citations(self) -> None: event = Event(Birth()) source = Source('The Source') citation = Citation(source) event.citations.append(citation) anonymize_event(event) self.assertEquals(0, len(event.citations))
def test_privatize_person_should_not_privatize_if_public(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 = False 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.assertEqual(False, person.private) self.assertIsNone(citation.private) self.assertIsNone(source.private) self.assertIsNone(person_file.private) self.assertIsNone(citation_file.private) self.assertIsNone(source_file.private) self.assertIsNone(event_as_subject.private) self.assertIsNone(event_as_attendee.private)
def test_clean_should_clean_event(self) -> None: ancestry = Ancestry() source = Source('S1', 'The Source') ancestry.sources[source.id] = source citation = Citation('C1', source) ancestry.citations[citation.id] = citation file = File('F1', __file__) ancestry.files[file.id] = file place = Place('P0', [LocalizedName('The Place')]) ancestry.places[place.id] = place event = IdentifiableEvent('E0', Event.Type.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_should_remove_citations(self) -> None: person = Person('P0') source = Source('S0', 'The Source') citation = Citation('C0', source) person.citations.append(citation) anonymize_person(person) self.assertEquals(0, len(person.citations))
def test_clean_should_not_clean_event_with_presences_with_people( self) -> None: ancestry = Ancestry() source = Source('S1', 'The Source') ancestry.sources[source.id] = source citation = Citation('C1', source) ancestry.citations[citation.id] = citation file = File('F1', __file__) ancestry.files[file.id] = file place = Place('P0', [LocalizedName('The Place')]) ancestry.places[place.id] = place person = Person('P0') event = IdentifiableEvent('E0', Event.Type.BIRTH) event.citations.append(citation) event.files.append(file) event.place = place ancestry.events[event.id] = event Presence(person, Presence.Role.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 setUpClass(cls): cls._outputDirectory = TemporaryDirectory() configuration = Configuration(cls._outputDirectory.name, 'https://ancestry.example.com') cls.site = Site(configuration) place1 = Place('PLACE1', 'one') event1 = Event('EVENT1', Event.Type.BIRTH) event1.place = place1 event1_person_1_presence = Presence(Presence.Role.SUBJECT) event1_person_1_presence.event = event1 person1 = Person('PERSON1', 'Janet', 'Dough') person1.presences.add(event1_person_1_presence) source1 = Source('SOURCE1', 'A Little Birdie') places = [place1] cls.site.ancestry.places.update({place.id: place for place in places}) events = [event1] cls.site.ancestry.events.update({event.id: event for event in events}) people = [person1] cls.site.ancestry.people.update( {person.id: person for person in people}) sources = [source1] cls.site.ancestry.sources.update( {source.id: source for source in sources}) render(cls.site)
def test_citation_should_encode_minimal(self): citation = IdentifiableCitation('the_citation', Source('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', }, ], } self.assert_encodes(expected, citation, 'citation')
async def test_citation(self): citation = IdentifiableCitation('CITATION1', Source('A Little Birdie')) self.site.ancestry.citations[citation.id] = citation await generate(self.site) self.assert_betty_html('/citation/%s/index.html' % citation.id) self.assert_betty_json('/citation/%s/index.json' % citation.id, 'citation')
async def test_post_parse(self): person = Person('P0') Presence(person, Subject(), Event(Birth())) source_file = File('F0', __file__) source = IdentifiableSource('S0', 'The Source') source.private = True source.files.append(source_file) citation_file = File('F0', __file__) citation_source = Source('The Source') citation = IdentifiableCitation('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.plugins[Privatizer] = None async with Site(configuration) as site: site.ancestry.people[person.id] = person site.ancestry.sources[source.id] = source site.ancestry.citations[citation.id] = citation await parse(site) self.assertTrue(person.private) self.assertTrue(source_file.private) self.assertTrue(citation_file.private)
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) privatize_person(person, 125) 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_should_remove_citations(self) -> None: event = Event(Event.Type.BIRTH) source = Source('S0', 'The Source') citation = Citation('C0', source) event.citations.append(citation) anonymize_event(event) self.assertEquals(0, len(event.citations))
def test_event_should_encode_full(self): event = IdentifiableEvent('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( IdentifiableCitation('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', }, ], } self.assert_encodes(expected, event, 'event')
async def test_with_citation(self): event = Event(Birth()) event.citations.append(Citation(Source('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_contains(self) -> None: source = IdentifiableSource('S0', 'The Source') contains = Source('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_source_should_encode_full(self): source = Source('the_source', 'The Source') source.author = 'The Author' source.publisher = 'The Publisher' source.date = Date(2000, 1, 1) source.contained_by = Source('the_containing_source', 'The Containing Source') source.links.add( Link('https://example.com/the-person', 'The Person Online')) source.contains.append( Source('the_contained_source', 'The Contained Source')) source.citations.append( Citation('the_citation', 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', 'author': 'The Author', 'publisher': 'The Publisher', 'contains': [ '/source/the_contained_source/index.json', ], 'citations': [ '/citation/the_citation/index.json', ], 'containedBy': '/source/the_containing_source/index.json', 'date': { 'year': 2000, 'month': 1, 'day': 1, }, 'links': [ { 'url': 'https://example.com/the-person', 'label': 'The Person Online', }, ], } self.assert_encodes(expected, source, 'source')
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 _parse_repository(ancestry: _IntermediateAncestry, element: Element) -> None: handle = _xpath1(element, './@handle') source = Source(_xpath1(element, './@id'), _xpath1(element, './ns:rname').text) _parse_urls(source, element) ancestry.sources[handle] = source
def test_clean_should_clean_source(self) -> None: ancestry = Ancestry() source = Source('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_citation_should_encode_minimal(self): citation = Citation('the_citation', Source('the_source', 'The Source')) expected = { '$schema': '/schema.json#/definitions/citation', '@type': 'https://schema.org/Thing', 'id': 'the_citation', 'source': '/source/the_source/index.json', 'facts': [], } self.assert_encodes(expected, citation, 'citation')
async def test_with_citation(self): name = PersonName('Jane') source = Source() citation = Citation(source) name.citations.append(citation) expected = '<span class="person-label" typeof="foaf:Person"><span property="foaf:individualName">Jane</span></span><a href="#reference-1" class="citation">[1]</a>' async with self._render(data={ 'name': name, }) as (actual, _): self.assertEqual(expected, actual)