v1_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package dpsapi
  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. "github.com/go-resty/resty/v2"
  10. "math/rand"
  11. "testing"
  12. "time"
  13. )
  14. func TestApiV1OperateParse(t *testing.T) {
  15. http_binding.Init("test", "10086")
  16. defer http_binding.Destroy()
  17. operatorID := simpleUUID()
  18. tableName := "students"
  19. exceptedKeyColumns := []string{"id", "name", "age", "rate", "time", "is_right"}
  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. ApiV1(binding, dpsAddress, func(c *binding_context.Context) (string, error) {
  30. return operatorID, nil
  31. })
  32. parsed := operateParse(t, parseSqlInsert)
  33. if parsed["table"].(string) != tableName {
  34. t.Fatal("表名不正确")
  35. }
  36. for i, keyColumn := range parsed["key_columns"].([]any) {
  37. if exceptedKeyColumns[i] != keyColumn {
  38. t.Fatal("没有关键列数值或顺序不正确")
  39. }
  40. }
  41. for columnName, value := range parsed["table_rows"].(map[string]any) {
  42. if exceptedTableRows[columnName] != value {
  43. t.Fatal("行数据不正确")
  44. }
  45. }
  46. }
  47. func TestApiV1Operate(t *testing.T) {
  48. http_binding.Init("test", "10086")
  49. defer http_binding.Destroy()
  50. operatorID := simpleUUID()
  51. tablePrefix := "test.a" + simpleUUID()[0:7]
  52. id := simpleUUID()
  53. name := simpleUUID()
  54. now := time.Now().Local()
  55. tableNum := rand.Intn(10) + 1
  56. autoMigrate(t, []client.AutoMigrateItem{
  57. {
  58. TablePrefixWithSchema: tablePrefix,
  59. Version: "v1",
  60. TableModelDescribe: tableModelDescribe,
  61. },
  62. })
  63. binding := http_binding.NewBinding("v1")
  64. ApiV1(binding, dpsAddress, func(c *binding_context.Context) (string, error) {
  65. return operatorID, nil
  66. })
  67. operate(t, fmt.Sprintf(sqlInsertFormat, tablePrefix, id, name, now.Format(time.DateTime), tableNum))
  68. }
  69. func autoMigrate(t *testing.T, items []client.AutoMigrateItem) {
  70. dpsClient, err := dps.NewClient(dpsAddress, "v1", testDatabaseID)
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. err = dpsClient.AutoMigrate(&client.AutoMigrateRequest{Items: items})
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. }
  79. func operateParse(t *testing.T, sql string) map[string]any {
  80. result := new(struct {
  81. response.MsgResponse
  82. Parsed map[string]any `json:"parsed"`
  83. })
  84. resp, err := resty.New().R().
  85. SetBody(&OperateParseRequest{
  86. SQL: sql,
  87. }).
  88. SetResult(result).
  89. Post("http://localhost:10086/test/api/v1/dpsv1/database/operate/parse")
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. if resp.IsError() {
  94. t.Fatal(resp.Status())
  95. }
  96. if !result.Success {
  97. t.Fatal(result.Msg)
  98. }
  99. return result.Parsed
  100. }
  101. func operate(t *testing.T, sql string) {
  102. result := new(response.MsgResponse)
  103. resp, err := resty.New().R().
  104. SetBody(&OperateRequest{
  105. DatabaseID: testDatabaseID,
  106. Version: "v1",
  107. SQL: sql,
  108. }).
  109. SetResult(result).
  110. Post("http://localhost:10086/test/api/v1/dpsv1/database/operate")
  111. if err != nil {
  112. t.Fatal(err)
  113. }
  114. if resp.IsError() {
  115. t.Fatal(resp.Status())
  116. }
  117. if !result.Success {
  118. t.Fatal(result.Msg)
  119. }
  120. }