diff --git a/IPython/nbformat/v3/tests/formattest.py b/IPython/nbformat/v3/tests/formattest.py new file mode 100644 index 0000000..0aed69a --- /dev/null +++ b/IPython/nbformat/v3/tests/formattest.py @@ -0,0 +1,61 @@ +# -*- coding: utf8 -*- +import io +import os +import shutil +import tempfile + +pjoin = os.path.join + +from unittest import TestCase + +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 + + +class NBFormatTestCase(TestCase): + + # 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.assertEquals(nba, nbb) + + def test_writes(self): + s = self.mod.writes(nb0) + if self.nb0_ref: + self.assertEquals(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 io.open(pjoin(self.wd, "nb0.%s" % self.ext), 'w') as f: + self.mod.write(nb0, f) + + def test_read_file(self): + with io.open(pjoin(self.wd, "nb0.%s" % self.ext), 'w') as f: + self.mod.write(nb0, f) + + with io.open(pjoin(self.wd, "nb0.%s" % self.ext), 'r') as f: + nb = self.mod.read(f) + + + diff --git a/IPython/nbformat/v3/tests/test_json.py b/IPython/nbformat/v3/tests/test_json.py index ecbb2d1..5b7d6f8 100644 --- a/IPython/nbformat/v3/tests/test_json.py +++ b/IPython/nbformat/v3/tests/test_json.py @@ -2,33 +2,32 @@ import pprint from unittest import TestCase from ..nbjson import reads, writes +from .. import nbjson from .nbexamples import nb0 +from . import formattest -class TestJSON(TestCase): +from .nbexamples import nb0 + + +class TestJSON(formattest.NBFormatTestCase): + + nb0_ref = None + ext = 'ipynb' + mod = nbjson - def test_roundtrip(self): - s = writes(nb0) - # print - # print pprint.pformat(nb0,indent=2) - # print - # print pprint.pformat(reads(s),indent=2) - # print - # print s - self.assertEquals(reads(s),nb0) - 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.assertEquals(reads(s),nb0) + self.assertEquals(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.assertEquals(reads(s),nb0) + self.assertEquals(nbjson.reads(s),nb0) diff --git a/IPython/nbformat/v3/tests/test_nbpy.py b/IPython/nbformat/v3/tests/test_nbpy.py index 4c12c4e..ca8f56b 100644 --- a/IPython/nbformat/v3/tests/test_nbpy.py +++ b/IPython/nbformat/v3/tests/test_nbpy.py @@ -1,17 +1,38 @@ -from unittest import TestCase +# -*- coding: utf8 -*- +from . import formattest -from ..nbbase import ( - NotebookNode, - new_code_cell, new_text_cell, new_worksheet, new_notebook -) - -from ..nbpy import reads, writes +from .. import nbpy from .nbexamples import nb0, nb0_py -class TestPy(TestCase): +class TestPy(formattest.NBFormatTestCase): - def test_write(self): - s = writes(nb0) - self.assertEquals(s,nb0_py) + 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 da.iteritems(): + 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: + self.assertEquals(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) +