Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Dec 11, 2022
1 parent b36663f commit 593a4a9
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 60 deletions.
13 changes: 6 additions & 7 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1385,7 +1385,9 @@ def test_jupytext_to_ipynb_does_not_update_timestamp_if_not_paired(


@pytest.mark.parametrize("formats", ["ipynb,py", "py:percent", "py", None])
def test_use_source_timestamp(tmpdir, cwd_tmpdir, python_notebook, capsys, formats):
async def test_use_source_timestamp(
cm, tmpdir, cwd_tmpdir, python_notebook, capsys, formats
):
# Write a text notebook
nb = python_notebook
if formats:
Expand All @@ -1410,14 +1412,11 @@ def test_use_source_timestamp(tmpdir, cwd_tmpdir, python_notebook, capsys, forma
assert src_timestamp - 1e-6 <= dest_timestamp <= src_timestamp

# Make sure that we can open the file in Jupyter
from jupytext.contentsmanager import TextFileContentsManager

cm = TextFileContentsManager()
cm.outdated_text_notebook_margin = 0.001
cm.root_dir = str(tmpdir)

# No error here
cm.get("test.ipynb")
await cm.get("test.ipynb")

# But now if we don't use --use-source-timestamp
jupytext(["--to", "ipynb", "test.py"])
Expand All @@ -1428,9 +1427,9 @@ def test_use_source_timestamp(tmpdir, cwd_tmpdir, python_notebook, capsys, forma
from tornado.web import HTTPError

with pytest.raises(HTTPError, match="is more recent than test.py"):
cm.get("test.ipynb")
await cm.get("test.ipynb")
else:
cm.get("test.ipynb")
await cm.get("test.ipynb")


def test_round_trip_with_null_metadata_792(tmpdir, cwd_tmpdir, python_notebook):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_combine():
assert nb_source.cells[5].outputs == ["5"]


def test_read_text_and_combine_with_outputs(tmpdir):
async def test_read_text_and_combine_with_outputs(tmpdir):
tmp_ipynb = "notebook.ipynb"
tmp_script = "notebook.py"

Expand Down Expand Up @@ -118,7 +118,7 @@ def test_read_text_and_combine_with_outputs(tmpdir):
cm.root_dir = str(tmpdir)

# load notebook from script
model = cm.get(tmp_script)
model = await cm.get(tmp_script)
nb = model["content"]

assert nb.cells[0]["source"] == "1+1"
Expand Down
6 changes: 3 additions & 3 deletions tests/test_escape_magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def test_magics_are_not_commented(fmt):
compare_notebooks(nb2, nb)


def test_force_comment_using_contents_manager(tmpdir):
async def test_force_comment_using_contents_manager(tmpdir):
tmp_py = "notebook.py"

cm = jupytext.TextFileContentsManager()
Expand All @@ -152,12 +152,12 @@ def test_force_comment_using_contents_manager(tmpdir):

nb = new_notebook(cells=[new_code_cell("%pylab inline")])

cm.save(model=notebook_model(nb), path=tmp_py)
await cm.save(model=notebook_model(nb), path=tmp_py)
with open(str(tmpdir.join(tmp_py))) as stream:
assert "# %pylab inline" in stream.read().splitlines()

cm.comment_magics = False
cm.save(model=notebook_model(nb), path=tmp_py)
await cm.save(model=notebook_model(nb), path=tmp_py)
with open(str(tmpdir.join(tmp_py))) as stream:
assert "%pylab inline" in stream.read().splitlines()

Expand Down
4 changes: 2 additions & 2 deletions tests/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ def test_write_raises_when_fmt_does_not_exists(tmpdir):
),
],
)
def test_configuration_examples_from_documentation(
async def test_configuration_examples_from_documentation(
config_file, config_contents, python_notebook, tmp_path
):
"""Here we make sure that the config examples from
Expand All @@ -434,7 +434,7 @@ def test_configuration_examples_from_documentation(

# Save the notebook
(tmp_path / "notebooks").mkdir()
cm.save(dict(type="notebook", content=python_notebook), "notebooks/nb.ipynb")
await cm.save(dict(type="notebook", content=python_notebook), "notebooks/nb.ipynb")

# Make sure that ipynb and text version are created
assert (tmp_path / "notebooks" / "nb.ipynb").is_file()
Expand Down
4 changes: 2 additions & 2 deletions tests/test_invalid_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ def test_convert_invalid_md_file_fails(invalid_md_file):
jupytext_cli(["--to", "ipynb", str(invalid_md_file)])


def test_open_invalid_md_file_fails(invalid_md_file, tmp_path):
async def test_open_invalid_md_file_fails(invalid_md_file, tmp_path):
cm = TextFileContentsManager()
cm.root_dir = str(invalid_md_file.parent)

with pytest.raises(HTTPError, match="invalid_file_896.md is not UTF-8 encoded"):
cm.get(invalid_md_file.name)
await cm.get(invalid_md_file.name)
8 changes: 4 additions & 4 deletions tests/test_ipynb_to_myst.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def test_meaningfull_error_write_myst_missing(tmpdir):


@requires_no_myst
def test_meaningfull_error_open_myst_missing(tmpdir):
async def test_meaningfull_error_open_myst_missing(tmpdir):
md_file = tmpdir.join("notebook.md")
md_file.write(
"""---
Expand All @@ -194,12 +194,12 @@ def test_meaningfull_error_open_myst_missing(tmpdir):
cm.root_dir = str(tmpdir)

with pytest.raises(HTTPError, match=PLEASE_INSTALL_MYST):
cm.get("notebook.md")
await cm.get("notebook.md")


@requires_myst
@pytest.mark.parametrize("language_info", ["none", "std", "no_pygments_lexer"])
def test_myst_representation_same_cli_or_contents_manager(
async def test_myst_representation_same_cli_or_contents_manager(
tmpdir, cwd_tmpdir, notebook_with_outputs, language_info
):
"""This test gives some information on #759. As of Jupytext 1.11.1, in the MyST Markdown format,
Expand Down Expand Up @@ -246,7 +246,7 @@ def test_myst_representation_same_cli_or_contents_manager(
cm.formats = "ipynb,md:myst"
cm.root_dir = str(tmpdir.mkdir("contents_manager"))

cm.save(model=dict(content=nb, type="notebook"), path="notebook.ipynb")
await cm.save(model=dict(content=nb, type="notebook"), path="notebook.ipynb")
text_cm = tmpdir.join("contents_manager").join("notebook.md").read()

compare(text_cm, text_api)
8 changes: 4 additions & 4 deletions tests/test_load_multiple.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import jupytext


def test_combine_same_version_ok(tmpdir):
async def test_combine_same_version_ok(tmpdir):
tmp_ipynb = "notebook.ipynb"
tmp_nbpy = "notebook.py"

Expand All @@ -28,14 +28,14 @@ def test_combine_same_version_ok(tmpdir):
cm.formats = "ipynb,py"
cm.root_dir = str(tmpdir)

nb = cm.get(tmp_ipynb)
nb = await cm.get(tmp_ipynb)
cells = nb["content"]["cells"]
assert len(cells) == 1
assert cells[0].cell_type == "markdown"
assert cells[0].source == "New cell"


def test_combine_lower_version_raises(tmpdir):
async def test_combine_lower_version_raises(tmpdir):
tmp_ipynb = "notebook.ipynb"
tmp_nbpy = "notebook.py"

Expand All @@ -59,4 +59,4 @@ def test_combine_lower_version_raises(tmpdir):
cm.root_dir = str(tmpdir)

with pytest.raises(HTTPError):
cm.get(tmp_ipynb)
await cm.get(tmp_ipynb)
12 changes: 6 additions & 6 deletions tests/test_pre_commit_1_sync_with_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from nbformat.v4.nbbase import new_markdown_cell
from pre_commit.main import main as pre_commit

from jupytext import TextFileContentsManager, read
from jupytext import read

from .utils import (
skip_pre_commit_tests_on_windows,
Expand All @@ -13,7 +13,8 @@

@skip_pre_commit_tests_on_windows
@skip_pre_commit_tests_when_jupytext_folder_is_not_a_git_repo
def test_pre_commit_hook_sync_with_config(
async def test_pre_commit_hook_sync_with_config(
cm,
tmpdir,
cwd_tmpdir,
tmp_repo,
Expand All @@ -38,9 +39,8 @@ def test_pre_commit_hook_sync_with_config(

# create a test notebook and save it in Jupyter
nb = python_notebook
cm = TextFileContentsManager()
cm.root_dir = str(tmpdir)
cm.save(dict(type="notebook", content=nb), "test.ipynb")
await cm.save(dict(type="notebook", content=nb), "test.ipynb")

# Assert that "text_representation" is in the Jupytext metadata #900
assert "text_representation" in tmpdir.join("test.py").read()
Expand Down Expand Up @@ -68,9 +68,9 @@ def test_pre_commit_hook_sync_with_config(

# modify the ipynb file in Jupyter
# Reload the notebook
nb = cm.get("test.ipynb")["content"]
nb = (await cm.get("test.ipynb"))["content"]
nb.cells.append(new_markdown_cell("A new cell"))
cm.save(dict(type="notebook", content=nb), "test.ipynb")
await cm.save(dict(type="notebook", content=nb), "test.ipynb")

# The text representation metadata is in the py file
assert "text_representation" in tmpdir.join("test.py").read()
Expand Down
7 changes: 4 additions & 3 deletions tests/test_pre_commit_1_sync_with_no_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

@skip_pre_commit_tests_on_windows
@skip_pre_commit_tests_when_jupytext_folder_is_not_a_git_repo
def test_pre_commit_hook_sync_with_no_config(
async def test_pre_commit_hook_sync_with_no_config(
cm,
tmpdir,
cwd_tmpdir,
tmp_repo,
Expand Down Expand Up @@ -45,7 +46,7 @@ def test_pre_commit_hook_sync_with_no_config(
(tmpdir / "notebooks").mkdir()
cm = TextFileContentsManager()
cm.root_dir = str(tmpdir)
cm.save(dict(type="notebook", content=nb), "nb.ipynb")
await cm.save(dict(type="notebook", content=nb), "nb.ipynb")

# Add it to git
tmp_repo.git.add("nb.ipynb")
Expand All @@ -58,7 +59,7 @@ def test_pre_commit_hook_sync_with_no_config(

# Modify and save the notebook
nb.cells.append(new_markdown_cell("New markdown cell"))
cm.save(dict(type="notebook", content=nb), "nb.ipynb")
await cm.save(dict(type="notebook", content=nb), "nb.ipynb")

# No .py file at the moment
assert not (tmpdir / "nb.py").exists()
Expand Down
16 changes: 8 additions & 8 deletions tests/test_read_simple_percent.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ def test_multiline_comments_in_markdown_cell_is_robust_to_additional_cell_marker
compare(py, text)


def test_cell_markers_option_in_contents_manager(tmpdir):
async def test_cell_markers_option_in_contents_manager(tmpdir):
tmp_ipynb = tmpdir / "notebook.ipynb"
tmp_py = tmpdir / "notebook.py"

Expand All @@ -354,7 +354,7 @@ def test_cell_markers_option_in_contents_manager(tmpdir):
}
},
)
cm.save(model=notebook_model(nb), path="notebook.ipynb")
await cm.save(model=notebook_model(nb), path="notebook.ipynb")

assert os.path.isfile(tmp_ipynb)
assert os.path.isfile(tmp_py)
Expand All @@ -380,15 +380,15 @@ def test_cell_markers_option_in_contents_manager(tmpdir):
compare_notebooks(nb, nb2)


def test_cell_markers_in_config(tmpdir, python_notebook):
async def test_cell_markers_in_config(tmpdir, python_notebook):
(tmpdir / "jupytext.toml").write('''cell_markers = '"""'\n''')

cm = jupytext.TextFileContentsManager()
cm.root_dir = str(tmpdir)
nb = python_notebook
nb.metadata["jupytext"] = {"formats": "ipynb,py:percent"}

cm.save(model=notebook_model(nb), path="notebook.ipynb")
await cm.save(model=notebook_model(nb), path="notebook.ipynb")

text = (tmpdir / "notebook.py").read()
assert (
Expand All @@ -404,7 +404,7 @@ def test_cell_markers_in_config(tmpdir, python_notebook):
compare_notebooks(nb, nb2)


def test_cell_markers_in_contents_manager(tmpdir):
async def test_cell_markers_in_contents_manager(tmpdir):
tmp_ipynb = tmpdir / "notebook.ipynb"
tmp_py = tmpdir / "notebook.py"

Expand All @@ -421,7 +421,7 @@ def test_cell_markers_in_contents_manager(tmpdir):
}
},
)
cm.save(model=notebook_model(nb), path="notebook.ipynb")
await cm.save(model=notebook_model(nb), path="notebook.ipynb")

assert os.path.isfile(tmp_ipynb)
assert os.path.isfile(tmp_py)
Expand All @@ -447,7 +447,7 @@ def test_cell_markers_in_contents_manager(tmpdir):
compare_notebooks(nb, nb2)


def test_cell_markers_in_contents_manager_does_not_impact_light_format(tmpdir):
async def test_cell_markers_in_contents_manager_does_not_impact_light_format(tmpdir):
tmp_ipynb = tmpdir / "notebook.ipynb"
tmp_py = tmpdir / "notebook.py"

Expand All @@ -462,7 +462,7 @@ def test_cell_markers_in_contents_manager_does_not_impact_light_format(tmpdir):
},
)
with pytest.warns(UserWarning, match="Ignored cell markers"):
cm.save(model=notebook_model(nb), path="notebook.ipynb")
await cm.save(model=notebook_model(nb), path="notebook.ipynb")

assert os.path.isfile(tmp_ipynb)
assert os.path.isfile(tmp_py)
Expand Down
10 changes: 5 additions & 5 deletions tests/test_remove_encoding.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import jupytext


def test_remove_encoding_907(tmp_path, python_notebook):
async def test_remove_encoding_907(tmp_path, python_notebook):
# Pair all notebooks to py:percent files
(tmp_path / "jupytext.toml").write_text('formats="ipynb,py:percent"')

Expand All @@ -10,7 +10,7 @@ def test_remove_encoding_907(tmp_path, python_notebook):
cm.root_dir = str(tmp_path)

# Save the notebook in Jupyter
cm.save(dict(type="notebook", content=python_notebook), path="nb.ipynb")
await cm.save(dict(type="notebook", content=python_notebook), path="nb.ipynb")

# No encoding is present in the py file
py = (tmp_path / "nb.py").read_text()
Expand All @@ -21,11 +21,11 @@ def test_remove_encoding_907(tmp_path, python_notebook):
(tmp_path / "nb.py").write_text(py)

# Reload the notebook
nb = cm.get("nb.ipynb")["content"]
nb = (await cm.get("nb.ipynb"))["content"]
assert "encoding" in nb.metadata["jupytext"]

# Save the notebook
cm.save(dict(type="notebook", content=nb), path="nb.ipynb")
await cm.save(dict(type="notebook", content=nb), path="nb.ipynb")

# The encoding is still present in the py file
py = (tmp_path / "nb.py").read_text()
Expand All @@ -36,7 +36,7 @@ def test_remove_encoding_907(tmp_path, python_notebook):
(tmp_path / "nb.py").write_text(py)

# Reload the notebook - the encoding is not there anymore
nb = cm.get("nb.ipynb")["content"]
nb = (await cm.get("nb.ipynb"))["content"]
assert "encoding" not in nb.metadata["jupytext"]

# Save the notebook - the encoding is not there anymore
Expand Down
Loading

0 comments on commit 593a4a9

Please sign in to comment.