Skip to content

Commit e0c3244

Browse files
author
zhangjk
committed
refactor: 进一步降低 TestChatCompletionRequest_MarshalJSON 函数的认知复杂度
- 将验证逻辑提取到单独的函数 validateChatCompletionRequestResult - 将扩展验证逻辑提取到单独的函数 validateExtensions - 降低主函数的认知复杂度从 25 到 20 以下
1 parent 781c0cb commit e0c3244

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

chat_test.go

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,7 +1232,7 @@ func TestChatCompletionRequest_MarshalJSON(t *testing.T) {
12321232
},
12331233
Extensions: map[string]interface{}{
12341234
"custom_field": "custom_value",
1235-
"number": 42,
1235+
"number": 42,
12361236
},
12371237
},
12381238
wantErr: false,
@@ -1260,30 +1260,38 @@ func TestChatCompletionRequest_MarshalJSON(t *testing.T) {
12601260
if tt.wantErr {
12611261
return
12621262
}
1263-
1263+
12641264
var result map[string]interface{}
12651265
if unmarshalErr := json.Unmarshal(data, &result); unmarshalErr != nil {
12661266
t.Errorf("Failed to unmarshal result: %v", unmarshalErr)
12671267
return
12681268
}
1269-
1270-
// Check that model is present
1271-
if result["model"] != tt.request.Model {
1272-
t.Errorf("Expected model %s, got %v", tt.request.Model, result["model"])
1273-
}
1274-
1275-
// Check extensions are merged properly when present
1276-
if len(tt.request.Extensions) > 0 {
1277-
for key, value := range tt.request.Extensions {
1278-
// Convert both to string for comparison to handle type differences
1279-
resultStr := fmt.Sprintf("%v", result[key])
1280-
valueStr := fmt.Sprintf("%v", value)
1281-
if resultStr != valueStr {
1282-
t.Errorf("Expected extension %s = %v (%s), got %v (%s)", key, value, valueStr, result[key], resultStr)
1283-
}
1284-
}
1285-
}
1269+
1270+
validateChatCompletionRequestResult(t, tt.request, result)
12861271
})
1272+
})
1273+
}
1274+
1275+
func validateChatCompletionRequestResult(t *testing.T, request openai.ChatCompletionRequest, result map[string]interface{}) {
1276+
// Check that model is present
1277+
if result["model"] != request.Model {
1278+
t.Errorf("Expected model %s, got %v", request.Model, result["model"])
1279+
}
1280+
1281+
// Check extensions are merged properly when present
1282+
if len(request.Extensions) > 0 {
1283+
validateExtensions(t, request.Extensions, result)
1284+
}
1285+
}
1286+
1287+
func validateExtensions(t *testing.T, extensions map[string]interface{}, result map[string]interface{}) {
1288+
for key, value := range extensions {
1289+
// Convert both to string for comparison to handle type differences
1290+
resultStr := fmt.Sprintf("%v", result[key])
1291+
valueStr := fmt.Sprintf("%v", value)
1292+
if resultStr != valueStr {
1293+
t.Errorf("Expected extension %s = %v (%s), got %v (%s)", key, value, valueStr, result[key], resultStr)
1294+
}
12871295
}
12881296
}
12891297

@@ -1295,8 +1303,8 @@ func TestChatMessagePart_NewFields(t *testing.T) {
12951303
{
12961304
name: "with audio part",
12971305
part: openai.ChatMessagePart{
1298-
Type: openai.ChatMessagePartTypeAudio,
1299-
Video: []string{"https://example.com/frame1.jpg", "https://example.com/frame2.jpg"},
1306+
Type: openai.ChatMessagePartTypeAudio,
1307+
Video: []string{"https://example.com/frame1.jpg", "https://example.com/frame2.jpg"},
13001308
MinPixels: 100,
13011309
MaxPixels: 1000,
13021310
},

0 commit comments

Comments
 (0)