From bac65875f12f728ec769ff6377ab54dd7d449108 Mon Sep 17 00:00:00 2001 From: Marc Wouts Date: Tue, 9 Oct 2018 23:15:05 +0200 Subject: [PATCH] More tests for #95 #93 --- tests/test_cli.py | 2 +- tests/test_contentsmanager.py | 60 ++++++++++++++++++++++++++++------- tests/test_header.py | 4 +-- 3 files changed, 51 insertions(+), 15 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 614ee2708..7809260db 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -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) diff --git a/tests/test_contentsmanager.py b/tests/test_contentsmanager.py index 6653befa4..6450b3b93 100644 --- a/tests/test_contentsmanager.py +++ b/tests/test_contentsmanager.py @@ -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 @@ -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): @@ -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 @@ -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): @@ -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 diff --git a/tests/test_header.py b/tests/test_header.py index b026e4e91..27a8f00bc 100644 --- a/tests/test_header.py +++ b/tests/test_header.py @@ -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: @@ -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