Show More
@@ -0,0 +1,61 b'' | |||||
|
1 | # -*- coding: utf8 -*- | |||
|
2 | import io | |||
|
3 | import os | |||
|
4 | import shutil | |||
|
5 | import tempfile | |||
|
6 | ||||
|
7 | pjoin = os.path.join | |||
|
8 | ||||
|
9 | from unittest import TestCase | |||
|
10 | ||||
|
11 | from ..nbbase import ( | |||
|
12 | NotebookNode, | |||
|
13 | new_code_cell, new_text_cell, new_worksheet, new_notebook | |||
|
14 | ) | |||
|
15 | ||||
|
16 | from ..nbpy import reads, writes, read, write | |||
|
17 | from .nbexamples import nb0, nb0_py | |||
|
18 | ||||
|
19 | ||||
|
20 | class NBFormatTestCase(TestCase): | |||
|
21 | ||||
|
22 | # override with appropriate values in subclasses | |||
|
23 | nb0_ref = None | |||
|
24 | ext = None | |||
|
25 | mod = None | |||
|
26 | ||||
|
27 | def setUp(self): | |||
|
28 | self.wd = tempfile.mkdtemp() | |||
|
29 | ||||
|
30 | def tearDown(self): | |||
|
31 | shutil.rmtree(self.wd) | |||
|
32 | ||||
|
33 | def assertNBEquals(self, nba, nbb): | |||
|
34 | self.assertEquals(nba, nbb) | |||
|
35 | ||||
|
36 | def test_writes(self): | |||
|
37 | s = self.mod.writes(nb0) | |||
|
38 | if self.nb0_ref: | |||
|
39 | self.assertEquals(s, self.nb0_ref) | |||
|
40 | ||||
|
41 | def test_reads(self): | |||
|
42 | s = self.mod.writes(nb0) | |||
|
43 | nb = self.mod.reads(s) | |||
|
44 | ||||
|
45 | def test_roundtrip(self): | |||
|
46 | s = self.mod.writes(nb0) | |||
|
47 | self.assertNBEquals(self.mod.reads(s),nb0) | |||
|
48 | ||||
|
49 | def test_write_file(self): | |||
|
50 | with io.open(pjoin(self.wd, "nb0.%s" % self.ext), 'w') as f: | |||
|
51 | self.mod.write(nb0, f) | |||
|
52 | ||||
|
53 | def test_read_file(self): | |||
|
54 | with io.open(pjoin(self.wd, "nb0.%s" % self.ext), 'w') as f: | |||
|
55 | self.mod.write(nb0, f) | |||
|
56 | ||||
|
57 | with io.open(pjoin(self.wd, "nb0.%s" % self.ext), 'r') as f: | |||
|
58 | nb = self.mod.read(f) | |||
|
59 | ||||
|
60 | ||||
|
61 |
@@ -2,33 +2,32 b' import pprint' | |||||
2 | from unittest import TestCase |
|
2 | from unittest import TestCase | |
3 |
|
3 | |||
4 | from ..nbjson import reads, writes |
|
4 | from ..nbjson import reads, writes | |
|
5 | from .. import nbjson | |||
5 | from .nbexamples import nb0 |
|
6 | from .nbexamples import nb0 | |
6 |
|
7 | |||
|
8 | from . import formattest | |||
7 |
|
9 | |||
8 | class TestJSON(TestCase): |
|
10 | from .nbexamples import nb0 | |
|
11 | ||||
|
12 | ||||
|
13 | class TestJSON(formattest.NBFormatTestCase): | |||
|
14 | ||||
|
15 | nb0_ref = None | |||
|
16 | ext = 'ipynb' | |||
|
17 | mod = nbjson | |||
9 |
|
18 | |||
10 | def test_roundtrip(self): |
|
|||
11 | s = writes(nb0) |
|
|||
12 |
|
||||
13 | # print pprint.pformat(nb0,indent=2) |
|
|||
14 |
|
||||
15 | # print pprint.pformat(reads(s),indent=2) |
|
|||
16 |
|
||||
17 | # print s |
|
|||
18 | self.assertEquals(reads(s),nb0) |
|
|||
19 |
|
||||
20 | def test_roundtrip_nosplit(self): |
|
19 | def test_roundtrip_nosplit(self): | |
21 | """Ensure that multiline blobs are still readable""" |
|
20 | """Ensure that multiline blobs are still readable""" | |
22 | # ensures that notebooks written prior to splitlines change |
|
21 | # ensures that notebooks written prior to splitlines change | |
23 | # are still readable. |
|
22 | # are still readable. | |
24 | s = writes(nb0, split_lines=False) |
|
23 | s = writes(nb0, split_lines=False) | |
25 | self.assertEquals(reads(s),nb0) |
|
24 | self.assertEquals(nbjson.reads(s),nb0) | |
26 |
|
25 | |||
27 | def test_roundtrip_split(self): |
|
26 | def test_roundtrip_split(self): | |
28 | """Ensure that splitting multiline blocks is safe""" |
|
27 | """Ensure that splitting multiline blocks is safe""" | |
29 | # This won't differ from test_roundtrip unless the default changes |
|
28 | # This won't differ from test_roundtrip unless the default changes | |
30 | s = writes(nb0, split_lines=True) |
|
29 | s = writes(nb0, split_lines=True) | |
31 | self.assertEquals(reads(s),nb0) |
|
30 | self.assertEquals(nbjson.reads(s),nb0) | |
32 |
|
31 | |||
33 |
|
32 | |||
34 |
|
33 |
@@ -1,17 +1,38 b'' | |||||
1 | from unittest import TestCase |
|
1 | # -*- coding: utf8 -*- | |
|
2 | from . import formattest | |||
2 |
|
3 | |||
3 |
from .. |
|
4 | from .. import nbpy | |
4 | NotebookNode, |
|
|||
5 | new_code_cell, new_text_cell, new_worksheet, new_notebook |
|
|||
6 | ) |
|
|||
7 |
|
||||
8 | from ..nbpy import reads, writes |
|
|||
9 | from .nbexamples import nb0, nb0_py |
|
5 | from .nbexamples import nb0, nb0_py | |
10 |
|
6 | |||
11 |
|
7 | |||
12 | class TestPy(TestCase): |
|
8 | class TestPy(formattest.NBFormatTestCase): | |
13 |
|
9 | |||
14 | def test_write(self): |
|
10 | nb0_ref = nb0_py | |
15 | s = writes(nb0) |
|
11 | ext = 'py' | |
16 | self.assertEquals(s,nb0_py) |
|
12 | mod = nbpy | |
|
13 | ignored_keys = ['collapsed', 'outputs', 'prompt_number', 'metadata'] | |||
17 |
|
14 | |||
|
15 | def assertSubset(self, da, db): | |||
|
16 | """assert that da is a subset of db, ignoring self.ignored_keys. | |||
|
17 | ||||
|
18 | Called recursively on containers, ultimately comparing individual | |||
|
19 | elements. | |||
|
20 | """ | |||
|
21 | if isinstance(da, dict): | |||
|
22 | for k,v in da.iteritems(): | |||
|
23 | if k in self.ignored_keys: | |||
|
24 | continue | |||
|
25 | self.assertTrue(k in db) | |||
|
26 | self.assertSubset(v, db[k]) | |||
|
27 | elif isinstance(da, list): | |||
|
28 | for a,b in zip(da, db): | |||
|
29 | self.assertSubset(a,b) | |||
|
30 | else: | |||
|
31 | self.assertEquals(da, db) | |||
|
32 | return True | |||
|
33 | ||||
|
34 | def assertNBEquals(self, nba, nbb): | |||
|
35 | # since roundtrip is lossy, only compare keys that are preserved | |||
|
36 | # assumes nba is read from my file format | |||
|
37 | return self.assertSubset(nba, nbb) | |||
|
38 |
General Comments 0
You need to be logged in to leave comments.
Login now