Skip to content

Commit

Permalink
code quality tools to cover test code (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
bailed22 authored Apr 22, 2024
1 parent 537de9a commit c134415
Show file tree
Hide file tree
Showing 11 changed files with 341 additions and 192 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/code_quality.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ jobs:
run: poetry install

- name: Check black has been ran (make fmt)
run: poetry run black dpypelines --diff
run: poetry run black dpypelines tests --diff

- name: Check isort has been ran (make fmt)
run: poetry run isort dpypelines --diff --check-only
run: poetry run isort dpypelines tests --diff --check-only

- name: Check ruff has been ran (make lint)
run: poetry run ruff check ./dpypelines/*
run: poetry run ruff check dpypelines tests
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ install: ## Install development dependencies
poetry install

fmt: install ## (Format) - runs black and isort against the codebase (auto triggered on pre-commit)
poetry run black ./dpypelines/*
poetry run isort ./dpypelines/*
poetry run black ./dpypelines/* ./tests/*
poetry run isort ./dpypelines/* ./tests/*

lint: install ## Run the ruff python linter
poetry run ruff check ./dpypelines/*
poetry run ruff check ./dpypelines/* ./tests/*

test: install ## Run pytest and check test coverage
poetry run pytest --cov-report term-missing --cov=dpypelines
Expand Down
2 changes: 1 addition & 1 deletion tests/README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# TODO
# TODO
3 changes: 1 addition & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import os
import sys
from pathlib import Path

from _pytest.monkeypatch import MonkeyPatch

import sys

# Add repo root path for imports
repo_root = Path(__file__).parent.parent
sys.path.append(str(repo_root.absolute()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from dpypelines.pipeline.dataset_ingress_v1 import dataset_ingress_v1
from dpypelines.pipeline.shared.pipelineconfig.matching import (
get_supplementary_distribution_patterns,
get_required_files_patterns,
get_supplementary_distribution_patterns,
)
from dpypelines.pipeline.shared.transforms.sdmx.v1 import (
sdmx_compact_2_0_prototype_1,
Expand Down
169 changes: 110 additions & 59 deletions tests/pipelines/pipeline/shared/test_message.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from dpytools.stores.directory.local import LocalDirectoryStore
import pytest

from dpypelines.pipeline.shared.message import (
unexpected_error,
cant_find_schema,
error_in_transform,
expected_local_file_missing,
invalid_config,
unknown_transform,
metadata_validation_error,
expected_local_file_missing,
pipeline_input_exception,
error_in_transform,
pipeline_input_sanity_check_exception
pipeline_input_sanity_check_exception,
unexpected_error,
unknown_transform,
)


Expand All @@ -29,71 +29,94 @@ def test_cant_find_scheama():
error = Exception("Something went wrong")
config_dict = {
"$schema": "http://json-schema.org/draft-04/schema#",
"$id": "https://raw.githubusercontent.com/ONSdigital/sandbox/initial-structure/schemas/ingress/config/v1.json"
"$id": "https://raw.githubusercontent.com/ONSdigital/sandbox/initial-structure/schemas/ingress/config/v1.json",
}

human_readable_output = cant_find_schema(config_dict, error)

assert 'We got an error when trying to identify the schema for the pipeline-config.json using the pipeline-config.json.' in human_readable_output
assert 'Pipeline-config.json:' in human_readable_output
assert '"$schema": "http://json-schema.org/draft-04/schema#"' in human_readable_output
assert '"$id": "https://raw.githubusercontent.com/ONSdigital/sandbox/initial-structure/schemas/ingress/config/v1.json"' in human_readable_output
assert 'Error type: Exception' in human_readable_output
assert 'Error: Something went wrong' in human_readable_output
assert type(human_readable_output) == str

assert (
"We got an error when trying to identify the schema for the pipeline-config.json using the pipeline-config.json."
in human_readable_output
)
assert "Pipeline-config.json:" in human_readable_output
assert (
'"$schema": "http://json-schema.org/draft-04/schema#"' in human_readable_output
)
assert (
'"$id": "https://raw.githubusercontent.com/ONSdigital/sandbox/initial-structure/schemas/ingress/config/v1.json"'
in human_readable_output
)
assert "Error type: Exception" in human_readable_output
assert "Error: Something went wrong" in human_readable_output
assert isinstance(human_readable_output, str)

def test_invalid_config():
error = Exception("Something went wrong")
config_dict = {
"$schema": "http://json-schema.org/invalid/schema#",
"$id": "https://raw.githubusercontent.com/ONSdigital/sandbox/initial-structure/schemas/ingress/config/v1.json"
"$id": "https://raw.githubusercontent.com/ONSdigital/sandbox/initial-structure/schemas/ingress/config/v1.json",
}
human_readable_output = invalid_config(config_dict, error)

assert "The pipeline config that was provided is failing to validate." in human_readable_output

assert (
"The pipeline config that was provided is failing to validate."
in human_readable_output
)
assert "Pipeline-config.json:" in human_readable_output
assert '"$schema": "http://json-schema.org/invalid/schema#"' in human_readable_output
assert '"$id": "https://raw.githubusercontent.com/ONSdigital/sandbox/initial-structure/schemas/ingress/config/v1.json"' in human_readable_output
assert 'Error type: Exception' in human_readable_output
assert (
'"$schema": "http://json-schema.org/invalid/schema#"' in human_readable_output
)
assert (
'"$id": "https://raw.githubusercontent.com/ONSdigital/sandbox/initial-structure/schemas/ingress/config/v1.json"'
in human_readable_output
)
assert "Error type: Exception" in human_readable_output
assert "Error: Something went wrong" in human_readable_output
assert type(human_readable_output) == str
assert isinstance(human_readable_output, str)


def test_unknown_transform():

transform_name = "sdmx.default"
all_transform_options = {
"pipeline idemtifier": "pipeline123"
}
all_transform_options = {"pipeline idemtifier": "pipeline123"}

human_readable_output = unknown_transform(transform_name, all_transform_options)

assert 'Pipeline name is missing from the pipeline transform configurations.' in human_readable_output
assert 'Pipeline: sdmx.default' in human_readable_output
assert 'Pipeline Configurations:' in human_readable_output
assert (
"Pipeline name is missing from the pipeline transform configurations."
in human_readable_output
)
assert "Pipeline: sdmx.default" in human_readable_output
assert "Pipeline Configurations:" in human_readable_output
assert '"pipeline idemtifier": "pipeline123"' in human_readable_output
assert type(human_readable_output) == str
assert isinstance(human_readable_output, str)


def test_metadata_validation_error():
error = Exception("Something went wrong")
human_readable_output = metadata_validation_error("/some_parent_dir/some_dir/metadata.json", error)
human_readable_output = metadata_validation_error(
"/some_parent_dir/some_dir/metadata.json", error
)
expected_output = """
Metadata json file has failed validation: /some_parent_dir/some_dir/metadata.json
Error type: Exception
Error: Something went wrong
"""

assert human_readable_output == expected_output
assert type(human_readable_output) == str
assert isinstance(human_readable_output, str)


def test_expected_local_file_missing():
store_path = "tests/fixtures/test-cases/message_test_directory"
test_input_store = LocalDirectoryStore(store_path)

human_readable_output = expected_local_file_missing("Something went wrong", "/some_parent_dir/some_dir/some_json.json", "Pipeline123", test_input_store)

human_readable_output = expected_local_file_missing(
"Something went wrong",
"/some_parent_dir/some_dir/some_json.json",
"Pipeline123",
test_input_store,
)
expected_output = """
A pipeline has encountered an issue finding a local file.
Expand All @@ -104,79 +127,107 @@ def test_expected_local_file_missing():
test_local_file.json
"""
assert human_readable_output == expected_output
assert type(human_readable_output) == str
assert isinstance(human_readable_output, str)


def test_pipeline_input_exception():
"""
Checks that the error message for a pipeline input exception contains
Checks that the error message for a pipeline input exception contains
the expected message text and details.
"""
store_path = "tests/fixtures/test-cases/message_test_directory"
test_input_store = LocalDirectoryStore(store_path)

pipeline_dict = {
"$schema": "http://json-schema.org/draft-04/schema#",
"$id": "https://raw.githubusercontent.com/ONSdigital/sandbox/initial-structure/schemas/ingress/config/v1.json"
"$id": "https://raw.githubusercontent.com/ONSdigital/sandbox/initial-structure/schemas/ingress/config/v1.json",
}

error = Exception("Something went wrong during the pipeline input process")
human_readable_output = pipeline_input_exception(pipeline_dict, test_input_store, error)
human_readable_output = pipeline_input_exception(
pipeline_dict, test_input_store, error
)

assert """File specified in pipeline details could not be retrieved from store: tests/fixtures/test-cases/message_test_directory
assert (
"""File specified in pipeline details could not be retrieved from store: tests/fixtures/test-cases/message_test_directory
Error type: Exception
Error: Something went wrong during the pipeline input process""" in human_readable_output
assert '"$schema": "http://json-schema.org/draft-04/schema#"' in human_readable_output
assert '"$id": "https://raw.githubusercontent.com/ONSdigital/sandbox/initial-structure/schemas/ingress/config/v1.json"' in human_readable_output
Error: Something went wrong during the pipeline input process"""
in human_readable_output
)
assert (
'"$schema": "http://json-schema.org/draft-04/schema#"' in human_readable_output
)
assert (
'"$id": "https://raw.githubusercontent.com/ONSdigital/sandbox/initial-structure/schemas/ingress/config/v1.json"'
in human_readable_output
)

assert type(human_readable_output) == str
assert isinstance(human_readable_output, str)


def test_error_in_transform():
"""
Checks that the error message for an error in the transformation process
Checks that the error message for an error in the transformation process
contains the expected message text and details.
"""
store_path = "tests/fixtures/test-cases/message_test_directory"
test_input_store = LocalDirectoryStore(store_path)

pipeline_dict = {
"$schema": "http://json-schema.org/draft-04/schema#",
"$id": "https://raw.githubusercontent.com/ONSdigital/sandbox/initial-structure/schemas/ingress/config/v1.json"
"$id": "https://raw.githubusercontent.com/ONSdigital/sandbox/initial-structure/schemas/ingress/config/v1.json",
}

error = Exception("Something went wrong during the transformation process")
human_readable_output = error_in_transform(pipeline_dict, test_input_store, error)

assert """An error occured during the transformation process for pipeline input with store: tests/fixtures/test-cases/message_test_directory
assert (
"""An error occured during the transformation process for pipeline input with store: tests/fixtures/test-cases/message_test_directory
Error type: Exception
Error: Something went wrong during the transformation process""" in human_readable_output
assert '"$schema": "http://json-schema.org/draft-04/schema#"' in human_readable_output
assert '"$id": "https://raw.githubusercontent.com/ONSdigital/sandbox/initial-structure/schemas/ingress/config/v1.json"' in human_readable_output
Error: Something went wrong during the transformation process"""
in human_readable_output
)
assert (
'"$schema": "http://json-schema.org/draft-04/schema#"' in human_readable_output
)
assert (
'"$id": "https://raw.githubusercontent.com/ONSdigital/sandbox/initial-structure/schemas/ingress/config/v1.json"'
in human_readable_output
)

assert type(human_readable_output) == str
assert isinstance(human_readable_output, str)


def test_pipeline_input_sanity_check_exception():
"""
Checks that the error message for an error during the sanity check of the
Checks that the error message for an error during the sanity check of the
input contains the expected message text and details.
"""
store_path = "tests/fixtures/test-cases/message_test_directory"
test_input_store = LocalDirectoryStore(store_path)

pipeline_dict = {
"$schema": "http://json-schema.org/draft-04/schema#",
"$id": "https://raw.githubusercontent.com/ONSdigital/sandbox/initial-structure/schemas/ingress/config/v1.json"
"$id": "https://raw.githubusercontent.com/ONSdigital/sandbox/initial-structure/schemas/ingress/config/v1.json",
}

error = Exception("Something went wrong during the pipeline input sanity check")
human_readable_output = pipeline_input_sanity_check_exception(pipeline_dict, test_input_store, error)
human_readable_output = pipeline_input_sanity_check_exception(
pipeline_dict, test_input_store, error
)

assert """An error was raised while performing a sanity check on pipeline with given store: tests/fixtures/test-cases/message_test_directory
assert (
"""An error was raised while performing a sanity check on pipeline with given store: tests/fixtures/test-cases/message_test_directory
Error type: Exception
Error: Something went wrong during the pipeline input sanity check""" in human_readable_output
assert '"$schema": "http://json-schema.org/draft-04/schema#"' in human_readable_output
assert '"$id": "https://raw.githubusercontent.com/ONSdigital/sandbox/initial-structure/schemas/ingress/config/v1.json"' in human_readable_output

assert type(human_readable_output) == str
Error: Something went wrong during the pipeline input sanity check"""
in human_readable_output
)
assert (
'"$schema": "http://json-schema.org/draft-04/schema#"' in human_readable_output
)
assert (
'"$id": "https://raw.githubusercontent.com/ONSdigital/sandbox/initial-structure/schemas/ingress/config/v1.json"'
in human_readable_output
)

assert isinstance(human_readable_output, str)
9 changes: 5 additions & 4 deletions tests/pipelines/pipeline/shared/test_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
from _pytest.monkeypatch import MonkeyPatch

from dpypelines.pipeline.shared.notification import (
PipelineNotifier,
NopNotifier,
notifier_from_env_var_webhook
PipelineNotifier,
notifier_from_env_var_webhook,
)


def test_notification_constructor():
"""
Test that the PipelineNotifier can be successfully created from
Expand Down Expand Up @@ -74,7 +75,7 @@ def test_notification_custom_postfix_success():
notifier.client.msg_str.assert_called_once_with(f":white_check_mark: {postfix_str}")


def test_notification_custom_postfix_success():
def test_notification_custom_postfix_failure():
"""
Test that we can add a custom postfix to the notification message
for a failure.
Expand All @@ -89,4 +90,4 @@ def test_notification_custom_postfix_success():
notifier.client = MagicMock()
notifier.failure()

notifier.client.msg_str.assert_called_once_with(f":boom: {postfix_str}")
notifier.client.msg_str.assert_called_once_with(f":boom: {postfix_str}")
7 changes: 5 additions & 2 deletions tests/pipelines/pipeline/shared/test_utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ def test_str_to_bool_valid_raises_for_invlaid_str_value():
with pytest.raises(ValueError) as err:
str_to_bool("foo")

assert "A str value representing a boolean should be one of 'True', 'true', 'False', 'false'" in str(err)
assert (
"A str value representing a boolean should be one of 'True', 'true', 'False', 'false'"
in str(err)
)


def test_str_to_bool_raises_for_not_string_argument():
Expand All @@ -40,4 +43,4 @@ def test_str_to_bool_raises_for_not_string_argument():
with pytest.raises(AssertionError) as err:
str_to_bool(invalid_type)

assert "Function str_to_bool only accepts strings" in str(err)
assert "Function str_to_bool only accepts strings" in str(err)
Loading

0 comments on commit c134415

Please sign in to comment.