Skip to content

Commit

Permalink
243 update in line with comments
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianMontaguSmartDCSIT committed Oct 25, 2024
1 parent 657dd3f commit 4eaa6a6
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 22 deletions.
8 changes: 1 addition & 7 deletions tel2puml/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@
from tel2puml.otel_to_pv.data_sources.json_data_source.json_jq_converter \
import JQCompileError, JQExtractionError

EXCEPTION_CLASSES = (
ValidationError,
JSONDecodeError,
JQCompileError,
JQExtractionError,
)

ERROR_MESSAGES = {
ValidationError: "Input validation failed. Please check the input data.",
Expand Down Expand Up @@ -350,7 +344,7 @@ def main(
args_dict["command"],
)
except tuple(errors_lookup.keys()) as e:
error_message = errors_lookup.get(type(e), str(e))
error_message = errors_lookup.get(type(e))
handle_exception(
e, debug, user_error=True, custom_message=error_message
)
Expand Down
5 changes: 3 additions & 2 deletions tel2puml/pv_event_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,10 @@ def transform_dict_into_pv_event(
timestamp=pv_event_model.timestamp,
applicationName=pv_event_model.applicationName,
jobName=pv_event_model.jobName,
previousEventIds=pv_event_model.previousEventIds,
)
if mapping_config.previousEventIds in pv_dict:
pv_event["previousEventIds"] = pv_event_model.previousEventIds
# if mapping_config.previousEventIds in pv_dict:
# pv_event["previousEventIds"] = pv_event_model.previousEventIds
return pv_event


Expand Down
13 changes: 6 additions & 7 deletions tel2puml/tel2puml_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,15 @@ class PVEventModel(BaseModel):
jobName: str
eventType: str

@model_validator(mode="before")
@field_validator("previousEventIds", mode="before")
def check_prev_event_ids(
cls: Type["PVEventModel"], values: dict[str, Any]
) -> dict[str, Any]:
cls: Type["PVEventModel"], value: Any
) -> Any:
"""Check if previousEventIds is a string and convert it to a list
of strings."""
previous_event_ids = values.get("previousEventIds")
if previous_event_ids and isinstance(previous_event_ids, str):
values["previousEventIds"] = [previous_event_ids]
return values
if value and isinstance(value, str):
return [value]
return value


class NestedEvent(TypedDict):
Expand Down
73 changes: 67 additions & 6 deletions tests/tel2puml/main/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pathlib import Path
from typing import Literal
from unittest.mock import Mock, patch
from json import JSONDecodeError

import pytest
from pydantic import ValidationError
Expand All @@ -18,8 +19,10 @@
handle_exception,
main,
)
from tel2puml.otel_to_pv.data_sources.json_data_source.json_jq_converter \
import JQCompileError, JQExtractionError
from tel2puml.otel_to_pv.data_sources.json_data_source.json_jq_converter import (
JQCompileError,
JQExtractionError,
)


def test_generate_config(tmp_path: Path) -> None:
Expand Down Expand Up @@ -223,12 +226,10 @@ def test_handle_exception(
assert (
"ERROR: Use the -d flag for more detailed information." in captured.out
)
assert (
"Please contact smartDCSIT support for assistance." in captured.out
)
assert "Please contact smartDCSIT support for assistance." in captured.out


def test_main(
def test_main_error_handling(
monkeypatch: MonkeyPatch,
capfd: CaptureFixture[str],
) -> None:
Expand All @@ -247,6 +248,10 @@ def test_main(
errors_lookup = {
JQCompileError: "Error occurred during JQ compiling.",
JQExtractionError: "Error occurred during JQ extraction.",
JSONDecodeError: "Invalid JSON format detected. Please check your"
" JSON files.",
ValidationError: "Input validation failed. Please check the input"
" data."
}

# Test 1: JQCompileError
Expand Down Expand Up @@ -280,3 +285,59 @@ def test_main(
assert (
"User error: Error occurred during JQ extraction."
) in captured.out

# Test 3: JSONDecodeError
with patch(
"tel2puml.__main__.generate_component_options"
) as mock_generate_options:
mock_generate_options.side_effect = JSONDecodeError(
"JSONDecodeError", "", 0
)

main(args_dict, errors_lookup)
captured = capfd.readouterr()
# Check if the correct error message was printed
assert (
"\nERROR: Use the -d flag for more detailed information."
) in captured.out
assert (
"User error: Invalid JSON format detected. Please check your"
" JSON files."
) in captured.out

# Test 4: ValidationError
with patch(
"tel2puml.__main__.generate_component_options"
) as mock_generate_options:
# Written like this as ValidationError in pydantic does not have a
# constructor
mock_generate_options.side_effect = (
ValidationError.from_exception_data("Invalid data", line_errors=[])
)

main(args_dict, errors_lookup)
captured = capfd.readouterr()
# Check if the correct error message was printed
assert (
"\nERROR: Use the -d flag for more detailed information."
) in captured.out
assert (
"User error: Input validation failed. Please check the input"
" data."
) in captured.out

# Test 5: Unexpected Exception
with patch(
"tel2puml.__main__.generate_component_options"
) as mock_generate_options:
mock_generate_options.side_effect = Exception("Unexpected error")

main(args_dict, errors_lookup)
captured = capfd.readouterr()
# Check if the correct error message was printed
assert (
"\nERROR: Use the -d flag for more detailed information."
) in captured.out
assert (
"Please contact smartDCSIT support for assistance."
) in captured.out
1 change: 1 addition & 0 deletions tests/tel2puml/pv_to_puml/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def mock_job_json_file() -> list[dict[str, Any]]:
"timestamp": "2024-09-01T07:45:00Z",
"applicationName": "BackupService",
"jobName": "TempFilesCleanup",
"previousEventIds": [],
},
{
"eventId": "evt_002",
Expand Down

0 comments on commit 4eaa6a6

Please sign in to comment.