Skip to content

Commit

Permalink
Merge pull request #23 from datarootsio/add-config-tests
Browse files Browse the repository at this point in the history
Add config tests
  • Loading branch information
murilo-cunha committed Feb 16, 2022
2 parents 9f58381 + b84abe9 commit aa4efdb
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 84 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,5 @@ maintained by [dataroots](https://github.com/datarootsio).

Special thanks to:

[Bart](https://github.com/Bart6114) and [Nick](https://github.com/NickSchouten) for
feedback and support.
[Bart](https://github.com/Bart6114), [Nick](https://github.com/NickSchouten) and
[Freddy](https://github.com/frederikdesmedt) for feedback and support.
1 change: 0 additions & 1 deletion databooks/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from databooks.logging import get_logger

TOML_CONFIG_FILE = "pyproject.toml"
INI_CONFIG_FILE = "settings.ini"

ConfigFields = Dict[str, Any]

Expand Down
141 changes: 85 additions & 56 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ mkdocs-autorefs = "^0.3.0"
mkdocs-coverage = "^0.2.4"
mike = "^1.1.2"
mkdocstrings = "^0.17.0"
click = "7.1.2"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.black]
include = "(databooks/|tests/)"
extend-exclude = "tests/files/"

[tool.mypy]
files = ["databooks"]
Expand Down
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions tests/files/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[tool.databooks.meta]
rm-outs=true
rm_exec=false
overwrite=true

[tool.databooks.fix]
metadata-head=false

[tool.databooks.test-config]
config-default = "config-value"
46 changes: 22 additions & 24 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import logging
from copy import deepcopy
from importlib import resources
from pathlib import Path
from textwrap import dedent

from _pytest.logging import LogCaptureFixture
from click import Command # type: ignore
from py._path.local import LocalPath
from typer import Context
from typer.testing import CliRunner

from databooks.cli import app
from databooks.cli import _config_callback, app
from databooks.common import write_notebook
from databooks.data_models.notebook import (
Cell,
Expand All @@ -20,18 +22,6 @@
from tests.test_data_models.test_notebook import TestJupyterNotebook # type: ignore
from tests.test_git_utils import init_repo_conflicts

SAMPLE_CONFIG = dedent(
"""
[tool.databooks.meta]
rm-outs=true
rm_exec=false
overwrite=true
[tool.databooks.fix]
metadata-head=false
"""
)

runner = CliRunner()


Expand All @@ -42,6 +32,16 @@ def test_version_callback() -> None:
assert f"databooks version: {__version__}\n" == result.stdout


def test_config_callback() -> None:
"""Overwrite default parameters from `typer.Context`."""
cmd = Command(name="test-config")
with Context(cmd) as ctx, resources.path("tests.files", "pyproject.toml") as conf:
assert ctx.default_map is None
parsed_config = _config_callback(ctx=ctx, config_path=conf)
assert ctx.default_map == dict(config_default="config-value")
assert parsed_config == conf


def test_meta(tmpdir: LocalPath) -> None:
"""Remove notebook metadata."""
read_path = tmpdir.mkdir("notebooks") / "test_meta_nb.ipynb" # type: ignore
Expand Down Expand Up @@ -103,12 +103,12 @@ def test_meta__config(tmpdir: LocalPath) -> None:
read_path = tmpdir.mkdir("notebooks") / "test_meta_nb.ipynb" # type: ignore
write_notebook(nb=TestJupyterNotebook().jupyter_notebook, path=read_path)

config_path = tmpdir / "pyproject.toml" # type: ignore
config_path.write_text(SAMPLE_CONFIG, encoding="utf-8")

nb_read = JupyterNotebook.parse_file(path=read_path)
# Take arguments from config file
result = runner.invoke(app, ["meta", str(read_path), "--config", str(config_path)])
with resources.path("tests.files", "pyproject.toml") as config_path:
# Take arguments from config file
result = runner.invoke(
app, ["meta", str(read_path), "--config", str(config_path)]
)
nb_write = JupyterNotebook.parse_file(path=read_path)

assert result.exit_code == 0
Expand Down Expand Up @@ -240,9 +240,6 @@ def test_fix(tmpdir: LocalPath) -> None:
def test_fix__config(tmpdir: LocalPath) -> None:
"""Fix notebook conflicts with configuration overriding defaults."""
# Setup
config_path = tmpdir / "pyproject.toml" # type: ignore
config_path.write_text(SAMPLE_CONFIG, encoding="utf-8")

nb_path = Path("test_conflicts_nb.ipynb")
notebook_1 = TestJupyterNotebook().jupyter_notebook
notebook_2 = TestJupyterNotebook().jupyter_notebook
Expand Down Expand Up @@ -277,8 +274,9 @@ def test_fix__config(tmpdir: LocalPath) -> None:
id_main = conflict_files[0].first_log
id_other = conflict_files[0].last_log

# Run CLI and check conflict resolution
result = runner.invoke(app, ["fix", str(tmpdir), "--config", str(config_path)])
with resources.path("tests.files", "pyproject.toml") as config_path:
# Run CLI and check conflict resolution
result = runner.invoke(app, ["fix", str(tmpdir), "--config", str(config_path)])
fixed_notebook = JupyterNotebook.parse_file(path=tmpdir / nb_path)

assert len(conflict_files) == 1
Expand Down
2 changes: 1 addition & 1 deletion tests/test_data_models/test_notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def test_notebook_sub(self) -> None:

def test_parse_file() -> None:
"""Deserialize `ipynb` file to `databooks.JupyterNotebook` models."""
with resources.path("tests.notebooks", "demo.ipynb") as nb_path:
with resources.path("tests.files", "demo.ipynb") as nb_path:
notebook = JupyterNotebook.parse_file(nb_path)

assert notebook.nbformat == 4
Expand Down

0 comments on commit aa4efdb

Please sign in to comment.