Skip to content

Commit

Permalink
More tests for #95 #93
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Oct 10, 2018
1 parent bf8b476 commit bac6587
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
2 changes: 1 addition & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def test_convert_to_percent_format(nb_file, tmpdir):

with open(tmp_nbpy) as stream:
py_script = stream.read()
assert 'py:percent' in py_script
assert 'format_name: percent' in py_script

nb1 = readf(tmp_ipynb)
nb2 = readf(tmp_nbpy)
Expand Down
60 changes: 48 additions & 12 deletions tests/test_contentsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import jupytext
from jupytext.compare import compare_notebooks
from jupytext.header import header_to_metadata_and_cell
from jupytext.formats import read_format_from_metadata
from jupytext.formats import read_format_from_metadata, auto_ext_from_metadata
from .utils import list_notebooks
from .utils import skip_if_dict_is_not_ordered

Expand Down Expand Up @@ -391,9 +391,9 @@ def test_preferred_format_allows_to_read_implicit_light_format(nb_file, tmpdir):

# check that format (missing) is recognized as light
if 'light' in nb_file:
assert 'py:light' in model['content']['metadata']['jupytext']['formats']
assert 'light' == model['content']['metadata']['jupytext']['text_representation']['format_name']
else:
assert 'py:percent' in model['content']['metadata']['jupytext']['formats']
assert 'percent' == model['content']['metadata']['jupytext']['text_representation']['format_name']


def test_preferred_formats_read_auto(tmpdir):
Expand All @@ -412,20 +412,18 @@ def test_preferred_formats_read_auto(tmpdir):
with mock.patch('jupytext.header.INSERT_AND_CHECK_VERSION_NUMBER', True):
model = cm.get(tmp_py)

# check that script is open as percent
assert 'py:percent' in model['content']['metadata']['jupytext']['formats']
# check that script is opened as percent
assert 'percent' == model['content']['metadata']['jupytext']['text_representation']['format_name']


@pytest.mark.parametrize('nb_file', list_notebooks('ipynb'))
def test_save_in_auto_extension(nb_file, tmpdir):
def test_save_in_auto_extension_global(nb_file, tmpdir):
# load notebook
nb = jupytext.readf(nb_file)
if 'language_info' not in nb.metadata:
return

auto_ext = nb.metadata['language_info']['file_extension']
if auto_ext == '.r':
auto_ext = '.R'
auto_ext = auto_ext_from_metadata(nb.metadata)
tmp_ipynb = 'notebook.ipynb'
tmp_script = 'notebook' + auto_ext

Expand All @@ -443,6 +441,46 @@ def test_save_in_auto_extension(nb_file, tmpdir):
with open(str(tmpdir.join(tmp_script))) as stream:
assert read_format_from_metadata(stream.read(), auto_ext) == 'percent'

# reload and compare with original notebook
with mock.patch('jupytext.header.INSERT_AND_CHECK_VERSION_NUMBER', True):
model = cm.get(path=tmp_script)

# saving should not create a format entry #95
assert 'formats' not in model['content'].metadata.get('jupytext', {})

compare_notebooks(nb, model['content'])


@pytest.mark.parametrize('nb_file', list_notebooks('ipynb'))
def test_save_in_auto_extension_local(nb_file, tmpdir):
# load notebook
nb = jupytext.readf(nb_file)
nb.metadata.setdefault('jupytext', {})['formats'] = 'ipynb,auto:percent'
if 'language_info' not in nb.metadata:
return

auto_ext = auto_ext_from_metadata(nb.metadata)
tmp_ipynb = 'notebook.ipynb'
tmp_script = 'notebook' + auto_ext

# create contents manager with default load format as percent
cm = jupytext.TextFileContentsManager()
cm.root_dir = str(tmpdir)

# save notebook
with mock.patch('jupytext.header.INSERT_AND_CHECK_VERSION_NUMBER', True):
cm.save(model=dict(type='notebook', content=nb), path=tmp_ipynb)

# check that text representation exists, and is in percent format
with open(str(tmpdir.join(tmp_script))) as stream:
assert read_format_from_metadata(stream.read(), auto_ext) == 'percent'

# reload and compare with original notebook
with mock.patch('jupytext.header.INSERT_AND_CHECK_VERSION_NUMBER', True):
model = cm.get(path=tmp_script)

compare_notebooks(nb, model['content'])


@pytest.mark.parametrize('nb_file', list_notebooks('ipynb'))
def test_save_in_pct_and_lgt_auto_extensions(nb_file, tmpdir):
Expand All @@ -451,9 +489,7 @@ def test_save_in_pct_and_lgt_auto_extensions(nb_file, tmpdir):
if 'language_info' not in nb.metadata:
return

auto_ext = nb.metadata['language_info']['file_extension']
if auto_ext == '.r':
auto_ext = '.R'
auto_ext = auto_ext_from_metadata(nb.metadata)
tmp_ipynb = 'notebook.ipynb'
tmp_pct_script = 'notebook.pct' + auto_ext
tmp_lgt_script = 'notebook.lgt' + auto_ext
Expand Down
4 changes: 2 additions & 2 deletions tests/test_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def test_metadata_and_cell_to_header():
cells=[new_raw_cell(
source="---\ntitle: Sample header\n---",
metadata={'noskipline': True})])
header = metadata_and_cell_to_header(nb, get_format('.md'))
header = metadata_and_cell_to_header(nb, get_format('.md'), '.md')
assert '\n'.join(header) == """---
title: Sample header
jupyter:
Expand All @@ -86,6 +86,6 @@ def test_metadata_and_cell_to_header():

def test_metadata_and_cell_to_header2():
nb = new_notebook(cells=[new_markdown_cell(source="Some markdown\ntext")])
header = metadata_and_cell_to_header(nb, get_format('.md'))
header = metadata_and_cell_to_header(nb, get_format('.md'), '.md')
assert header == []
assert len(nb.cells) == 1

0 comments on commit bac6587

Please sign in to comment.