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