Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nbconvert: Transformer tests #3914

Merged
merged 21 commits into from
Aug 12, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 1 addition & 5 deletions IPython/nbconvert/exporters/tests/test_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_extract_outputs(self):
exporter = self._make_exporter(config=config)
(output, resources) = exporter.from_filename(self._get_notebook())
assert resources is not None
assert 'outputs' in resources
assert isinstance(resources['outputs'], dict)
assert len(resources['outputs']) > 0


Expand All @@ -65,7 +65,6 @@ def test_transformer_class(self):
exporter = self._make_exporter(config=config)
(output, resources) = exporter.from_filename(self._get_notebook())
assert resources is not None
assert 'cheese' in resources
assert resources['cheese'] == 'real'


Expand All @@ -77,7 +76,6 @@ def test_transformer_instance(self):
exporter = self._make_exporter(config=config)
(output, resources) = exporter.from_filename(self._get_notebook())
assert resources is not None
assert 'cheese' in resources
assert resources['cheese'] == 'real'


Expand All @@ -89,7 +87,6 @@ def test_transformer_dottedobjectname(self):
exporter = self._make_exporter(config=config)
(output, resources) = exporter.from_filename(self._get_notebook())
assert resources is not None
assert 'cheese' in resources
assert resources['cheese'] == 'real'


Expand All @@ -101,7 +98,6 @@ def test_transformer_via_method(self):
exporter.register_transformer(CheeseTransformer, enabled=True)
(output, resources) = exporter.from_filename(self._get_notebook())
assert resources is not None
assert 'cheese' in resources
assert resources['cheese'] == 'real'


Expand Down
5 changes: 3 additions & 2 deletions IPython/nbconvert/transformers/extractoutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,20 @@ def transform_cell(self, cell, resources, cell_index):
output_files_dir = resources.get('output_files_dir', None)

#Make sure outputs key exists
if not 'outputs' in resources:
if not isinstance(resources['outputs'], dict):
resources['outputs'] = {}

#Loop through all of the outputs in the cell
for index, out in enumerate(cell.get('outputs', [])):

#Get the output in data formats that the template is interested in.
for out_type in self.display_data_priority:
if out.hasattr(out_type):
if out.hasattr(out_type):
data = out[out_type]

#Binary files are base64-encoded, SVG is already XML
if out_type in ('png', 'jpg', 'jpeg', 'pdf'):

# data is b64-encoded as text (str, unicode)
# decodestring only accepts bytes
data = py3compat.cast_bytes(data)
Expand Down
2 changes: 1 addition & 1 deletion IPython/nbconvert/transformers/revealhelp.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def call(self, nb, resources):
worksheet.cells[index - 1].metadata.slide_helper = 'subslide_end'


if 'reveal' not in resources:
if not isinstance(resources['reveal'], dict):
resources['reveal'] = {}
resources['reveal']['url_prefix'] = self.url_prefix

Expand Down
2 changes: 1 addition & 1 deletion IPython/nbconvert/transformers/sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def call(self, nb, resources):
# TODO: Add versatile method of additional notebook metadata. Include
# handling of multiple files. For now use a temporay namespace,
# '_draft' to signify that this needs to change.
if not "sphinx" in resources:
if not isinstance(resources["sphinx"], dict):
resources["sphinx"] = {}

if self.interactive:
Expand Down
Empty file.
53 changes: 53 additions & 0 deletions IPython/nbconvert/transformers/tests/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""
Module with utility functions for transformer tests
"""

#-----------------------------------------------------------------------------
# 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.nbformat import current as nbformat

from ...tests.base import TestsBase
from ...exporters.exporter import ResourcesDict

#-----------------------------------------------------------------------------
# Class
#-----------------------------------------------------------------------------

class TransformerTestsBase(TestsBase):
"""Contains test functions transformer tests"""


def build_notebook(self):
"""Build a notebook in memory for use with transformer tests"""

outputs = [nbformat.new_output(output_type="stream", stream="stdout", output_text="a"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also use IPython.nbformat.v3.tests.nbexamples for an example notebook

nbformat.new_output(output_type="text", output_text="b"),
nbformat.new_output(output_type="stream", stream="stdout", output_text="c"),
nbformat.new_output(output_type="stream", stream="stdout", output_text="d"),
nbformat.new_output(output_type="stream", stream="stderr", output_text="e"),
nbformat.new_output(output_type="stream", stream="stderr", output_text="f"),
nbformat.new_output(output_type="png", output_png=b'Zw==')] #g

cells=[nbformat.new_code_cell(input="$ e $", prompt_number=1,outputs=outputs),
nbformat.new_text_cell('markdown', source="$ e $")]
worksheets = [nbformat.new_worksheet(name="worksheet1", cells=cells)]

return nbformat.new_notebook(name="notebook1", worksheets=worksheets)


def build_resources(self):
"""Build an empty resources dictionary."""

res = ResourcesDict()
res['metadata'] = ResourcesDict()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does metadata need to be a ResourceDict ? Or should we also test the fact that when it not a resource dict thing works ? (you probably know nbconvert more than me now)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, just so we can do conversions if something doesn't exist (like notebook name). Otherwise Jinja will crash when parsing if exporting without a notebook name.

return res
38 changes: 38 additions & 0 deletions IPython/nbconvert/transformers/tests/test_coalescestreams.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
Module with tests for the coalescestreams transformer
"""

#-----------------------------------------------------------------------------
# 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 .base import TransformerTestsBase
from ..coalescestreams import coalesce_streams


#-----------------------------------------------------------------------------
# Class
#-----------------------------------------------------------------------------

class TestCoalesceStreams(TransformerTestsBase):
"""Contains test functions for coalescestreams.py"""

def test_coalesce_streams(self):
"""coalesce_streams transformer output test"""
nb = self.build_notebook()
res = self.build_resources()
nb, res = coalesce_streams(nb, res)
outputs = nb.worksheets[0].cells[0].outputs
self.assertEqual(outputs[0].text, "a")
self.assertEqual(outputs[1].output_type, "text")
self.assertEqual(outputs[2].text, "cd")
self.assertEqual(outputs[3].text, "ef")

47 changes: 47 additions & 0 deletions IPython/nbconvert/transformers/tests/test_csshtmlheader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
Module with tests for the csshtmlheader transformer
"""

#-----------------------------------------------------------------------------
# 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 .base import TransformerTestsBase
from ..csshtmlheader import CSSHTMLHeaderTransformer


#-----------------------------------------------------------------------------
# Class
#-----------------------------------------------------------------------------

class TestCSSHTMLHeader(TransformerTestsBase):
"""Contains test functions for csshtmlheader.py"""


def build_transformer(self):
"""Make an instance of a transformer"""
transformer = CSSHTMLHeaderTransformer()
transformer.enabled = True
return transformer


def test_constructor(self):
"""Can a CSSHTMLHeaderTransformer be constructed?"""
self.build_transformer()


def test_output(self):
"""Test the output of the CSSHTMLHeaderTransformer"""
nb = self.build_notebook()
res = self.build_resources()
transformer = self.build_transformer()
nb, res = transformer(nb, res)
assert 'css' in res['inlining']
62 changes: 62 additions & 0 deletions IPython/nbconvert/transformers/tests/test_extractoutput.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""
Module with tests for the extractoutput transformer
"""

#-----------------------------------------------------------------------------
# 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 .base import TransformerTestsBase
from ..extractoutput import ExtractOutputTransformer


#-----------------------------------------------------------------------------
# Class
#-----------------------------------------------------------------------------

class TestExtractOutput(TransformerTestsBase):
"""Contains test functions for extractoutput.py"""


def build_transformer(self):
"""Make an instance of a transformer"""
transformer = ExtractOutputTransformer()
transformer.enabled = True
return transformer


def test_constructor(self):
"""Can a ExtractOutputTransformer be constructed?"""
self.build_transformer()


def test_output(self):
"""Test the output of the ExtractOutputTransformer"""
nb = self.build_notebook()
res = self.build_resources()
transformer = self.build_transformer()
nb, res = transformer(nb, res)

# Check if text was extracted.
assert 'text_filename' in nb.worksheets[0].cells[0].outputs[1]
text_filename = nb.worksheets[0].cells[0].outputs[1]['text_filename']

# Check if png was extracted.
assert 'png_filename' in nb.worksheets[0].cells[0].outputs[6]
png_filename = nb.worksheets[0].cells[0].outputs[6]['png_filename']

# Verify text output
assert text_filename in res['outputs']
self.assertEqual(res['outputs'][text_filename], b'b')

# Verify png output
assert png_filename in res['outputs']
self.assertEqual(res['outputs'][png_filename], b'g')
51 changes: 51 additions & 0 deletions IPython/nbconvert/transformers/tests/test_latex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""
Module with tests for the latex transformer
"""

#-----------------------------------------------------------------------------
# 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 .base import TransformerTestsBase
from ..latex import LatexTransformer


#-----------------------------------------------------------------------------
# Class
#-----------------------------------------------------------------------------

class TestLatex(TransformerTestsBase):
"""Contains test functions for latex.py"""


def build_transformer(self):
"""Make an instance of a transformer"""
transformer = LatexTransformer()
transformer.enabled = True
return transformer

def test_constructor(self):
"""Can a LatexTransformer be constructed?"""
self.build_transformer()


def test_output(self):
"""Test the output of the LatexTransformer"""
nb = self.build_notebook()
res = self.build_resources()
transformer = self.build_transformer()
nb, res = transformer(nb, res)

# Make sure the code cell wasn't modified.
self.assertEqual(nb.worksheets[0].cells[0].input, '$ e $')

# Verify that the markdown cell was processed.
self.assertEqual(nb.worksheets[0].cells[1].source, '$e$')