class FilterImageTest(TemplateTestCase): image_path = Path(__file__).parents[ 1] / 'assets' / 'public' / 'static' / 'betty-512x512.png' @parameterized.expand([ ('/file/F1-99x-.png', '{{ file | image(width=99) }}', File('F1', image_path, media_type=MediaType('image/png'))), ('/file/F1--x99.png', '{{ file | image(height=99) }}', File('F1', image_path, media_type=MediaType('image/png'))), ('/file/F1-99x99.png', '{{ file | image(width=99, height=99) }}', File('F1', image_path, media_type=MediaType('image/png'))), ('/file/F1-99x99.png:/file/F1-99x99.png', '{{ file | image(width=99, height=99) }}:{{ file | image(width=99, height=99) }}', File('F1', image_path, media_type=MediaType('image/png'))), ]) @sync async def test(self, expected, template, file): async with self._render(template_string=template, data={ 'file': file, }) as (actual, app): self.assertEquals(expected, actual) for file_path in actual.split(':'): self.assertTrue((app.configuration.www_directory_path / file_path[1:]).exists()) @sync async def test_without_width(self): file = File('F1', self.image_path, media_type=MediaType('image/png')) with self.assertRaises(ValueError): async with self._render(template_string='{{ file | image }}', data={ 'file': file, }): pass
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_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 test_with_private_file_should_anonymize(self, m_anonymize_file) -> None: file = File('F0', __file__) file.private = True ancestry = Ancestry() ancestry.entities.append(file) anonymize(ancestry, AnonymousCitation(AnonymousSource())) m_anonymize_file.assert_called_once_with(file)
def test_private(self) -> None: file_id = 'BETTY01' file_path = Path('~') sut = File(file_id, file_path) self.assertIsNone(sut.private) private = True sut.private = private self.assertEquals(private, sut.private)
def test_media_type(self) -> None: file_id = 'BETTY01' file_path = Path('~') sut = File(file_id, file_path) self.assertIsNone(sut.media_type) media_type = MediaType('text/plain') sut.media_type = media_type self.assertEquals(media_type, sut.media_type)
def test_description(self) -> None: file_id = 'BETTY01' file_path = Path('~') sut = File(file_id, file_path) self.assertIsNone(sut.description) description = 'Hi, my name is Betty!' sut.description = description self.assertEquals(description, sut.description)
def test_notes(self) -> None: file_id = 'BETTY01' file_path = Path('~') sut = File(file_id, file_path) self.assertCountEqual([], sut.notes) notes = [Mock(Note), Mock(Note)] sut.notes = notes self.assertCountEqual(notes, sut.notes)
def test_privatize_file_should_not_privatize_if_public(self): source = Source(None, 'The Source') citation = Citation(None, source) file = File('F0', __file__) file.private = False file.citations.append(citation) ancestry = Ancestry() ancestry.entities.append(file) privatize(ancestry) self.assertEqual(False, file.private) self.assertIsNone(citation.private)
def test_privatize_file_should_privatize_if_private(self): source = Source(None, 'The Source') citation = Citation(None, source) file = File('F0', __file__) file.private = True file.citations.append(citation) ancestry = Ancestry() ancestry.entities.append(file) privatize(ancestry) self.assertTrue(True, file.private) self.assertTrue(citation.private)
def test_entities(self) -> None: file_id = 'BETTY01' file_path = Path('~') sut = File(file_id, file_path) self.assertCountEqual([], sut.entities) class _HasFiles(Entity, HasFiles): pass entities = [_HasFiles(), _HasFiles()] sut.entities = entities self.assertCountEqual(entities, sut.entities)
def test_privatize_citation_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 = Citation('C0', source) citation.private = False citation.files.append(citation_file) ancestry = Ancestry() ancestry.entities.append(citation) privatize(ancestry) self.assertEqual(False, citation.private) self.assertIsNone(source.private) self.assertIsNone(citation_file.private) self.assertIsNone(source_file.private)
def test_privatize_citation_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.private = True citation.files.append(citation_file) ancestry = Ancestry() ancestry.entities.append(citation) privatize(ancestry) self.assertTrue(citation.private) self.assertTrue(source.private) self.assertTrue(citation_file.private) self.assertTrue(source_file.private)
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])
async def test_file_should_encode_minimal(self): with NamedTemporaryFile() as f: file = File('the_file', f.name) expected = { '$schema': '/schema.json#/definitions/file', 'id': 'the_file', 'entities': [], 'notes': [], 'links': [ { 'url': '/en/file/the_file/index.json', 'relationship': 'canonical', 'mediaType': 'application/json', }, { 'url': '/nl/file/the_file/index.json', 'relationship': 'alternate', 'locale': 'nl-NL', }, { 'url': '/en/file/the_file/index.html', 'relationship': 'alternate', 'mediaType': 'text/html', }, ], } await self.assert_encodes(expected, file, 'file')
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_without_width(self): file = File('F1', self.image_path, media_type=MediaType('image/png')) with self.assertRaises(ValueError): async with self._render(template_string='{{ file | image }}', data={ 'file': file, }): pass
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_clean_should_clean_file(self) -> None: ancestry = Ancestry() file = File('F0', __file__) ancestry.entities.append(file) clean(ancestry) self.assertNotIn(file.id, ancestry.entities[File])
def _load_object(loader: _Loader, element: ElementTree.Element, gramps_tree_directory_path: Path): file_handle = element.get('handle') file_id = element.get('id') file_element = _xpath1(element, './ns:file') file_path = gramps_tree_directory_path / file_element.get('src') file = File(file_id, file_path) file.media_type = MediaType(file_element.get('mime')) description = file_element.get('description') if description: file.description = description _load_attribute_privacy(file, element, 'attribute') loader.add_entity(FlattenedEntity(file, file_handle)) for citation_handle in _load_handles('citationref', element): loader.add_association(File, file_handle, 'citations', Citation, citation_handle) for note_handle in _load_handles('noteref', element): loader.add_association(File, file_handle, 'notes', Note, note_handle)
async def test_file(self, expected: str, locale: str): file_id = 'F1' file = File(file_id, __file__) file.description = '"file" is Dutch for "traffic jam"' with TemporaryDirectory() as output_directory_path: configuration = Configuration(output_directory_path, 'https://example.com') configuration.locales.replace([ LocaleConfiguration('en-US', 'en'), LocaleConfiguration('nl-NL', 'nl'), ]) async with App(configuration).with_locale(locale) as app: app.ancestry.entities.append(file) indexed = [item for item in Index(app).build()] self.assertEquals('"file" is dutch for "traffic jam"', indexed[0]['text']) self.assertIn(expected, indexed[0]['result'])
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)
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)
async def test_file(self): configuration = Configuration(self._output_directory.name, 'https://ancestry.example.com') app = App(configuration) async with app: with NamedTemporaryFile() as f: file = File('FILE1', Path(f.name)) app.ancestry.entities.append(file) await generate(app) self.assert_betty_html(app, '/file/%s/index.html' % file.id) self.assert_betty_json(app, '/file/%s/index.json' % file.id, 'file')
async def test_file_should_encode_full(self): with NamedTemporaryFile() as f: note = Note('the_note', 'The Note') file = File('the_file', f.name) file.media_type = MediaType('text/plain') file.notes.append(note) Person('the_person').files.append(file) expected = { '$schema': '/schema.json#/definitions/file', 'id': 'the_file', 'mediaType': 'text/plain', 'entities': [ '/en/person/the_person/index.json', ], 'notes': [ '/en/note/the_note/index.json', ], 'links': [ { 'url': '/en/file/the_file/index.json', 'relationship': 'canonical', 'mediaType': 'application/json', }, { 'url': '/nl/file/the_file/index.json', 'relationship': 'alternate', 'locale': 'nl-NL', }, { 'url': '/en/file/the_file/index.html', 'relationship': 'alternate', 'mediaType': 'text/html', }, ], } await self.assert_encodes(expected, file, 'file')
class FilterFileTest(TemplateTestCase): @parameterized.expand([ ( '/file/F1/file/test_jinja2.py', '{{ file | file }}', File('F1', Path(__file__)), ), ( '/file/F1/file/test_jinja2.py:/file/F1/file/test_jinja2.py', '{{ file | file }}:{{ file | file }}', File('F1', Path(__file__)), ), ]) @sync async def test(self, expected, template, file): async with self._render(template_string=template, data={ 'file': file, }) as (actual, app): self.assertEquals(expected, actual) for file_path in actual.split(':'): self.assertTrue((app.configuration.www_directory_path / file_path[1:]).exists())
def test_replace(self): class _HasCitations(HasCitations, Entity): pass facts = [_HasCitations()] files = [File('F1', __file__)] source = Mock(Source) sut = AnonymousCitation(source) other = AnonymousCitation(source) other.facts = facts other.files = files sut.replace(other) self.assertEquals(facts, list(sut.facts)) self.assertEquals(files, list(sut.files))
def test_clean_should_not_clean_source_with_files(self) -> None: ancestry = Ancestry() file = File('F0', __file__) ancestry.entities.append(file) source = Source('S0', 'The Source') source.files.append(file) ancestry.entities.append(source) clean(ancestry) self.assertEqual(source, ancestry.entities[Source][source.id]) self.assertIn(source, file.entities) self.assertEqual(file, ancestry.entities[File][file.id])
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)