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 = IdentifiableSource('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 Site(configuration) as site: site.ancestry.sources[resource.id] = resource sut = _Populator(site, 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)
async def test_populate_should_ignore_resource_without_links( self, m_retriever) -> None: resource = IdentifiableSource('the_source', 'The Source') 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 Site(configuration) as site: site.ancestry.sources[resource.id] = resource sut = _Populator(site, m_retriever) await sut.populate() self.assertSetEqual(set(), resource.links)
async def test_populate_should_ignore_resource_without_link_support( self, m_retriever) -> None: source = Source('The Source') resource = IdentifiableCitation('the_citation', source) 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 Site(configuration) as site: site.ancestry.citations[resource.id] = resource sut = _Populator(site, m_retriever) await sut.populate()
async def test_populate_link_should_convert_http_to_https( self, m_retriever) -> None: link = Link('http://en.wikipedia.org/wiki/Amsterdam') entry_language = 'nl' 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 Site(configuration) as site: sut = _Populator(site, m_retriever) await sut.populate_link(link, entry_language) self.assertEqual('https://en.wikipedia.org/wiki/Amsterdam', link.url)
async def test_populate_should_add_translation_links(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) added_entry_language = 'nl' added_entry_name = 'Amsterdam' added_entry_title = 'Amsterdam' added_entry_content = 'Hoofdstad van Nederland' added_entry = Entry(added_entry_language, added_entry_name, added_entry_title, added_entry_content) m_retriever.get_entry.side_effect = [entry, added_entry] m_retriever.get_translations.return_value = { entry_language: entry_name, added_entry_language: added_entry_name, } resource = IdentifiableSource('the_source', 'The Source') link_en = Link('https://en.wikipedia.org/wiki/Amsterdam') resource.links.add(link_en) 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 configuration.locales.clear() configuration.locales['en-US'] = LocaleConfiguration( 'en-US', 'en') configuration.locales['nl-NL'] = LocaleConfiguration( 'nl-NL', 'nl') async with Site(configuration) as site: site.ancestry.sources[resource.id] = resource sut = _Populator(site, m_retriever) await sut.populate() m_retriever.get_entry.assert_has_calls([ call(entry_language, entry_name), call(added_entry_language, added_entry_name), ]) m_retriever.get_translations.assert_called_once_with( entry_language, entry_name) self.assertEqual(2, len(resource.links)) link_nl = resource.links.difference({link_en}).pop() self.assertEqual('Amsterdam', link_nl.label) self.assertEqual('nl', link_nl.locale) self.assertEqual(MediaType('text/html'), link_nl.media_type) self.assertIsNotNone(link_nl.description) self.assertEqual('external', link_nl.relationship)
async def test_populate_link_should_set_description( self, expected: str, description: str, m_retriever) -> None: link = Link('http://en.wikipedia.org/wiki/Amsterdam') link.description = description entry_language = 'en' 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 Site(configuration) as site: sut = _Populator(site, m_retriever) await sut.populate_link(link, entry_language) self.assertEqual(expected, link.description)
async def test_populate_link_should_set_relationship( self, expected: str, relationship: Optional[str], m_retriever) -> None: link = Link('http://en.wikipedia.org/wiki/Amsterdam') link.relationship = relationship 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 Site(configuration) as site: sut = _Populator(site, m_retriever) await sut.populate_link(link, 'en') self.assertEqual(expected, link.relationship)
async def test_populate_link_should_set_label(self, expected: str, label: Optional[str], m_retriever) -> None: link = Link('http://en.wikipedia.org/wiki/Amsterdam') link.label = label entry = Entry('en', 'The_city_of_Amsterdam', 'The city of Amsterdam', 'Amsterdam, such a lovely place!') 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 Site(configuration) as site: sut = _Populator(site, m_retriever) await sut.populate_link(link, 'en', entry) self.assertEqual(expected, link.label)