def test_remove(): """Test removing figures from a report.""" r = Report() fig1, fig2 = _get_example_figures() r.add_figure(fig=fig1, title='figure1', tags=('slider', )) r.add_figure(fig=[fig1, fig2], title='figure1', tags=('othertag', )) r.add_figure(fig=fig2, title='figure1', tags=('slider', )) r.add_figure(fig=fig2, title='figure2', tags=('slider', )) # Test removal by title r2 = copy.deepcopy(r) removed_index = r2.remove(title='figure1') assert removed_index == 2 assert len(r2.html) == 3 assert r2.html[0] == r.html[0] assert r2.html[1] == r.html[1] assert r2.html[2] == r.html[3] # Test restricting to section r2 = copy.deepcopy(r) removed_index = r2.remove(title='figure1', tags=('othertag', )) assert removed_index == 1 assert len(r2.html) == 3 assert r2.html[0] == r.html[0] assert r2.html[1] == r.html[2] assert r2.html[2] == r.html[3]
def test_scraper(tmp_path): """Test report scraping.""" r = Report() fig1, fig2 = _get_example_figures() r.add_figure(fig=fig1, title='a') r.add_figure(fig=fig2, title='b') # Mock a Sphinx + sphinx_gallery config app = Bunch(builder=Bunch(srcdir=str(tmp_path), outdir=op.join(str(tmp_path), '_build', 'html'))) scraper = _ReportScraper() scraper.app = app gallery_conf = dict(src_dir=app.builder.srcdir, builder_name='html') img_fname = op.join(app.builder.srcdir, 'auto_examples', 'images', 'sg_img.png') target_file = op.join(app.builder.srcdir, 'auto_examples', 'sg.py') os.makedirs(op.dirname(img_fname)) os.makedirs(app.builder.outdir) block_vars = dict(image_path_iterator=(img for img in [img_fname]), example_globals=dict(a=1), target_file=target_file) # Nothing yet block = None rst = scraper(block, block_vars, gallery_conf) assert rst == '' # Still nothing block_vars['example_globals']['r'] = r rst = scraper(block, block_vars, gallery_conf) # Once it's saved, add it assert rst == '' fname = op.join(str(tmp_path), 'my_html.html') r.save(fname, open_browser=False) rst = scraper(block, block_vars, gallery_conf) out_html = op.join(app.builder.outdir, 'auto_examples', 'my_html.html') assert not op.isfile(out_html) scraper.copyfiles() assert op.isfile(out_html) assert rst.count('"') == 6 assert "<iframe" in rst assert op.isfile(img_fname.replace('png', 'svg'))
def test_add_or_replace(): """Test replacing existing figures in a report.""" r = Report() fig1, fig2 = _get_example_figures() r.add_figure(fig=fig1, title='duplicate', tags=('foo', )) r.add_figure(fig=fig1, title='duplicate', tags=('foo', )) r.add_figure(fig=fig1, title='duplicate', tags=('bar', )) r.add_figure(fig=fig2, title='nonduplicate', tags=('foo', )) # By default, replace=False, so all figures should be there assert len(r.html) == 4 old_r = copy.deepcopy(r) # Replace last occurrence of `fig1` tagges as `foo` r.add_figure(fig=fig2, title='duplicate', tags=('foo', ), replace=True) assert len(r._content) == len(r.html) == 4 assert r.html[1] != old_r.html[1] # This figure should have changed # All other figures should be the same assert r.html[0] == old_r.html[0] assert r.html[2] == old_r.html[2] assert r.html[3] == old_r.html[3]