mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-08 00:47:36 +08:00
fix: remove unneccessary tests
This commit is contained in:
@@ -1 +1 @@
|
|||||||
v5.0.0-beta-2506102124
|
v5.0.0-beta-2506102252
|
||||||
|
|||||||
+58
-339
@@ -7,47 +7,24 @@ import (
|
|||||||
|
|
||||||
"github.com/httprunner/httprunner/v5/internal/builtin"
|
"github.com/httprunner/httprunner/v5/internal/builtin"
|
||||||
"github.com/httprunner/httprunner/v5/uixt/option"
|
"github.com/httprunner/httprunner/v5/uixt/option"
|
||||||
|
"github.com/httprunner/httprunner/v5/uixt/types"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Data structures for testing custom output schemas
|
// Test data structures
|
||||||
|
|
||||||
// GameIcon represents a single icon in the game grid
|
// GameInfo represents basic game information for testing
|
||||||
type GameIcon struct {
|
type GameInfo struct {
|
||||||
Name string `json:"name"` // Icon name (e.g., "beach_ball", "glove")
|
|
||||||
Row int `json:"row"` // Row position (0-based)
|
|
||||||
Col int `json:"col"` // Column position (0-based)
|
|
||||||
}
|
|
||||||
|
|
||||||
// GameGrid represents the complete game grid
|
|
||||||
type GameGrid struct {
|
|
||||||
Grid [][]GameIcon `json:"grid"` // 2D array of game icons
|
|
||||||
Rows int `json:"rows"` // Number of rows
|
|
||||||
Cols int `json:"cols"` // Number of columns
|
|
||||||
Icons []string `json:"icons"` // List of unique icon names
|
|
||||||
}
|
|
||||||
|
|
||||||
// LianliankanResponse represents the structured response for lianliankan game analysis
|
|
||||||
type LianliankanResponse struct {
|
|
||||||
Content string `json:"content"` // Description of the analysis
|
|
||||||
Thought string `json:"thought"` // Reasoning process
|
|
||||||
Data GameGrid `json:"data"` // Structured game grid data
|
|
||||||
}
|
|
||||||
|
|
||||||
// SimpleGameInfo represents basic game information
|
|
||||||
type SimpleGameInfo struct {
|
|
||||||
Content string `json:"content"` // Description
|
Content string `json:"content"` // Description
|
||||||
Thought string `json:"thought"` // Reasoning
|
Thought string `json:"thought"` // Reasoning
|
||||||
Rows int `json:"rows"` // Number of rows
|
Rows int `json:"rows"` // Number of rows
|
||||||
Cols int `json:"cols"` // Number of columns
|
Cols int `json:"cols"` // Number of columns
|
||||||
IconTypes []string `json:"iconTypes"` // List of icon types
|
Icons []string `json:"icons"` // List of icon types
|
||||||
TotalIcons int `json:"totalIcons"` // Total number of icons
|
TotalIcons int `json:"totalIcons"` // Total number of icons
|
||||||
}
|
}
|
||||||
|
|
||||||
// Additional data structures for comprehensive testing
|
// GameAnalysisResult represents comprehensive game analysis for testing
|
||||||
|
|
||||||
// GameAnalysisResult represents structured analysis of a game interface
|
|
||||||
type GameAnalysisResult struct {
|
type GameAnalysisResult struct {
|
||||||
Content string `json:"content"` // Human-readable description
|
Content string `json:"content"` // Human-readable description
|
||||||
Thought string `json:"thought"` // AI reasoning process
|
Thought string `json:"thought"` // AI reasoning process
|
||||||
@@ -91,21 +68,21 @@ type TypeCount struct {
|
|||||||
Count int `json:"count"` // Number of occurrences
|
Count int `json:"count"` // Number of occurrences
|
||||||
}
|
}
|
||||||
|
|
||||||
// UIElementsResult represents structured analysis of UI elements
|
// Test helper functions
|
||||||
type UIElementsResult struct {
|
|
||||||
Content string `json:"content"` // Description
|
func setupTestQuerier(t *testing.T) *Querier {
|
||||||
Thought string `json:"thought"` // Reasoning
|
ctx := context.Background()
|
||||||
Elements []UIElement `json:"elements"` // UI elements found
|
modelConfig, err := GetModelConfig(option.OPENAI_GPT_4O)
|
||||||
Categories []string `json:"categories"` // Categories of elements
|
require.NoError(t, err)
|
||||||
|
querier, err := NewQuerier(ctx, modelConfig)
|
||||||
|
require.NoError(t, err)
|
||||||
|
return querier
|
||||||
}
|
}
|
||||||
|
|
||||||
type UIElement struct {
|
func loadTestImage(t *testing.T) (string, types.Size) {
|
||||||
Type string `json:"type"` // Element type (button, text, image, etc.)
|
screenshot, size, err := builtin.LoadImage("testdata/llk_1.png")
|
||||||
Text string `json:"text"` // Text content if any
|
require.NoError(t, err)
|
||||||
Description string `json:"description"` // Element description
|
return screenshot, size
|
||||||
BoundBox BoundingBox `json:"boundBox"` // Pixel coordinates
|
|
||||||
Clickable bool `json:"clickable"` // Whether element is clickable
|
|
||||||
Visible bool `json:"visible"` // Whether element is visible
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test functions
|
// Test functions
|
||||||
@@ -151,14 +128,6 @@ func TestParseQueryResult(t *testing.T) {
|
|||||||
Thought: "Direct response from model",
|
Thought: "Direct response from model",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: "malformed JSON that can be extracted but not parsed",
|
|
||||||
content: `{"content": "test", "invalid": }`,
|
|
||||||
expected: &QueryResult{
|
|
||||||
Content: `{"content": "test", "invalid": }`,
|
|
||||||
Thought: "Failed to parse as JSON, returning raw content",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
@@ -171,31 +140,18 @@ func TestParseQueryResult(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupTestQuerier(t *testing.T) *Querier {
|
// TestQueryFunctionality tests both basic and custom schema query functionality
|
||||||
ctx := context.Background()
|
func TestQueryFunctionality(t *testing.T) {
|
||||||
modelConfig, err := GetModelConfig(option.OPENAI_GPT_4O)
|
|
||||||
require.NoError(t, err)
|
|
||||||
querier, err := NewQuerier(ctx, modelConfig)
|
|
||||||
require.NoError(t, err)
|
|
||||||
return querier
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestQueryBasicUsage demonstrates basic query functionality without custom schema
|
|
||||||
func TestQueryBasicUsage(t *testing.T) {
|
|
||||||
querier := setupTestQuerier(t)
|
querier := setupTestQuerier(t)
|
||||||
|
screenshot, size := loadTestImage(t)
|
||||||
|
|
||||||
// Load screenshot
|
t.Run("BasicQuery", func(t *testing.T) {
|
||||||
screenshot, size, err := builtin.LoadImage("testdata/llk_1.png")
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
// Prepare query options
|
|
||||||
opts := &QueryOptions{
|
opts := &QueryOptions{
|
||||||
Query: "这是一张连连看小游戏的界面,请将其转换为一个二维数组,数组中的每个元素包含图案名称及其坐标",
|
Query: "这是一张连连看小游戏的界面,请分析游戏界面的基本信息",
|
||||||
Screenshot: screenshot,
|
Screenshot: screenshot,
|
||||||
Size: size,
|
Size: size,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perform query
|
|
||||||
result, err := querier.Query(context.Background(), opts)
|
result, err := querier.Query(context.Background(), opts)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.NotNil(t, result)
|
assert.NotNil(t, result)
|
||||||
@@ -203,103 +159,35 @@ func TestQueryBasicUsage(t *testing.T) {
|
|||||||
assert.NotEmpty(t, result.Thought)
|
assert.NotEmpty(t, result.Thought)
|
||||||
assert.Nil(t, result.Data) // Should be nil for standard query
|
assert.Nil(t, result.Data) // Should be nil for standard query
|
||||||
|
|
||||||
t.Logf("Query Result:")
|
t.Logf("Basic Query Result: %s", result.Content)
|
||||||
t.Logf("Content: %s", result.Content)
|
})
|
||||||
t.Logf("Thought: %s", result.Thought)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestQueryWithCustomSchema tests the query functionality with custom output schema
|
t.Run("CustomSchemaQuery", func(t *testing.T) {
|
||||||
func TestQueryWithCustomSchema(t *testing.T) {
|
|
||||||
querier := setupTestQuerier(t)
|
|
||||||
|
|
||||||
// Load test image
|
|
||||||
screenshot, size, err := builtin.LoadImage("testdata/llk_1.png")
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
// Define custom output schema for lianliankan game
|
|
||||||
outputSchema := LianliankanResponse{}
|
|
||||||
|
|
||||||
// Prepare query options with custom schema
|
|
||||||
opts := &QueryOptions{
|
opts := &QueryOptions{
|
||||||
Query: `这是一张连连看小游戏的界面,请分析游戏界面并返回结构化数据:
|
Query: "请分析这个连连看游戏界面,告诉我有多少行多少列,有哪些不同类型的图案",
|
||||||
1. 游戏网格的行数和列数
|
|
||||||
2. 每个位置的图案名称和坐标
|
|
||||||
3. 所有不同类型的图案列表
|
|
||||||
请将结果组织成二维数组格式,每个元素包含图案名称及其坐标位置。`,
|
|
||||||
Screenshot: screenshot,
|
Screenshot: screenshot,
|
||||||
Size: size,
|
Size: size,
|
||||||
OutputSchema: outputSchema,
|
OutputSchema: GameInfo{},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perform query
|
|
||||||
result, err := querier.Query(context.Background(), opts)
|
result, err := querier.Query(context.Background(), opts)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.NotNil(t, result)
|
assert.NotNil(t, result)
|
||||||
assert.NotEmpty(t, result.Content)
|
assert.NotEmpty(t, result.Content)
|
||||||
assert.NotEmpty(t, result.Thought)
|
assert.NotEmpty(t, result.Thought)
|
||||||
assert.NotNil(t, result.Data)
|
assert.NotNil(t, result.Data) // Should contain structured data
|
||||||
|
|
||||||
t.Logf("Query result content: %s", result.Content)
|
// Verify structured data
|
||||||
t.Logf("Query result thought: %s", result.Thought)
|
gameInfo, ok := result.Data.(*GameInfo)
|
||||||
t.Logf("Structured data: %+v", result.Data)
|
assert.True(t, ok)
|
||||||
|
assert.NotNil(t, gameInfo)
|
||||||
|
assert.NotEmpty(t, gameInfo.Content)
|
||||||
|
assert.NotEmpty(t, gameInfo.Thought)
|
||||||
|
|
||||||
// Try to parse the structured data
|
t.Logf("Custom Schema Query Result: %+v", gameInfo)
|
||||||
if dataMap, ok := result.Data.(map[string]interface{}); ok {
|
})
|
||||||
if gridData, exists := dataMap["data"]; exists {
|
|
||||||
t.Logf("Game grid data: %+v", gridData)
|
|
||||||
}
|
|
||||||
if rows, exists := dataMap["rows"]; exists {
|
|
||||||
t.Logf("Rows: %v", rows)
|
|
||||||
}
|
|
||||||
if cols, exists := dataMap["cols"]; exists {
|
|
||||||
t.Logf("Cols: %v", cols)
|
|
||||||
}
|
|
||||||
if icons, exists := dataMap["icons"]; exists {
|
|
||||||
t.Logf("Icon Types: %v", icons)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestQueryWithSimpleSchema tests with a simpler custom schema
|
t.Run("ComprehensiveAnalysis", func(t *testing.T) {
|
||||||
func TestQueryWithSimpleSchema(t *testing.T) {
|
|
||||||
querier := setupTestQuerier(t)
|
|
||||||
|
|
||||||
// Load test image
|
|
||||||
screenshot, size, err := builtin.LoadImage("testdata/llk_1.png")
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
outputSchema := SimpleGameInfo{}
|
|
||||||
|
|
||||||
// Prepare query options
|
|
||||||
opts := &QueryOptions{
|
|
||||||
Query: "请分析这个连连看游戏界面,告诉我有多少行多少列,有哪些不同类型的图案,总共有多少个图标",
|
|
||||||
Screenshot: screenshot,
|
|
||||||
Size: size,
|
|
||||||
OutputSchema: outputSchema,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Perform query
|
|
||||||
result, err := querier.Query(context.Background(), opts)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.NotNil(t, result)
|
|
||||||
assert.NotEmpty(t, result.Content)
|
|
||||||
assert.NotEmpty(t, result.Thought)
|
|
||||||
assert.NotNil(t, result.Data)
|
|
||||||
|
|
||||||
t.Logf("Simple schema result: %+v", result)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestQueryWithGameAnalysisSchema tests with comprehensive game analysis schema
|
|
||||||
func TestQueryWithGameAnalysisSchema(t *testing.T) {
|
|
||||||
querier := setupTestQuerier(t)
|
|
||||||
|
|
||||||
// Load test image
|
|
||||||
screenshot, size, err := builtin.LoadImage("testdata/llk_1.png")
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
outputSchema := GameAnalysisResult{}
|
|
||||||
|
|
||||||
// Prepare query options
|
|
||||||
opts := &QueryOptions{
|
opts := &QueryOptions{
|
||||||
Query: `Analyze this game interface and provide structured information about:
|
Query: `Analyze this game interface and provide structured information about:
|
||||||
1. The type of game
|
1. The type of game
|
||||||
@@ -308,10 +196,9 @@ func TestQueryWithGameAnalysisSchema(t *testing.T) {
|
|||||||
4. Statistics about element distribution`,
|
4. Statistics about element distribution`,
|
||||||
Screenshot: screenshot,
|
Screenshot: screenshot,
|
||||||
Size: size,
|
Size: size,
|
||||||
OutputSchema: outputSchema,
|
OutputSchema: GameAnalysisResult{},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perform query
|
|
||||||
result, err := querier.Query(context.Background(), opts)
|
result, err := querier.Query(context.Background(), opts)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.NotNil(t, result)
|
assert.NotNil(t, result)
|
||||||
@@ -319,114 +206,15 @@ func TestQueryWithGameAnalysisSchema(t *testing.T) {
|
|||||||
assert.NotEmpty(t, result.Thought)
|
assert.NotEmpty(t, result.Thought)
|
||||||
assert.NotNil(t, result.Data)
|
assert.NotNil(t, result.Data)
|
||||||
|
|
||||||
t.Logf("Game analysis result: %+v", result)
|
t.Logf("Comprehensive Analysis Result: %+v", result.Data)
|
||||||
}
|
|
||||||
|
|
||||||
// TestQueryWithUIElementsSchema tests UI elements analysis
|
|
||||||
func TestQueryWithUIElementsSchema(t *testing.T) {
|
|
||||||
querier := setupTestQuerier(t)
|
|
||||||
|
|
||||||
// Load test image
|
|
||||||
screenshot, size, err := builtin.LoadImage("testdata/llk_1.png")
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
outputSchema := UIElementsResult{}
|
|
||||||
|
|
||||||
// Prepare query options
|
|
||||||
opts := &QueryOptions{
|
|
||||||
Query: `Analyze this interface and identify all UI elements including:
|
|
||||||
1. Buttons and their text
|
|
||||||
2. Text labels and content
|
|
||||||
3. Images and icons
|
|
||||||
4. Interactive elements
|
|
||||||
5. Their positions and properties`,
|
|
||||||
Screenshot: screenshot,
|
|
||||||
Size: size,
|
|
||||||
OutputSchema: outputSchema,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Perform query
|
|
||||||
result, err := querier.Query(context.Background(), opts)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.NotNil(t, result)
|
|
||||||
assert.NotEmpty(t, result.Content)
|
|
||||||
assert.NotEmpty(t, result.Thought)
|
|
||||||
assert.NotNil(t, result.Data)
|
|
||||||
|
|
||||||
t.Logf("UI elements analysis result: %+v", result)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestQuerySchemaComparison compares standard vs custom schema queries
|
|
||||||
func TestQuerySchemaComparison(t *testing.T) {
|
|
||||||
querier := setupTestQuerier(t)
|
|
||||||
|
|
||||||
screenshot, size, err := builtin.LoadImage("testdata/llk_1.png")
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
query := "请分析这个连连看游戏界面的基本信息"
|
|
||||||
|
|
||||||
// Standard query (without custom schema)
|
|
||||||
t.Run("StandardQuery", func(t *testing.T) {
|
|
||||||
standardOpts := &QueryOptions{
|
|
||||||
Query: query,
|
|
||||||
Screenshot: screenshot,
|
|
||||||
Size: size,
|
|
||||||
// No OutputSchema specified
|
|
||||||
}
|
|
||||||
|
|
||||||
standardResult, err := querier.Query(context.Background(), standardOpts)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.NotNil(t, standardResult)
|
|
||||||
assert.NotEmpty(t, standardResult.Content)
|
|
||||||
assert.NotEmpty(t, standardResult.Thought)
|
|
||||||
assert.Nil(t, standardResult.Data) // Should be nil for standard query
|
|
||||||
|
|
||||||
t.Logf("Standard Query Result:")
|
|
||||||
t.Logf("Content: %s", standardResult.Content)
|
|
||||||
t.Logf("Thought: %s", standardResult.Thought)
|
|
||||||
t.Logf("Data: %+v", standardResult.Data)
|
|
||||||
})
|
|
||||||
|
|
||||||
// Custom schema query
|
|
||||||
t.Run("CustomSchemaQuery", func(t *testing.T) {
|
|
||||||
type GameInfo struct {
|
|
||||||
Content string `json:"content"`
|
|
||||||
Thought string `json:"thought"`
|
|
||||||
Rows int `json:"rows"`
|
|
||||||
Cols int `json:"cols"`
|
|
||||||
Icons []string `json:"icons"`
|
|
||||||
}
|
|
||||||
|
|
||||||
customOpts := &QueryOptions{
|
|
||||||
Query: query,
|
|
||||||
Screenshot: screenshot,
|
|
||||||
Size: size,
|
|
||||||
OutputSchema: GameInfo{},
|
|
||||||
}
|
|
||||||
|
|
||||||
customResult, err := querier.Query(context.Background(), customOpts)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.NotNil(t, customResult)
|
|
||||||
assert.NotEmpty(t, customResult.Content)
|
|
||||||
assert.NotEmpty(t, customResult.Thought)
|
|
||||||
assert.NotNil(t, customResult.Data) // Should contain structured data
|
|
||||||
|
|
||||||
t.Logf("Custom Schema Query Result:")
|
|
||||||
t.Logf("Content: %s", customResult.Content)
|
|
||||||
t.Logf("Thought: %s", customResult.Thought)
|
|
||||||
t.Logf("Structured Data: %+v", customResult.Data)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestQueryWithDifferentPrompts tests various types of queries on the same screenshot
|
// TestQueryWithDifferentPrompts tests various types of queries on the same screenshot
|
||||||
func TestQueryWithDifferentPrompts(t *testing.T) {
|
func TestQueryWithDifferentPrompts(t *testing.T) {
|
||||||
querier := setupTestQuerier(t)
|
querier := setupTestQuerier(t)
|
||||||
|
screenshot, size := loadTestImage(t)
|
||||||
|
|
||||||
// Load screenshot
|
|
||||||
screenshot, size, err := builtin.LoadImage("testdata/llk_1.png")
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
// Example queries
|
|
||||||
queries := []string{
|
queries := []string{
|
||||||
"请描述这张图片中的内容",
|
"请描述这张图片中的内容",
|
||||||
"这个游戏界面有多少行多少列?",
|
"这个游戏界面有多少行多少列?",
|
||||||
@@ -450,13 +238,12 @@ func TestQueryWithDifferentPrompts(t *testing.T) {
|
|||||||
|
|
||||||
t.Logf("Query %d: %s", i+1, query)
|
t.Logf("Query %d: %s", i+1, query)
|
||||||
t.Logf("Answer: %s", result.Content)
|
t.Logf("Answer: %s", result.Content)
|
||||||
t.Logf("Reasoning: %s", result.Thought)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestConvertQueryResultData tests the type conversion functionality
|
// TestTypeConversionAndAssertion tests data type conversion and assertion functionality
|
||||||
func TestConvertQueryResultData(t *testing.T) {
|
func TestTypeConversionAndAssertion(t *testing.T) {
|
||||||
// Test data structure
|
// Test data structure
|
||||||
type TestSchema struct {
|
type TestSchema struct {
|
||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
@@ -465,6 +252,7 @@ func TestConvertQueryResultData(t *testing.T) {
|
|||||||
Items []string `json:"items"`
|
Items []string `json:"items"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
t.Run("ConvertQueryResultData", func(t *testing.T) {
|
||||||
// Create a QueryResult with structured data
|
// Create a QueryResult with structured data
|
||||||
testData := &TestSchema{
|
testData := &TestSchema{
|
||||||
Content: "Test content",
|
Content: "Test content",
|
||||||
@@ -487,64 +275,9 @@ func TestConvertQueryResultData(t *testing.T) {
|
|||||||
assert.Equal(t, "Test thought", converted.Thought)
|
assert.Equal(t, "Test thought", converted.Thought)
|
||||||
assert.Equal(t, 5, converted.Count)
|
assert.Equal(t, 5, converted.Count)
|
||||||
assert.Equal(t, []string{"item1", "item2", "item3"}, converted.Items)
|
assert.Equal(t, []string{"item1", "item2", "item3"}, converted.Items)
|
||||||
|
})
|
||||||
|
|
||||||
t.Logf("Successfully converted data: %+v", converted)
|
t.Run("AutoTypeConversion", func(t *testing.T) {
|
||||||
}
|
|
||||||
|
|
||||||
// TestQueryResultDataConsistency tests that QueryResult.Data matches OutputSchema
|
|
||||||
func TestQueryResultDataConsistency(t *testing.T) {
|
|
||||||
querier := setupTestQuerier(t)
|
|
||||||
|
|
||||||
// Load test image
|
|
||||||
screenshot, size, err := builtin.LoadImage("testdata/llk_1.png")
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
// Define a simple test schema
|
|
||||||
type TestGameInfo struct {
|
|
||||||
Content string `json:"content"`
|
|
||||||
Thought string `json:"thought"`
|
|
||||||
Rows int `json:"rows"`
|
|
||||||
Cols int `json:"cols"`
|
|
||||||
Icons []string `json:"icons"`
|
|
||||||
}
|
|
||||||
|
|
||||||
outputSchema := TestGameInfo{}
|
|
||||||
|
|
||||||
// Prepare query options
|
|
||||||
opts := &QueryOptions{
|
|
||||||
Query: "请分析这个连连看游戏界面,告诉我有多少行多少列,有哪些不同类型的图案",
|
|
||||||
Screenshot: screenshot,
|
|
||||||
Size: size,
|
|
||||||
OutputSchema: outputSchema,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Perform query
|
|
||||||
result, err := querier.Query(context.Background(), opts)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.NotNil(t, result)
|
|
||||||
assert.NotNil(t, result.Data)
|
|
||||||
gameInfo, ok := result.Data.(*TestGameInfo)
|
|
||||||
assert.True(t, ok)
|
|
||||||
assert.NotNil(t, gameInfo)
|
|
||||||
|
|
||||||
// Verify that the converted data has the expected structure
|
|
||||||
assert.NotEmpty(t, gameInfo.Content)
|
|
||||||
assert.NotEmpty(t, gameInfo.Thought)
|
|
||||||
assert.NotEmpty(t, gameInfo.Rows)
|
|
||||||
assert.NotEmpty(t, gameInfo.Cols)
|
|
||||||
assert.NotEmpty(t, gameInfo.Icons)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestAutoTypeConversion tests that QueryResult.Data is automatically converted to the correct type
|
|
||||||
func TestAutoTypeConversion(t *testing.T) {
|
|
||||||
// Test data structure
|
|
||||||
type TestSchema struct {
|
|
||||||
Content string `json:"content"`
|
|
||||||
Thought string `json:"thought"`
|
|
||||||
Count int `json:"count"`
|
|
||||||
Items []string `json:"items"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Simulate a JSON response from the model
|
// Simulate a JSON response from the model
|
||||||
jsonResponse := `{
|
jsonResponse := `{
|
||||||
"content": "Test content from model",
|
"content": "Test content from model",
|
||||||
@@ -573,45 +306,31 @@ func TestAutoTypeConversion(t *testing.T) {
|
|||||||
// Verify that QueryResult fields are also populated
|
// Verify that QueryResult fields are also populated
|
||||||
assert.Equal(t, "Test content from model", result.Content)
|
assert.Equal(t, "Test content from model", result.Content)
|
||||||
assert.Equal(t, "Test reasoning process", result.Thought)
|
assert.Equal(t, "Test reasoning process", result.Thought)
|
||||||
|
})
|
||||||
|
|
||||||
t.Logf("Auto-converted data: %+v", typedData)
|
t.Run("DirectTypeAssertion", func(t *testing.T) {
|
||||||
}
|
|
||||||
|
|
||||||
// TestDirectTypeAssertion tests that users can directly use type assertion on QueryResult.Data
|
|
||||||
func TestDirectTypeAssertion(t *testing.T) {
|
|
||||||
// Test data structure
|
|
||||||
type GameInfo struct {
|
|
||||||
Content string `json:"content"`
|
|
||||||
Thought string `json:"thought"`
|
|
||||||
Rows int `json:"rows"`
|
|
||||||
Cols int `json:"cols"`
|
|
||||||
Icons []string `json:"icons"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Simulate a JSON response
|
// Simulate a JSON response
|
||||||
jsonResponse := `{
|
jsonResponse := `{
|
||||||
"content": "Game analysis complete",
|
"content": "Game analysis complete",
|
||||||
"thought": "Analyzed the game grid structure",
|
"thought": "Analyzed the game grid structure",
|
||||||
"rows": 8,
|
"count": 100,
|
||||||
"cols": 10,
|
"items": ["apple", "banana", "cherry", "grape"]
|
||||||
"icons": ["apple", "banana", "cherry", "grape"]
|
|
||||||
}`
|
}`
|
||||||
|
|
||||||
// Test the parseCustomSchemaResult function
|
// Test the parseCustomSchemaResult function
|
||||||
result, err := parseCustomSchemaResult(jsonResponse, GameInfo{})
|
result, err := parseCustomSchemaResult(jsonResponse, TestSchema{})
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.NotNil(t, result)
|
assert.NotNil(t, result)
|
||||||
assert.NotNil(t, result.Data)
|
assert.NotNil(t, result.Data)
|
||||||
|
|
||||||
// Users can now directly use type assertion
|
// Users can now directly use type assertion
|
||||||
if gameInfo, ok := result.Data.(*GameInfo); ok {
|
if testData, ok := result.Data.(*TestSchema); ok {
|
||||||
assert.Equal(t, "Game analysis complete", gameInfo.Content)
|
assert.Equal(t, "Game analysis complete", testData.Content)
|
||||||
assert.Equal(t, "Analyzed the game grid structure", gameInfo.Thought)
|
assert.Equal(t, "Analyzed the game grid structure", testData.Thought)
|
||||||
assert.Equal(t, 8, gameInfo.Rows)
|
assert.Equal(t, 100, testData.Count)
|
||||||
assert.Equal(t, 10, gameInfo.Cols)
|
assert.Equal(t, []string{"apple", "banana", "cherry", "grape"}, testData.Items)
|
||||||
assert.Equal(t, []string{"apple", "banana", "cherry", "grape"}, gameInfo.Icons)
|
|
||||||
t.Logf("Direct type assertion successful: %+v", gameInfo)
|
|
||||||
} else {
|
} else {
|
||||||
t.Fatalf("Type assertion failed, Data type: %T", result.Data)
|
t.Fatalf("Type assertion failed, Data type: %T", result.Data)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user