##// END OF EJS Templates
Refactor assert_notebooks_equal
Julia Evans -
Show More
@@ -1,80 +1,83 b''
1 """
1 """
2 Module with tests for the execute preprocessor.
2 Module with tests for the execute preprocessor.
3 """
3 """
4
4
5 # Copyright (c) IPython Development Team.
5 # Copyright (c) IPython Development Team.
6 # Distributed under the terms of the Modified BSD License.
6 # Distributed under the terms of the Modified BSD License.
7
7
8 #-----------------------------------------------------------------------------
8 #-----------------------------------------------------------------------------
9 # Imports
9 # Imports
10 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
11 import copy
11 import copy
12 import os
12 import os
13 import re
13 import re
14
14
15 from IPython.nbformat import current as nbformat
15 from IPython.nbformat import current as nbformat
16
16
17 from .base import PreprocessorTestsBase
17 from .base import PreprocessorTestsBase
18 from ..execute import ExecutePreprocessor
18 from ..execute import ExecutePreprocessor
19
19
20
20
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22 # Class
22 # Class
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24
24
25 class TestExecute(PreprocessorTestsBase):
25 class TestExecute(PreprocessorTestsBase):
26 """Contains test functions for execute.py"""
26 """Contains test functions for execute.py"""
27
27
28 def prepare_cell(self, cell):
28 @staticmethod
29 def normalize_cell(cell):
30 """
31 Normalizes cells for comparison.
32 """
29 cell = dict(cell)
33 cell = dict(cell)
30 if 'metadata' in cell:
34 if 'metadata' in cell:
31 del cell['metadata']
35 del cell['metadata']
32 if 'text' in cell:
36 if 'text' in cell:
33 cell['text'] = re.sub('0x[0-9a-f]{7,9}', '<HEXADDR>', cell['text'])
37 cell['text'] = re.sub('0x[0-9a-f]{7,9}', '<HEXADDR>', cell['text'])
34 if 'svg' in cell:
38 if 'svg' in cell:
35 del cell['text']
39 del cell['text']
36 return cell
40 return cell
37
41
38
42
39 def assert_notebooks_equal(self, expected, actual):
43 def assert_notebooks_equal(self, expected, actual):
40 expected_cells = expected['worksheets'][0]['cells']
44 expected_cells = expected['worksheets'][0]['cells']
41 actual_cells = actual['worksheets'][0]['cells']
45 actual_cells = actual['worksheets'][0]['cells']
42 assert len(expected_cells) == len(actual_cells)
46 assert len(expected_cells) == len(actual_cells)
43
47
44 # TODO: what does this code do?
48 for expected_cell, actual_cell in zip(expected_cells, actual_cells):
45 for expected_out, actual_out in zip(expected_cells, actual_cells):
49 expected_outputs = expected_cell.get('outputs', [])
46 for k in set(expected_out).union(actual_out):
50 actual_outputs = actual_cell.get('outputs', [])
47 if k == 'outputs':
51 normalized_expected_outputs = map(self.normalize_cell, expected_outputs)
48 self.assertEquals(len(expected_out[k]), len(actual_out[k]))
52 normalized_actual_outputs = map(self.normalize_cell, actual_outputs)
49 for e, a in zip(expected_out[k], actual_out[k]):
53 assert normalized_expected_outputs == normalized_actual_outputs
50 assert self.prepare_cell(e) == self.prepare_cell(a)
51
54
52
55
53 def build_preprocessor(self):
56 def build_preprocessor(self):
54 """Make an instance of a preprocessor"""
57 """Make an instance of a preprocessor"""
55 preprocessor = ExecutePreprocessor()
58 preprocessor = ExecutePreprocessor()
56 preprocessor.enabled = True
59 preprocessor.enabled = True
57 return preprocessor
60 return preprocessor
58
61
59
62
60 def test_constructor(self):
63 def test_constructor(self):
61 """Can a ExecutePreprocessor be constructed?"""
64 """Can a ExecutePreprocessor be constructed?"""
62 self.build_preprocessor()
65 self.build_preprocessor()
63
66
64
67
65 def test_run_notebooks(self):
68 def test_run_notebooks(self):
66 """Runs a series of test notebooks and compares them to their actual output"""
69 """Runs a series of test notebooks and compares them to their actual output"""
67 current_dir = os.path.dirname(__file__)
70 current_dir = os.path.dirname(__file__)
68 input_files = os.listdir(os.path.join(current_dir, 'input'))
71 input_files = os.listdir(os.path.join(current_dir, 'input'))
69 for filename in input_files:
72 for filename in input_files:
70 if not filename.endswith(".ipynb"):
73 if not filename.endswith(".ipynb"):
71 continue
74 continue
72 with open(os.path.join(current_dir, 'input', filename)) as f:
75 with open(os.path.join(current_dir, 'input', filename)) as f:
73 input_nb = nbformat.read(f, 'ipynb')
76 input_nb = nbformat.read(f, 'ipynb')
74 with open(os.path.join(current_dir, 'expected', filename)) as f:
77 with open(os.path.join(current_dir, 'expected', filename)) as f:
75 expected_nb = nbformat.read(f, 'ipynb')
78 expected_nb = nbformat.read(f, 'ipynb')
76 res = self.build_resources()
79 res = self.build_resources()
77 preprocessor = self.build_preprocessor()
80 preprocessor = self.build_preprocessor()
78 output_nb, _ = preprocessor(input_nb, res)
81 output_nb, _ = preprocessor(input_nb, res)
79 self.assert_notebooks_equal(output_nb, expected_nb)
82 self.assert_notebooks_equal(output_nb, expected_nb)
80
83
General Comments 0
You need to be logged in to leave comments. Login now