1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
# -*- coding: utf-8 -*-
# Copyright 2021 - 2023 Avram Lubkin, All Rights Reserved
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
"""
Test module for enlighten._notebook_manager
"""
import unittest
try:
from nbconvert.preprocessors import ExecutePreprocessor
import nbformat
RUN_NOTEBOOK = True
except ImportError:
RUN_NOTEBOOK = False
from tests import TestCase
def run_notebook(path):
"""
Run a notebook at the given path
The notebook's path is set to the current working directory
"""
with open(path, encoding='utf-8') as notebook_file:
notebook = nbformat.read(notebook_file, as_version=nbformat.NO_CONVERT)
process = ExecutePreprocessor(timeout=60, allow_errors=True)
process.preprocess(notebook, {})
return notebook
def has_html_output(cell):
"""
Check if cell has HTML output
"""
for output in cell.get('outputs', []):
if output.output_type == 'display_data':
return 'text/html' in output['data']
return False
@unittest.skipUnless(RUN_NOTEBOOK, 'Notebook testing packages not installed')
class TestNotebookManager(TestCase):
"""
Tests for NotebookManager
"""
maxDiff = None
def test_notebook(self):
"""
All the tests run in the notebook. This just runs it and checks for errors.
"""
notebook = run_notebook('tests/test_notebook_manager.ipynb')
# Make sure there are no errors
for cell in notebook.cells:
for output in cell.get('outputs', []):
if output.output_type == 'stream' and output.name == 'stderr':
errors = ''.join(output.text)
print(errors)
if output.output_type == 'error':
raise AssertionError(
'%s: %s\n%s' % (output.ename, output.evalue, '\n'.join(output.traceback))
)
self.assertNotEqual(output.output_type, 'error')
# Setup: should have no output
self.assertFalse(notebook.cells[0].outputs)
# test_get_manager: should have no output
self.assertFalse(notebook.cells[1].outputs)
# test_standard: should have output
self.assertTrue(has_html_output(notebook.cells[2]), 'display_data not found in outputs')
# # test_disabled: should have no output
self.assertFalse(notebook.cells[3].outputs)
# test_bare_no_flush: should have no output
self.assertFalse(notebook.cells[4].outputs)
# test_advanced: should have output
self.assertTrue(has_html_output(notebook.cells[5]), 'display_data not found in outputs')
# test_styles: should have output
self.assertTrue(has_html_output(notebook.cells[6]), 'display_data not found in outputs')
# test_stop_no_counters: should have output
self.assertTrue(has_html_output(notebook.cells[7]), 'display_data not found in outputs')
# Cleanup: should have no output
self.assertFalse(notebook.cells[8].outputs)
|