OpenMetadata/ingestion/tests/unit/sdk/test_test_case_entity.py.bak

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

137 lines
4.9 KiB
Python
Raw Normal View History

"""
Comprehensive unit tests for Test Case entity.
"""
import unittest
from unittest.mock import MagicMock
from uuid import UUID
from metadata.generated.schema.api.tests.createTestCase import CreateTestCaseRequest
from metadata.generated.schema.tests.testCase import TestCase as TestCaseEntity
from metadata.sdk.entities.testcase import TestCase
class TestTestCaseEntity(unittest.TestCase):
"""Comprehensive tests for TestCase entity operations"""
def setUp(self):
"""Set up test fixtures"""
self.mock_ometa = MagicMock()
TestCase._default_client = self.mock_ometa
self.entity_id = "550e8400-e29b-41d4-a716-446655440000"
self.entity_fqn = "service.test_case.test_test_case"
def test_create_test_case(self):
"""Test creating a test case"""
create_request = MagicMock(spec=CreateTestCaseRequest)
create_request.name = "test_test_case"
create_request.displayName = "Test Test Case"
create_request.description = "Test test case for unit tests"
expected_entity = MagicMock(spec=TestCaseEntity)
expected_entity.id = UUID(self.entity_id)
expected_entity.name = "test_test_case"
self.mock_ometa.create_or_update.return_value = expected_entity
result = TestCase.create(create_request)
self.assertEqual(str(result.id), self.entity_id)
self.assertEqual(result.name, "test_test_case")
self.mock_ometa.create_or_update.assert_called_once_with(create_request)
def test_retrieve_test_case_by_id(self):
"""Test retrieving a test case by ID"""
expected_entity = MagicMock(spec=TestCaseEntity)
expected_entity.id = UUID(self.entity_id)
expected_entity.name = "test_test_case"
self.mock_ometa.get_by_id.return_value = expected_entity
result = TestCase.retrieve(self.entity_id)
self.assertEqual(str(result.id), self.entity_id)
self.mock_ometa.get_by_id.assert_called_once_with(
entity=TestCaseEntity, entity_id=self.entity_id, fields=None
)
def test_retrieve_test_case_by_name(self):
"""Test retrieving a test case by name"""
expected_entity = MagicMock(spec=TestCaseEntity)
expected_entity.fullyQualifiedName = self.entity_fqn
self.mock_ometa.get_by_name.return_value = expected_entity
result = TestCase.retrieve_by_name(self.entity_fqn)
self.assertEqual(result.fullyQualifiedName, self.entity_fqn)
self.mock_ometa.get_by_name.assert_called_once_with(
entity=TestCaseEntity, fqn=self.entity_fqn, fields=None
)
def test_update_test_case(self):
"""Test updating a test case"""
entity_to_update = MagicMock(spec=TestCaseEntity)
entity_to_update.id = UUID(self.entity_id)
entity_to_update.description = "Updated description"
self.mock_ometa.create_or_update.return_value = entity_to_update
result = TestCase.update(self.entity_id, entity_to_update)
self.assertEqual(result.description, "Updated description")
self.mock_ometa.create_or_update.assert_called_once_with(entity_to_update)
def test_patch_test_case(self):
"""Test patching a test case"""
json_patch = [
{"op": "add", "path": "/description", "value": "Patched description"},
{"op": "add", "path": "/tags/0", "value": {"tagFQN": "Important.High"}},
]
patched_entity = MagicMock(spec=TestCaseEntity)
patched_entity.id = UUID(self.entity_id)
patched_entity.description = "Patched description"
self.mock_ometa.patch.return_value = patched_entity
result = TestCase.patch(self.entity_id, json_patch)
self.assertEqual(result.description, "Patched description")
self.mock_ometa.patch.assert_called_once_with(
entity=TestCaseEntity, entity_id=self.entity_id, json_patch=json_patch
)
def test_delete_test_case(self):
"""Test deleting a test case"""
TestCase.delete(self.entity_id, recursive=True, hard_delete=False)
self.mock_ometa.delete.assert_called_once_with(
entity=TestCaseEntity,
entity_id=self.entity_id,
recursive=True,
hard_delete=False,
)
def test_list_test_cases(self):
"""Test listing test cases"""
mock_entity1 = MagicMock(spec=TestCaseEntity)
mock_entity1.name = "entity1"
mock_entity2 = MagicMock(spec=TestCaseEntity)
mock_entity2.name = "entity2"
mock_response = MagicMock()
mock_response.entities = [mock_entity1, mock_entity2]
self.mock_ometa.list_entities.return_value = mock_response
result = TestCase.list(limit=10)
self.assertEqual(len(result), 2)
self.assertEqual(result[0].name, "entity1")
self.mock_ometa.list_entities.assert_called_once()
if __name__ == "__main__":
unittest.main()