sink_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. package memory
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/internal/conf"
  18. "github.com/lf-edge/ekuiper/internal/io/memory/pubsub"
  19. "github.com/lf-edge/ekuiper/internal/topo/context"
  20. "github.com/lf-edge/ekuiper/pkg/api"
  21. "reflect"
  22. "testing"
  23. )
  24. func TestUpdate(t *testing.T) {
  25. contextLogger := conf.Log.WithField("rule", "test2")
  26. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  27. ms := GetSink()
  28. err := ms.Configure(map[string]interface{}{"topic": "testupdate", "rowkindField": "verb", "keyField": "id"})
  29. if err != nil {
  30. t.Error(err)
  31. return
  32. }
  33. err = ms.Open(ctx)
  34. if err != nil {
  35. t.Error(err)
  36. return
  37. }
  38. var data = []map[string]interface{}{
  39. {"id": "1", "verb": "insert", "name": "test1"},
  40. {"id": "2", "verb": "insert", "name": "test2"},
  41. {"id": "1", "verb": "update", "name": "test1"},
  42. {"id": "2", "verb": "delete", "name": "test2"},
  43. }
  44. c := pubsub.CreateSub("testupdate", nil, "testSource", 100)
  45. go func() {
  46. for _, d := range data {
  47. ms.Collect(ctx, d)
  48. }
  49. }()
  50. var actual []api.SourceTuple
  51. for i := 0; i < 4; i++ {
  52. d := <-c
  53. fmt.Println(d)
  54. actual = append(actual, d)
  55. }
  56. expects := []api.SourceTuple{
  57. &pubsub.UpdatableTuple{
  58. DefaultSourceTuple: api.NewDefaultSourceTuple(map[string]interface{}{"id": "1", "verb": "insert", "name": "test1"}, map[string]interface{}{"topic": "testupdate"}),
  59. Rowkind: "insert",
  60. Keyval: "1",
  61. },
  62. &pubsub.UpdatableTuple{
  63. DefaultSourceTuple: api.NewDefaultSourceTuple(map[string]interface{}{"id": "2", "verb": "insert", "name": "test2"}, map[string]interface{}{"topic": "testupdate"}),
  64. Rowkind: "insert",
  65. Keyval: "2",
  66. },
  67. &pubsub.UpdatableTuple{
  68. DefaultSourceTuple: api.NewDefaultSourceTuple(map[string]interface{}{"id": "1", "verb": "update", "name": "test1"}, map[string]interface{}{"topic": "testupdate"}),
  69. Rowkind: "update",
  70. Keyval: "1",
  71. },
  72. &pubsub.UpdatableTuple{
  73. DefaultSourceTuple: api.NewDefaultSourceTuple(map[string]interface{}{"id": "2", "verb": "delete", "name": "test2"}, map[string]interface{}{"topic": "testupdate"}),
  74. Rowkind: "delete",
  75. Keyval: "2",
  76. },
  77. }
  78. if !reflect.DeepEqual(actual, expects) {
  79. t.Errorf("expect %v but got %v", expects, actual)
  80. }
  81. }