##// END OF EJS Templates
add pdf to extract output tests
MinRK -
Show More
@@ -1,53 +1,56 b''
1 """
1 """
2 Module with utility functions for preprocessor tests
2 Module with utility functions for preprocessor tests
3 """
3 """
4
4
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6 # Copyright (c) 2013, the IPython Development Team.
6 # Copyright (c) 2013, the IPython Development Team.
7 #
7 #
8 # Distributed under the terms of the Modified BSD License.
8 # Distributed under the terms of the Modified BSD License.
9 #
9 #
10 # The full license is in the file COPYING.txt, distributed with this software.
10 # The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 from IPython.nbformat import current as nbformat
17 from IPython.nbformat import current as nbformat
18
18
19 from ...tests.base import TestsBase
19 from ...tests.base import TestsBase
20 from ...exporters.exporter import ResourcesDict
20 from ...exporters.exporter import ResourcesDict
21
21
22 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
23 # Class
23 # Class
24 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
25
25
26 class PreprocessorTestsBase(TestsBase):
26 class PreprocessorTestsBase(TestsBase):
27 """Contains test functions preprocessor tests"""
27 """Contains test functions preprocessor tests"""
28
28
29
29
30 def build_notebook(self):
30 def build_notebook(self):
31 """Build a notebook in memory for use with preprocessor tests"""
31 """Build a notebook in memory for use with preprocessor tests"""
32
32
33 outputs = [nbformat.new_output(output_type="stream", stream="stdout", output_text="a"),
33 outputs = [nbformat.new_output(output_type="stream", stream="stdout", output_text="a"),
34 nbformat.new_output(output_type="text", output_text="b"),
34 nbformat.new_output(output_type="text", output_text="b"),
35 nbformat.new_output(output_type="stream", stream="stdout", output_text="c"),
35 nbformat.new_output(output_type="stream", stream="stdout", output_text="c"),
36 nbformat.new_output(output_type="stream", stream="stdout", output_text="d"),
36 nbformat.new_output(output_type="stream", stream="stdout", output_text="d"),
37 nbformat.new_output(output_type="stream", stream="stderr", output_text="e"),
37 nbformat.new_output(output_type="stream", stream="stderr", output_text="e"),
38 nbformat.new_output(output_type="stream", stream="stderr", output_text="f"),
38 nbformat.new_output(output_type="stream", stream="stderr", output_text="f"),
39 nbformat.new_output(output_type="png", output_png='Zw==')] #g
39 nbformat.new_output(output_type="png", output_png='Zw==')] # g
40 out = nbformat.new_output(output_type="application/pdf")
41 out['application/pdf'] = 'aA==' # h
42 outputs.append(out)
40
43
41 cells=[nbformat.new_code_cell(input="$ e $", prompt_number=1,outputs=outputs),
44 cells=[nbformat.new_code_cell(input="$ e $", prompt_number=1,outputs=outputs),
42 nbformat.new_text_cell('markdown', source="$ e $")]
45 nbformat.new_text_cell('markdown', source="$ e $")]
43 worksheets = [nbformat.new_worksheet(name="worksheet1", cells=cells)]
46 worksheets = [nbformat.new_worksheet(cells=cells)]
44
47
45 return nbformat.new_notebook(name="notebook1", worksheets=worksheets)
48 return nbformat.new_notebook(name="notebook1", worksheets=worksheets)
46
49
47
50
48 def build_resources(self):
51 def build_resources(self):
49 """Build an empty resources dictionary."""
52 """Build an empty resources dictionary."""
50
53
51 res = ResourcesDict()
54 res = ResourcesDict()
52 res['metadata'] = ResourcesDict()
55 res['metadata'] = ResourcesDict()
53 return res
56 return res
@@ -1,65 +1,74 b''
1 """
1 """
2 Module with tests for the extractoutput preprocessor
2 Module with tests for the extractoutput preprocessor
3 """
3 """
4
4
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6 # Copyright (c) 2013, the IPython Development Team.
6 # Copyright (c) 2013, the IPython Development Team.
7 #
7 #
8 # Distributed under the terms of the Modified BSD License.
8 # Distributed under the terms of the Modified BSD License.
9 #
9 #
10 # The full license is in the file COPYING.txt, distributed with this software.
10 # The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 from .base import PreprocessorTestsBase
17 from .base import PreprocessorTestsBase
18 from ..extractoutput import ExtractOutputPreprocessor
18 from ..extractoutput import ExtractOutputPreprocessor
19
19
20
20
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22 # Class
22 # Class
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24
24
25 class TestExtractOutput(PreprocessorTestsBase):
25 class TestExtractOutput(PreprocessorTestsBase):
26 """Contains test functions for extractoutput.py"""
26 """Contains test functions for extractoutput.py"""
27
27
28
28
29 def build_preprocessor(self):
29 def build_preprocessor(self):
30 """Make an instance of a preprocessor"""
30 """Make an instance of a preprocessor"""
31 preprocessor = ExtractOutputPreprocessor()
31 preprocessor = ExtractOutputPreprocessor()
32 preprocessor.extract_output_types = {'text', 'png'}
32 preprocessor.extract_output_types = {'text', 'png', 'application/pdf'}
33 preprocessor.enabled = True
33 preprocessor.enabled = True
34 return preprocessor
34 return preprocessor
35
35
36
36
37 def test_constructor(self):
37 def test_constructor(self):
38 """Can a ExtractOutputPreprocessor be constructed?"""
38 """Can a ExtractOutputPreprocessor be constructed?"""
39 self.build_preprocessor()
39 self.build_preprocessor()
40
40
41
41
42 def test_output(self):
42 def test_output(self):
43 """Test the output of the ExtractOutputPreprocessor"""
43 """Test the output of the ExtractOutputPreprocessor"""
44 nb = self.build_notebook()
44 nb = self.build_notebook()
45 res = self.build_resources()
45 res = self.build_resources()
46 preprocessor = self.build_preprocessor()
46 preprocessor = self.build_preprocessor()
47 nb, res = preprocessor(nb, res)
47 nb, res = preprocessor(nb, res)
48
48
49 # Check if text was extracted.
49 # Check if text was extracted.
50 output = nb.worksheets[0].cells[0].outputs[1]
50 output = nb.worksheets[0].cells[0].outputs[1]
51 assert 'text_filename' in output
51 assert 'text_filename' in output
52 text_filename = output['text_filename']
52 text_filename = output['text_filename']
53
53
54 # Check if png was extracted.
54 # Check if png was extracted.
55 output = nb.worksheets[0].cells[0].outputs[6]
55 output = nb.worksheets[0].cells[0].outputs[6]
56 assert 'png_filename' in output
56 assert 'png_filename' in output
57 png_filename = output['png_filename']
57 png_filename = output['png_filename']
58
59 # Check that pdf was extracted
60 output = nb.worksheets[0].cells[0].outputs[7]
61 assert 'application/pdf_filename' in output
62 pdf_filename = output['application/pdf_filename']
58
63
59 # Verify text output
64 # Verify text output
60 assert text_filename in res['outputs']
65 assert text_filename in res['outputs']
61 self.assertEqual(res['outputs'][text_filename], b'b')
66 self.assertEqual(res['outputs'][text_filename], b'b')
62
67
63 # Verify png output
68 # Verify png output
64 assert png_filename in res['outputs']
69 assert png_filename in res['outputs']
65 self.assertEqual(res['outputs'][png_filename], b'g')
70 self.assertEqual(res['outputs'][png_filename], b'g')
71
72 # Verify pdf output
73 assert pdf_filename in res['outputs']
74 self.assertEqual(res['outputs'][pdf_filename], b'h')
General Comments 0
You need to be logged in to leave comments. Login now