Skip to content

Commit

Permalink
Test Jupytext with pytest-randomly (#892)
Browse files Browse the repository at this point in the history
* Use pytest-randomly

* Refactor the tests to avoid interferences with pytest-randomly

* Update CHANGELOG.md
  • Loading branch information
mwouts authored Dec 9, 2021
1 parent 6e3fd18 commit 0662680
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 39 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Jupytext ChangeLog

**Fixed**
- The parsing of notebooks that don't have a YAML header (like `docs/formats.md`) was improved.
- The test suite works with `pytest-randomly` ([#838](https://github.com/mwouts/jupytext/issues/838))


1.13.3 (2021-12-04)
Expand Down
1 change: 1 addition & 0 deletions environment-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies:
- mdit-py-plugins
- pip
- pytest
- pytest-randomly
- pytest-cov
- coverage
- flake8
Expand Down
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies:
- jupyter_contrib_nbextensions
- pytest
- pytest-xdist
- pytest-randomly
- pytest-cov
- pre-commit
- gitpython
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ flake8
isort
black
pytest>=3.6
pytest-randomly
pytest-cov
notebook
jupyter_client
Expand Down
84 changes: 45 additions & 39 deletions tests/test_cell_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,66 +14,72 @@
from jupytext.compare import compare
from jupytext.metadata_filter import filter_metadata

SAMPLES = [
("r", ("R", {})),
(
'r plot_1, dpi=72, fig.path="fig_path/"',
("R", {"name": "plot_1", "dpi": 72, "fig.path": "fig_path/"}),
),
(
'r plot_1, bool=TRUE, fig.path="fig_path/"',
("R", {"name": "plot_1", "bool": True, "fig.path": "fig_path/"}),
),
("r echo=FALSE", ("R", {"tags": ["remove_input"]})),
("r plot_1, echo=TRUE", ("R", {"name": "plot_1", "echo": True})),
(
"python echo=if a==5 then TRUE else FALSE",
("python", {"echo": "#R_CODE#if a==5 then TRUE else FALSE"}),
),
(
'python noname, tags=c("a", "b", "c"), echo={sum(a+c(1,2))>1}',

def r_options_language_metadata():
for r_options, language, metadata in [
("r", "R", {}),
(
'r plot_1, dpi=72, fig.path="fig_path/"',
"R",
{"name": "plot_1", "dpi": 72, "fig.path": "fig_path/"},
),
(
'r plot_1, bool=TRUE, fig.path="fig_path/"',
"R",
{"name": "plot_1", "bool": True, "fig.path": "fig_path/"},
),
("r echo=FALSE", "R", {"tags": ["remove_input"]}),
("r plot_1, echo=TRUE", "R", {"name": "plot_1", "echo": True}),
(
"python echo=if a==5 then TRUE else FALSE",
"python",
{"echo": "#R_CODE#if a==5 then TRUE else FALSE"},
),
(
'python noname, tags=c("a", "b", "c"), echo={sum(a+c(1,2))>1}',
"python",
{
"name": "noname",
"tags": ["a", "b", "c"],
"echo": "#R_CODE#{sum(a+c(1,2))>1}",
},
),
),
('python active="ipynb,py"', ("python", {"active": "ipynb,py"})),
(
'python include=FALSE, active="Rmd"',
("python", {"active": "Rmd", "tags": ["remove_cell"]}),
),
(
'r chunk_name, include=FALSE, active="Rmd"',
("R", {"name": "chunk_name", "active": "Rmd", "tags": ["remove_cell"]}),
),
('python tags=c("parameters")', ("python", {"tags": ["parameters"]})),
]
('python active="ipynb,py"', "python", {"active": "ipynb,py"}),
(
'python include=FALSE, active="Rmd"',
"python",
{"active": "Rmd", "tags": ["remove_cell"]},
),
(
'r chunk_name, include=FALSE, active="Rmd"',
"R",
{"name": "chunk_name", "active": "Rmd", "tags": ["remove_cell"]},
),
('python tags=c("parameters")', "python", {"tags": ["parameters"]}),
]:
yield r_options, language, metadata


@pytest.mark.parametrize("options,language_and_metadata", SAMPLES)
def test_parse_rmd_options(options, language_and_metadata):
compare(rmd_options_to_metadata(options), language_and_metadata)
@pytest.mark.parametrize("options,language, metadata", r_options_language_metadata())
def test_parse_rmd_options(options, language, metadata):
compare(rmd_options_to_metadata(options), (language, metadata))


@pytest.mark.parametrize("options,language_and_metadata", SAMPLES)
def test_build_options(options, language_and_metadata):
compare(metadata_to_rmd_options(*language_and_metadata), options)
@pytest.mark.parametrize("options,language, metadata", r_options_language_metadata())
def test_build_options(options, language, metadata):
compare(metadata_to_rmd_options(*(language, metadata)), options)


@pytest.mark.parametrize("options,language_and_metadata", SAMPLES)
def test_build_options_random_order(options, language_and_metadata):
@pytest.mark.parametrize("options,language, metadata", r_options_language_metadata())
def test_build_options_random_order(options, language, metadata):
# Older python has no respect for order...
# assert to_chunk_options(metadata) == options

def split_and_strip(opt):
set([o.strip() for o in opt.split(",")])

assert split_and_strip(
metadata_to_rmd_options(*language_and_metadata)
metadata_to_rmd_options(*(language, metadata))
) == split_and_strip(options)


Expand Down

0 comments on commit 0662680

Please sign in to comment.