Uploaded Test files
This commit is contained in:
parent
f584ad9d97
commit
2e81cb7d99
16627 changed files with 2065359 additions and 102444 deletions
0
venv/Lib/site-packages/nbformat/v3/tests/__init__.py
Normal file
0
venv/Lib/site-packages/nbformat/v3/tests/__init__.py
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
63
venv/Lib/site-packages/nbformat/v3/tests/formattest.py
Normal file
63
venv/Lib/site-packages/nbformat/v3/tests/formattest.py
Normal file
|
@ -0,0 +1,63 @@
|
|||
# -*- coding: utf8 -*-
|
||||
import io
|
||||
import os
|
||||
import shutil
|
||||
import tempfile
|
||||
|
||||
pjoin = os.path.join
|
||||
|
||||
from ..nbbase import (
|
||||
NotebookNode,
|
||||
new_code_cell, new_text_cell, new_worksheet, new_notebook
|
||||
)
|
||||
|
||||
from ..nbpy import reads, writes, read, write
|
||||
from .nbexamples import nb0, nb0_py
|
||||
|
||||
|
||||
def open_utf8(fname, mode):
|
||||
return io.open(fname, mode=mode, encoding='utf-8')
|
||||
|
||||
class NBFormatTest:
|
||||
"""Mixin for writing notebook format tests"""
|
||||
|
||||
# override with appropriate values in subclasses
|
||||
nb0_ref = None
|
||||
ext = None
|
||||
mod = None
|
||||
|
||||
def setUp(self):
|
||||
self.wd = tempfile.mkdtemp()
|
||||
|
||||
def tearDown(self):
|
||||
shutil.rmtree(self.wd)
|
||||
|
||||
def assertNBEquals(self, nba, nbb):
|
||||
self.assertEqual(nba, nbb)
|
||||
|
||||
def test_writes(self):
|
||||
s = self.mod.writes(nb0)
|
||||
if self.nb0_ref:
|
||||
self.assertEqual(s, self.nb0_ref)
|
||||
|
||||
def test_reads(self):
|
||||
s = self.mod.writes(nb0)
|
||||
nb = self.mod.reads(s)
|
||||
|
||||
def test_roundtrip(self):
|
||||
s = self.mod.writes(nb0)
|
||||
self.assertNBEquals(self.mod.reads(s),nb0)
|
||||
|
||||
def test_write_file(self):
|
||||
with open_utf8(pjoin(self.wd, "nb0.%s" % self.ext), 'w') as f:
|
||||
self.mod.write(nb0, f)
|
||||
|
||||
def test_read_file(self):
|
||||
with open_utf8(pjoin(self.wd, "nb0.%s" % self.ext), 'w') as f:
|
||||
self.mod.write(nb0, f)
|
||||
|
||||
with open_utf8(pjoin(self.wd, "nb0.%s" % self.ext), 'r') as f:
|
||||
nb = self.mod.read(f)
|
||||
|
||||
|
||||
|
152
venv/Lib/site-packages/nbformat/v3/tests/nbexamples.py
Normal file
152
venv/Lib/site-packages/nbformat/v3/tests/nbexamples.py
Normal file
|
@ -0,0 +1,152 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os
|
||||
|
||||
from ..._compat import encodebytes
|
||||
from ..nbbase import (
|
||||
NotebookNode,
|
||||
new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output,
|
||||
new_metadata, new_author, new_heading_cell, nbformat, nbformat_minor
|
||||
)
|
||||
|
||||
# some random base64-encoded *text*
|
||||
png = encodebytes(os.urandom(5)).decode('ascii')
|
||||
jpeg = encodebytes(os.urandom(6)).decode('ascii')
|
||||
|
||||
ws = new_worksheet()
|
||||
|
||||
ws.cells.append(new_text_cell(
|
||||
u'html',
|
||||
source='Some NumPy Examples',
|
||||
))
|
||||
|
||||
|
||||
ws.cells.append(new_code_cell(
|
||||
input='import numpy',
|
||||
prompt_number=1,
|
||||
collapsed=False
|
||||
))
|
||||
|
||||
ws.cells.append(new_text_cell(
|
||||
u'markdown',
|
||||
source='A random array',
|
||||
))
|
||||
|
||||
ws.cells.append(new_text_cell(
|
||||
u'raw',
|
||||
source='A random array',
|
||||
))
|
||||
|
||||
ws.cells.append(new_heading_cell(
|
||||
u'My Heading',
|
||||
level=2
|
||||
))
|
||||
|
||||
ws.cells.append(new_code_cell(
|
||||
input='a = numpy.random.rand(100)',
|
||||
prompt_number=2,
|
||||
collapsed=True
|
||||
))
|
||||
ws.cells.append(new_code_cell(
|
||||
input='a = 10\nb = 5\n',
|
||||
prompt_number=3,
|
||||
))
|
||||
ws.cells.append(new_code_cell(
|
||||
input='a = 10\nb = 5',
|
||||
prompt_number=4,
|
||||
))
|
||||
|
||||
ws.cells.append(new_code_cell(
|
||||
input=u'print "ünîcødé"',
|
||||
prompt_number=3,
|
||||
collapsed=False,
|
||||
outputs=[new_output(
|
||||
output_type=u'pyout',
|
||||
output_text=u'<array a>',
|
||||
output_html=u'The HTML rep',
|
||||
output_latex=u'$a$',
|
||||
output_png=png,
|
||||
output_jpeg=jpeg,
|
||||
output_svg=u'<svg>',
|
||||
output_json=u'{"json": "data"}',
|
||||
output_javascript=u'var i=0;',
|
||||
prompt_number=3
|
||||
),new_output(
|
||||
output_type=u'display_data',
|
||||
output_text=u'<array a>',
|
||||
output_html=u'The HTML rep',
|
||||
output_latex=u'$a$',
|
||||
output_png=png,
|
||||
output_jpeg=jpeg,
|
||||
output_svg=u'<svg>',
|
||||
output_json=u'{"json": "data"}',
|
||||
output_javascript=u'var i=0;'
|
||||
),new_output(
|
||||
output_type=u'pyerr',
|
||||
ename=u'NameError',
|
||||
evalue=u'NameError was here',
|
||||
traceback=[u'frame 0', u'frame 1', u'frame 2']
|
||||
),new_output(
|
||||
output_type=u'stream',
|
||||
output_text='foo\rbar\r\n'
|
||||
),new_output(
|
||||
output_type=u'stream',
|
||||
stream='stderr',
|
||||
output_text='\rfoo\rbar\n'
|
||||
)]
|
||||
))
|
||||
|
||||
authors = [new_author(name='Bart Simpson',email='bsimpson@fox.com',
|
||||
affiliation=u'Fox',url=u'http://www.fox.com')]
|
||||
md = new_metadata(name=u'My Notebook',license=u'BSD',created=u'8601_goes_here',
|
||||
modified=u'8601_goes_here',gistid=u'21341231',authors=authors)
|
||||
|
||||
nb0 = new_notebook(
|
||||
worksheets=[ws, new_worksheet()],
|
||||
metadata=md
|
||||
)
|
||||
|
||||
nb0_py = u"""# -*- coding: utf-8 -*-
|
||||
# <nbformat>%i.%i</nbformat>
|
||||
|
||||
# <htmlcell>
|
||||
|
||||
# Some NumPy Examples
|
||||
|
||||
# <codecell>
|
||||
|
||||
import numpy
|
||||
|
||||
# <markdowncell>
|
||||
|
||||
# A random array
|
||||
|
||||
# <rawcell>
|
||||
|
||||
# A random array
|
||||
|
||||
# <headingcell level=2>
|
||||
|
||||
# My Heading
|
||||
|
||||
# <codecell>
|
||||
|
||||
a = numpy.random.rand(100)
|
||||
|
||||
# <codecell>
|
||||
|
||||
a = 10
|
||||
b = 5
|
||||
|
||||
# <codecell>
|
||||
|
||||
a = 10
|
||||
b = 5
|
||||
|
||||
# <codecell>
|
||||
|
||||
print "ünîcødé"
|
||||
|
||||
""" % (nbformat, nbformat_minor)
|
||||
|
||||
|
102
venv/Lib/site-packages/nbformat/v3/tests/test_json.py
Normal file
102
venv/Lib/site-packages/nbformat/v3/tests/test_json.py
Normal file
|
@ -0,0 +1,102 @@
|
|||
import copy
|
||||
import json
|
||||
from unittest import TestCase
|
||||
|
||||
from ..._compat import decodebytes
|
||||
from ..nbjson import reads, writes
|
||||
from ..nbbase import from_dict
|
||||
from .. import nbjson
|
||||
from .nbexamples import nb0
|
||||
|
||||
from . import formattest
|
||||
|
||||
from .nbexamples import nb0
|
||||
|
||||
|
||||
class TestJSON(formattest.NBFormatTest, TestCase):
|
||||
|
||||
nb0_ref = None
|
||||
ext = 'ipynb'
|
||||
mod = nbjson
|
||||
|
||||
def test_roundtrip_nosplit(self):
|
||||
"""Ensure that multiline blobs are still readable"""
|
||||
# ensures that notebooks written prior to splitlines change
|
||||
# are still readable.
|
||||
s = writes(nb0, split_lines=False)
|
||||
self.assertEqual(nbjson.reads(s),nb0)
|
||||
|
||||
def test_roundtrip_split(self):
|
||||
"""Ensure that splitting multiline blocks is safe"""
|
||||
# This won't differ from test_roundtrip unless the default changes
|
||||
s = writes(nb0, split_lines=True)
|
||||
self.assertEqual(nbjson.reads(s),nb0)
|
||||
|
||||
def test_strip_transient(self):
|
||||
"""transient values aren't written to files"""
|
||||
nb = copy.deepcopy(nb0)
|
||||
nb.orig_nbformat = 2
|
||||
nb.orig_nbformat_minor = 3
|
||||
nb.worksheets[0].cells[0].metadata.trusted = False
|
||||
nbs = nbjson.writes(nb)
|
||||
|
||||
nb2 = from_dict(json.loads(nbs))
|
||||
self.assertNotIn('orig_nbformat', nb2)
|
||||
self.assertNotIn('orig_nbformat_minor', nb2)
|
||||
for cell in nb2.worksheets[0].cells:
|
||||
self.assertNotIn('trusted', cell.metadata)
|
||||
|
||||
def test_to_json(self):
|
||||
"""to_notebook_json doesn't strip transient"""
|
||||
nb = copy.deepcopy(nb0)
|
||||
nb.orig_nbformat = 2
|
||||
nb.orig_nbformat_minor = 3
|
||||
nb.worksheets[0].cells[0].metadata.trusted = False
|
||||
nbs = json.dumps(nb)
|
||||
nb2 = nbjson.to_notebook(json.loads(nbs))
|
||||
|
||||
nb2 = from_dict(json.loads(nbs))
|
||||
self.assertIn('orig_nbformat', nb2)
|
||||
self.assertIn('orig_nbformat_minor', nb2)
|
||||
cell = nb2.worksheets[0].cells[0]
|
||||
self.assertIn('trusted', cell.metadata)
|
||||
|
||||
def test_read_png(self):
|
||||
"""PNG output data is b64 unicode"""
|
||||
s = writes(nb0)
|
||||
nb1 = nbjson.reads(s)
|
||||
found_png = False
|
||||
for cell in nb1.worksheets[0].cells:
|
||||
if not 'outputs' in cell:
|
||||
continue
|
||||
for output in cell.outputs:
|
||||
if 'png' in output:
|
||||
found_png = True
|
||||
pngdata = output['png']
|
||||
self.assertEqual(type(pngdata), str)
|
||||
# test that it is valid b64 data
|
||||
b64bytes = pngdata.encode('ascii')
|
||||
raw_bytes = decodebytes(b64bytes)
|
||||
assert found_png, "never found png output"
|
||||
|
||||
def test_read_jpeg(self):
|
||||
"""JPEG output data is b64 unicode"""
|
||||
s = writes(nb0)
|
||||
nb1 = nbjson.reads(s)
|
||||
found_jpeg = False
|
||||
for cell in nb1.worksheets[0].cells:
|
||||
if not 'outputs' in cell:
|
||||
continue
|
||||
for output in cell.outputs:
|
||||
if 'jpeg' in output:
|
||||
found_jpeg = True
|
||||
jpegdata = output['jpeg']
|
||||
self.assertEqual(type(jpegdata), str)
|
||||
# test that it is valid b64 data
|
||||
b64bytes = jpegdata.encode('ascii')
|
||||
raw_bytes = decodebytes(b64bytes)
|
||||
assert found_jpeg, "never found jpeg output"
|
||||
|
||||
|
||||
|
||||
|
32
venv/Lib/site-packages/nbformat/v3/tests/test_misc.py
Normal file
32
venv/Lib/site-packages/nbformat/v3/tests/test_misc.py
Normal file
|
@ -0,0 +1,32 @@
|
|||
import os
|
||||
from unittest import TestCase
|
||||
|
||||
from .. import parse_filename
|
||||
|
||||
|
||||
class MiscTests(TestCase):
|
||||
|
||||
def check_filename(self, path, exp_fname, exp_bname, exp_format):
|
||||
fname, bname, format = parse_filename(path)
|
||||
self.assertEqual(fname, exp_fname)
|
||||
self.assertEqual(bname, exp_bname)
|
||||
self.assertEqual(format, exp_format)
|
||||
|
||||
def test_parse_filename(self):
|
||||
|
||||
# check format detection
|
||||
self.check_filename("test.ipynb", "test.ipynb", "test", "json")
|
||||
self.check_filename("test.json", "test.json", "test", "json")
|
||||
self.check_filename("test.py", "test.py", "test", "py")
|
||||
|
||||
# check parsing an unknown format
|
||||
self.check_filename("test.nb", "test.nb.ipynb", "test.nb", "json")
|
||||
|
||||
# check parsing a full file path
|
||||
abs_path = os.path.abspath("test.ipynb")
|
||||
basename, ext = os.path.splitext(abs_path)
|
||||
self.check_filename(abs_path, abs_path, basename, "json")
|
||||
|
||||
# check parsing a file name containing dots
|
||||
self.check_filename("test.nb.ipynb", "test.nb.ipynb", "test.nb",
|
||||
"json")
|
148
venv/Lib/site-packages/nbformat/v3/tests/test_nbbase.py
Normal file
148
venv/Lib/site-packages/nbformat/v3/tests/test_nbbase.py
Normal file
|
@ -0,0 +1,148 @@
|
|||
from unittest import TestCase
|
||||
|
||||
from ..nbbase import (
|
||||
NotebookNode,
|
||||
new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output,
|
||||
new_author, new_metadata, new_heading_cell, nbformat
|
||||
)
|
||||
|
||||
class TestCell(TestCase):
|
||||
|
||||
def test_empty_code_cell(self):
|
||||
cc = new_code_cell()
|
||||
self.assertEqual(cc.cell_type,u'code')
|
||||
self.assertEqual(u'input' not in cc, True)
|
||||
self.assertEqual(u'prompt_number' not in cc, True)
|
||||
self.assertEqual(cc.outputs, [])
|
||||
self.assertEqual(cc.collapsed, False)
|
||||
|
||||
def test_code_cell(self):
|
||||
cc = new_code_cell(input='a=10', prompt_number=0, collapsed=True)
|
||||
cc.outputs = [new_output(output_type=u'pyout',
|
||||
output_svg=u'foo',output_text=u'10',prompt_number=0)]
|
||||
self.assertEqual(cc.input, u'a=10')
|
||||
self.assertEqual(cc.prompt_number, 0)
|
||||
self.assertEqual(cc.language, u'python')
|
||||
self.assertEqual(cc.outputs[0].svg, u'foo')
|
||||
self.assertEqual(cc.outputs[0].text, u'10')
|
||||
self.assertEqual(cc.outputs[0].prompt_number, 0)
|
||||
self.assertEqual(cc.collapsed, True)
|
||||
|
||||
def test_pyerr(self):
|
||||
o = new_output(output_type=u'pyerr', ename=u'NameError',
|
||||
evalue=u'Name not found', traceback=[u'frame 0', u'frame 1', u'frame 2']
|
||||
)
|
||||
self.assertEqual(o.output_type, u'pyerr')
|
||||
self.assertEqual(o.ename, u'NameError')
|
||||
self.assertEqual(o.evalue, u'Name not found')
|
||||
self.assertEqual(o.traceback, [u'frame 0', u'frame 1', u'frame 2'])
|
||||
|
||||
def test_empty_html_cell(self):
|
||||
tc = new_text_cell(u'html')
|
||||
self.assertEqual(tc.cell_type, u'html')
|
||||
self.assertEqual(u'source' not in tc, True)
|
||||
|
||||
def test_html_cell(self):
|
||||
tc = new_text_cell(u'html', 'hi')
|
||||
self.assertEqual(tc.source, u'hi')
|
||||
|
||||
def test_empty_markdown_cell(self):
|
||||
tc = new_text_cell(u'markdown')
|
||||
self.assertEqual(tc.cell_type, u'markdown')
|
||||
self.assertEqual(u'source' not in tc, True)
|
||||
|
||||
def test_markdown_cell(self):
|
||||
tc = new_text_cell(u'markdown', 'hi')
|
||||
self.assertEqual(tc.source, u'hi')
|
||||
|
||||
def test_empty_raw_cell(self):
|
||||
tc = new_text_cell(u'raw')
|
||||
self.assertEqual(tc.cell_type, u'raw')
|
||||
self.assertEqual(u'source' not in tc, True)
|
||||
|
||||
def test_raw_cell(self):
|
||||
tc = new_text_cell(u'raw', 'hi')
|
||||
self.assertEqual(tc.source, u'hi')
|
||||
|
||||
def test_empty_heading_cell(self):
|
||||
tc = new_heading_cell()
|
||||
self.assertEqual(tc.cell_type, u'heading')
|
||||
self.assertEqual(u'source' not in tc, True)
|
||||
|
||||
def test_heading_cell(self):
|
||||
tc = new_heading_cell(u'hi', level=2)
|
||||
self.assertEqual(tc.source, u'hi')
|
||||
self.assertEqual(tc.level, 2)
|
||||
|
||||
|
||||
class TestWorksheet(TestCase):
|
||||
|
||||
def test_empty_worksheet(self):
|
||||
ws = new_worksheet()
|
||||
self.assertEqual(ws.cells,[])
|
||||
self.assertEqual(u'name' not in ws, True)
|
||||
|
||||
def test_worksheet(self):
|
||||
cells = [new_code_cell(), new_text_cell(u'html')]
|
||||
ws = new_worksheet(cells=cells)
|
||||
self.assertEqual(ws.cells,cells)
|
||||
|
||||
class TestNotebook(TestCase):
|
||||
|
||||
def test_empty_notebook(self):
|
||||
nb = new_notebook()
|
||||
self.assertEqual(nb.worksheets, [])
|
||||
self.assertEqual(nb.metadata, NotebookNode())
|
||||
self.assertEqual(nb.nbformat,nbformat)
|
||||
|
||||
def test_notebook(self):
|
||||
worksheets = [new_worksheet(),new_worksheet()]
|
||||
metadata = new_metadata(name=u'foo')
|
||||
nb = new_notebook(metadata=metadata,worksheets=worksheets)
|
||||
self.assertEqual(nb.metadata.name,u'foo')
|
||||
self.assertEqual(nb.worksheets,worksheets)
|
||||
self.assertEqual(nb.nbformat,nbformat)
|
||||
|
||||
def test_notebook_name(self):
|
||||
worksheets = [new_worksheet(),new_worksheet()]
|
||||
nb = new_notebook(name='foo',worksheets=worksheets)
|
||||
self.assertEqual(nb.metadata.name,u'foo')
|
||||
self.assertEqual(nb.worksheets,worksheets)
|
||||
self.assertEqual(nb.nbformat,nbformat)
|
||||
|
||||
class TestMetadata(TestCase):
|
||||
|
||||
def test_empty_metadata(self):
|
||||
md = new_metadata()
|
||||
self.assertEqual(u'name' not in md, True)
|
||||
self.assertEqual(u'authors' not in md, True)
|
||||
self.assertEqual(u'license' not in md, True)
|
||||
self.assertEqual(u'saved' not in md, True)
|
||||
self.assertEqual(u'modified' not in md, True)
|
||||
self.assertEqual(u'gistid' not in md, True)
|
||||
|
||||
def test_metadata(self):
|
||||
authors = [new_author(name='Bart Simpson',email='bsimpson@fox.com')]
|
||||
md = new_metadata(name=u'foo',license=u'BSD',created=u'today',
|
||||
modified=u'now',gistid=u'21341231',authors=authors)
|
||||
self.assertEqual(md.name, u'foo')
|
||||
self.assertEqual(md.license, u'BSD')
|
||||
self.assertEqual(md.created, u'today')
|
||||
self.assertEqual(md.modified, u'now')
|
||||
self.assertEqual(md.gistid, u'21341231')
|
||||
self.assertEqual(md.authors, authors)
|
||||
|
||||
class TestOutputs(TestCase):
|
||||
def test_binary_png(self):
|
||||
out = new_output(output_png=b'\x89PNG\r\n\x1a\n', output_type='display_data')
|
||||
|
||||
def test_b64b6tes_png(self):
|
||||
out = new_output(output_png=b'iVBORw0KG', output_type='display_data')
|
||||
|
||||
def test_binary_jpeg(self):
|
||||
out = new_output(output_jpeg=b'\xff\xd8', output_type='display_data')
|
||||
|
||||
def test_b64b6tes_jpeg(self):
|
||||
out = new_output(output_jpeg=b'/9', output_type='display_data')
|
||||
|
||||
|
48
venv/Lib/site-packages/nbformat/v3/tests/test_nbpy.py
Normal file
48
venv/Lib/site-packages/nbformat/v3/tests/test_nbpy.py
Normal file
|
@ -0,0 +1,48 @@
|
|||
# -*- coding: utf8 -*-
|
||||
|
||||
from unittest import TestCase
|
||||
|
||||
from ipython_genutils.py3compat import string_types, iteritems
|
||||
|
||||
from . import formattest
|
||||
|
||||
from .. import nbpy
|
||||
from .nbexamples import nb0, nb0_py
|
||||
|
||||
|
||||
class TestPy(formattest.NBFormatTest, TestCase):
|
||||
|
||||
nb0_ref = nb0_py
|
||||
ext = 'py'
|
||||
mod = nbpy
|
||||
ignored_keys = ['collapsed', 'outputs', 'prompt_number', 'metadata']
|
||||
|
||||
def assertSubset(self, da, db):
|
||||
"""assert that da is a subset of db, ignoring self.ignored_keys.
|
||||
|
||||
Called recursively on containers, ultimately comparing individual
|
||||
elements.
|
||||
"""
|
||||
if isinstance(da, dict):
|
||||
for k,v in iteritems(da):
|
||||
if k in self.ignored_keys:
|
||||
continue
|
||||
self.assertTrue(k in db)
|
||||
self.assertSubset(v, db[k])
|
||||
elif isinstance(da, list):
|
||||
for a,b in zip(da, db):
|
||||
self.assertSubset(a,b)
|
||||
else:
|
||||
if isinstance(da, string_types) and isinstance(db, string_types):
|
||||
# pyfile is not sensitive to preserving leading/trailing
|
||||
# newlines in blocks through roundtrip
|
||||
da = da.strip('\n')
|
||||
db = db.strip('\n')
|
||||
self.assertEqual(da, db)
|
||||
return True
|
||||
|
||||
def assertNBEquals(self, nba, nbb):
|
||||
# since roundtrip is lossy, only compare keys that are preserved
|
||||
# assumes nba is read from my file format
|
||||
return self.assertSubset(nba, nbb)
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue