test_cleaned_api.py
4.98 KB
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
#!/usr/bin/env python3
"""
测试清理后的API行为
验证用户不再需要传递enable_translation等参数
"""
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
def test_cleaned_api():
"""测试清理后的API行为"""
print("🧪 测试清理后的API行为")
print("=" * 60)
try:
from api.models import SearchRequest
from search.searcher import Searcher
from config.config_loader import ConfigLoader
from context.request_context import create_request_context
# 测试API模型不再包含内部参数
print("📝 测试API模型...")
# 创建搜索请求
search_request = SearchRequest(
query="消防",
size=10,
from_=0,
filters=None,
min_score=None
)
print(f"✅ SearchRequest创建成功:")
print(f" - query: {search_request.query}")
print(f" - size: {search_request.size}")
print(f" - from_: {search_request.from_}")
print(f" - filters: {search_request.filters}")
print(f" - min_score: {search_request.min_score}")
# 验证不再包含内部参数
print(f"\n🚫 验证内部参数已移除:")
internal_params = ['enable_translation', 'enable_embedding', 'enable_rerank']
for param in internal_params:
if hasattr(search_request, param):
print(f" ❌ {param} 仍然存在")
return False
else:
print(f" ✅ {param} 已移除")
# 测试搜索器使用配置默认值
print(f"\n🔧 测试搜索器使用配置默认值...")
loader = ConfigLoader()
config = loader.load_customer_config("customer1")
print(f"✅ 配置默认值:")
print(f" - enable_translation: {config.query_config.enable_translation}")
print(f" - enable_text_embedding: {config.query_config.enable_text_embedding}")
# 创建模拟搜索器测试
class MockESClient:
def search(self, **kwargs):
return {
"hits": {"hits": [], "total": {"value": 0}, "max_score": 0.0},
"took": 15
}
es_client = MockESClient()
searcher = Searcher(config, es_client)
# 测试搜索器方法签名
import inspect
search_signature = inspect.signature(searcher.search)
search_params = list(search_signature.parameters.keys())
print(f"\n📋 搜索器方法参数:")
for param in search_params:
print(f" - {param}")
# 验证不再包含内部参数
print(f"\n🚫 验证搜索器参数已清理:")
for param in internal_params:
if param in search_params:
print(f" ❌ {param} 仍然存在")
return False
else:
print(f" ✅ {param} 已移除")
# 测试实际的搜索调用
print(f"\n🧪 测试实际搜索调用...")
context = create_request_context("cleaned_api_test", "test_user")
result = searcher.search(
query="消防",
size=10,
from_=0,
filters=None,
min_score=None,
context=context
)
print(f"✅ 搜索调用成功!")
print(f" - 返回结果类型: {type(result).__name__}")
print(f" - 总命中数: {result.total}")
# 检查上下文中的功能标志
feature_flags = context.metadata.get('feature_flags', {})
print(f"\n🚩 实际使用的功能标志:")
for flag, value in feature_flags.items():
print(f" - {flag}: {value}")
# 验证使用了配置默认值
expected_translation = config.query_config.enable_translation
expected_embedding = config.query_config.enable_text_embedding
actual_translation = feature_flags.get('translation_enabled')
actual_embedding = feature_flags.get('embedding_enabled')
print(f"\n📊 功能验证:")
print(f" 翻译功能: 期望={expected_translation}, 实际={actual_translation} {'✅' if expected_translation == actual_translation else '❌'}")
print(f" 向量功能: 期望={expected_embedding}, 实际={actual_embedding} {'✅' if expected_embedding == actual_embedding else '❌'}")
if expected_translation == actual_translation and expected_embedding == actual_embedding:
print(f"\n🎉 API清理成功!")
print(f"✅ 用户不再需要传递内部参数")
print(f"✅ 后端自动使用配置默认值")
print(f"✅ 功能完全透明")
return True
else:
print(f"\n⚠️ 功能验证失败")
return False
except Exception as e:
print(f"❌ 测试失败: {e}")
import traceback
traceback.print_exc()
return False
if __name__ == "__main__":
success = test_cleaned_api()
sys.exit(0 if success else 1)