Uploaded Test files

This commit is contained in:
Batuhan Berk Başoğlu 2020-11-12 11:05:57 -05:00
parent f584ad9d97
commit 2e81cb7d99
16627 changed files with 2065359 additions and 102444 deletions

View file

@ -0,0 +1,54 @@
# -*- coding: utf8 -*-
import io
import os
import shutil
import tempfile
pjoin = os.path.join
from .nbexamples import nb0
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)

View file

@ -0,0 +1,135 @@
# -*- coding: utf-8 -*-
import os
from ..._compat import encodebytes
from ..nbbase import (
new_code_cell, new_markdown_cell, new_notebook,
new_output, new_raw_cell
)
# some random base64-encoded *text*
png = encodebytes(os.urandom(5)).decode('ascii')
jpeg = encodebytes(os.urandom(6)).decode('ascii')
cells = []
cells.append(new_markdown_cell(
source='Some NumPy Examples',
))
cells.append(new_code_cell(
source='import numpy',
execution_count=1,
))
cells.append(new_markdown_cell(
source='Cell with attachments',
attachments={
'attachment1': {
'text/plain': '\n'.join(['a', 'b', 'c']),
'application/vnd.stuff+json': ['a', 1, 'x'],
}
}
))
cells.append(new_raw_cell(
source='A random array',
))
cells.append(new_markdown_cell(
source=u'## My Heading',
))
cells.append(new_code_cell(
source='a = numpy.random.rand(100)',
execution_count=2,
))
cells.append(new_code_cell(
source='a = 10\nb = 5\n',
execution_count=3,
))
cells.append(new_code_cell(
source='a = 10\nb = 5',
execution_count=4,
))
cells.append(new_code_cell(
source=u'json_outputs()',
execution_count=12,
outputs=[new_output(
output_type=u'display_data',
data={
'text/plain': u'<json outputs>',
'application/json': {
'key': 'value',
'x': 5,
'lis': [1, 2, 'x']
},
'application/vnd.listofstr+json': ['a', 'b', 'c'],
'application/vnd.numbers+json': [1, 2, 3],
'application/vnd.number+json': 42,
'application/vnd.object+json': {
'number': 5,
'array': [1,2],
'str': 'x'
},
'application/vnd.string+json': 'ok',
},
)]
))
cells.append(new_code_cell(
source=u'print "ünîcødé"',
execution_count=3,
outputs=[new_output(
output_type=u'execute_result',
data={
'text/plain': u'<array a>',
'text/html': u'The HTML rep',
'text/latex': u'$a$',
'image/png': png,
'image/jpeg': jpeg,
'image/svg+xml': u'<svg>',
'application/json': {
'key': 'value'
},
'application/javascript': u'var i=0;'
},
execution_count=3
),new_output(
output_type=u'display_data',
data={
'text/plain': u'<array a>',
'text/html': u'The HTML rep',
'text/latex': u'$a$',
'image/png': png,
'image/jpeg': jpeg,
'image/svg+xml': u'<svg>',
'application/json': {
'key': 'value'
},
'application/javascript': u'var i=0;'
},
),new_output(
output_type=u'error',
ename=u'NameError',
evalue=u'NameError was here',
traceback=[u'frame 0', u'frame 1', u'frame 2']
),new_output(
output_type=u'stream',
text='foo\rbar\r\n'
),new_output(
output_type=u'stream',
name='stderr',
text='\rfoo\rbar\n'
)]
))
nb0 = new_notebook(cells=cells,
metadata={
'language': 'python',
}
)

View file

@ -0,0 +1,70 @@
# -*- coding: utf-8 -*-
import copy
from nbformat import validate
from .. import convert
from . import nbexamples
from nbformat.v3.tests import nbexamples as v3examples
from nbformat import v3, v4
def test_upgrade_notebook():
nb03 = copy.deepcopy(v3examples.nb0)
validate(nb03)
nb04 = convert.upgrade(nb03)
validate(nb04)
def test_downgrade_notebook():
nb04 = copy.deepcopy(nbexamples.nb0)
validate(nb04)
nb03 = convert.downgrade(nb04)
validate(nb03)
def test_upgrade_heading():
v3h = v3.new_heading_cell
v4m = v4.new_markdown_cell
for v3cell, expected in [
(
v3h(source='foo', level=1),
v4m(source='# foo'),
),
(
v3h(source='foo\nbar\nmulti-line\n', level=4),
v4m(source='#### foo bar multi-line'),
),
(
v3h(source=u'ünìcö∂ecønvërsioñ', level=4),
v4m(source=u'#### ünìcö∂ecønvërsioñ'),
),
]:
upgraded = convert.upgrade_cell(v3cell)
assert upgraded == expected
def test_downgrade_heading():
v3h = v3.new_heading_cell
v4m = v4.new_markdown_cell
v3m = lambda source: v3.new_text_cell('markdown', source)
for v4cell, expected in [
(
v4m(source='# foo'),
v3h(source='foo', level=1),
),
(
v4m(source='#foo'),
v3h(source='foo', level=1),
),
(
v4m(source='#\tfoo'),
v3h(source='foo', level=1),
),
(
v4m(source='# \t foo'),
v3h(source='foo', level=1),
),
(
v4m(source='# foo\nbar'),
v3m(source='# foo\nbar'),
),
]:
downgraded = convert.downgrade_cell(v4cell)
assert downgraded == expected

View file

@ -0,0 +1,122 @@
import os
import json
from unittest import TestCase
from ..._compat import decodebytes
from ..nbjson import reads, writes
from .. import nbjson, nbformat, nbformat_minor
from .nbexamples import nb0
from . import formattest
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_splitlines(self):
"""Test splitlines in mime-bundles"""
s = writes(nb0, split_lines=True)
raw_nb = json.loads(s)
for i, ref_cell in enumerate(nb0.cells):
if ref_cell.source.strip() == 'Cell with attachments':
attach_ref = ref_cell['attachments']['attachment1']
attach_json = raw_nb['cells'][i]['attachments']['attachment1']
if ref_cell.source.strip() == 'json_outputs()':
output_ref = ref_cell['outputs'][0]['data']
output_json = raw_nb['cells'][i]['outputs'][0]['data']
for key, json_value in attach_json.items():
if key == 'text/plain':
# text should be split
assert json_value == attach_ref['text/plain'].splitlines(True)
else:
# JSON attachments
assert json_value == attach_ref[key]
# check that JSON outputs are left alone:
for key, json_value in output_json.items():
if key == 'text/plain':
# text should be split
assert json_value == output_ref['text/plain'].splitlines(True)
else:
# JSON outputs should be left alone
assert json_value == output_ref[key]
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.cells:
if not 'outputs' in cell:
continue
for output in cell.outputs:
if not 'data' in output:
continue
if 'image/png' in output.data:
found_png = True
pngdata = output.data['image/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.cells:
if not 'outputs' in cell:
continue
for output in cell.outputs:
if not 'data' in output:
continue
if 'image/jpeg' in output.data:
found_jpeg = True
jpegdata = output.data['image/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"
def test_latest_schema_matches(self):
"""Test to ensure all schema is locked to a known version"""
assert nbformat == 4
assert nbformat_minor == 4
def test_base_version_matches_latest(self):
"""Test to ensure latest version file matches latest verison"""
with open(os.path.join(os.path.dirname(__file__), '..', 'nbformat.v4.schema.json'), 'r') as schema_file:
latest_schema = json.load(schema_file)
with open(os.path.join(os.path.dirname(__file__), '..', 'nbformat.v{major}.{minor}.schema.json'.format(
major=nbformat, minor=nbformat_minor)), 'r') as schema_file:
ver_schema = json.load(schema_file)
assert latest_schema == ver_schema
def test_latest_matches_nbformat(self):
"""Test to ensure that the nbformat version matches the description of the latest schema"""
with open(os.path.join(os.path.dirname(__file__), '..', 'nbformat.v4.schema.json'), 'r') as schema_file:
schema = json.load(schema_file)
assert schema['description'] == 'Jupyter Notebook v{major}.{minor} JSON schema.'.format(
major=nbformat, minor=nbformat_minor
)

View file

@ -0,0 +1,99 @@
# coding: utf-8
"""Tests for the Python API for composing notebook elements"""
from nbformat.validator import isvalid, validate, ValidationError
from ..nbbase import (
NotebookNode, nbformat,
new_code_cell, new_markdown_cell, new_notebook,
new_output, new_raw_cell,
)
def test_empty_notebook():
nb = new_notebook()
assert nb.cells == []
assert nb.metadata == NotebookNode()
assert nb.nbformat == nbformat
def test_empty_markdown_cell():
cell = new_markdown_cell()
assert cell.cell_type == 'markdown'
assert cell.source == ''
def test_markdown_cell():
cell = new_markdown_cell(u'* Søme markdown')
assert cell.source == u'* Søme markdown'
def test_empty_raw_cell():
cell = new_raw_cell()
assert cell.cell_type == u'raw'
assert cell.source == ''
def test_raw_cell():
cell = new_raw_cell('hi')
assert cell.source == u'hi'
def test_empty_code_cell():
cell = new_code_cell('hi')
assert cell.cell_type == 'code'
assert cell.source == u'hi'
def test_empty_display_data():
output = new_output('display_data')
assert output.output_type == 'display_data'
def test_empty_stream():
output = new_output('stream')
assert output.output_type == 'stream'
assert output.name == 'stdout'
assert output.text == ''
def test_empty_execute_result():
output = new_output('execute_result', execution_count=1)
assert output.output_type == 'execute_result'
mimebundle = {
'text/plain': "some text",
"application/json": {
"key": "value"
},
"image/svg+xml": 'ABCDEF',
"application/octet-stream": 'ABC-123',
"application/vnd.foo+bar": "Some other stuff",
}
def test_display_data():
output = new_output('display_data', mimebundle)
for key, expected in mimebundle.items():
assert output.data[key] == expected
def test_execute_result():
output = new_output('execute_result', mimebundle, execution_count=10)
assert output.execution_count == 10
for key, expected in mimebundle.items():
assert output.data[key] == expected
def test_error():
o = new_output(output_type=u'error', ename=u'NameError',
evalue=u'Name not found', traceback=[u'frame 0', u'frame 1', u'frame 2']
)
assert o.output_type == u'error'
assert o.ename == u'NameError'
assert o.evalue == u'Name not found'
assert o.traceback == [u'frame 0', u'frame 1', u'frame 2']
def test_code_cell_with_outputs():
cell = new_code_cell(execution_count=10, outputs=[
new_output('display_data', mimebundle),
new_output('stream', text='hello'),
new_output('execute_result', mimebundle, execution_count=10),
])
assert cell.execution_count == 10
assert len(cell.outputs) == 3
er = cell.outputs[-1]
assert er.execution_count == 10
assert er['output_type'] == 'execute_result'
def test_stream():
output = new_output('stream', name='stderr', text='hello there')
assert output.name == 'stderr'
assert output.text == 'hello there'

View file

@ -0,0 +1,105 @@
"""Tests for nbformat validation"""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
import io
import os
import pytest
from nbformat.validator import validate, ValidationError
from ..nbjson import reads
from ..nbbase import (
nbformat,
new_code_cell, new_markdown_cell, new_notebook,
new_output, new_raw_cell,
)
def validate4(obj, ref=None):
return validate(obj, ref, version=nbformat)
def test_valid_code_cell():
cell = new_code_cell()
validate4(cell, 'code_cell')
def test_invalid_code_cell():
cell = new_code_cell()
cell['source'] = 5
with pytest.raises(ValidationError):
validate4(cell, 'code_cell')
cell = new_code_cell()
del cell['metadata']
with pytest.raises(ValidationError):
validate4(cell, 'code_cell')
cell = new_code_cell()
del cell['source']
with pytest.raises(ValidationError):
validate4(cell, 'code_cell')
cell = new_code_cell()
del cell['cell_type']
with pytest.raises(ValidationError):
validate4(cell, 'code_cell')
def test_invalid_markdown_cell():
cell = new_markdown_cell()
cell['source'] = 5
with pytest.raises(ValidationError):
validate4(cell, 'markdown_cell')
cell = new_markdown_cell()
del cell['metadata']
with pytest.raises(ValidationError):
validate4(cell, 'markdown_cell')
cell = new_markdown_cell()
del cell['source']
with pytest.raises(ValidationError):
validate4(cell, 'markdown_cell')
cell = new_markdown_cell()
del cell['cell_type']
with pytest.raises(ValidationError):
validate4(cell, 'markdown_cell')
def test_invalid_raw_cell():
cell = new_raw_cell()
cell['source'] = 5
with pytest.raises(ValidationError):
validate4(cell, 'raw_cell')
cell = new_raw_cell()
del cell['metadata']
with pytest.raises(ValidationError):
validate4(cell, 'raw_cell')
cell = new_raw_cell()
del cell['source']
with pytest.raises(ValidationError):
validate4(cell, 'raw_cell')
cell = new_raw_cell()
del cell['cell_type']
with pytest.raises(ValidationError):
validate4(cell, 'raw_cell')
def test_sample_notebook():
here = os.path.dirname(__file__)
with io.open(os.path.join(here, os.pardir, os.pardir, 'tests', "test4.ipynb"), encoding='utf-8') as f:
nb = reads(f.read())
validate4(nb)