db_test.go 3.7 KB

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