binder_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // Copyright 2021-2023 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 io
  15. import (
  16. "errors"
  17. "fmt"
  18. "testing"
  19. "github.com/lf-edge/ekuiper/internal/binder"
  20. "github.com/lf-edge/ekuiper/internal/binder/mock"
  21. "github.com/lf-edge/ekuiper/pkg/errorx"
  22. )
  23. func TestBindings(t *testing.T) {
  24. m := mock.NewMockFactory()
  25. e := binder.FactoryEntry{
  26. Name: "mock",
  27. Factory: m,
  28. }
  29. err := Initialize([]binder.FactoryEntry{e})
  30. if err != nil {
  31. t.Error(err)
  32. return
  33. }
  34. tests := []struct {
  35. name string
  36. isSource bool
  37. isLookupSource bool
  38. isSink bool
  39. }{
  40. {
  41. name: "unknown",
  42. isSource: false,
  43. isLookupSource: false,
  44. isSink: false,
  45. }, {
  46. name: "mqtt",
  47. isSource: true,
  48. isLookupSource: false,
  49. isSink: true,
  50. }, {
  51. name: "mock1",
  52. isSource: true,
  53. isLookupSource: false,
  54. isSink: true,
  55. }, {
  56. name: "rest",
  57. isSource: false,
  58. isLookupSource: false,
  59. isSink: true,
  60. }, {
  61. name: "redis",
  62. isSource: false,
  63. isLookupSource: true,
  64. isSink: true,
  65. },
  66. }
  67. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  68. for _, tt := range tests {
  69. _, err := Source(tt.name)
  70. isSource := err == nil
  71. if tt.isSource != isSource {
  72. t.Errorf("%s is source: expect %v but got %v", tt.name, tt.isSource, isSource)
  73. }
  74. _, err = LookupSource(tt.name)
  75. if tt.isLookupSource != (err == nil) {
  76. t.Errorf("%s is lookup source: expect %v but got %v", tt.name, tt.isLookupSource, err == nil)
  77. }
  78. _, err = Sink(tt.name)
  79. isSink := err == nil
  80. if tt.isSink != isSink {
  81. t.Errorf("%s is sink: expect %v but got %v", tt.name, tt.isSink, isSink)
  82. }
  83. }
  84. }
  85. func TestSource(t *testing.T) {
  86. m1 := mock.NewMockFactory()
  87. m2 := mock.NewMockFactory()
  88. e1 := binder.FactoryEntry{
  89. Name: "mock1",
  90. Factory: m1,
  91. }
  92. e2 := binder.FactoryEntry{
  93. Name: "mock2",
  94. Factory: m2,
  95. }
  96. err := Initialize([]binder.FactoryEntry{e1, e2})
  97. if err != nil {
  98. t.Error(err)
  99. return
  100. }
  101. type args struct {
  102. name string
  103. }
  104. tests := []struct {
  105. name string
  106. args args
  107. isSrc bool
  108. wantErr bool
  109. errs error
  110. }{
  111. {
  112. name: "mockFunc1",
  113. args: args{
  114. name: "mock",
  115. },
  116. isSrc: true,
  117. wantErr: false,
  118. errs: nil,
  119. },
  120. {
  121. name: "mockFunc2",
  122. args: args{
  123. name: "echo",
  124. },
  125. isSrc: false,
  126. wantErr: true,
  127. errs: errors.Join(fmt.Errorf("mock1: %v", errorx.NotFoundErr), fmt.Errorf("mock2: %v", errorx.NotFoundErr)),
  128. },
  129. }
  130. for _, tt := range tests {
  131. t.Run(tt.name, func(t *testing.T) {
  132. src, err := Source(tt.args.name)
  133. if (src != nil) != tt.isSrc {
  134. t.Errorf("Source() src = %v, isSrc = %v", src, tt.isSrc)
  135. }
  136. if (err != nil) != tt.wantErr {
  137. t.Errorf("Source() error = %v, wantErr %v", err, tt.wantErr)
  138. return
  139. }
  140. if err != nil {
  141. if errors.Is(err, tt.errs) {
  142. t.Errorf("Source() error = %v, wantErr %v", err.Error(), tt.errs)
  143. }
  144. }
  145. })
  146. }
  147. }
  148. func TestSink(t *testing.T) {
  149. m1 := mock.NewMockFactory()
  150. m2 := mock.NewMockFactory()
  151. e1 := binder.FactoryEntry{
  152. Name: "mock1",
  153. Factory: m1,
  154. }
  155. e2 := binder.FactoryEntry{
  156. Name: "mock2",
  157. Factory: m2,
  158. }
  159. err := Initialize([]binder.FactoryEntry{e1, e2})
  160. if err != nil {
  161. t.Error(err)
  162. return
  163. }
  164. type args struct {
  165. name string
  166. }
  167. tests := []struct {
  168. name string
  169. args args
  170. isSink bool
  171. wantErr bool
  172. errs error
  173. }{
  174. {
  175. name: "mockFunc1",
  176. args: args{
  177. name: "mock",
  178. },
  179. isSink: true,
  180. wantErr: false,
  181. errs: nil,
  182. },
  183. {
  184. name: "mockFunc2",
  185. args: args{
  186. name: "echo",
  187. },
  188. isSink: false,
  189. wantErr: true,
  190. errs: errors.Join(fmt.Errorf("mock1: %v", errorx.NotFoundErr), fmt.Errorf("mock2: %v", errorx.NotFoundErr)),
  191. },
  192. }
  193. for _, tt := range tests {
  194. t.Run(tt.name, func(t *testing.T) {
  195. sink, err := Sink(tt.args.name)
  196. if (sink != nil) != tt.isSink {
  197. t.Errorf("Sink() sink = %v, isSink = %v", sink, tt.isSink)
  198. }
  199. if (err != nil) != tt.wantErr {
  200. t.Errorf("Sink() error = %v, wantErr %v", err, tt.wantErr)
  201. return
  202. }
  203. if err != nil {
  204. if errors.Is(err, tt.errs) {
  205. t.Errorf("Sink() error = %v, wantErr %v", err.Error(), tt.errs)
  206. }
  207. }
  208. })
  209. }
  210. }
  211. func TestLookupSource(t *testing.T) {
  212. m := mock.NewMockFactory()
  213. e := binder.FactoryEntry{
  214. Name: "mock",
  215. Factory: m,
  216. }
  217. err := Initialize([]binder.FactoryEntry{e})
  218. if err != nil {
  219. t.Error(err)
  220. return
  221. }
  222. type args struct {
  223. name string
  224. }
  225. tests := []struct {
  226. name string
  227. args args
  228. isSrc bool
  229. wantErr bool
  230. errs error
  231. }{
  232. {
  233. name: "mockFunc1",
  234. args: args{
  235. name: "mock",
  236. },
  237. isSrc: false,
  238. wantErr: true,
  239. errs: fmt.Errorf("lookup source type mock not found"),
  240. },
  241. }
  242. for _, tt := range tests {
  243. t.Run(tt.name, func(t *testing.T) {
  244. src, err := LookupSource(tt.args.name)
  245. if (src != nil) != tt.isSrc {
  246. t.Errorf("LookupSource() src = %v, isSrc = %v", src, tt.isSrc)
  247. }
  248. if (err != nil) != tt.wantErr {
  249. t.Errorf("LookupSource() error = %v, wantErr %v", err, tt.wantErr)
  250. return
  251. }
  252. if err != nil {
  253. if errors.Is(err, tt.errs) {
  254. t.Errorf("LookupSource() error = %v, wantErr %v", err.Error(), tt.errs)
  255. }
  256. }
  257. })
  258. }
  259. }