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