package main import ( "bytes" "encoding/json" "io" "net/http" "testing" "git.x2erp.com/qdy/go-base/model/request/queryreq" "git.x2erp.com/qdy/go-svc-configure/internal/service/dicmanagement" ) const ( testTableID = "test_table_001" testTableName = "测试表001" testTableDesc = "测试表描述" ) // TestDicTableCRUD 测试数据库表字典的增删改查 func TestDicTableCRUD(t *testing.T) { // 1. 创建数据库表字典 t.Run("CreateDicTable", testCreateDicTable) // 2. 查询单个数据库表字典详情 t.Run("GetDicTableDetail", testGetDicTableDetail) // 3. 查询数据库表字典列表 t.Run("ListDicTables", testListDicTables) // 4. 更新数据库表字典 t.Run("UpdateDicTable", testUpdateDicTable) // 5. 查询更新后的数据库表字典详情 t.Run("GetUpdatedDicTableDetail", testGetUpdatedDicTableDetail) // 6. 删除数据库表字典 t.Run("DeleteDicTable", testDeleteDicTable) // 7. 验证数据库表字典已删除 t.Run("VerifyDicTableDeleted", testVerifyDicTableDeleted) } func testCreateDicTable(t *testing.T) { httpClient := &http.Client{} url := baseURL + "/api/dic-table/save" // 创建主表和子表字段的请求 reqBody := dicmanagement.DicTableRequest{ TableID: testTableID, TableType: "实体表", Name: testTableName, Description: testTableDesc, Fields: []dicmanagement.DicTableFieldRequest{ { FieldID: testTableID + ".id", TableID: testTableID, FiledType: "实际字段", DataType: "数值型", FieldName: "id", FieldNameCN: "主键ID", Description: "主键字段", }, { FieldID: testTableID + ".name", TableID: testTableID, FiledType: "实际字段", DataType: "字符型", FieldName: "name", FieldNameCN: "名称", Description: "名称字段", }, { FieldID: testTableID + ".created_at", TableID: testTableID, FiledType: "实际字段", DataType: "日期型", FieldName: "created_at", FieldNameCN: "创建时间", Description: "记录创建时间", }, }, } jsonData, err := json.Marshal(reqBody) if err != nil { t.Fatalf("JSON序列化失败: %v", err) } req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) if err != nil { t.Fatalf("创建请求失败: %v", err) } // 使用Basic认证 req.SetBasicAuth("admin", "123") req.Header.Set("Content-Type", "application/json") resp, err := httpClient.Do(req) if err != nil { t.Fatalf("请求失败: %v", err) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { t.Fatalf("读取响应失败: %v", err) } var result map[string]interface{} if err := json.Unmarshal(body, &result); err != nil { t.Fatalf("JSON解析失败: %v", err) } if success, ok := result["success"].(bool); !ok || !success { t.Errorf("创建数据库表字典失败: %v", result) } // 验证返回的详情数据 if data, ok := result["data"].(map[string]interface{}); ok { if tableData, ok := data["table"].(map[string]interface{}); ok { if tableID, ok := tableData["tableID"].(string); !ok || tableID != testTableID { t.Errorf("表ID不匹配: 期望 %s, 实际 %v", testTableID, tableData["tableID"]) } } } t.Logf("创建数据库表字典成功: %s", string(body)) } func testGetDicTableDetail(t *testing.T) { httpClient := &http.Client{} url := baseURL + "/api/dic-table/detail/" + testTableID req, err := http.NewRequest("POST", url, nil) if err != nil { t.Fatalf("创建请求失败: %v", err) } req.SetBasicAuth("admin", "123") req.Header.Set("Content-Type", "application/json") resp, err := httpClient.Do(req) if err != nil { t.Fatalf("请求失败: %v", err) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { t.Fatalf("读取响应失败: %v", err) } var result map[string]interface{} if err := json.Unmarshal(body, &result); err != nil { t.Fatalf("JSON解析失败: %v", err) } if success, ok := result["success"].(bool); !ok || !success { t.Errorf("查询数据库表字典详情失败: %v", result) } // 验证详情信息 if data, ok := result["data"].(map[string]interface{}); ok { if tableData, ok := data["table"].(map[string]interface{}); ok { if tableID, ok := tableData["tableID"].(string); !ok || tableID != testTableID { t.Errorf("表ID不匹配: 期望 %s, 实际 %v", testTableID, tableData["tableID"]) } if tableType, ok := tableData["tableType"].(string); !ok || tableType != "实体表" { t.Errorf("表类型不匹配: 期望 '实体表', 实际 %v", tableData["tableType"]) } if name, ok := tableData["name"].(string); !ok || name != testTableName { t.Errorf("表名称不匹配: 期望 %s, 实际 %v", testTableName, tableData["name"]) } if description, ok := tableData["description"].(string); !ok || description != testTableDesc { t.Errorf("表描述不匹配: 期望 %s, 实际 %v", testTableDesc, tableData["description"]) } } // 验证字段列表 if fields, ok := data["fields"].([]interface{}); ok { if len(fields) != 3 { t.Errorf("字段数量不匹配: 期望 3, 实际 %d", len(fields)) } // 验证第一个字段 if len(fields) > 0 { field1 := fields[0].(map[string]interface{}) if fieldName, ok := field1["fieldName"].(string); !ok || fieldName != "id" { t.Errorf("第一个字段名称不匹配: 期望 'id', 实际 %v", field1["fieldName"]) } if fieldNameCN, ok := field1["fieldNameCN"].(string); !ok || fieldNameCN != "主键ID" { t.Errorf("第一个字段中文名称不匹配: 期望 '主键ID', 实际 %v", field1["fieldNameCN"]) } } } } t.Logf("查询数据库表字典详情成功: %s", string(body)) } func testListDicTables(t *testing.T) { httpClient := &http.Client{} url := baseURL + "/api/dic-table/list" // 创建查询请求 queryReq := dicmanagement.DicTableQueryRequest{ QueryRequest: queryreq.QueryRequest{ Page: 0, PageSize: 10, }, TableID: testTableID, } jsonData, err := json.Marshal(queryReq) if err != nil { t.Fatalf("JSON序列化失败: %v", err) } req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) if err != nil { t.Fatalf("创建请求失败: %v", err) } req.SetBasicAuth("admin", "123") req.Header.Set("Content-Type", "application/json") resp, err := httpClient.Do(req) if err != nil { t.Fatalf("请求失败: %v", err) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { t.Fatalf("读取响应失败: %v", err) } var result map[string]interface{} if err := json.Unmarshal(body, &result); err != nil { t.Fatalf("JSON解析失败: %v", err) } if success, ok := result["success"].(bool); !ok || !success { t.Errorf("查询数据库表字典列表失败: %v", result) } // 验证返回的项目列表包含测试表 if count, ok := result["totalCount"].(float64); !ok || count < 1 { t.Errorf("数据库表字典列表为空或计数错误: %v", result) } // 验证列表包含测试表 if data, ok := result["data"].([]interface{}); ok { found := false for _, item := range data { if table, ok := item.(map[string]interface{}); ok { if tableID, ok := table["tableID"].(string); ok && tableID == testTableID { found = true break } } } if !found { t.Errorf("数据库表字典列表未找到测试表: %s", testTableID) } } t.Logf("查询数据库表字典列表成功: %s", string(body)) } func testUpdateDicTable(t *testing.T) { httpClient := &http.Client{} url := baseURL + "/api/dic-table/save" // 更新请求,修改表描述和字段信息 reqBody := dicmanagement.DicTableRequest{ TableID: testTableID, TableType: "实体表", Name: "更新后的表名", Description: "更新后的表描述", Fields: []dicmanagement.DicTableFieldRequest{ { FieldID: testTableID + ".id", TableID: testTableID, FiledType: "实际字段", DataType: "数值型", FieldName: "id", FieldNameCN: "更新后的主键ID", Description: "更新后的主键字段描述", }, { FieldID: testTableID + ".status", TableID: testTableID, FiledType: "实际字段", DataType: "字符型", FieldName: "status", FieldNameCN: "状态", Description: "新增的状态字段", }, }, } jsonData, err := json.Marshal(reqBody) if err != nil { t.Fatalf("JSON序列化失败: %v", err) } req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) if err != nil { t.Fatalf("创建请求失败: %v", err) } req.SetBasicAuth("admin", "123") req.Header.Set("Content-Type", "application/json") resp, err := httpClient.Do(req) if err != nil { t.Fatalf("请求失败: %v", err) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { t.Fatalf("读取响应失败: %v", err) } var result map[string]interface{} if err := json.Unmarshal(body, &result); err != nil { t.Fatalf("JSON解析失败: %v", err) } if success, ok := result["success"].(bool); !ok || !success { t.Errorf("更新数据库表字典失败: %v", result) } t.Logf("更新数据库表字典成功: %s", string(body)) } func testGetUpdatedDicTableDetail(t *testing.T) { httpClient := &http.Client{} url := baseURL + "/api/dic-table/detail/" + testTableID req, err := http.NewRequest("POST", url, nil) if err != nil { t.Fatalf("创建请求失败: %v", err) } req.SetBasicAuth("admin", "123") req.Header.Set("Content-Type", "application/json") resp, err := httpClient.Do(req) if err != nil { t.Fatalf("请求失败: %v", err) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { t.Fatalf("读取响应失败: %v", err) } var result map[string]interface{} if err := json.Unmarshal(body, &result); err != nil { t.Fatalf("JSON解析失败: %v", err) } if success, ok := result["success"].(bool); !ok || !success { t.Errorf("查询更新后的数据库表字典详情失败: %v", result) } // 验证更新后的信息 if data, ok := result["data"].(map[string]interface{}); ok { if tableData, ok := data["table"].(map[string]interface{}); ok { if name, ok := tableData["name"].(string); !ok || name != "更新后的表名" { t.Errorf("表名称未更新: 期望 '更新后的表名', 实际 %v", tableData["name"]) } if description, ok := tableData["description"].(string); !ok || description != "更新后的表描述" { t.Errorf("表描述未更新: 期望 '更新后的表描述', 实际 %v", tableData["description"]) } } // 验证字段列表已更新(应该只有2个字段,且字段信息已更新) if fields, ok := data["fields"].([]interface{}); ok { if len(fields) != 2 { t.Errorf("更新后字段数量不匹配: 期望 2, 实际 %d", len(fields)) } // 验证字段信息已更新 for _, field := range fields { fieldMap := field.(map[string]interface{}) if fieldName, ok := fieldMap["fieldName"].(string); ok { if fieldName == "id" { if fieldNameCN, ok := fieldMap["fieldNameCN"].(string); !ok || fieldNameCN != "更新后的主键ID" { t.Errorf("id字段中文名称未更新: 期望 '更新后的主键ID', 实际 %v", fieldMap["fieldNameCN"]) } if description, ok := fieldMap["description"].(string); !ok || description != "更新后的主键字段描述" { t.Errorf("id字段描述未更新: 期望 '更新后的主键字段描述', 实际 %v", fieldMap["description"]) } } else if fieldName == "status" { if fieldNameCN, ok := fieldMap["fieldNameCN"].(string); !ok || fieldNameCN != "状态" { t.Errorf("status字段中文名称不匹配: 期望 '状态', 实际 %v", fieldMap["fieldNameCN"]) } } } } } } t.Logf("查询更新后的数据库表字典详情成功: %s", string(body)) } func testDeleteDicTable(t *testing.T) { httpClient := &http.Client{} url := baseURL + "/api/dic-table/delete/" + testTableID req, err := http.NewRequest("POST", url, nil) if err != nil { t.Fatalf("创建请求失败: %v", err) } // 删除操作使用Basic认证 req.SetBasicAuth("admin", "123") req.Header.Set("Content-Type", "application/json") resp, err := httpClient.Do(req) if err != nil { t.Fatalf("请求失败: %v", err) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { t.Fatalf("读取响应失败: %v", err) } var result map[string]interface{} if err := json.Unmarshal(body, &result); err != nil { t.Fatalf("JSON解析失败: %v", err) } if success, ok := result["success"].(bool); !ok || !success { t.Errorf("删除数据库表字典失败: %v", result) } t.Logf("删除数据库表字典成功: %s", string(body)) } func testVerifyDicTableDeleted(t *testing.T) { httpClient := &http.Client{} url := baseURL + "/api/dic-table/detail/" + testTableID req, err := http.NewRequest("POST", url, nil) if err != nil { t.Fatalf("创建请求失败: %v", err) } req.SetBasicAuth("admin", "123") req.Header.Set("Content-Type", "application/json") resp, err := httpClient.Do(req) if err != nil { t.Fatalf("请求失败: %v", err) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { t.Fatalf("读取响应失败: %v", err) } var result map[string]interface{} if err := json.Unmarshal(body, &result); err != nil { t.Fatalf("JSON解析失败: %v", err) } // 期望查询失败,因为数据库表字典已删除 if success, ok := result["success"].(bool); ok && success { t.Errorf("数据库表字典删除验证失败: 数据库表字典仍存在") } t.Logf("数据库表字典删除验证成功: 数据库表字典已不存在") }