feat(database): 实现数据库功能并优化数据导出
- 新增数据库相关 API 和服务 - 实现数据导出功能,支持导出到 JSON 文件 - 优化数据导入流程,增加数据校验 - 新增数据库页面,展示解析数据和统计信息 - 更新捕获页面,支持导入数据到数据库
This commit is contained in:
@@ -1,85 +1,13 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"equipment-analyzer/internal/config"
|
||||
"equipment-analyzer/internal/utils"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestReadRawJsonFile(t *testing.T) {
|
||||
// 创建测试用的配置和日志器
|
||||
config := &config.Config{}
|
||||
logger := utils.NewLogger()
|
||||
|
||||
// 创建ParserService实例
|
||||
ps := NewParserService(config, logger)
|
||||
|
||||
// 调用ReadRawJsonFile方法
|
||||
result, err := ps.ReadRawJsonFile()
|
||||
if err != nil {
|
||||
t.Fatalf("ReadRawJsonFile failed: %v", err)
|
||||
}
|
||||
|
||||
fmt.Printf("Raw result length: %d\n", len(result))
|
||||
fmt.Printf("Raw result preview: %s\n", result[:min(200, len(result))])
|
||||
|
||||
// 解析JSON结果
|
||||
var parsedData map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(result), &parsedData); err != nil {
|
||||
t.Fatalf("Failed to parse JSON result: %v", err)
|
||||
}
|
||||
|
||||
// 检查数据结构
|
||||
fmt.Printf("Parsed data keys: %v\n", getKeys(parsedData))
|
||||
|
||||
// 检查items字段
|
||||
if items, exists := parsedData["items"]; exists {
|
||||
if itemsArray, ok := items.([]interface{}); ok {
|
||||
fmt.Printf("Items count: %d\n", len(itemsArray))
|
||||
if len(itemsArray) > 0 {
|
||||
fmt.Printf("First item: %+v\n", itemsArray[0])
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("Items is not an array: %T\n", items)
|
||||
}
|
||||
} else {
|
||||
fmt.Println("Items field not found")
|
||||
}
|
||||
|
||||
// 检查heroes字段
|
||||
if heroes, exists := parsedData["heroes"]; exists {
|
||||
if heroesArray, ok := heroes.([]interface{}); ok {
|
||||
fmt.Printf("Heroes count: %d\n", len(heroesArray))
|
||||
if len(heroesArray) > 0 {
|
||||
fmt.Printf("First hero: %+v\n", heroesArray[0])
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("Heroes is not an array: %T\n", heroes)
|
||||
}
|
||||
} else {
|
||||
fmt.Println("Heroes field not found")
|
||||
}
|
||||
|
||||
// 如果没有数据,输出更多调试信息
|
||||
if len(result) < 100 {
|
||||
fmt.Printf("Result seems empty or very short: %q\n", result)
|
||||
}
|
||||
// 此测试已废弃,因为ReadRawJsonFile方法已被移除
|
||||
// 现在数据存储在SQLite数据库中,不再依赖output_raw.json文件
|
||||
t.Skip("ReadRawJsonFile测试已废弃,数据现在存储在SQLite数据库中")
|
||||
}
|
||||
|
||||
// 辅助函数
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func getKeys(m map[string]interface{}) []string {
|
||||
keys := make([]string, 0, len(m))
|
||||
for k := range m {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
return keys
|
||||
}
|
||||
// 辅助函数已移除,因为测试已废弃
|
||||
|
||||
Reference in New Issue
Block a user