Skip to content

Commit

Permalink
added test_cli.py and test_settings.py (#114)
Browse files Browse the repository at this point in the history
* added test_cli.py and test_settings.py

Signed-off-by: tanishq-ids <[email protected]>

* changes in test_settings.py and test_cli.py

Signed-off-by: tanishq-ids <[email protected]>

* Chore: pre-commit autoupdate

* changes in test_settings.py and test_cli.py

Signed-off-by: tanishq-ids <[email protected]>

* changes in test_settings.py

Signed-off-by: tanishq-ids <[email protected]>

* Chore: pre-commit autoupdate

* changes in test_settings.py

Signed-off-by: tanishq-ids <[email protected]>

* Chore: pre-commit autoupdate

* changes in test_cli.py

Signed-off-by: tanishq-ids <[email protected]>

* changes in test_cli.py

Signed-off-by: tanishq-ids <[email protected]>

* Chore: pre-commit autoupdate

* Fix: Address flagged linting issues

Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

---------

Signed-off-by: tanishq-ids <[email protected]>
Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: tanishq-ids <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
4 people authored Jul 11, 2024
1 parent 81a9733 commit ea51c28
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@
"sklearn": ("https://scikit-learn.org/stable", None),
"pandas": ("https://pandas.pydata.org/pandas-docs/stable", None),
"scipy": ("https://docs.scipy.org/doc/scipy/reference", None),
"setuptools": ("https://setuptools.pypa.io/en/stable/", None),
# "setuptools": ("https://setuptools.pypa.io/en/stable/", None),
"pyscaffold": ("https://pyscaffold.org/en/stable", None),
}

Expand Down
112 changes: 112 additions & 0 deletions tests/osc_transformer_presteps/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
"""The module contains tests for the Typer-based CLI application `osc_transformer_presteps`.
The tests include checking the execution and output of various CLI commands.
Fixtures
--------
runner : CliRunner
Provides a CliRunner instance for invoking CLI commands.
Functions
---------
test_extraction_command(runner)
Tests the 'extraction' command.
test_curation_command(runner)
Tests the 'curation' command.
test_no_args(runner)
Tests running the CLI with no arguments.
test_invalid_command(runner)
Tests running the CLI with an invalid command.
"""

import pytest
from typer.testing import CliRunner
from osc_transformer_presteps.cli import app # Import the Typer app
import re


@pytest.fixture
def runner():
"""Fixture that provides a CliRunner instance for invoking CLI commands.
Returns
-------
CliRunner: An instance of CliRunner to invoke commands.
"""
return CliRunner()


def strip_ansi(text):
"""Return modified text string."""
ansi_escape = re.compile(
r"(?:\x1B[@-_]|[\x80-\x9F]|\x1B\[0?m|\x1B\[38;5;\d+m|\x1B\[48;5;\d+m|\x1B\[\d+;\d+;\d+;\d+;\d+m|\x1B\[\d+;\d+m|\x1B\[\d+m)"
)
return ansi_escape.sub("", text)


def test_extraction_command(runner):
"""Test the 'extraction' command.
Args:
----
runner (CliRunner): The CLI runner fixture.
"""
result = runner.invoke(app, ["extraction"])
output = strip_ansi(result.output)
assert result.exit_code == 0
assert (
"If you want to run local extraction of text from files to json then this is the subcommand to use."
in output
)


def test_curation_command(runner):
"""Test the 'curation' command.
Args:
----
runner (CliRunner): The CLI runner fixture.
"""
result = runner.invoke(app, ["curation"])
output = strip_ansi(result.output)
assert result.exit_code == 0
assert (
"If you want to run local creation of dataset of json files then this is the subcommand to use."
in output
)


def test_no_args(runner):
"""Test running the CLI with no arguments.
Args:
----
runner (CliRunner): The CLI runner fixture.
"""
result = runner.invoke(app, [])
output = strip_ansi(result.output)
assert result.exit_code == 0
assert "Usage:" in output
assert "extraction" in output
assert "curation" in output


def test_invalid_command(runner):
"""Test running the CLI with an invalid command.
Args:
----
runner (CliRunner): The CLI runner fixture.
"""
result = runner.invoke(app, ["invalid_command"])
output = strip_ansi(result.output)
assert result.exit_code != 0
assert "No such command" in output
73 changes: 73 additions & 0 deletions tests/osc_transformer_presteps/test_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""Module to test the settings.py."""

import pytest
import logging
from osc_transformer_presteps.settings import ExtractionSettings

_log_dict = {
"critical": logging.CRITICAL,
"error": logging.ERROR,
"warning": logging.WARNING,
"info": logging.INFO,
"debug": logging.DEBUG,
"notset": logging.NOTSET,
}


@pytest.fixture
def default_server_settings():
"""Fixture that provides default server settings for testing.
Returns
-------
dict: A dictionary containing default server settings.
"""
return {
"port": 8000,
"host": "localhost",
"log_type": 20,
"log_level": "info",
}


def test_default_extraction_settings():
"""Test the default values of ExtractionSettings.
Ensures that the default values for `skip_extracted_files` and `store_to_file` are set correctly.
"""
settings = ExtractionSettings()
assert not settings.skip_extracted_files
assert settings.store_to_file
assert not settings.protected_extraction


@pytest.mark.parametrize(
"skip_extracted_files, store_to_file, protected_extraction",
[(True, False, True), (False, False, False), (True, True, False)],
)
def test_extraction_settings_variations(
skip_extracted_files, store_to_file, protected_extraction
):
"""Test different variations of ExtractionSettings.
Parameters
----------
skip_extracted_files : bool
Whether to skip extracted files.
store_to_file : bool
Whether to store the results to a file.
protected_extraction : bool
Whether to allow extraction of protected PDFs.
Ensures that the settings are correctly applied based on the parameters.
"""
settings = ExtractionSettings(
skip_extracted_files=skip_extracted_files,
store_to_file=store_to_file,
protected_extraction=protected_extraction,
)
assert settings.skip_extracted_files == skip_extracted_files
assert settings.store_to_file == store_to_file
assert settings.protected_extraction == protected_extraction
7 changes: 0 additions & 7 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ setenv =
TOXINIDIR = {toxinidir}
passenv =
HOME
SETUPTOOLS_*
extras =
test
commands =
Expand All @@ -34,7 +33,6 @@ setenv =
TOXINIDIR = {toxinidir}
passenv =
HOME
SETUPTOOLS_*
extras =
test

Expand All @@ -57,13 +55,10 @@ commands =
description =
build: Build the package in isolation according to PEP517, see https://github.com/pypa/build
clean: Remove old distribution files and temporary build artifacts (./build and ./dist)
# https://setuptools.pypa.io/en/stable/build_meta.html#how-to-use-it
skip_install = True
changedir = {toxinidir}
deps =
build: build[virtualenv]
passenv =
SETUPTOOLS_*
commands =
clean: python -c 'import shutil; [shutil.rmtree(p, True) for p in ("build", "dist", "docs/_build")]'
clean: python -c 'import pathlib, shutil; [shutil.rmtree(p, True) for p in pathlib.Path("src").glob("*.egg-info")]'
Expand All @@ -77,8 +72,6 @@ description =
docs: Invoke sphinx-build to build the docs
doctests: Invoke sphinx-build to run doctests
linkcheck: Check for broken links in the documentation
passenv =
SETUPTOOLS_*
setenv =
DOCSDIR = {toxinidir}/docs
BUILDDIR = {toxinidir}/docs/_build
Expand Down

0 comments on commit ea51c28

Please sign in to comment.