influx2_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. "github.com/lf-edge/ekuiper/internal/testx"
  18. "reflect"
  19. "testing"
  20. )
  21. func TestConfig(t *testing.T) {
  22. var tests = []struct {
  23. conf map[string]interface{}
  24. expected *influxSink2
  25. error string
  26. }{
  27. { //0
  28. conf: map[string]interface{}{
  29. "addr": "http://192.168.0.3:8086",
  30. "token": "Token_test",
  31. "measurement": "test",
  32. "org": "admin",
  33. "bucket": "bucket_one",
  34. "tagKey": "tag",
  35. "tagValue": "value",
  36. "fields": "temperature",
  37. },
  38. expected: &influxSink2{
  39. addr: "http://192.168.0.3:8086",
  40. token: "Token_test",
  41. measurement: "test",
  42. org: "admin",
  43. bucket: "bucket_one",
  44. tagKey: "tag",
  45. tagValue: "value",
  46. fields: "temperature",
  47. cli: nil,
  48. fieldMap: nil,
  49. hasTransform: false,
  50. },
  51. },
  52. { //1
  53. conf: map[string]interface{}{
  54. "addr": "http://192.168.0.3:8086",
  55. "token": "Token_test",
  56. "measurement": "test",
  57. "org": "admin",
  58. "bucket": "bucket_one",
  59. "tagKey": "tag",
  60. "tagValue": "value",
  61. "fields": "temperature",
  62. "dataTemplate": "",
  63. },
  64. expected: &influxSink2{
  65. addr: "http://192.168.0.3:8086",
  66. token: "Token_test",
  67. measurement: "test",
  68. org: "admin",
  69. bucket: "bucket_one",
  70. tagKey: "tag",
  71. tagValue: "value",
  72. fields: "temperature",
  73. cli: nil,
  74. fieldMap: nil,
  75. hasTransform: false,
  76. },
  77. },
  78. { //2
  79. conf: map[string]interface{}{
  80. "addr": "http://192.168.0.3:8086",
  81. "token": "Token_test",
  82. "measurement": "test",
  83. "org": "admin",
  84. "bucket": "bucket_one",
  85. "tagKey": "tag",
  86. "tagValue": "value",
  87. "fields": "temperature",
  88. "dataTemplate": "{{funcname .arg1 .arg2}}",
  89. },
  90. expected: &influxSink2{
  91. addr: "http://192.168.0.3:8086",
  92. token: "Token_test",
  93. measurement: "test",
  94. org: "admin",
  95. bucket: "bucket_one",
  96. tagKey: "tag",
  97. tagValue: "value",
  98. fields: "temperature",
  99. cli: nil,
  100. fieldMap: nil,
  101. hasTransform: true,
  102. },
  103. },
  104. }
  105. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  106. for i, test := range tests {
  107. ifsink := &influxSink2{}
  108. err := ifsink.Configure(test.conf)
  109. if !reflect.DeepEqual(test.error, testx.Errstring(err)) {
  110. t.Errorf("%d: error mismatch:\n exp=%s\n got=%s\n\n", i, test.error, err)
  111. } else if test.error == "" && !reflect.DeepEqual(test.expected, ifsink) {
  112. t.Errorf("%d\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, test.expected, ifsink)
  113. }
  114. }
  115. }