def test_event_on_post_build_multi_lang(self, mock_copy_file, mock_write_file): plugin = search.SearchPlugin() plugin.load_config({'lang': ['es', 'fr']}) config = load_config(theme='elstir') plugin.on_pre_build(config) plugin.on_post_build(config) self.assertEqual(mock_copy_file.call_count, 4) self.assertEqual(mock_write_file.call_count, 1)
def test_event_on_post_build_defaults(self, mock_copy_file, mock_write_file): plugin = search.SearchPlugin() plugin.load_config({}) config = load_config(theme='elstir') plugin.on_pre_build(config) plugin.on_post_build(config) self.assertEqual(mock_copy_file.call_count, 0) self.assertEqual(mock_write_file.call_count, 1)
def test_event_on_config_defaults(self): plugin = search.SearchPlugin() plugin.load_config({}) result = plugin.on_config( load_config(theme='elstir', extra_javascript=[])) self.assertFalse(result['theme']['search_index_only']) self.assertFalse(result['theme']['include_search_page']) self.assertEqual(result['theme'].static_templates, {'404.html', 'sitemap.xml'}) self.assertEqual(len(result['theme'].dirs), 3) self.assertEqual(result['extra_javascript'], ['search/main.js'])
def test_event_on_post_build_search_index_only(self, mock_copy_file, mock_write_file): plugin = search.SearchPlugin() plugin.load_config({'lang': ['es']}) config = load_config(theme={ 'name': 'elstir', 'search_index_only': True }) plugin.on_pre_build(config) plugin.on_post_build(config) self.assertEqual(mock_copy_file.call_count, 0) self.assertEqual(mock_write_file.call_count, 1)
def test_plugin_config_min_search_length(self): expected = { 'lang': ['en'], 'separator': r'[\s\-]+', 'min_search_length': 2, 'prebuild_index': False, 'indexing': 'full' } plugin = search.SearchPlugin() errors, warnings = plugin.load_config({'min_search_length': 2}) self.assertEqual(plugin.config, expected) self.assertEqual(errors, []) self.assertEqual(warnings, [])
def test_event_on_config_search_index_only(self): plugin = search.SearchPlugin() plugin.load_config({}) config = load_config(theme={ 'name': 'elstir', 'search_index_only': True }, extra_javascript=[]) result = plugin.on_config(config) self.assertTrue(result['theme']['search_index_only']) self.assertFalse(result['theme']['include_search_page']) self.assertEqual(result['theme'].static_templates, {'404.html', 'sitemap.xml'}) self.assertEqual(len(result['theme'].dirs), 2) self.assertEqual(len(result['extra_javascript']), 0)
def test_search_indexing_options(self): def test_page(title, filename, config): test_page = Page( title, File(filename, config['docs_dir'], config['site_dir'], config['use_directory_urls']), config) test_page.content = """ <h1 id="heading-1">Heading 1</h1> <p>Content 1</p> <h2 id="heading-2">Heading 2</h1> <p>Content 2</p> <h3 id="heading-3">Heading 3</h1> <p>Content 3</p>""" test_page.markdown = dedent(""" # Heading 1 ## Heading 2 ### Heading 3""") test_page.toc = get_toc(get_markdown_toc(test_page.markdown)) return test_page validate = { 'full': (lambda data: self.assertEqual(len(data[0]), 4) and self. assertTrue([x for x in data[0][0] if x['title'] and x['text']])), 'sections': ( lambda data: # Sanity self.assertEqual(len(data[0]), 4) and # Page (self.assertEqual(data[0][0]['title'], data[1].title) and self. assertTrue(data[0][0]['text'])) and # Headings self.assertTrue( [x for x in data[0][1:] if x['title'] and not x['text']])), 'titles': ( lambda data: # Sanity self.assertEqual(len(data[0]), 1) and self.assertFalse( [x for x in data[0] if x['text']])) } for option in ['full', 'sections', 'titles']: plugin = search.SearchPlugin() # Load plugin config, overriding indexing for test case errors, warnings = plugin.load_config({'indexing': option}) self.assertEqual(errors, []) self.assertEqual(warnings, []) base_cfg = load_config() base_cfg['plugins']['search'].config['indexing'] = option pages = [ test_page('Home', 'index.md', base_cfg), test_page('About', 'about.md', base_cfg) ] for page in pages: index = search_index.SearchIndex(**plugin.config) index.add_entry_from_context(page) data = index.generate_search_index() validate[option]((json.loads(data)['docs'], page))
def test_create_search_index(self): html_content = """ <h1 id="heading-1">Heading 1</h1> <p>Content 1</p> <h2 id="heading-2">Heading 2</h1> <p>Content 2</p> <h3 id="heading-3">Heading 3</h1> <p>Content 3</p> """ base_cfg = load_config() pages = [ Page( 'Home', File('index.md', base_cfg['docs_dir'], base_cfg['site_dir'], base_cfg['use_directory_urls']), base_cfg), Page( 'About', File('about.md', base_cfg['docs_dir'], base_cfg['site_dir'], base_cfg['use_directory_urls']), base_cfg) ] md = dedent(""" # Heading 1 ## Heading 2 ### Heading 3 """) toc = get_toc(get_markdown_toc(md)) full_content = ''.join("""Heading{0}Content{0}""".format(i) for i in range(1, 4)) plugin = search.SearchPlugin() errors, warnings = plugin.load_config({}) for page in pages: # Fake page.read_source() and page.render() page.markdown = md page.toc = toc page.content = html_content index = search_index.SearchIndex(**plugin.config) index.add_entry_from_context(page) self.assertEqual(len(index._entries), 4) loc = page.url self.assertEqual(index._entries[0]['title'], page.title) self.assertEqual(strip_whitespace(index._entries[0]['text']), full_content) self.assertEqual(index._entries[0]['location'], loc) self.assertEqual(index._entries[1]['title'], "Heading 1") self.assertEqual(index._entries[1]['text'], "Content 1") self.assertEqual(index._entries[1]['location'], "{}#heading-1".format(loc)) self.assertEqual(index._entries[2]['title'], "Heading 2") self.assertEqual(strip_whitespace(index._entries[2]['text']), "Content2") self.assertEqual(index._entries[2]['location'], "{}#heading-2".format(loc)) self.assertEqual(index._entries[3]['title'], "Heading 3") self.assertEqual(strip_whitespace(index._entries[3]['text']), "Content3") self.assertEqual(index._entries[3]['location'], "{}#heading-3".format(loc))