tdengine_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // Copyright 2022 EMQ Technologies Co., Ltd.
  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 main
  15. import (
  16. "fmt"
  17. "reflect"
  18. "testing"
  19. "github.com/lf-edge/ekuiper/internal/conf"
  20. "github.com/lf-edge/ekuiper/internal/testx"
  21. "github.com/lf-edge/ekuiper/internal/topo/context"
  22. "github.com/lf-edge/ekuiper/internal/topo/state"
  23. )
  24. func TestConfig(t *testing.T) {
  25. tests := []struct {
  26. conf map[string]interface{}
  27. expected *taosConfig
  28. error string
  29. }{
  30. { // 0
  31. conf: map[string]interface{}{
  32. "host": "e0d9d8089bef",
  33. "port": 6030,
  34. "user": "root",
  35. "password": "taosdata",
  36. "database": "db",
  37. "table": "t",
  38. "tsfieldname": "ts",
  39. },
  40. expected: &taosConfig{
  41. ProvideTs: false,
  42. Host: "e0d9d8089bef",
  43. Port: 6030,
  44. User: "root",
  45. Password: "taosdata",
  46. Database: "db",
  47. Table: "t",
  48. TsFieldName: "ts",
  49. Fields: nil,
  50. },
  51. },
  52. { // 1
  53. conf: map[string]interface{}{
  54. "ip": "e0d9d8089bef",
  55. "port": 6030,
  56. "user": "root1",
  57. "password": "taosdata1",
  58. "database": "db",
  59. "table": "t",
  60. "provideTs": true,
  61. "tsfieldname": "ts",
  62. },
  63. expected: &taosConfig{
  64. ProvideTs: true,
  65. Ip: "e0d9d8089bef",
  66. Host: "e0d9d8089bef",
  67. Port: 6030,
  68. User: "root1",
  69. Password: "taosdata1",
  70. Database: "db",
  71. Table: "t",
  72. TsFieldName: "ts",
  73. Fields: nil,
  74. },
  75. },
  76. { // 2
  77. conf: map[string]interface{}{
  78. "port": 6030,
  79. "database": "dab",
  80. "table": "tt",
  81. "tsfieldname": "tst",
  82. "fields": []string{"f1", "f2"},
  83. "sTable": "s",
  84. "tagFields": []string{"a", "b"},
  85. },
  86. expected: &taosConfig{
  87. ProvideTs: false,
  88. Ip: "",
  89. Host: "localhost",
  90. Port: 6030,
  91. User: "root",
  92. Password: "taosdata",
  93. Database: "dab",
  94. Table: "tt",
  95. TsFieldName: "tst",
  96. Fields: []string{"f1", "f2"},
  97. STable: "s",
  98. TagFields: []string{"a", "b"},
  99. },
  100. },
  101. { // 3
  102. conf: map[string]interface{}{
  103. "port": 6030,
  104. "database": "dab",
  105. "table": "t",
  106. "fields": []string{"f1", "f2"},
  107. },
  108. error: "property TsFieldName is required",
  109. },
  110. { // 4
  111. conf: map[string]interface{}{
  112. "port": 6030,
  113. "database": "dab",
  114. "table": "tt",
  115. "tsfieldname": "tst",
  116. "fields": []string{"f1", "f2"},
  117. "sTable": "s",
  118. },
  119. error: "property tagFields is required when sTable is set",
  120. },
  121. }
  122. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  123. for i, test := range tests {
  124. tdsink := &taosSink{}
  125. err := tdsink.Configure(test.conf)
  126. if !reflect.DeepEqual(test.error, testx.Errstring(err)) {
  127. t.Errorf("%d: error mismatch:\n exp=%s\n got=%s\n\n", i, test.error, err)
  128. } else if test.error == "" && !reflect.DeepEqual(test.expected, tdsink.conf) {
  129. t.Errorf("%d\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, test.expected, tdsink.conf)
  130. }
  131. }
  132. }
  133. func TestBuildSql(t *testing.T) {
  134. tests := []struct {
  135. conf *taosConfig
  136. data map[string]interface{}
  137. expected string
  138. error string
  139. }{
  140. {
  141. conf: &taosConfig{
  142. ProvideTs: false,
  143. Host: "e0d9d8089bef",
  144. Port: 6030,
  145. User: "root",
  146. Password: "taosdata",
  147. Database: "db",
  148. Table: "t",
  149. TsFieldName: "ts",
  150. Fields: nil,
  151. },
  152. data: map[string]interface{}{
  153. "f1": "v1",
  154. },
  155. expected: `t (ts,f1) values (now,"v1")`,
  156. },
  157. {
  158. conf: &taosConfig{
  159. ProvideTs: true,
  160. Ip: "e0d9d8089bef",
  161. Host: "e0d9d8089bef",
  162. Port: 6030,
  163. User: "root1",
  164. Password: "taosdata1",
  165. Database: "db",
  166. Table: "t",
  167. TsFieldName: "ts",
  168. Fields: nil,
  169. },
  170. data: map[string]interface{}{
  171. "ts": 1.2345678e+06,
  172. "f2": 65,
  173. },
  174. expected: `t (ts,f2) values (12345678,65)`,
  175. },
  176. {
  177. conf: &taosConfig{
  178. ProvideTs: true,
  179. Ip: "e0d9d8089bef",
  180. Host: "e0d9d8089bef",
  181. Port: 6030,
  182. User: "root1",
  183. Password: "taosdata1",
  184. Database: "db",
  185. Table: "t",
  186. TsFieldName: "ts",
  187. Fields: nil,
  188. },
  189. data: map[string]interface{}{
  190. "ts": 12345678,
  191. "f2": 65,
  192. },
  193. expected: `t (ts,f2) values (12345678,65)`,
  194. },
  195. {
  196. conf: &taosConfig{
  197. ProvideTs: false,
  198. Ip: "",
  199. Host: "localhost",
  200. Port: 6030,
  201. User: "root",
  202. Password: "taosdata",
  203. Database: "dab",
  204. Table: "{{.table}}",
  205. TsFieldName: "tst",
  206. Fields: []string{"f1", "f2"},
  207. STable: "s",
  208. TagFields: []string{"a", "b"},
  209. },
  210. data: map[string]interface{}{
  211. "table": "t1",
  212. "ts": 12345678,
  213. "f2": 65,
  214. "f1": 12.3,
  215. "a": "a1",
  216. "b": 2,
  217. },
  218. expected: `t1 (tst,f1,f2) using s tags ("a1",2) values (now,12.3,65)`,
  219. },
  220. }
  221. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  222. contextLogger := conf.Log.WithField("rule", "mockRule0")
  223. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger).WithMeta("testTD", "op1", &state.MemoryStore{})
  224. for i, test := range tests {
  225. sql, err := test.conf.buildSql(ctx, test.data)
  226. if !reflect.DeepEqual(test.error, testx.Errstring(err)) {
  227. t.Errorf("%d: error mismatch:\n exp=%s\n got=%s\n\n", i, test.error, err)
  228. } else if test.error == "" && !reflect.DeepEqual(test.expected, sql) {
  229. t.Errorf("%d\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, test.expected, sql)
  230. }
  231. }
  232. }