From 5853654d734b8fa17db1e144263b6886986552c2 Mon Sep 17 00:00:00 2001 From: Ratheesh kumar R Date: Tue, 24 Sep 2024 09:46:04 -0700 Subject: [PATCH] Updated unit test --- python/common/error_middleware.py | 2 +- python/common/tests/record_error/__init__.py | 0 .../tests/record_error/test_record_error.py | 115 ++++++++++++++++++ 3 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 python/common/tests/record_error/__init__.py create mode 100644 python/common/tests/record_error/test_record_error.py diff --git a/python/common/error_middleware.py b/python/common/error_middleware.py index 07413b9ee..cc58e3638 100644 --- a/python/common/error_middleware.py +++ b/python/common/error_middleware.py @@ -72,7 +72,7 @@ def record_error(error_code: ErrorCode, error_details, event_id: int = None, eve error_severity_level_cd=str(error_code.severity), error_status_cd=str(ErrorStatus.NEW), event_id=event_id, - event_type=str(event_type) if event_type else None, # Convert to string if not None + event_type=str(event_type) if event_type else None, ticket_no=ticket_no, req_payload=payload, error_details=error_details, diff --git a/python/common/tests/record_error/__init__.py b/python/common/tests/record_error/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/python/common/tests/record_error/test_record_error.py b/python/common/tests/record_error/test_record_error.py new file mode 100644 index 000000000..e9fe15964 --- /dev/null +++ b/python/common/tests/record_error/test_record_error.py @@ -0,0 +1,115 @@ +import datetime +import json +import pytest +from flask_api import FlaskAPI + +from python.common.error_middleware import record_error, get_safe_payload, get_function_info +from python.common.models import db, DFErrors +from python.form_handler.config import Config +from python.common.enums import ErrorCode, ErrorStatus, ErrorCategory, ErrorSeverity, EventType + +application = FlaskAPI(__name__) +application.config['SQLALCHEMY_DATABASE_URI'] = Config.DATABASE_URI +db.init_app(application) + +@pytest.fixture +def error_data(): + return { + 'error_code': ErrorCode.G00, + 'error_details': 'Test error details', + 'event_id': 123, + 'event_type': EventType.TWELVE_HOUR, + } + +@pytest.fixture +def app(): + return application + +@pytest.fixture +def client(app): + return app.test_client() + +def test_record_error_success(app, error_data): + with app.app_context(): + record_error( + error_data['error_code'], + error_data['error_details'], + error_data['event_id'], + error_data['event_type'] + ) + + error = DFErrors.query.first() + assert error is not None + assert error.error_cd == error_data['error_code'].code + assert error.error_cd_desc == error_data['error_code'].description + assert error.error_category_cd == str(error_data['error_code'].category) + assert error.error_severity_level_cd == str(error_data['error_code'].severity) + assert error.error_status_cd == str(ErrorStatus.NEW) + assert error.event_id == error_data['event_id'] + assert error.event_type == str(error_data['event_type']) + assert error.error_details == error_data['error_details'] + + # Clear the database after the test + db.session.query(DFErrors).delete() + db.session.commit() + +@pytest.mark.parametrize("error_code,event_id,event_type", [ + (ErrorCode.E01, 456, EventType.IRP), + (ErrorCode.F01, None, None), + (ErrorCode.G00, 789, EventType.VI), +]) +def test_record_error_different_scenarios(app, error_code, event_id, event_type): + with app.app_context(): + record_error(error_code, "Test error", event_id, event_type) + + error = DFErrors.query.first() + assert error is not None + assert error.error_cd == error_code.code + assert error.event_id == event_id + assert error.event_type == (str(event_type) if event_type else None) + + # Clear the database after each test + db.session.query(DFErrors).delete() + db.session.commit() + +def test_get_safe_payload_json(client): + with client.application.test_request_context(json={"key": "value"}): + payload = get_safe_payload() + assert payload == '{"key": "value"}' + +def test_get_safe_payload_form(client): + with client.application.test_request_context(data={"key": "value"}): + payload = get_safe_payload() + assert payload == '{"key": "value"}' + +def test_get_safe_payload_query(client): + with client.application.test_request_context("/?key=value"): + payload = get_safe_payload() + assert payload == '{"key": "value"}' + +def test_get_safe_payload_empty(app): + with app.test_request_context(): + payload = get_safe_payload() + assert payload == '{"message": "No payload found in request"}' + +def test_get_function_info(): + def test_function(): + pass + + function_info = get_function_info(test_function) + assert "test_function" in function_info + +def test_record_error_with_function_info(app): + def test_function(): + pass + + with app.app_context(): + record_error(ErrorCode.G00, "Test error", func=test_function) + + error = DFErrors.query.first() + assert error is not None + assert "test_function" in error.error_path + + # Clear the database after the test + db.session.query(DFErrors).delete() + db.session.commit() \ No newline at end of file