-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added test_cli.py and test_settings.py (#114)
* 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
1 parent
81a9733
commit ea51c28
Showing
4 changed files
with
186 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters