db_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 store
  15. import (
  16. "reflect"
  17. "testing"
  18. "github.com/benbjohnson/clock"
  19. "github.com/lf-edge/ekuiper/internal/conf"
  20. "github.com/lf-edge/ekuiper/pkg/api"
  21. )
  22. func TestTable(t *testing.T) {
  23. mc := conf.Clock.(*clock.Mock)
  24. tb := createTable("topicT", "a")
  25. tb.add(api.NewDefaultSourceTupleWithTime(map[string]interface{}{"a": 1, "b": "0"}, nil, mc.Now()))
  26. tb.add(api.NewDefaultSourceTupleWithTime(map[string]interface{}{"a": 2, "b": "0"}, nil, mc.Now()))
  27. tb.add(api.NewDefaultSourceTupleWithTime(map[string]interface{}{"a": 3, "b": "4"}, nil, mc.Now()))
  28. tb.add(api.NewDefaultSourceTupleWithTime(map[string]interface{}{"a": 1, "b": "1"}, nil, mc.Now()))
  29. v, _ := tb.Read([]string{"a"}, []interface{}{1})
  30. exp := []api.SourceTuple{
  31. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"a": 1, "b": "1"}, nil, mc.Now()),
  32. }
  33. if !reflect.DeepEqual(v, exp) {
  34. t.Errorf("read a 1 expect %v, but got %v", exp, v)
  35. return
  36. }
  37. v, _ = tb.Read([]string{"b"}, []interface{}{"0"})
  38. exp = []api.SourceTuple{
  39. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"a": 2, "b": "0"}, nil, mc.Now()),
  40. }
  41. if !reflect.DeepEqual(v, exp) {
  42. t.Errorf("read b 0 expect %v, but got %v", exp, v)
  43. return
  44. }
  45. tb.add(api.NewDefaultSourceTupleWithTime(map[string]interface{}{"a": 5, "b": "0"}, nil, mc.Now()))
  46. tb.delete(3)
  47. tb.add(api.NewDefaultSourceTupleWithTime(map[string]interface{}{"a": 1, "b": "1"}, nil, mc.Now()))
  48. v, _ = tb.Read([]string{"b"}, []interface{}{"0"})
  49. exp = []api.SourceTuple{
  50. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"a": 2, "b": "0"}, nil, mc.Now()),
  51. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"a": 5, "b": "0"}, nil, mc.Now()),
  52. }
  53. if len(v) != 2 {
  54. t.Errorf("read 1 again expect %v, but got %v", exp, v)
  55. return
  56. } else {
  57. if v[0].Message()["a"] != 2 {
  58. v[0], v[1] = v[1], v[0]
  59. }
  60. if !reflect.DeepEqual(v, exp) {
  61. t.Errorf("read 1 again expect %v, but got %v", exp, v)
  62. return
  63. }
  64. }
  65. v, _ = tb.Read([]string{"a", "b"}, []interface{}{1, "1"})
  66. exp = []api.SourceTuple{
  67. api.NewDefaultSourceTupleWithTime(map[string]interface{}{"a": 1, "b": "1"}, nil, mc.Now()),
  68. }
  69. if !reflect.DeepEqual(v, exp) {
  70. t.Errorf("read a,b expect %v, but got %v", exp, v)
  71. return
  72. }
  73. v, _ = tb.Read([]string{"a"}, []interface{}{3})
  74. if v != nil {
  75. t.Errorf("read a 3 expect nil, but got %v", v)
  76. return
  77. }
  78. tb.delete(1)
  79. v, _ = tb.Read([]string{"a"}, []interface{}{1})
  80. if v != nil {
  81. t.Errorf("read a 1 expect nil, but got %v", v)
  82. }
  83. }
  84. func TestDb(t *testing.T) {
  85. db = &database{
  86. tables: make(map[string]*tableCount),
  87. }
  88. db.addTable("t1", "a")
  89. db.addTable("t1", "b")
  90. db.addTable("t2", "a")
  91. db.addTable("t1", "a")
  92. _, ok := db.getTable("t1", "a")
  93. if !ok {
  94. t.Errorf("table t1 a should exist")
  95. return
  96. }
  97. _, ok = db.getTable("t1", "b")
  98. if !ok {
  99. t.Errorf("table t1 b should exist")
  100. return
  101. }
  102. _, ok = db.getTable("t3", "a")
  103. if ok {
  104. t.Errorf("table t1 c should not exist")
  105. return
  106. }
  107. tc := db.tables["t1_a"]
  108. if tc.count != 2 {
  109. t.Errorf("table t1 a should have 2 instances but got %d", tc.count)
  110. return
  111. }
  112. tc = db.tables["t2_a"]
  113. if tc.count != 1 {
  114. t.Errorf("table t2 a should have 1 instances")
  115. return
  116. }
  117. db.dropTable("t1", "a")
  118. db.dropTable("t2", "a")
  119. _, ok = db.getTable("t2", "a")
  120. if ok {
  121. t.Errorf("table t2 a should not exist")
  122. return
  123. }
  124. tc = db.tables["t1_a"]
  125. if tc.count != 1 {
  126. t.Errorf("table t1 a should have 1 instances but got %d", tc.count)
  127. return
  128. }
  129. }