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 20 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
3 changes: 2 additions & 1 deletion IPython/nbconvert/transformers/extractoutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@ def transform_cell(self, cell, resources, cell_index):

#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
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")

48 changes: 48 additions & 0 deletions IPython/nbconvert/transformers/tests/test_csshtmlheader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""
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 'inlining' in res
Copy link
Member

Choose a reason for hiding this comment

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

slightly dangerous as if something try to read something from res, then it will exist and not assert if res is a Resource dict.

In [29]: dd = collections.defaultdict(lambda :'')

In [30]: dd['stuff']
Out[30]: ''

In [32]: assert 'stuff' in dd
# does not throw.

Or am I miss understanding ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch :) Will change

assert 'css' in res['inlining']
65 changes: 65 additions & 0 deletions IPython/nbconvert/transformers/tests/test_extractoutput.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""
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']

# Make sure an entry to the resources was added.
assert 'outputs' in res

# 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$')
94 changes: 94 additions & 0 deletions IPython/nbconvert/transformers/tests/test_revealhelp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
"""
Module with tests for the revealhelp 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 IPython.nbformat import current as nbformat

from .base import TransformerTestsBase
from ..revealhelp import RevealHelpTransformer


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

class Testrevealhelp(TransformerTestsBase):
"""Contains test functions for revealhelp.py"""

def build_notebook(self):
"""Build a reveal slides notebook in memory for use with tests.
Overrides base in TransformerTestsBase"""

outputs = [nbformat.new_output(output_type="stream", stream="stdout", output_text="a")]

slide_metadata = {'slideshow' : {'slide_type': 'slide'}}
subslide_metadata = {'slideshow' : {'slide_type': 'subslide'}}

cells=[nbformat.new_code_cell(input="", prompt_number=1, outputs=outputs),
nbformat.new_text_cell('markdown', source="", metadata=slide_metadata),
nbformat.new_code_cell(input="", prompt_number=2, outputs=outputs),
nbformat.new_text_cell('markdown', source="", metadata=slide_metadata),
nbformat.new_text_cell('markdown', source="", metadata=subslide_metadata)]
worksheets = [nbformat.new_worksheet(name="worksheet1", cells=cells)]

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


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


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


def test_reveal_attribute(self):
"""Make sure the reveal url_prefix resources is set"""
nb = self.build_notebook()
res = self.build_resources()
transformer = self.build_transformer()
nb, res = transformer(nb, res)
assert 'reveal' in res
Copy link
Member

Choose a reason for hiding this comment

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

Change to self.assertEqual

assert 'url_prefix' in res['reveal']


def test_reveal_output(self):
"""Make sure that the reveal transformer """
nb = self.build_notebook()
res = self.build_resources()
transformer = self.build_transformer()
nb, res = transformer(nb, res)
cells = nb.worksheets[0].cells

# Make sure correct metadata tags are available on every cell.
for cell in cells:
assert 'slide_type' in cell.metadata
Copy link
Member

Choose a reason for hiding this comment

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

Let's change all raw assert in to self.assertTrue.

Copy link
Member

Choose a reason for hiding this comment

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

We're just about to drop support for Python 2.6, so we'll be able to use self.assertIn shortly anyway. Also, I much prefer regular assert statements to assertTrue.

Copy link
Member

Choose a reason for hiding this comment

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

I think @takluyver don't like assertTrue. Why not just assert ?

assert 'align_type' in cell.metadata

# Make sure slide end is only applied to the cells preceeding slide
# cells.
assert 'slide_helper' not in cells[1].metadata

# Verify 'slide-end'
assert 'slide_helper' in cells[0].metadata
self.assertEqual(cells[0].metadata['slide_helper'], 'slide_end')
assert 'slide_helper' in cells[2].metadata
self.assertEqual(cells[2].metadata['slide_helper'], 'slide_end')
assert 'slide_helper' in cells[3].metadata
self.assertEqual(cells[3].metadata['slide_helper'], 'subslide_end')