Skip to content

Commit 1ec28a3

Browse files
Fix: Use UUIDs for mock user and team IDs in tests
Co-authored-by: yourton.ma <yourton.ma@gmail.com>
1 parent 7289517 commit 1ec28a3

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# UUID 验证错误修复
2+
3+
## 问题
4+
5+
运行测试时出现 Pydantic 验证错误:
6+
7+
```
8+
pydantic_core._pydantic_core.ValidationError: 1 validation error for User
9+
id
10+
Input should be a valid UUID, invalid character: expected an optional prefix of `urn:uuid:`
11+
followed by [0-9a-fA-F-], found `u` at 1 [type=uuid_parsing, input_value='user-alice', input_type=str]
12+
```
13+
14+
## 根本原因
15+
16+
User 和 Team 实体的 `id` 字段要求 UUID 格式,但代码使用了字符串:
17+
```python
18+
# ❌ 错误
19+
id="user-" + name # 'user-alice' 不是有效的 UUID
20+
```
21+
22+
## 修复方案
23+
24+
### 1. 添加 uuid 模块导入
25+
26+
```python
27+
import uuid
28+
from typing import Any, Dict, List, Optional, Union
29+
```
30+
31+
### 2. 修复 `_create_mock_user` 方法
32+
33+
```python
34+
def _create_mock_user(self, name: str, email: str) -> User:
35+
"""Create a mock User entity"""
36+
return User(
37+
id=uuid.uuid4(), # ✅ 使用真实的 UUID
38+
name=EntityName(name),
39+
fullyQualifiedName=FullyQualifiedEntityName(name),
40+
email=Email(email),
41+
displayName=name.capitalize(),
42+
)
43+
```
44+
45+
### 3. 修复 `_create_mock_team` 方法
46+
47+
```python
48+
def _create_mock_team(self, name: str, display_name: str) -> Team:
49+
"""Create a mock Team entity"""
50+
return Team(
51+
id=uuid.uuid4(), # ✅ 使用真实的 UUID
52+
name=EntityName(name),
53+
fullyQualifiedName=FullyQualifiedEntityName(name),
54+
displayName=display_name,
55+
teamType="Group",
56+
)
57+
```
58+
59+
## 验证
60+
61+
修复后,请重新运行测试:
62+
63+
```bash
64+
cd ingestion
65+
pytest tests/unit/metadata/ingestion/test_owner_config.py -v
66+
```
67+
68+
预期结果:所有 10 个测试应该通过 ✅
69+
70+
## 修复状态
71+
72+
- ✅ 已添加 `import uuid`
73+
- ✅ 已修复 `_create_mock_user()`
74+
- ✅ 已修复 `_create_mock_team()`
75+
- ✅ 通过 linter 检查(无错误)
76+
77+
## 参考
78+
79+
参考项目中其他测试如何创建 User/Team:
80+
- `ingestion/tests/unit/topology/dashboard/test_grafana.py:545`
81+
- `ingestion/tests/unit/topology/dashboard/test_powerbi.py:160`
82+
83+
使用 `uuid.uuid4()` 是标准做法。

ingestion/tests/unit/metadata/ingestion/test_owner_config.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
Replaces the bash/YAML-based tests previously in owner_config_tests/ directory.
2020
"""
2121

22+
import uuid
2223
from typing import Any, Dict, List, Optional, Union
2324
from unittest import TestCase
2425
from unittest.mock import MagicMock, Mock, patch
@@ -199,7 +200,7 @@ def get_by_name_side_effect(
199200
def _create_mock_user(self, name: str, email: str) -> User:
200201
"""Create a mock User entity"""
201202
return User(
202-
id="user-" + name,
203+
id=uuid.uuid4(),
203204
name=EntityName(name),
204205
fullyQualifiedName=FullyQualifiedEntityName(name),
205206
email=Email(email),
@@ -209,7 +210,7 @@ def _create_mock_user(self, name: str, email: str) -> User:
209210
def _create_mock_team(self, name: str, display_name: str) -> Team:
210211
"""Create a mock Team entity"""
211212
return Team(
212-
id="team-" + name,
213+
id=uuid.uuid4(),
213214
name=EntityName(name),
214215
fullyQualifiedName=FullyQualifiedEntityName(name),
215216
displayName=display_name,

0 commit comments

Comments
 (0)