Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io"
  6. "net/http"
  7. "testing"
  8. "git.x2erp.com/qdy/go-base/model/request/queryreq"
  9. "git.x2erp.com/qdy/go-svc-configure/internal/service/dicmanagement"
  10. )
  11. const (
  12. testTableID = "test_table_001"
  13. testTableName = "测试表001"
  14. testTableDesc = "测试表描述"
  15. )
  16. // TestDicTableCRUD 测试数据库表字典的增删改查
  17. func TestDicTableCRUD(t *testing.T) {
  18. // 1. 创建数据库表字典
  19. t.Run("CreateDicTable", testCreateDicTable)
  20. // 2. 查询单个数据库表字典详情
  21. t.Run("GetDicTableDetail", testGetDicTableDetail)
  22. // 3. 查询数据库表字典列表
  23. t.Run("ListDicTables", testListDicTables)
  24. // 4. 更新数据库表字典
  25. t.Run("UpdateDicTable", testUpdateDicTable)
  26. // 5. 查询更新后的数据库表字典详情
  27. t.Run("GetUpdatedDicTableDetail", testGetUpdatedDicTableDetail)
  28. // 6. 删除数据库表字典
  29. t.Run("DeleteDicTable", testDeleteDicTable)
  30. // 7. 验证数据库表字典已删除
  31. t.Run("VerifyDicTableDeleted", testVerifyDicTableDeleted)
  32. }
  33. func testCreateDicTable(t *testing.T) {
  34. httpClient := &http.Client{}
  35. url := baseURL + "/api/dic-table/save"
  36. // 创建主表和子表字段的请求
  37. reqBody := dicmanagement.DicTableRequest{
  38. TableID: testTableID,
  39. TableType: "实体表",
  40. Name: testTableName,
  41. Description: testTableDesc,
  42. Fields: []dicmanagement.DicTableFieldRequest{
  43. {
  44. FieldID: testTableID + ".id",
  45. TableID: testTableID,
  46. FiledType: "实际字段",
  47. DataType: "数值型",
  48. FieldName: "id",
  49. FieldNameCN: "主键ID",
  50. Description: "主键字段",
  51. },
  52. {
  53. FieldID: testTableID + ".name",
  54. TableID: testTableID,
  55. FiledType: "实际字段",
  56. DataType: "字符型",
  57. FieldName: "name",
  58. FieldNameCN: "名称",
  59. Description: "名称字段",
  60. },
  61. {
  62. FieldID: testTableID + ".created_at",
  63. TableID: testTableID,
  64. FiledType: "实际字段",
  65. DataType: "日期型",
  66. FieldName: "created_at",
  67. FieldNameCN: "创建时间",
  68. Description: "记录创建时间",
  69. },
  70. },
  71. }
  72. jsonData, err := json.Marshal(reqBody)
  73. if err != nil {
  74. t.Fatalf("JSON序列化失败: %v", err)
  75. }
  76. req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
  77. if err != nil {
  78. t.Fatalf("创建请求失败: %v", err)
  79. }
  80. // 使用Basic认证
  81. req.SetBasicAuth("admin", "123")
  82. req.Header.Set("Content-Type", "application/json")
  83. resp, err := httpClient.Do(req)
  84. if err != nil {
  85. t.Fatalf("请求失败: %v", err)
  86. }
  87. defer resp.Body.Close()
  88. body, err := io.ReadAll(resp.Body)
  89. if err != nil {
  90. t.Fatalf("读取响应失败: %v", err)
  91. }
  92. var result map[string]interface{}
  93. if err := json.Unmarshal(body, &result); err != nil {
  94. t.Fatalf("JSON解析失败: %v", err)
  95. }
  96. if success, ok := result["success"].(bool); !ok || !success {
  97. t.Errorf("创建数据库表字典失败: %v", result)
  98. }
  99. // 验证返回的详情数据
  100. if data, ok := result["data"].(map[string]interface{}); ok {
  101. if tableData, ok := data["table"].(map[string]interface{}); ok {
  102. if tableID, ok := tableData["tableID"].(string); !ok || tableID != testTableID {
  103. t.Errorf("表ID不匹配: 期望 %s, 实际 %v", testTableID, tableData["tableID"])
  104. }
  105. }
  106. }
  107. t.Logf("创建数据库表字典成功: %s", string(body))
  108. }
  109. func testGetDicTableDetail(t *testing.T) {
  110. httpClient := &http.Client{}
  111. url := baseURL + "/api/dic-table/detail/" + testTableID
  112. req, err := http.NewRequest("POST", url, nil)
  113. if err != nil {
  114. t.Fatalf("创建请求失败: %v", err)
  115. }
  116. req.SetBasicAuth("admin", "123")
  117. req.Header.Set("Content-Type", "application/json")
  118. resp, err := httpClient.Do(req)
  119. if err != nil {
  120. t.Fatalf("请求失败: %v", err)
  121. }
  122. defer resp.Body.Close()
  123. body, err := io.ReadAll(resp.Body)
  124. if err != nil {
  125. t.Fatalf("读取响应失败: %v", err)
  126. }
  127. var result map[string]interface{}
  128. if err := json.Unmarshal(body, &result); err != nil {
  129. t.Fatalf("JSON解析失败: %v", err)
  130. }
  131. if success, ok := result["success"].(bool); !ok || !success {
  132. t.Errorf("查询数据库表字典详情失败: %v", result)
  133. }
  134. // 验证详情信息
  135. if data, ok := result["data"].(map[string]interface{}); ok {
  136. if tableData, ok := data["table"].(map[string]interface{}); ok {
  137. if tableID, ok := tableData["tableID"].(string); !ok || tableID != testTableID {
  138. t.Errorf("表ID不匹配: 期望 %s, 实际 %v", testTableID, tableData["tableID"])
  139. }
  140. if tableType, ok := tableData["tableType"].(string); !ok || tableType != "实体表" {
  141. t.Errorf("表类型不匹配: 期望 '实体表', 实际 %v", tableData["tableType"])
  142. }
  143. if name, ok := tableData["name"].(string); !ok || name != testTableName {
  144. t.Errorf("表名称不匹配: 期望 %s, 实际 %v", testTableName, tableData["name"])
  145. }
  146. if description, ok := tableData["description"].(string); !ok || description != testTableDesc {
  147. t.Errorf("表描述不匹配: 期望 %s, 实际 %v", testTableDesc, tableData["description"])
  148. }
  149. }
  150. // 验证字段列表
  151. if fields, ok := data["fields"].([]interface{}); ok {
  152. if len(fields) != 3 {
  153. t.Errorf("字段数量不匹配: 期望 3, 实际 %d", len(fields))
  154. }
  155. // 验证第一个字段
  156. if len(fields) > 0 {
  157. field1 := fields[0].(map[string]interface{})
  158. if fieldName, ok := field1["fieldName"].(string); !ok || fieldName != "id" {
  159. t.Errorf("第一个字段名称不匹配: 期望 'id', 实际 %v", field1["fieldName"])
  160. }
  161. if fieldNameCN, ok := field1["fieldNameCN"].(string); !ok || fieldNameCN != "主键ID" {
  162. t.Errorf("第一个字段中文名称不匹配: 期望 '主键ID', 实际 %v", field1["fieldNameCN"])
  163. }
  164. }
  165. }
  166. }
  167. t.Logf("查询数据库表字典详情成功: %s", string(body))
  168. }
  169. func testListDicTables(t *testing.T) {
  170. httpClient := &http.Client{}
  171. url := baseURL + "/api/dic-table/list"
  172. // 创建查询请求
  173. queryReq := dicmanagement.DicTableQueryRequest{
  174. QueryRequest: queryreq.QueryRequest{
  175. Page: 0,
  176. PageSize: 10,
  177. },
  178. TableID: testTableID,
  179. }
  180. jsonData, err := json.Marshal(queryReq)
  181. if err != nil {
  182. t.Fatalf("JSON序列化失败: %v", err)
  183. }
  184. req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
  185. if err != nil {
  186. t.Fatalf("创建请求失败: %v", err)
  187. }
  188. req.SetBasicAuth("admin", "123")
  189. req.Header.Set("Content-Type", "application/json")
  190. resp, err := httpClient.Do(req)
  191. if err != nil {
  192. t.Fatalf("请求失败: %v", err)
  193. }
  194. defer resp.Body.Close()
  195. body, err := io.ReadAll(resp.Body)
  196. if err != nil {
  197. t.Fatalf("读取响应失败: %v", err)
  198. }
  199. var result map[string]interface{}
  200. if err := json.Unmarshal(body, &result); err != nil {
  201. t.Fatalf("JSON解析失败: %v", err)
  202. }
  203. if success, ok := result["success"].(bool); !ok || !success {
  204. t.Errorf("查询数据库表字典列表失败: %v", result)
  205. }
  206. // 验证返回的项目列表包含测试表
  207. if count, ok := result["totalCount"].(float64); !ok || count < 1 {
  208. t.Errorf("数据库表字典列表为空或计数错误: %v", result)
  209. }
  210. // 验证列表包含测试表
  211. if data, ok := result["data"].([]interface{}); ok {
  212. found := false
  213. for _, item := range data {
  214. if table, ok := item.(map[string]interface{}); ok {
  215. if tableID, ok := table["tableID"].(string); ok && tableID == testTableID {
  216. found = true
  217. break
  218. }
  219. }
  220. }
  221. if !found {
  222. t.Errorf("数据库表字典列表未找到测试表: %s", testTableID)
  223. }
  224. }
  225. t.Logf("查询数据库表字典列表成功: %s", string(body))
  226. }
  227. func testUpdateDicTable(t *testing.T) {
  228. httpClient := &http.Client{}
  229. url := baseURL + "/api/dic-table/save"
  230. // 更新请求,修改表描述和字段信息
  231. reqBody := dicmanagement.DicTableRequest{
  232. TableID: testTableID,
  233. TableType: "实体表",
  234. Name: "更新后的表名",
  235. Description: "更新后的表描述",
  236. Fields: []dicmanagement.DicTableFieldRequest{
  237. {
  238. FieldID: testTableID + ".id",
  239. TableID: testTableID,
  240. FiledType: "实际字段",
  241. DataType: "数值型",
  242. FieldName: "id",
  243. FieldNameCN: "更新后的主键ID",
  244. Description: "更新后的主键字段描述",
  245. },
  246. {
  247. FieldID: testTableID + ".status",
  248. TableID: testTableID,
  249. FiledType: "实际字段",
  250. DataType: "字符型",
  251. FieldName: "status",
  252. FieldNameCN: "状态",
  253. Description: "新增的状态字段",
  254. },
  255. },
  256. }
  257. jsonData, err := json.Marshal(reqBody)
  258. if err != nil {
  259. t.Fatalf("JSON序列化失败: %v", err)
  260. }
  261. req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
  262. if err != nil {
  263. t.Fatalf("创建请求失败: %v", err)
  264. }
  265. req.SetBasicAuth("admin", "123")
  266. req.Header.Set("Content-Type", "application/json")
  267. resp, err := httpClient.Do(req)
  268. if err != nil {
  269. t.Fatalf("请求失败: %v", err)
  270. }
  271. defer resp.Body.Close()
  272. body, err := io.ReadAll(resp.Body)
  273. if err != nil {
  274. t.Fatalf("读取响应失败: %v", err)
  275. }
  276. var result map[string]interface{}
  277. if err := json.Unmarshal(body, &result); err != nil {
  278. t.Fatalf("JSON解析失败: %v", err)
  279. }
  280. if success, ok := result["success"].(bool); !ok || !success {
  281. t.Errorf("更新数据库表字典失败: %v", result)
  282. }
  283. t.Logf("更新数据库表字典成功: %s", string(body))
  284. }
  285. func testGetUpdatedDicTableDetail(t *testing.T) {
  286. httpClient := &http.Client{}
  287. url := baseURL + "/api/dic-table/detail/" + testTableID
  288. req, err := http.NewRequest("POST", url, nil)
  289. if err != nil {
  290. t.Fatalf("创建请求失败: %v", err)
  291. }
  292. req.SetBasicAuth("admin", "123")
  293. req.Header.Set("Content-Type", "application/json")
  294. resp, err := httpClient.Do(req)
  295. if err != nil {
  296. t.Fatalf("请求失败: %v", err)
  297. }
  298. defer resp.Body.Close()
  299. body, err := io.ReadAll(resp.Body)
  300. if err != nil {
  301. t.Fatalf("读取响应失败: %v", err)
  302. }
  303. var result map[string]interface{}
  304. if err := json.Unmarshal(body, &result); err != nil {
  305. t.Fatalf("JSON解析失败: %v", err)
  306. }
  307. if success, ok := result["success"].(bool); !ok || !success {
  308. t.Errorf("查询更新后的数据库表字典详情失败: %v", result)
  309. }
  310. // 验证更新后的信息
  311. if data, ok := result["data"].(map[string]interface{}); ok {
  312. if tableData, ok := data["table"].(map[string]interface{}); ok {
  313. if name, ok := tableData["name"].(string); !ok || name != "更新后的表名" {
  314. t.Errorf("表名称未更新: 期望 '更新后的表名', 实际 %v", tableData["name"])
  315. }
  316. if description, ok := tableData["description"].(string); !ok || description != "更新后的表描述" {
  317. t.Errorf("表描述未更新: 期望 '更新后的表描述', 实际 %v", tableData["description"])
  318. }
  319. }
  320. // 验证字段列表已更新(应该只有2个字段,且字段信息已更新)
  321. if fields, ok := data["fields"].([]interface{}); ok {
  322. if len(fields) != 2 {
  323. t.Errorf("更新后字段数量不匹配: 期望 2, 实际 %d", len(fields))
  324. }
  325. // 验证字段信息已更新
  326. for _, field := range fields {
  327. fieldMap := field.(map[string]interface{})
  328. if fieldName, ok := fieldMap["fieldName"].(string); ok {
  329. if fieldName == "id" {
  330. if fieldNameCN, ok := fieldMap["fieldNameCN"].(string); !ok || fieldNameCN != "更新后的主键ID" {
  331. t.Errorf("id字段中文名称未更新: 期望 '更新后的主键ID', 实际 %v", fieldMap["fieldNameCN"])
  332. }
  333. if description, ok := fieldMap["description"].(string); !ok || description != "更新后的主键字段描述" {
  334. t.Errorf("id字段描述未更新: 期望 '更新后的主键字段描述', 实际 %v", fieldMap["description"])
  335. }
  336. } else if fieldName == "status" {
  337. if fieldNameCN, ok := fieldMap["fieldNameCN"].(string); !ok || fieldNameCN != "状态" {
  338. t.Errorf("status字段中文名称不匹配: 期望 '状态', 实际 %v", fieldMap["fieldNameCN"])
  339. }
  340. }
  341. }
  342. }
  343. }
  344. }
  345. t.Logf("查询更新后的数据库表字典详情成功: %s", string(body))
  346. }
  347. func testDeleteDicTable(t *testing.T) {
  348. httpClient := &http.Client{}
  349. url := baseURL + "/api/dic-table/delete/" + testTableID
  350. req, err := http.NewRequest("POST", url, nil)
  351. if err != nil {
  352. t.Fatalf("创建请求失败: %v", err)
  353. }
  354. // 删除操作使用Basic认证
  355. req.SetBasicAuth("admin", "123")
  356. req.Header.Set("Content-Type", "application/json")
  357. resp, err := httpClient.Do(req)
  358. if err != nil {
  359. t.Fatalf("请求失败: %v", err)
  360. }
  361. defer resp.Body.Close()
  362. body, err := io.ReadAll(resp.Body)
  363. if err != nil {
  364. t.Fatalf("读取响应失败: %v", err)
  365. }
  366. var result map[string]interface{}
  367. if err := json.Unmarshal(body, &result); err != nil {
  368. t.Fatalf("JSON解析失败: %v", err)
  369. }
  370. if success, ok := result["success"].(bool); !ok || !success {
  371. t.Errorf("删除数据库表字典失败: %v", result)
  372. }
  373. t.Logf("删除数据库表字典成功: %s", string(body))
  374. }
  375. func testVerifyDicTableDeleted(t *testing.T) {
  376. httpClient := &http.Client{}
  377. url := baseURL + "/api/dic-table/detail/" + testTableID
  378. req, err := http.NewRequest("POST", url, nil)
  379. if err != nil {
  380. t.Fatalf("创建请求失败: %v", err)
  381. }
  382. req.SetBasicAuth("admin", "123")
  383. req.Header.Set("Content-Type", "application/json")
  384. resp, err := httpClient.Do(req)
  385. if err != nil {
  386. t.Fatalf("请求失败: %v", err)
  387. }
  388. defer resp.Body.Close()
  389. body, err := io.ReadAll(resp.Body)
  390. if err != nil {
  391. t.Fatalf("读取响应失败: %v", err)
  392. }
  393. var result map[string]interface{}
  394. if err := json.Unmarshal(body, &result); err != nil {
  395. t.Fatalf("JSON解析失败: %v", err)
  396. }
  397. // 期望查询失败,因为数据库表字典已删除
  398. if success, ok := result["success"].(bool); ok && success {
  399. t.Errorf("数据库表字典删除验证失败: 数据库表字典仍存在")
  400. }
  401. t.Logf("数据库表字典删除验证成功: 数据库表字典已不存在")
  402. }