clickhouse_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package dbutils
  15. import (
  16. "testing"
  17. )
  18. func TestClickhouseSqlStrV1ToV2(t *testing.T) {
  19. for _, c := range []struct {
  20. in string
  21. want string
  22. }{
  23. {
  24. in: "tcp://192.168.222.4:9000?database=yunionmeter&read_timeout=10&write_timeout=20",
  25. want: "clickhouse://192.168.222.4:9000/yunionmeter?dial_timeout=200ms&max_execution_time=60",
  26. },
  27. {
  28. in: "tcp://192.168.222.4:9000?username=admin&database=yunionmeter&read_timeout=10&write_timeout=20",
  29. want: "clickhouse://admin@192.168.222.4:9000/yunionmeter?dial_timeout=200ms&max_execution_time=60",
  30. },
  31. {
  32. in: "tcp://192.168.222.4:9000?username=admin&password=pass&database=yunionmeter&read_timeout=10&write_timeout=20",
  33. want: "clickhouse://admin:pass@192.168.222.4:9000/yunionmeter?dial_timeout=200ms&max_execution_time=60",
  34. },
  35. } {
  36. got, err := ClickhouseSqlStrV1ToV2(c.in)
  37. if err != nil {
  38. t.Errorf("%s", err)
  39. } else if got != c.want {
  40. t.Errorf("got %s want %s", got, c.want)
  41. }
  42. }
  43. }
  44. func TestClickhouseSqlStrV2ToV1(t *testing.T) {
  45. for _, c := range []struct {
  46. in string
  47. want string
  48. }{
  49. {
  50. in: "clickhouse://admin:pass@192.168.222.4:9000/yunionmeter",
  51. want: "tcp://192.168.222.4:9000?database=yunionmeter&password=pass&username=admin&read_timeout=10&write_timeout=20",
  52. },
  53. {
  54. in: "clickhouse://192.168.222.4:9000/yunionmeter?dial_timeout=200ms&max_execution_time=60",
  55. want: "tcp://192.168.222.4:9000?database=yunionmeter&read_timeout=10&write_timeout=20",
  56. },
  57. {
  58. in: "clickhouse://admin@192.168.222.4:9000/yunionmeter?dial_timeout=200ms&max_execution_time=60",
  59. want: "tcp://192.168.222.4:9000?database=yunionmeter&username=admin&read_timeout=10&write_timeout=20",
  60. },
  61. {
  62. in: "clickhouse://admin:pass@192.168.222.4:9000/yunionmeter?dial_timeout=200ms&max_execution_time=60",
  63. want: "tcp://192.168.222.4:9000?database=yunionmeter&password=pass&username=admin&read_timeout=10&write_timeout=20",
  64. },
  65. } {
  66. got, err := ClickhouseSqlStrV2ToV1(c.in)
  67. if err != nil {
  68. t.Errorf("%s", err)
  69. } else if got != c.want {
  70. t.Errorf("got %s want %s", got, c.want)
  71. }
  72. }
  73. }
  74. func TestValidateClickhouseSqlstrV1(t *testing.T) {
  75. for _, c := range []struct {
  76. in string
  77. valid bool
  78. }{
  79. {
  80. in: "",
  81. valid: false,
  82. },
  83. {
  84. in: "tcp://192.168.222.4:9000?read_timeout=10&write_timeout=20",
  85. valid: false,
  86. },
  87. {
  88. in: "tcp://192.168.222.4:9000?database=yunionmeter&read_timeout=10&write_timeout=20",
  89. valid: true,
  90. },
  91. {
  92. in: "clickhouse://username:password@host1:9000,host2:9000?dial_timeout=200ms&max_execution_time=60",
  93. valid: false,
  94. },
  95. {
  96. in: "clickhouse://username:password@host1:9000,host2:9000/database?dial_timeout=200ms&max_execution_time=60",
  97. valid: false,
  98. },
  99. } {
  100. err := ValidateClickhouseV1Str(c.in)
  101. if err != nil && c.valid {
  102. t.Errorf("%s", err)
  103. }
  104. }
  105. }
  106. func TestValidateClickhouseSqlstrV2(t *testing.T) {
  107. for _, c := range []struct {
  108. in string
  109. valid bool
  110. }{
  111. {
  112. in: "",
  113. valid: false,
  114. },
  115. {
  116. in: "tcp://192.168.222.4:9000?database=yunionmeter&read_timeout=10&write_timeout=20",
  117. valid: false,
  118. },
  119. {
  120. in: "clickhouse://username:password@host1:9000,host2:9000?dial_timeout=200ms&max_execution_time=60",
  121. valid: false,
  122. },
  123. {
  124. in: "clickhouse://username:password@host1:9000,host2:9000/database?dial_timeout=200ms&max_execution_time=60",
  125. valid: true,
  126. },
  127. } {
  128. err := ValidateClickhouseV2Str(c.in)
  129. if err != nil && c.valid {
  130. t.Errorf("%s", err)
  131. }
  132. }
  133. }