python
1 week ago
# test_alias_service.py
import unittest
from unittest.mock import Mock, patch, MagicMock
from alias_service import AliasService
import pandas as pd
from fastapi.responses import StreamingResponse
import io
class TestAliasService(unittest.TestCase):
def setUp(self):
# Mock dependencies
self.oracle_service = Mock()
self.auth_util = Mock()
self.alias_service = AliasService(self.oracle_service, self.auth_util)
def test_validate_excel_valid(self):
# Test when the Excel sheet has the required columns and no nulls
data = {
"INTENT": ["Intent1"],
"INTENT_CATEGORY_1": ["Category1"],
"INTENT_CATEGORY_2": ["Category2"],
"INTENT_CATEGORY_3": ["Category3"],
"CALLER_TYPE": ["Type1"],
"CLIENT_CODE": ["Code1"],
"LOB": ["LOB1"],
"SYSTEM_GEN_CATEGORY_1": ["SysCat1"],
"SYSTEM_GEN_CATEGORY_2": ["SysCat2"],
"SYSTEM_GEN_CATEGORY_3": ["SysCat3"]
}
df = pd.DataFrame(data)
result = self.alias_service.validate_excel(df)
self.assertTrue(result)
def test_validate_excel_missing_columns(self):
# Test when the Excel sheet is missing required columns
data = {"INTENT": ["Intent1"]}
df = pd.DataFrame(data)
result = self.alias_service.validate_excel(df)
self.assertFalse(result)
def test_validate_excel_null_values(self):
# Test when the Excel sheet has null values
data = {
"INTENT": ["Intent1", None],
"INTENT_CATEGORY_1": ["Category1", "Category2"],
"INTENT_CATEGORY_2": ["Category2", "Category2"],
"INTENT_CATEGORY_3": ["Category3", None],
"CALLER_TYPE": ["Type1", "Type2"],
"CLIENT_CODE": ["Code1", None],
"LOB": ["LOB1", "LOB1"],
"SYSTEM_GEN_CATEGORY_1": ["SysCat1", "SysCat1"],
"SYSTEM_GEN_CATEGORY_2": ["SysCat2", "SysCat2"],
"SYSTEM_GEN_CATEGORY_3": ["SysCat3", None]
}
df = pd.DataFrame(data)
result = self.alias_service.validate_excel(df)
self.assertFalse(result)
@patch('pandas.read_excel')
def test_alias_intent_successful(self, mock_read_excel):
# Mock data and methods for alias_intent method
mock_file = Mock()
mock_file.filename = "test.xlsx"
mock_file.file.read = MagicMock(return_value=b"mock content")
# Simulate valid Excel data
mock_df = pd.DataFrame({
"INTENT": ["Intent1"],
"INTENT_CATEGORY_1": ["Category1"],
"INTENT_CATEGORY_2": ["Category2"],
"INTENT_CATEGORY_3": ["Category3"],
"CALLER_TYPE": ["Type1"],
"CLIENT_CODE": ["Code1"],
"LOB": ["LOB1"],
"SYSTEM_GEN_CATEGORY_1": ["SysCat1"],
"SYSTEM_GEN_CATEGORY_2": ["SysCat2"],
"SYSTEM_GEN_CATEGORY_3": ["SysCat3"]
})
mock_read_excel.return_value = mock_df
self.auth_util.get_user_email.return_value = "test@example.com"
# Execute the alias_intent method
result = self.alias_service.alias_intent(mock_file, "mock_token")
# Assert results and method calls
self.assertEqual(result["Status"], "Successfully finished aliasing test.xlsx")
self.oracle_service.execute_query.assert_called() # Check if database queries were made
def test_get_category_spreadsheet_valid(self):
# Test for successful spreadsheet generation
self.oracle_service.execute_select_query.return_value = '[{"INTENT": "Intent1", "INTENT_CATEGORY_1": "Category1", "INTENT_CATEGORY_2": "Category2", "INTENT_CATEGORY_3": "Category3", "CALLER_TYPE": "Type1", "CLIENT_CODE": "Code1", "LOB": "LOB1", "SYSTEM_GEN_CATEGORY_1": "SysCat1", "SYSTEM_GEN_CATEGORY_2": "SysCat2", "SYSTEM_GEN_CATEGORY_3": "SysCat3"}]'
result = self.alias_service.get_category_spreadsheet("LOB1", "Code1", "intent")
self.assertIsInstance(result, StreamingResponse)
self.assertIn("attachment; filename=", result.headers["Content-Disposition"])
def test_get_category_spreadsheet_invalid_category_type(self):
# Test when an invalid category type is passed
result = self.alias_service.get_category_spreadsheet("LOB1", "Code1", "invalid_type")
self.assertEqual(result["Status"], "Invalid category type. Must be either intent action or reason")
0 Comments
Please Login to Comment Here