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)
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_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])
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_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_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_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_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_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)
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_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 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_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_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)
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_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)
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')
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_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))
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_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)
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_clean_should_not_clean_file_with_entities(self) -> None: ancestry = Ancestry() person = Person('P0') ancestry.entities.append(person) file = File('F0', __file__) file.entities.append(person) ancestry.entities.append(file) clean(ancestry) self.assertEqual(file, ancestry.entities[File][file.id]) self.assertIn(person, file.entities) self.assertEqual(person, ancestry.entities[Person][person.id])
async def test_file_without_description(self): file_id = 'F1' file = File(file_id, __file__) 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) as app: app.ancestry.entities.append(file) indexed = [item for item in Index(app).build()] self.assertEquals([], indexed)
def test_clean_should_not_clean_file_with_citations(self) -> None: ancestry = Ancestry() source = Source(None) citation = Citation('C1', source) ancestry.entities.append(citation) file = File('F0', __file__) file.citations.append(citation) ancestry.entities.append(file) clean(ancestry) self.assertEqual(file, ancestry.entities[File][file.id]) self.assertIn(citation, file.citations) self.assertEqual(citation, ancestry.entities[Citation][citation.id])