test_exporter.py
108 lines
| 3.8 KiB
| text/x-python
|
PythonLexer
Jonathan Frederic
|
r11480 | """ | ||
Module with tests for exporter.py | ||||
""" | ||||
#----------------------------------------------------------------------------- | ||||
# Copyright (c) 2013, the IPython Development Team. | ||||
# | ||||
# Distributed under the terms of the Modified BSD License. | ||||
# | ||||
# The full license is in the file COPYING.txt, distributed with this software. | ||||
#----------------------------------------------------------------------------- | ||||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
from IPython.config import Config | ||||
from .base import ExportersTestsBase | ||||
Paul Ivanov
|
r12219 | from .cheese import CheesePreprocessor | ||
Jonathan Frederic
|
r11480 | from ..exporter import Exporter | ||
#----------------------------------------------------------------------------- | ||||
# Class | ||||
#----------------------------------------------------------------------------- | ||||
Jonathan Frederic
|
r11494 | class TestExporter(ExportersTestsBase): | ||
Jonathan Frederic
|
r11480 | """Contains test functions for exporter.py""" | ||
def test_constructor(self): | ||||
""" | ||||
Can an Exporter be constructed? | ||||
""" | ||||
Exporter() | ||||
def test_export(self): | ||||
""" | ||||
Can an Exporter export something? | ||||
""" | ||||
exporter = self._make_exporter() | ||||
(output, resources) = exporter.from_filename(self._get_notebook()) | ||||
assert len(output) > 0 | ||||
Jonathan Frederic
|
r11638 | def test_extract_outputs(self): | ||
Jonathan Frederic
|
r11480 | """ | ||
Paul Ivanov
|
r12219 | If the ExtractOutputPreprocessor is enabled, are outputs extracted? | ||
Jonathan Frederic
|
r11480 | """ | ||
Paul Ivanov
|
r12219 | config = Config({'ExtractOutputPreprocessor': {'enabled': True}}) | ||
Jonathan Frederic
|
r11480 | exporter = self._make_exporter(config=config) | ||
(output, resources) = exporter.from_filename(self._get_notebook()) | ||||
assert resources is not None | ||||
Jonathan Frederic
|
r12143 | assert isinstance(resources['outputs'], dict) | ||
Jonathan Frederic
|
r11638 | assert len(resources['outputs']) > 0 | ||
Jonathan Frederic
|
r11480 | |||
Paul Ivanov
|
r12219 | def test_preprocessor_class(self): | ||
Jonathan Frederic
|
r11480 | """ | ||
Paul Ivanov
|
r12219 | Can a preprocessor be added to the preprocessors list by class type? | ||
Jonathan Frederic
|
r11480 | """ | ||
Paul Ivanov
|
r12219 | config = Config({'Exporter': {'preprocessors': [CheesePreprocessor]}}) | ||
Jonathan Frederic
|
r11480 | exporter = self._make_exporter(config=config) | ||
(output, resources) = exporter.from_filename(self._get_notebook()) | ||||
assert resources is not None | ||||
assert resources['cheese'] == 'real' | ||||
Paul Ivanov
|
r12219 | def test_preprocessor_instance(self): | ||
Jonathan Frederic
|
r11480 | """ | ||
Paul Ivanov
|
r12219 | Can a preprocessor be added to the preprocessors list by instance? | ||
Jonathan Frederic
|
r11480 | """ | ||
Paul Ivanov
|
r12219 | config = Config({'Exporter': {'preprocessors': [CheesePreprocessor()]}}) | ||
Jonathan Frederic
|
r11480 | exporter = self._make_exporter(config=config) | ||
(output, resources) = exporter.from_filename(self._get_notebook()) | ||||
assert resources is not None | ||||
assert resources['cheese'] == 'real' | ||||
Paul Ivanov
|
r12219 | def test_preprocessor_dottedobjectname(self): | ||
Jonathan Frederic
|
r11480 | """ | ||
Paul Ivanov
|
r12219 | Can a preprocessor be added to the preprocessors list by dotted object name? | ||
Jonathan Frederic
|
r11480 | """ | ||
Paul Ivanov
|
r12219 | config = Config({'Exporter': {'preprocessors': ['IPython.nbconvert.exporters.tests.cheese.CheesePreprocessor']}}) | ||
Jonathan Frederic
|
r11480 | exporter = self._make_exporter(config=config) | ||
(output, resources) = exporter.from_filename(self._get_notebook()) | ||||
assert resources is not None | ||||
assert resources['cheese'] == 'real' | ||||
Paul Ivanov
|
r12219 | def test_preprocessor_via_method(self): | ||
Jonathan Frederic
|
r11480 | """ | ||
Paul Ivanov
|
r12219 | Can a preprocessor be added via the Exporter convenience method? | ||
Jonathan Frederic
|
r11480 | """ | ||
exporter = self._make_exporter() | ||||
Paul Ivanov
|
r12219 | exporter.register_preprocessor(CheesePreprocessor, enabled=True) | ||
Jonathan Frederic
|
r11480 | (output, resources) = exporter.from_filename(self._get_notebook()) | ||
assert resources is not None | ||||
assert resources['cheese'] == 'real' | ||||
def _make_exporter(self, config=None): | ||||
#Create the exporter instance, make sure to set a template name since | ||||
#the base Exporter doesn't have a template associated with it. | ||||
MinRK
|
r11852 | exporter = Exporter(config=config, template_file='python') | ||
Paul Ivanov
|
r12219 | return exporter | ||