binder_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 function
  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 TestBinding(t *testing.T) {
  24. // Initialize binding
  25. m := mock.NewMockFactory()
  26. e := binder.FactoryEntry{
  27. Name: "mock",
  28. Factory: m,
  29. }
  30. err := Initialize([]binder.FactoryEntry{e})
  31. if err != nil {
  32. t.Error(err)
  33. return
  34. }
  35. tests := []struct {
  36. name string
  37. isFunc bool
  38. isFuncset bool
  39. hasAgg bool
  40. isAgg bool
  41. }{
  42. {
  43. name: "mockFunc1",
  44. isFunc: true,
  45. isFuncset: true,
  46. hasAgg: false,
  47. }, {
  48. name: "mockFunc2",
  49. isFunc: true,
  50. isFuncset: true,
  51. hasAgg: false,
  52. }, {
  53. name: "count",
  54. isFunc: true,
  55. isFuncset: false,
  56. hasAgg: true,
  57. isAgg: true,
  58. }, {
  59. name: "echo",
  60. isFunc: false,
  61. isFuncset: false,
  62. hasAgg: false,
  63. }, {
  64. name: "internal",
  65. isFunc: false,
  66. isFuncset: true,
  67. hasAgg: false,
  68. }, {
  69. name: "cast",
  70. isFunc: true,
  71. isFuncset: false,
  72. hasAgg: true,
  73. isAgg: false,
  74. },
  75. }
  76. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  77. for _, tt := range tests {
  78. _, err := Function(tt.name)
  79. isFunc := err == nil
  80. if tt.isFunc != isFunc {
  81. t.Errorf("%s is function: expect %v but got %v", tt.name, tt.isFunc, isFunc)
  82. }
  83. isFuncset := HasFunctionSet(tt.name)
  84. if tt.isFuncset != isFuncset {
  85. t.Errorf("%s is function set: expect %v but got %v", tt.name, tt.isFuncset, isFuncset)
  86. }
  87. if tt.hasAgg {
  88. isAgg := IsAggFunc(tt.name)
  89. if tt.isAgg != isAgg {
  90. t.Errorf("%s is aggregate: expect %v but got %v", tt.name, tt.isAgg, isAgg)
  91. }
  92. }
  93. }
  94. }
  95. func TestFunction(t *testing.T) {
  96. m1 := mock.NewMockFactory()
  97. m2 := mock.NewMockFactory()
  98. e1 := binder.FactoryEntry{
  99. Name: "mock1",
  100. Factory: m1,
  101. }
  102. e2 := binder.FactoryEntry{
  103. Name: "mock2",
  104. Factory: m2,
  105. }
  106. err := Initialize([]binder.FactoryEntry{e1, e2})
  107. if err != nil {
  108. t.Error(err)
  109. return
  110. }
  111. type args struct {
  112. name string
  113. }
  114. tests := []struct {
  115. name string
  116. args args
  117. isFunc bool
  118. wantErr bool
  119. errs error
  120. }{
  121. {
  122. name: "mockFunc1",
  123. args: args{
  124. name: "mock",
  125. },
  126. isFunc: true,
  127. wantErr: false,
  128. errs: nil,
  129. },
  130. {
  131. name: "mockFunc2",
  132. args: args{
  133. name: "echo",
  134. },
  135. isFunc: false,
  136. wantErr: true,
  137. errs: errors.Join(fmt.Errorf("%s:%v", "mock1", errorx.NotFoundErr), fmt.Errorf("%s:%v", "mock2", errorx.NotFoundErr)),
  138. },
  139. }
  140. for _, tt := range tests {
  141. t.Run(tt.name, func(t *testing.T) {
  142. function, err := Function(tt.args.name)
  143. if (function != nil) != tt.isFunc {
  144. t.Errorf("Function() function = %v, isFunc %v", function, tt.isFunc)
  145. }
  146. if (err != nil) != tt.wantErr {
  147. t.Errorf("Function() error = %v, wantErr %v", err, tt.wantErr)
  148. return
  149. }
  150. if err != nil {
  151. if errors.Is(err, tt.errs) {
  152. t.Errorf("Function() error = %v, wantErr %v", err.Error(), tt.errs.Error())
  153. }
  154. }
  155. })
  156. }
  157. }