lookup_test.go 4.2 KB

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