class SelectLocalizedsTest(TemplateTestCase): @parameterized.expand([ ('', 'en', []), ('Apple', 'en', [PlaceName('Apple', 'en')]), ('Apple', 'en', [PlaceName('Apple', 'en-US')]), ('Apple', 'en-US', [PlaceName('Apple', 'en')]), ('', 'nl', [PlaceName('Apple', 'en')]), ('', 'nl-NL', [PlaceName('Apple', 'en')]), ]) @sync async def test(self, expected: str, locale: str, data: Iterable[Localized]): template = '{{ data | select_localizeds | map(attribute="name") | join(", ") }}' def _update_configuration(configuration: Configuration) -> None: configuration.locales.clear() configuration.locales[locale] = LocaleConfiguration(locale) async with self._render( template_string=template, data={ 'data': data, }, update_configuration=_update_configuration) as (actual, _): self.assertEquals(expected, actual)
def test_enclosed_by_should_sync_references(self): sut = Place('1', [PlaceName('one')]) enclosing_place = Place('2', [PlaceName('two')]) enclosure = Enclosure(sut, enclosing_place) self.assertIn(enclosure, sut.enclosed_by) self.assertEquals(sut, enclosure.encloses) sut.enclosed_by.remove(enclosure) self.assertCountEqual([], sut.enclosed_by) self.assertIsNone(enclosure.encloses)
def test_encloses(self) -> None: sut = Place('P1', [PlaceName('The Place')]) self.assertCountEqual([], sut.encloses) enclosed_place = Place('P2', [PlaceName('The Other Place')]) enclosure = Enclosure(enclosed_place, sut) self.assertIn(enclosure, sut.encloses) self.assertEquals(sut, enclosure.enclosed_by) sut.encloses.remove(enclosure) self.assertCountEqual([], sut.encloses) self.assertIsNone(enclosure.enclosed_by)
async def test_with_enclosing_place_without_place_context(self): place = Place('P0', [PlaceName('The Place')]) enclosing_place = Place('P1', [PlaceName('The Enclosing Place')]) Enclosure(place, enclosing_place) all_enclosing_place = Place('P2', [PlaceName('The All-enclosing Place')]) Enclosure(enclosing_place, all_enclosing_place) expected = '<div class="meta">in <address><a href="/place/P1/index.html"><span>The Enclosing Place</span></a></address>, <address><a href="/place/P2/index.html"><span>The All-enclosing Place</span></a></address></div>' async with self._render(data={ 'place': place, }) as (actual, _): self.assertEqual(expected, actual)
async def test_place(self, expected: str, locale: str): place_id = 'P1' place = Place(place_id, [PlaceName('Netherlands', 'en'), PlaceName('Nederland', 'nl')]) with TemporaryDirectory() as output_directory_path: configuration = Configuration( output_directory_path, 'https://example.com') configuration.locales['en-US'] = LocaleConfiguration('en-US', 'en') configuration.locales['nl-NL'] = LocaleConfiguration('nl-NL', 'nl') async with App(configuration).with_locale(locale) as app: app.ancestry.places[place_id] = place indexed = [item for item in Index(app).build()] self.assertEquals('netherlands nederland', indexed[0]['text']) self.assertIn(expected, indexed[0]['result'])
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)
async def test_without_enclosing_places(self): place = Place('P0', [PlaceName('The Place')]) expected = '<div class="meta"></div>' async with self._render(data={ 'place': place, }) as (actual, _): self.assertEqual(expected, actual)
class TestResourceTest(TemplateTestCase): @parameterized.expand([ ('true', Person, Person('P1')), ('false', Person, Place('P1', [PlaceName('The Place')])), ('true', Place, Place('P1', [PlaceName('The Place')])), ('false', Place, Person('P1')), ('false', Place, 999), ('false', Person, object()), ]) @sync async def test(self, expected, resource_type: Type[Resource], data) -> None: template = f'{{% if data is {resource_type.resource_type_name()}_resource %}}true{{% else %}}false{{% endif %}}' async with self._render(template_string=template, data={ 'data': data, }) as (actual, _): self.assertEquals(expected, actual)
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_place(self): event = Event(Birth()) event.place = Place('P0', [PlaceName('The Place')]) expected = 'in <address><a href="/place/P0/index.html"><span>The Place</span></a></address>' async with self._render(data={ 'event': event, }) as (actual, _): self.assertEqual(expected, actual)
class Test(TemplateTestCase): template_file = 'label/place.html.j2' @parameterized.expand([ ('<address><a href="/place/P0/index.html"><span>The Place</span></a></address>', { 'place': Place('P0', [PlaceName('The Place')]), }), ('<address><a href="/place/P0/index.html"><span lang="en">The Place</span></a></address>', { 'place': Place('P0', [PlaceName('The Place', 'en')]), }), ('<address><a href="/place/P0/index.html"><span lang="nl">De Plaats</span></a></address>', { 'place': Place('P0', [PlaceName('The Place', 'en'), PlaceName('De Plaats', 'nl')]), 'locale': 'nl', }), ('<address><span>The Place</span></address>', { 'place': Place('P0', [PlaceName('The Place')]), 'embedded': True, }), ('<address><a href="/place/P0/index.html"><span lang="nl">De Nieuwe Plaats</span></a></address>', { 'place': Place('P0', [PlaceName('The Old Place', 'en', date=DateRange(None, Date(1969, 12, 31))), PlaceName('De Nieuwe Plaats', 'nl', date=DateRange(Date(1970, 1, 1)))]), 'locale': 'nl', 'date_context': Date(1970, 1, 1), }) ]) @sync async def test(self, expected, data): async with self._render(data=data) as (actual, _): self.assertEqual(expected, actual)
def test_events(self) -> None: sut = Place('P1', [PlaceName('The Place')]) event = IdentifiableEvent('1', Birth()) sut.events.append(event) self.assertIn(event, sut.events) self.assertEquals(sut, event.place) sut.events.remove(event) self.assertCountEqual([], sut.events) self.assertEquals(None, event.place)
def test_events_should_sync_references(self): sut = Place('1', [PlaceName('one')]) event = IdentifiableEvent('1', Birth()) sut.events.append(event) self.assertIn(event, sut.events) self.assertEquals(sut, event.place) sut.events.remove(event) self.assertCountEqual([], sut.events) self.assertEquals(None, event.place)
def test_place(self) -> None: place = Place('1', [PlaceName('one')]) sut = Event(Mock(EventType)) sut.place = place self.assertEquals(place, sut.place) self.assertIn(sut, place.events) sut.place = None self.assertEquals(None, sut.place) self.assertNotIn(sut, place.events)
def test_place_should_sync_references(self): place = Place('1', [PlaceName('one')]) sut = IdentifiableEvent('1', Birth()) sut.place = place self.assertEquals(place, sut.place) self.assertIn(sut, place.events) sut.place = None self.assertEquals(None, sut.place) self.assertNotIn(sut, place.events)
async def test(self): template = '{{ data | sort_localizeds(localized_attribute="names", sort_attribute="name") }}' data = [ self.WithLocalizedNames('third', [ PlaceName('3', 'nl-NL'), ]), self.WithLocalizedNames('second', [ PlaceName('2', 'en'), PlaceName('1', 'nl-NL'), ]), self.WithLocalizedNames('first', [ PlaceName('2', 'nl-NL'), PlaceName('1', 'en-US'), ]), ] async with self._render(template_string=template, data={ 'data': data, }) as (actual, _): self.assertEquals('[first, second, third]', actual)
async def test_with_place_is_place_context(self): event = Event(Birth()) place = Place('P0', [PlaceName('The Place')]) event.place = place expected = '' async with self._render(data={ 'event': event, 'place_context': place, }) as (actual, _): self.assertEqual(expected, actual)
class LocalizedNameTest(TestCase): @parameterized.expand([ (True, PlaceName('Ikke'), PlaceName('Ikke')), (True, PlaceName('Ikke', 'nl-NL'), PlaceName('Ikke', 'nl-NL')), (False, PlaceName('Ikke', 'nl-NL'), PlaceName('Ikke', 'nl-BE')), (False, PlaceName('Ikke', 'nl-NL'), PlaceName('Ik', 'nl-NL')), (False, PlaceName('Ikke'), PlaceName('Ik')), ]) def test_eq(self, expected, a, b): self.assertEquals(expected, a == b) def test_str(self): name = 'Ikke' sut = PlaceName(name) self.assertEquals(name, str(sut)) def test_name(self): name = 'Ikke' sut = PlaceName(name) self.assertEquals(name, sut.name)
async def test_embedded(self): event = Event(Birth()) event.date = Date(1970) event.place = Place('P0', [PlaceName('The Place')]) event.citations.append(Citation(Source('The Source'))) expected = '1970 in <address><span>The Place</span></address>' async with self._render(data={ 'event': event, 'embedded': True, }) as (actual, _): self.assertEqual(expected, actual)
def test_clean(self) -> None: ancestry = Ancestry() onymous_event = IdentifiableEvent('E0', Birth()) Presence(Person('P0'), Subject(), onymous_event) ancestry.events[onymous_event.id] = onymous_event anonymous_event = IdentifiableEvent('E1', Birth()) ancestry.events[anonymous_event.id] = anonymous_event onymous_place = Place('P0', [PlaceName('Amsterdam')]) onymous_place.events.append(onymous_event) ancestry.places[onymous_place.id] = onymous_place anonymous_place = Place('P1', [PlaceName('Almelo')]) ancestry.places[anonymous_place.id] = 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.places[onmyous_place_because_encloses_onmyous_places. id] = onmyous_place_because_encloses_onmyous_places clean(ancestry) self.assertDictEqual({ onymous_event.id: onymous_event, }, ancestry.events) self.assertDictEqual( { onymous_place.id: onymous_place, onmyous_place_because_encloses_onmyous_places.id: onmyous_place_because_encloses_onmyous_places, }, ancestry.places) self.assertNotIn( anonymous_place, onmyous_place_because_encloses_onmyous_places.encloses)
async def test_include_unspecified(self): template = '{{ data | select_localizeds(include_unspecified=true) | map(attribute="name") | join(", ") }}' data = [ PlaceName('Apple', 'zxx'), PlaceName('Apple', 'und'), PlaceName('Apple', 'mul'), PlaceName('Apple', 'mis'), PlaceName('Apple', None), ] def _update_configuration(configuration: Configuration) -> None: configuration.locales.clear() configuration.locales['en-US'] = LocaleConfiguration('en-US') async with self._render( template_string=template, data={ 'data': data, }, update_configuration=_update_configuration) as (actual, _): self.assertEquals('Apple, Apple, Apple, Apple, Apple', actual)
def test_place_should_encode_minimal(self): place_id = 'the_place' name = 'The Place' place = Place(place_id, [PlaceName(name)]) expected = { '$schema': '/schema.json#/definitions/place', '@context': { 'enclosedBy': 'https://schema.org/containedInPlace', 'encloses': 'https://schema.org/containsPlace', 'events': 'https://schema.org/event' }, '@type': 'https://schema.org/Place', 'id': place_id, 'names': [ { 'name': name, }, ], 'enclosedBy': [], 'encloses': [], 'events': [], 'links': [ { 'url': '/en/place/the_place/index.json', 'relationship': 'canonical', 'mediaType': 'application/json', }, { 'url': '/nl/place/the_place/index.json', 'relationship': 'alternate', 'locale': 'nl-NL', }, { 'url': '/en/place/the_place/index.html', 'relationship': 'alternate', 'mediaType': 'text/html', }, ], } self.assert_encodes(expected, place, 'place')
class SiteUrlGeneratorTest(TestCase): @parameterized.expand([ ('/index.html', '/index.html'), ('/person/P1/index.html', Person('P1')), ('/event/E1/index.html', IdentifiableEvent('E1', Death())), ('/place/P1/index.html', Place('P1', [PlaceName('Place 1')])), ('/file/F1/index.html', File('F1', '/tmp')), ('/source/S1/index.html', IdentifiableSource('S1', 'Source 1')), ('/citation/C1/index.html', IdentifiableCitation('C1', Source('Source 1'))), ]) def test_generate(self, expected: str, resource: Any): configuration = Configuration('/tmp', 'https://example.com') sut = SiteUrlGenerator(configuration) self.assertEquals(expected, sut.generate(resource, 'text/html')) def test_generate_with_invalid_value(self): configuration = Configuration('/tmp', 'https://example.com') sut = SiteUrlGenerator(configuration) with self.assertRaises(ValueError): sut.generate(9, 'text/html')
def _load_place(element: ElementTree.Element) -> Tuple[str, _IntermediatePlace]: handle = element.get('handle') names = [] for name_element in _xpath(element, './ns:pname'): # The Gramps language is a single ISO language code, which is a valid BCP 47 locale. language = name_element.get('lang') date = _load_date(name_element) name = PlaceName(name_element.get('value'), locale=language, date=date) names.append(name) place = Place(element.get('id'), names) coordinates = _load_coordinates(element) if coordinates: place.coordinates = coordinates enclosed_by_handles = [element.get('hlink') for element in _xpath(element, './ns:placeref')] _load_urls(place, element) return handle, _IntermediatePlace(place, enclosed_by_handles)
def _parse_place(element: Element) -> Tuple[str, _IntermediatePlace]: handle = _xpath1(element, './@handle') names = [] for name_element in _xpath(element, './ns:pname'): # The Gramps language is a single ISO language code, which is a valid BCP 47 locale. language = _xpath1(name_element, './@lang') date = _parse_date(name_element) name = PlaceName(str(_xpath1(name_element, './@value')), locale=language, date=date) names.append(name) place = Place(_xpath1(element, './@id'), names) coordinates = _parse_coordinates(element) if coordinates: place.coordinates = coordinates enclosed_by_handles = _xpath(element, './ns:placeref/@hlink') _parse_urls(place, element) return handle, _IntermediatePlace(place, enclosed_by_handles)
def test_coordinates(self) -> None: name = PlaceName('The Place') sut = Place('P1', [name]) coordinates = Point() sut.coordinates = coordinates self.assertEquals(coordinates, sut.coordinates)
def test_links(self) -> None: sut = Place('P1', [PlaceName('The Place')]) self.assertCountEqual([], sut.links)
def test_names(self) -> None: name = PlaceName('The Place') sut = Place('P1', [name]) self.assertCountEqual([name], sut.names)
def test_id(self) -> None: place_id = 'C1' sut = Place(place_id, [PlaceName('one')]) self.assertEquals(place_id, sut.id)