Skip to content

Commit

Permalink
feat: add test based in use_audit_backend
Browse files Browse the repository at this point in the history
  • Loading branch information
johanseto committed Jul 12, 2024
1 parent d54e4d4 commit 54d0888
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion eox_nelp/pearson_vue/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def wrapper(self, *args, **kwargs):

@audit_method(action=f"Backend Execution: {backend_name}")
@rename_function(name=f"audit_backend_{camel_to_snake(backend_name)}")
def audit_backend_manager(backend_data, **kwargs):
def audit_backend_manager(backend_data, **kwargs): # pylint: disable=unused-argument
logger.info(
"Backend %s executed. \n backend_data: %s",
backend_name,
Expand Down
4 changes: 2 additions & 2 deletions eox_nelp/pearson_vue/rti_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def run_pipeline(self):
try:
result = func(**self.backend_data) or {}
except PearsonBaseError as pearson_error:
self.backend_data["catched_pearson_error"] = True
self.handle_error(pearson_error, func.__name__)
break

Expand Down Expand Up @@ -113,6 +114,7 @@ def handle_error(self, exception, failed_step_pipeline):

class ErrorRealTimeImportHandler(AbstractBackend):
"""Class for managing validation error pipe executing the pipeline for data validation."""
use_audit_backend = False

def handle_error(self, exception, failed_step_pipeline):
"""
Expand Down Expand Up @@ -143,8 +145,6 @@ class RealTimeImport(AbstractBackend):
run_pipeline(): Executes the RTI pipeline by iterating through the pipeline functions.
get_pipeline(): Returns the RTI pipeline, which is a list of functions to be executed.
"""
use_audit_backend = False

def handle_error(self, exception, failed_step_pipeline):
"""
Handles errors during pipeline execution.
Expand Down
23 changes: 20 additions & 3 deletions eox_nelp/pearson_vue/tests/test_rti_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from ddt import data, ddt

from eox_nelp.pearson_vue import decorators
from eox_nelp.pearson_vue.exceptions import PearsonAttributeError, PearsonKeyError, PearsonValidationError
from eox_nelp.pearson_vue.rti_backend import (
CandidateDemographicsDataImport,
Expand Down Expand Up @@ -46,13 +47,25 @@ def test_run_pipeline(self):
- Pipeline method 1 is called with the original data.
- Pipeline method 2 is called with updated data.
- backend_data attribute is the expected value.
- assert logs if the class has truthy `use_audit_backend`
"""
# Mock pipeline functions
func1 = MagicMock(return_value={"updated_key": "value1"})
func2 = MagicMock(return_value={"additional_key": "value2"})
self.rti.get_pipeline = MagicMock(return_value=[func1, func2])

self.rti.run_pipeline()
if self.rti.use_audit_backend:
with self.assertLogs(decorators.__name__, level="INFO") as logs:
self.rti.run_pipeline()
self.assertEqual(
logs.output,
[
f"INFO:{decorators.__name__}:"
f"Backend {self.rti.__class__.__name__} executed. \n backend_data: {self.rti.backend_data}"
]
)
else:
self.rti.run_pipeline()

func1.assert_called_once_with(**self.backend_data)
func2.assert_called_once_with(**{"updated_key": "value1", "pipeline_index": 1})
Expand Down Expand Up @@ -165,13 +178,14 @@ class TestRealTimeImport(TestAbstractBackendMixin, unittest.TestCase):
"""
rti_backend_class = RealTimeImport

@patch("eox_nelp.pearson_vue.decorators.logger")
@patch("eox_nelp.pearson_vue.tasks.rti_error_handler_task")
@data(
PearsonValidationError(inspect.currentframe(), "error: ['String to short.']"),
PearsonKeyError(inspect.currentframe(), "eligibility_appt_date_first"),
PearsonAttributeError(inspect.currentframe(), "Settings' object has no attribute PERITA")
)
@patch("eox_nelp.pearson_vue.tasks.rti_error_handler_task")
def test_launch_validation_error_pipeline(self, pearson_error, rti_error_handler_task_mock):
def test_launch_validation_error_pipeline(self, pearson_error, rti_error_handler_task_mock, audit_logger):
"""
Test the execution of the RTI finished after the second function call due
`launch_validation_error_pipeline` kwarg.
Expand All @@ -184,6 +198,7 @@ def test_launch_validation_error_pipeline(self, pearson_error, rti_error_handler
- backend_data attribute is the expected value.
Without func3,func4 data and pipeline index in the last.
- rti_error_handler_task is called with executed__pipeline_kwargs and error_validation_kwargs.
- audit_method_mock is not called due catched_pearson_error.
"""
# Mock pipeline functions
func1 = MagicMock(return_value={"updated_key": "value1"})
Expand All @@ -208,6 +223,7 @@ def test_launch_validation_error_pipeline(self, pearson_error, rti_error_handler
self.rti.backend_data,
{
"pipeline_index": 1, # includes the pipe executed until break due exception
'catched_pearson_error': True, # pipeline error flag
**func1(), # Include data from func1 ()
},
)
Expand All @@ -217,6 +233,7 @@ def test_launch_validation_error_pipeline(self, pearson_error, rti_error_handler
user_id=None,
course_id=None,
)
audit_logger.info.assert_not_called()


class TestExamAuthorizationDataImport(TestRealTimeImport):
Expand Down

0 comments on commit 54d0888

Please sign in to comment.