v1_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. package test
  2. import (
  3. "fmt"
  4. "git.sxidc.com/go-tools/api_binding/http_binding"
  5. "git.sxidc.com/go-tools/api_binding/http_binding/binding_context"
  6. "git.sxidc.com/go-tools/api_binding/http_binding/response"
  7. "git.sxidc.com/service-supports/dps-sdk"
  8. "git.sxidc.com/service-supports/dps-sdk/client"
  9. "git.sxidc.com/service-supports/dps-sql/api"
  10. "github.com/go-resty/resty/v2"
  11. "math/rand"
  12. "testing"
  13. "time"
  14. )
  15. func TestApiV1OperateParse(t *testing.T) {
  16. http_binding.Init("test", "10086")
  17. defer http_binding.Destroy()
  18. operatorID := simpleUUID()
  19. tableName := "students"
  20. exceptedTableRows := map[string]any{
  21. "id": "aaa",
  22. "name": "yjp",
  23. "age": float64(5),
  24. "rate": 92.5,
  25. "time": "2024-01-01T00:00:00+08:00",
  26. "is_right": false,
  27. }
  28. binding := http_binding.NewBinding("v1")
  29. api.ApiV1(binding, dpsAddress, func(c *binding_context.Context) (string, error) {
  30. return operatorID, nil
  31. })
  32. parsed := operateParse(t, parseSqlInsert)
  33. if parsed["table"] != tableName {
  34. t.Fatal("表名不正确")
  35. }
  36. for columnName, value := range parsed["tableRow"].(map[string]any) {
  37. if exceptedTableRows[columnName] != value {
  38. t.Fatal(columnName + "行数据不正确")
  39. }
  40. }
  41. parsed = operateParse(t, parseSqlUpdate)
  42. if parsed["table"] != tableName {
  43. t.Fatal("表名不正确")
  44. }
  45. if parsed["where"] != `(((id = 'aaa') AND (name = 'yjp')) AND (age < 100)) AND (describe IN ('yjp',))` {
  46. t.Fatal("where不正确")
  47. }
  48. for columnName, value := range parsed["newTableRow"].(map[string]any) {
  49. if exceptedTableRows[columnName] != value {
  50. t.Fatal(columnName + "行数据不正确")
  51. }
  52. }
  53. parsed = operateParse(t, parseSqlDelete)
  54. if parsed["table"] != tableName {
  55. t.Fatal("表名不正确")
  56. }
  57. if parsed["where"] != `(((id = 'aaa') AND (name = 'yjp')) AND (age < 100)) AND (describe IN ('yjp',))` {
  58. t.Fatal("where不正确")
  59. }
  60. parsed = operateParse(t, parseSqlSelectWithJoin)
  61. if parsed["table"] != "" {
  62. t.Fatal("table不正确")
  63. }
  64. if parsed["fromSubQuery"] != `students LEFT JOIN classes ON students.id = classes.student_id LEFT JOIN identities ON students.id = identities.student_id` {
  65. t.Fatal("fromSubQuery不正确")
  66. }
  67. if parsed["selectClause"] != "id, name, age" {
  68. t.Fatal("select语句不正确")
  69. }
  70. if parsed["where"] != `students.id = 'aaa'` {
  71. t.Fatal("where不正确")
  72. }
  73. if parsed["orderBy"] != "students.name DESC" {
  74. t.Fatal("orderBy不正确")
  75. }
  76. if parsed["groupBy"] != "" {
  77. t.Fatal("groupBy不正确")
  78. }
  79. if parsed["having"] != "" {
  80. t.Fatal("having不正确")
  81. }
  82. if parsed["pageNo"] != float64(2) {
  83. t.Fatal("pageNo不正确")
  84. }
  85. if parsed["pageSize"] != float64(20) {
  86. t.Fatal("pageSize不正确")
  87. }
  88. parsed = operateParse(t, parseSqlSelectWithSubQuery)
  89. if parsed["table"] != "" {
  90. t.Fatal("table不正确")
  91. }
  92. if parsed["fromSubQuery"] != `(SELECT * FROM class WHERE id = 'bbb')` {
  93. t.Fatal("fromSubQuery不正确")
  94. }
  95. if parsed["selectClause"] != "" {
  96. t.Fatal("select语句不正确")
  97. }
  98. if parsed["where"] != `(students.id = 'aaa') AND (students.age = 3)` {
  99. t.Fatal("where不正确")
  100. }
  101. if parsed["orderBy"] != "" {
  102. t.Fatal("orderBy不正确")
  103. }
  104. if parsed["groupBy"] != "" {
  105. t.Fatal("groupBy不正确")
  106. }
  107. if parsed["having"] != "" {
  108. t.Fatal("having不正确")
  109. }
  110. if parsed["pageNo"] != float64(0) {
  111. t.Fatal("pageNo不正确")
  112. }
  113. if parsed["pageSize"] != float64(0) {
  114. t.Fatal("pageSize不正确")
  115. }
  116. parsed = operateParse(t, parseSqlSelectWithGroupBy)
  117. if parsed["table"] != tableName {
  118. t.Fatal("table不正确")
  119. }
  120. if parsed["fromSubQuery"] != "" {
  121. t.Fatal("fromSubQuery不正确")
  122. }
  123. if parsed["selectClause"] != "count(*)" {
  124. t.Fatal("select语句不正确")
  125. }
  126. if parsed["where"] != "" {
  127. t.Fatal("where不正确")
  128. }
  129. if parsed["orderBy"] != "" {
  130. t.Fatal("orderBy不正确")
  131. }
  132. if parsed["groupBy"] == "name, age" {
  133. t.Fatal("groupBy不正确")
  134. }
  135. if parsed["having"] != "(name = 'aaa') AND (age = 5)" {
  136. t.Fatal("having不正确")
  137. }
  138. if parsed["pageNo"] != float64(0) {
  139. t.Fatal("pageNo不正确")
  140. }
  141. if parsed["pageSize"] != float64(0) {
  142. t.Fatal("pageSize不正确")
  143. }
  144. }
  145. func TestApiV1Operate(t *testing.T) {
  146. http_binding.Init("test", "10086")
  147. defer http_binding.Destroy()
  148. operatorID := simpleUUID()
  149. tablePrefix := "test.a" + simpleUUID()[0:7]
  150. id := simpleUUID()
  151. name := simpleUUID()
  152. now := time.Now().Local()
  153. tableNum := rand.Intn(10) + 1
  154. newName := simpleUUID()
  155. newNow := time.Now().Local()
  156. newTableNum := rand.Intn(10) + 1
  157. keyColumns := []string{"id"}
  158. autoMigrate(t, []client.AutoMigrateItem{
  159. {
  160. TablePrefixWithSchema: tablePrefix,
  161. Version: "v1",
  162. TableModelDescribe: tableModelDescribe,
  163. },
  164. })
  165. binding := http_binding.NewBinding("v1")
  166. api.ApiV1(binding, dpsAddress, func(c *binding_context.Context) (string, error) {
  167. return operatorID, nil
  168. })
  169. operate(t, fmt.Sprintf(sqlInsertFormat, tablePrefix, id, name, now.Format(time.DateTime), tableNum), keyColumns)
  170. tableRows := operate(t, fmt.Sprintf(sqlSelectFormat, tablePrefix, id), nil)
  171. if tableRows[0]["id"] != id {
  172. t.Fatal("id不正确")
  173. }
  174. if tableRows[0]["name"] != name {
  175. t.Fatal("name不正确")
  176. }
  177. if tableRows[0]["time"] != now.Format("2006-01-02T15:04:05+08:00") {
  178. t.Fatal("time不正确")
  179. }
  180. if tableRows[0]["table_num"] != float64(tableNum) {
  181. t.Fatal("tableNum不正确")
  182. }
  183. tableRows = operate(t, fmt.Sprintf(sqlCountFormat, tablePrefix, id), nil)
  184. if tableRows[0]["count"] != float64(1) {
  185. t.Fatal("数量不正确")
  186. }
  187. operate(t, fmt.Sprintf(sqlUpdateFormat, tablePrefix, newName, newNow.Format(time.DateTime), newTableNum, id), keyColumns)
  188. tableRows = operate(t, fmt.Sprintf(sqlSelectFormat, tablePrefix, id), nil)
  189. if tableRows[0]["id"] != id {
  190. t.Fatal("id不正确")
  191. }
  192. if tableRows[0]["name"] != newName {
  193. t.Fatal("name不正确")
  194. }
  195. if tableRows[0]["time"] != newNow.Format("2006-01-02T15:04:05+08:00") {
  196. t.Fatal("time不正确")
  197. }
  198. if tableRows[0]["table_num"] != float64(newTableNum) {
  199. t.Fatal("tableNum不正确")
  200. }
  201. tableRows = operate(t, fmt.Sprintf(sqlCountFormat, tablePrefix, id), nil)
  202. if tableRows[0]["count"] != float64(1) {
  203. t.Fatal("数量不正确")
  204. }
  205. operate(t, fmt.Sprintf(sqlDeleteFormat, tablePrefix, id), keyColumns)
  206. tableRows = operate(t, fmt.Sprintf(sqlCountFormat, tablePrefix, id), nil)
  207. if tableRows[0]["count"] != float64(0) {
  208. t.Fatal("数量不正确")
  209. }
  210. }
  211. func autoMigrate(t *testing.T, items []client.AutoMigrateItem) {
  212. dpsClient, err := dps.NewClient(dpsAddress, "v1", testDatabaseID)
  213. if err != nil {
  214. t.Fatal(err)
  215. }
  216. err = dpsClient.AutoMigrate(&client.AutoMigrateRequest{Items: items})
  217. if err != nil {
  218. t.Fatal(err)
  219. }
  220. }
  221. func operateParse(t *testing.T, sql string) map[string]any {
  222. result := new(struct {
  223. response.MsgResponse
  224. Parsed map[string]any `json:"parsed"`
  225. })
  226. resp, err := resty.New().R().
  227. SetBody(&api.OperateParseRequest{
  228. SQL: sql,
  229. }).
  230. SetResult(result).
  231. Post("http://localhost:10086/test/api/v1/dpsv1/database/operate/parse")
  232. if err != nil {
  233. t.Fatal(err)
  234. }
  235. if resp.IsError() {
  236. t.Fatal(resp.Status())
  237. }
  238. if !result.Success {
  239. t.Fatal(result.Msg)
  240. }
  241. return result.Parsed
  242. }
  243. func operate(t *testing.T, sql string, keyColumns []string) []map[string]any {
  244. result := new(struct {
  245. response.MsgResponse
  246. TableRows []map[string]any `json:"tableRows"`
  247. })
  248. resp, err := resty.New().R().
  249. SetBody(&api.OperateRequest{
  250. DatabaseID: testDatabaseID,
  251. Version: "v1",
  252. KeyColumns: keyColumns,
  253. SQL: sql,
  254. }).
  255. SetResult(result).
  256. Post("http://localhost:10086/test/api/v1/dpsv1/database/operate")
  257. if err != nil {
  258. t.Fatal(err)
  259. }
  260. if resp.IsError() {
  261. t.Fatal(resp.Status())
  262. }
  263. if !result.Success {
  264. t.Fatal(result.Msg)
  265. }
  266. return result.TableRows
  267. }