lookup_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2022-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. //go:build redisdb || !core
  15. package redis
  16. import (
  17. "github.com/alicebob/miniredis/v2"
  18. econf "github.com/lf-edge/ekuiper/internal/conf"
  19. "github.com/lf-edge/ekuiper/internal/topo/context"
  20. "github.com/lf-edge/ekuiper/pkg/api"
  21. "reflect"
  22. "testing"
  23. )
  24. var (
  25. addr string
  26. mr *miniredis.Miniredis
  27. )
  28. func init() {
  29. s, err := miniredis.Run()
  30. if err != nil {
  31. panic(err)
  32. }
  33. addr = "localhost:" + s.Port()
  34. // Mock id key data
  35. s.Set("1", `{"id":1,"name":"John","address":34,"mobile":"334433"}`)
  36. s.Set("2", `{"id":2,"name":"Susan","address":22,"mobile":"666433"}`)
  37. // Mock group key list data
  38. s.Lpush("group1", `{"id":1,"name":"John"}`)
  39. s.Lpush("group1", `{"id":2,"name":"Susan"}`)
  40. s.Lpush("group2", `{"id":3,"name":"Nancy"}`)
  41. s.Lpush("group3", `{"id":4,"name":"Tom"}`)
  42. mr = s
  43. }
  44. // TestSingle test lookup value of a single map
  45. func TestSingle(t *testing.T) {
  46. contextLogger := econf.Log.WithField("rule", "test")
  47. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  48. ls := GetLookupSource()
  49. err := ls.Configure("0", map[string]interface{}{"addr": addr, "datatype": "string"})
  50. if err != nil {
  51. t.Error(err)
  52. return
  53. }
  54. err = ls.Open(ctx)
  55. if err != nil {
  56. t.Error(err)
  57. return
  58. }
  59. var tests = []struct {
  60. value int
  61. result []api.SourceTuple
  62. }{
  63. {
  64. value: 1,
  65. result: []api.SourceTuple{
  66. api.NewDefaultSourceTuple(map[string]interface{}{"id": float64(1), "name": "John", "address": float64(34), "mobile": "334433"}, nil),
  67. },
  68. }, {
  69. value: 2,
  70. result: []api.SourceTuple{
  71. api.NewDefaultSourceTuple(map[string]interface{}{"id": float64(2), "name": "Susan", "address": float64(22), "mobile": "666433"}, nil),
  72. },
  73. }, {
  74. value: 3,
  75. result: []api.SourceTuple{},
  76. },
  77. }
  78. for i, tt := range tests {
  79. actual, err := ls.Lookup(ctx, []string{}, []string{"id"}, []interface{}{tt.value})
  80. if err != nil {
  81. t.Errorf("Test %d: %v", i, err)
  82. continue
  83. }
  84. if !reflect.DeepEqual(actual, tt.result) {
  85. t.Errorf("Test %d: expected %v, actual %v", i, tt.result, actual)
  86. continue
  87. }
  88. }
  89. }
  90. func TestList(t *testing.T) {
  91. contextLogger := econf.Log.WithField("rule", "test")
  92. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  93. ls := GetLookupSource()
  94. err := ls.Configure("0", map[string]interface{}{"addr": addr, "datatype": "list"})
  95. if err != nil {
  96. t.Error(err)
  97. return
  98. }
  99. err = ls.Open(ctx)
  100. if err != nil {
  101. t.Error(err)
  102. return
  103. }
  104. var tests = []struct {
  105. value string
  106. result []api.SourceTuple
  107. }{
  108. {
  109. value: "group1",
  110. result: []api.SourceTuple{
  111. api.NewDefaultSourceTuple(map[string]interface{}{"id": float64(2), "name": "Susan"}, nil),
  112. api.NewDefaultSourceTuple(map[string]interface{}{"id": float64(1), "name": "John"}, nil),
  113. },
  114. }, {
  115. value: "group2",
  116. result: []api.SourceTuple{
  117. api.NewDefaultSourceTuple(map[string]interface{}{"id": float64(3), "name": "Nancy"}, nil),
  118. },
  119. }, {
  120. value: "group4",
  121. result: []api.SourceTuple{},
  122. },
  123. }
  124. for i, tt := range tests {
  125. actual, err := ls.Lookup(ctx, []string{}, []string{"id"}, []interface{}{tt.value})
  126. if err != nil {
  127. t.Errorf("Test %d: %v", i, err)
  128. continue
  129. }
  130. if !reflect.DeepEqual(actual, tt.result) {
  131. t.Errorf("Test %d: expected %v, actual %v", i, tt.result, actual)
  132. continue
  133. }
  134. }
  135. }