Spaces:
Sleeping
Sleeping
File size: 9,699 Bytes
0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 96cc367 0cc7766 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
"""
Test Suite for Unity Catalog Chatbot
"""
import pytest
from unittest.mock import Mock, patch
from unity_catalog_service import UnityCatalogService
from app import parse_with_claude, execute_intent
class TestUnityCatalogService:
"""Tests for Unity Catalog Service"""
def test_parse_object_path_catalog(self, uc_service):
"""Test parsing catalog path"""
result = uc_service.parse_object_path("my_catalog")
assert result == {'catalog': 'my_catalog'}
def test_parse_object_path_schema(self, uc_service):
"""Test parsing schema path"""
result = uc_service.parse_object_path("my_catalog.my_schema")
assert result == {
'catalog': 'my_catalog',
'schema': 'my_schema'
}
def test_parse_object_path_table(self, uc_service):
"""Test parsing table path"""
result = uc_service.parse_object_path("my_catalog.my_schema.my_table")
assert result == {
'catalog': 'my_catalog',
'schema': 'my_schema',
'table': 'my_table'
}
def test_create_catalog_success(self, uc_service):
"""Test successful catalog creation"""
mock_catalog = Mock()
mock_catalog.name = "test_catalog"
mock_catalog.owner = "test_user"
mock_catalog.created_at = "2025-01-01T00:00:00Z"
uc_service.client.catalogs.create = Mock(return_value=mock_catalog)
result = uc_service.create_catalog("test_catalog")
assert result['success'] is True
assert result['catalog']['name'] == "test_catalog"
assert "CREATE CATALOG" in result['sql']
def test_create_catalog_error(self, uc_service):
"""Test catalog creation error handling"""
uc_service.client.catalogs.create = Mock(
side_effect=Exception("Permission denied")
)
result = uc_service.create_catalog("test_catalog")
assert result['success'] is False
assert "Permission denied" in result['message']
def test_create_schema_success(self, uc_service):
"""Test successful schema creation"""
mock_schema = Mock()
mock_schema.name = "test_schema"
mock_schema.catalog_name = "test_catalog"
mock_schema.owner = "test_user"
uc_service.client.schemas.create = Mock(return_value=mock_schema)
result = uc_service.create_schema("test_catalog", "test_schema")
assert result['success'] is True
assert "CREATE SCHEMA" in result['sql']
def test_validate_name_valid(self, uc_service):
"""Test name validation with valid names"""
assert uc_service.validate_name("valid_name") is True
assert uc_service.validate_name("ValidName123") is True
def test_validate_name_invalid(self, uc_service):
"""Test name validation with invalid names"""
assert uc_service.validate_name("invalid-name") is False
assert uc_service.validate_name("invalid name") is False
assert uc_service.validate_name("invalid.name") is False
class TestIntentParsing:
"""Tests for intent parsing with Claude"""
def test_parse_create_catalog(self, claude_client_mock):
"""Test parsing create catalog intent"""
mock_response = Mock()
mock_response.content = [
Mock(
text='{"intent": "createCatalog", "params": {"catalog": "sales"}, "explanation": "Creating catalog"}',
type='text'
)
]
claude_client_mock.messages.create.return_value = mock_response
result = parse_with_claude("Create a catalog named sales")
assert result['intent'] == 'createCatalog'
assert result['params']['catalog'] == 'sales'
def test_parse_grant_permission(self, claude_client_mock):
"""Test parsing grant permission intent"""
mock_response = Mock()
mock_response.content = [
Mock(
text='{"intent": "grantPermission", "params": {"privilege": "SELECT", "object": "sales.customers", "principal": "analysts"}}',
type='text'
)
]
claude_client_mock.messages.create.return_value = mock_response
result = parse_with_claude("Grant SELECT on sales.customers to analysts")
assert result['intent'] == 'grantPermission'
assert result['params']['privilege'] == 'SELECT'
def test_parse_error_handling(self, claude_client_mock):
"""Test error handling in parsing"""
claude_client_mock.messages.create.side_effect = Exception("API Error")
result = parse_with_claude("Invalid request")
assert result['intent'] == 'help'
class TestExecuteIntent:
"""Tests for intent execution"""
def test_execute_create_catalog(self):
"""Test executing create catalog intent"""
mock_uc = Mock()
mock_uc.create_catalog.return_value = {
'success': True,
'message': 'Catalog created',
'sql': 'CREATE CATALOG test'
}
with patch('app._init_services', return_value=(mock_uc, Mock())):
intent_data = {
'intent': 'createCatalog',
'params': {'catalog': 'test'}
}
result = execute_intent(intent_data)
assert result['success'] is True
mock_uc.create_catalog.assert_called_once()
def test_execute_grant_permission(self):
"""Test executing grant permission intent"""
mock_uc = Mock()
mock_uc.grant_permission.return_value = {
'success': True,
'message': 'Permission granted'
}
with patch('app._init_services', return_value=(mock_uc, Mock())):
intent_data = {
'intent': 'grantPermission',
'params': {
'privilege': 'SELECT',
'object': 'catalog.schema.table',
'principal': 'user'
}
}
result = execute_intent(intent_data)
assert result['success'] is True
mock_uc.grant_permission.assert_called_once()
def test_execute_help(self, uc_service):
"""Test executing help intent"""
intent_data = {'intent': 'help', 'params': {}}
result = execute_intent(intent_data)
assert result['success'] is True
assert 'help' in result['message'].lower()
class TestAPIEndpoints:
"""Integration tests for API endpoints"""
@pytest.fixture
def client(self):
"""Flask test client"""
from app import app
app.config['TESTING'] = True
with app.test_client() as client:
yield client
def test_health_endpoint(self, client):
"""Test health check endpoint"""
response = client.get('/api/health')
assert response.status_code == 200
assert response.json['status'] == 'healthy'
@patch('app.parse_with_claude')
@patch('app.execute_intent')
def test_chat_endpoint_success(self, mock_execute, mock_parse, client):
"""Test chat endpoint with successful response"""
mock_parse.return_value = {
'intent': 'createCatalog',
'params': {'catalog': 'test'},
'explanation': 'Creating catalog'
}
mock_execute.return_value = {
'success': True,
'message': 'Catalog created',
'sql': 'CREATE CATALOG test'
}
response = client.post('/api/chat', json={
'message': 'Create catalog test'
})
assert response.status_code == 200
assert response.json['success'] is True
def test_chat_endpoint_no_message(self, client):
"""Test chat endpoint without message"""
response = client.post('/api/chat', json={})
assert response.status_code == 400
def test_get_catalogs(self, client):
"""Test get catalogs endpoint"""
mock_uc = Mock()
mock_uc.list_catalogs.return_value = {
'success': True,
'catalogs': [{'name': 'test'}]
}
with patch('app._init_services', return_value=(mock_uc, Mock())):
response = client.get('/api/catalogs')
assert response.status_code == 200
assert 'catalogs' in response.json
class TestComplexScenarios:
"""Integration tests for complex scenarios"""
@pytest.fixture
def service(self, uc_service):
"""Service with mocked client"""
return uc_service
def test_medallion_architecture_setup(self, service):
"""Test setting up medallion architecture"""
# This would be a comprehensive integration test
# that tests creating multiple catalogs, schemas, and tables
pass
def test_permission_workflow(self, service):
"""Test complete permission management workflow"""
# Test grant, show, and revoke permissions
pass
def test_error_recovery(self, service):
"""Test error recovery scenarios"""
# Test handling of API errors, network issues, etc.
pass
# Performance tests
class TestPerformance:
"""Performance tests"""
def test_concurrent_requests(self):
"""Test handling concurrent requests"""
# Test with multiple simultaneous API calls
pass
def test_large_catalog_listing(self):
"""Test listing large number of catalogs"""
# Test performance with hundreds of catalogs
pass
|