forked from ipython/ipython
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ipython#1480 from minrk/npmagic
Fix %notebook magic, etc. nbformat unicode tests and fixes. * json.writes always gives unicode, so that `current.writes` can be trusted to give the same interface * setup base TestCase for nbformat tests, to consolidate code, and better test both file formats * add tests for reading/writing to files * allow `name` as kwarg to new_notebook to avoid unnecessary breakage of previous API. * remove fallback to xml, which would hide corrupt notebook files behind a nonsensical 'xml unsupported' message. Closes ipython#1545, ipython#1487.
- Loading branch information
Showing
11 changed files
with
207 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# -*- coding: utf8 -*- | ||
import io | ||
import os | ||
import shutil | ||
import tempfile | ||
|
||
pjoin = os.path.join | ||
|
||
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 | ||
|
||
|
||
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.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 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) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.