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