Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenbreddels committed Oct 17, 2018
1 parent 465087b commit c0cf315
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# needs these two lines:
sudo: required
dist: trusty
addons:
chrome: stable

language: python
python:
Expand Down
110 changes: 110 additions & 0 deletions nbconvert/preprocessors/tests/files/single-pixel.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "9aa0769fd66e42fcb83d86241d228062",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Image(value=b'\\x89PNG\\r\\n\\x1a\\n\\x00\\x00\\x00\\rIHDR\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x01\\x08\\x06\\x00\\x00\\x00\\x1f\\x15\\"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# this generates a 1 pixel image with the rgba values (100, 200, 250, 255)\n",
"# generated with the code in the next cells (kept for debugging purposes)\n",
"import base64\n",
"import ipywidgets\n",
"image_data = base64.b64decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR4nGNIOfHrPwAG4AMmv//laQAAAABJRU5ErkJggg==')\n",
"ipywidgets.Image(value=image_data)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# try:\n",
"# from io import BytesIO as StringIO\n",
"# except:\n",
"# from cStringIO import StringIO\n",
"# import PIL.Image\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# f = StringIO(image_data)\n",
"# image = PIL.Image.new('RGBA', (1,1))\n",
"# image.putpixel((0, 0), (100, 200, 250, 255))\n",
"# image.save(f, format='png')\n",
"# base64.b64encode(f.getvalue())"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# base64.b64encode(f.getvalue())"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# image = PIL.Image.open(f)\n",
"# pixel = image.getpixel((0, 0))\n",
"\n",
"# #assert pixel == (100, 200, 250)\n",
"\n",
"# pixel"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
32 changes: 32 additions & 0 deletions nbconvert/preprocessors/tests/test_snapshot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import os

import nbformat
from ..snapshot import SnapshotPreProcessor
from ..execute import ExecutePreprocessor
import PIL.Image
try:
from io import BytesIO as StringIO
except:
from cStringIO import StringIO
import base64

current_dir = os.path.dirname(__file__)

def test_red_pixel():
execute_preprocessor = ExecutePreprocessor(enabled=True)

input_file = os.path.join(current_dir, 'files', 'single-pixel.ipynb')
spp = SnapshotPreProcessor(page_opener_class='headless')
nb = nbformat.read(input_file, 4)
resources = {}
nb, resources = execute_preprocessor.preprocess(nb, resources)
assert 'image/png' not in nb.cells[0].outputs[0].data
nb, resources = spp.preprocess(nb, resources)
assert 'image/png' in nb.cells[0].outputs[0].data
# we cannot be sure the browser encodes it the same way
# assert nb.cells[0].outputs[0].data['image/png'] == 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR4nGNIOfHrPwAG4AMmv//laQAAAABJRU5ErkJggg=='
# instead, we read the magic pixel values
f = StringIO(base64.b64decode(nb.cells[0].outputs[0].data['image/png']))
image = PIL.Image.open(f)
pixel = image.getpixel((0, 0))
assert pixel == (100, 200, 250, 255)
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,9 @@ def run(self):
jupyter_client_req = 'jupyter_client>=4.2'

extra_requirements = {
'test': ['pytest', 'pytest-cov', 'ipykernel', 'jupyter_client>=4.2', 'ipywidgets>=7'],
'test': ['pytest', 'pytest-cov', 'ipykernel', 'jupyter_client>=4.2', 'ipywidgets>=7', 'PyChromeDevTools', 'pillow'],
'serve': ['tornado>=4.0'],
'snapshot': ['PyChromeDevTools']
'execute': [jupyter_client_req],
'docs': ['sphinx>=1.5.1',
'sphinx_rtd_theme',
Expand Down

0 comments on commit c0cf315

Please sign in to comment.