python
12 hours, 22 minutes ago
import unittest
from unittest.mock import Mock, patch
from starlette.responses import JSONResponse
from your_module import IntentCallidCountReport # Replace 'your_module' with the actual module name
class TestIntentCallidCountReport(unittest.TestCase):
def setUp(self):
# Mocking services and configuration
self.mock_oracle_service = Mock()
self.mock_config_service = Mock()
self.mock_config = {}
# Instantiate IntentCallidCountReport with mocked services
self.report_service = IntentCallidCountReport(self.mock_config, self.mock_oracle_service, self.mock_config_service)
def test_executor_with_caller_type(self):
# Test case where `caller_type` is provided
mock_query_result = [
['LOB1', 'ClientA', 'TypeA', 100, 25, 20, 15, 10, 5, 25, 25.0, 20.0, 15.0, 10.0, 5.0, 25.0]
]
# Configuring mocks
self.mock_config_service.get_config_for_process.return_value = {
'INTENT_CALLID_COUNT_1': "SELECT * FROM DUMMY_TABLE WHERE LOB='{0}' AND CLIENT_CODE='{1}' AND CALLER_TYPE='{2}' AND DATE BETWEEN '{3}' AND '{4}'"
}
self.mock_oracle_service.execute_select_query_non_string.return_value = mock_query_result
# Run method
response = self.report_service.executor('LOB1', 'ClientA', 'TypeA', '2024-01-01', '2024-12-31', 'trace123')
# Assertions
self.assertIsInstance(response, JSONResponse)
self.mock_config_service.get_config_for_process.assert_called_once_with(
namespace='CALL_CENTER', process='COUNT', config_type='QUERY', sub_process=None, lookup_key='INTENT_CALLID_COUNT_1'
)
self.mock_oracle_service.execute_select_query_non_string.assert_called_once()
response_data = response.body.decode('utf-8')
assert 'Success' in response_data
def test_executor_without_caller_type(self):
# Test case where `caller_type` is None
mock_query_result = [
['LOB1', 'ClientB', None, 200, 50, 40, 30, 20, 10, 50, 25.0, 20.0, 15.0, 10.0, 5.0, 25.0]
]
# Configuring mocks
self.mock_config_service.get_config_for_process.return_value = {
'INTENT_CALLID_COUNT_2': "SELECT * FROM DUMMY_TABLE WHERE LOB='{0}' AND CLIENT_CODE='{1}' AND DATE BETWEEN '{2}' AND '{3}'"
}
self.mock_oracle_service.execute_select_query_non_string.return_value = mock_query_result
# Run method
response = self.report_service.executor('LOB1', 'ClientB', None, '2024-01-01', '2024-12-31', 'trace123')
# Assertions
self.assertIsInstance(response, JSONResponse)
self.mock_config_service.get_config_for_process.assert_called_once_with(
namespace='CALL_CENTER', process='COUNT', config_type='QUERY', sub_process=None, lookup_key='INTENT_CALLID_COUNT_2'
)
self.mock_oracle_service.execute_select_query_non_string.assert_called_once()
response_data = response.body.decode('utf-8')
assert 'Success' in response_data
def test_executor_query_failure(self):
# Test case where Oracle query fails
self.mock_config_service.get_config_for_process.return_value = {
'INTENT_CALLID_COUNT_1': "SELECT * FROM DUMMY_TABLE WHERE LOB='{0}' AND CLIENT_CODE='{1}' AND CALLER_TYPE='{2}' AND DATE BETWEEN '{3}' AND '{4}'"
}
self.mock_oracle_service.execute_select_query_non_string.side_effect = Exception("Database error")
with self.assertRaises(Exception) as context:
self.report_service.executor('LOB1', 'ClientA', 'TypeA', '2024-01-01', '2024-12-31', 'trace123')
self.assertTrue("Database error" in str(context.exception))
def test_executor_empty_result(self):
# Test case where Oracle returns no data
self.mock_config_service.get_config_for_process.return_value = {
'INTENT_CALLID_COUNT_2': "SELECT * FROM DUMMY_TABLE WHERE LOB='{0}' AND CLIENT_CODE='{1}' AND DATE BETWEEN '{2}' AND '{3}'"
}
self.mock_oracle_service.execute_select_query_non_string.return_value = []
response = self.report_service.executor('LOB1', 'ClientB', None, '2024-01-01', '2024-12-31', 'trace123')
self.assertIsInstance(response, JSONResponse)
response_data = response.body.decode('utf-8')
assert 'DATA' in response_data # Expecting empty data in response
if __name__ == '__main__':
unittest.main()
0 Comments
Please Login to Comment Here