v1_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package dpsapi
  2. import (
  3. "git.sxidc.com/go-tools/api_binding/http_binding"
  4. "git.sxidc.com/go-tools/api_binding/http_binding/response"
  5. "github.com/go-resty/resty/v2"
  6. "testing"
  7. )
  8. func TestApiV1OperateParse(t *testing.T) {
  9. http_binding.Init("test", "10086")
  10. defer http_binding.Destroy()
  11. tableName := "students"
  12. exceptedKeyColumns := []string{"id", "name", "age", "rate", "time", "is_right"}
  13. exceptedTableRows := map[string]any{
  14. "id": "aaa",
  15. "name": "yjp",
  16. "age": float64(5),
  17. "rate": 92.5,
  18. "time": "2024-01-01T00:00:00+08:00",
  19. "is_right": false,
  20. }
  21. binding := http_binding.NewBinding("v1")
  22. ApiV1(binding, "localhost:30170")
  23. parsed := operateParse(t, parseSqlSelect)
  24. if parsed["table"].(string) != tableName {
  25. t.Fatal("表名不正确")
  26. }
  27. for i, keyColumn := range parsed["key_columns"].([]any) {
  28. if exceptedKeyColumns[i] != keyColumn {
  29. t.Fatal("没有关键列数值或顺序不正确")
  30. }
  31. }
  32. for columnName, value := range parsed["table_rows"].(map[string]any) {
  33. if exceptedTableRows[columnName] != value {
  34. t.Fatal("行数据不正确")
  35. }
  36. }
  37. }
  38. func TestApiV1Operate(t *testing.T) {
  39. http_binding.Init("test", "10086")
  40. defer http_binding.Destroy()
  41. binding := http_binding.NewBinding("v1")
  42. ApiV1(binding, "localhost:30170")
  43. operate(t, sqlSelect)
  44. }
  45. func operateParse(t *testing.T, sql string) map[string]any {
  46. result := new(struct {
  47. response.MsgResponse
  48. Parsed map[string]any `json:"parsed"`
  49. })
  50. resp, err := resty.New().R().
  51. SetBody(&OperateParseRequest{
  52. SQL: sql,
  53. }).
  54. SetResult(result).
  55. Post("http://localhost:10086/test/api/v1/dpsv1/database/operate/parse")
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. if resp.IsError() {
  60. t.Fatal(resp.Status())
  61. }
  62. if !result.Success {
  63. t.Fatal(result.Msg)
  64. }
  65. return result.Parsed
  66. }
  67. func operate(t *testing.T, sql string) {
  68. result := new(response.MsgResponse)
  69. resp, err := resty.New().R().
  70. SetBody(&OperateRequest{
  71. DatabaseID: "ee2d7dabe56646ce835d80873348ee0e",
  72. Version: "v1",
  73. SQL: sql,
  74. }).
  75. SetResult(result).
  76. Post("http://localhost:10086/test/api/v1/dpsv1/database/operate")
  77. if err != nil {
  78. t.Fatal(err)
  79. }
  80. if resp.IsError() {
  81. t.Fatal(resp.Status())
  82. }
  83. if !result.Success {
  84. t.Fatal(result.Msg)
  85. }
  86. }