db_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2022 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 !reflect.DeepEqual(v, exp) {
  51. t.Errorf("read 1 again expect %v, but got %v", exp, v)
  52. return
  53. }
  54. v, _ = tb.Read([]string{"a", "b"}, []interface{}{1, "1"})
  55. exp = []api.SourceTuple{
  56. api.NewDefaultSourceTuple(map[string]interface{}{"a": 1, "b": "1"}, nil),
  57. }
  58. if !reflect.DeepEqual(v, exp) {
  59. t.Errorf("read a,b expect %v, but got %v", exp, v)
  60. return
  61. }
  62. v, _ = tb.Read([]string{"a"}, []interface{}{3})
  63. if v != nil {
  64. t.Errorf("read a 3 expect nil, but got %v", v)
  65. return
  66. }
  67. tb.delete(1)
  68. v, _ = tb.Read([]string{"a"}, []interface{}{1})
  69. if v != nil {
  70. t.Errorf("read a 1 expect nil, but got %v", v)
  71. }
  72. }
  73. func TestDb(t *testing.T) {
  74. db = &database{
  75. tables: make(map[string]*tableCount),
  76. }
  77. db.addTable("t1", "a")
  78. db.addTable("t1", "b")
  79. db.addTable("t2", "a")
  80. db.addTable("t1", "a")
  81. _, ok := db.getTable("t1", "a")
  82. if !ok {
  83. t.Errorf("table t1 a should exist")
  84. return
  85. }
  86. _, ok = db.getTable("t1", "b")
  87. if !ok {
  88. t.Errorf("table t1 b should exist")
  89. return
  90. }
  91. _, ok = db.getTable("t3", "a")
  92. if ok {
  93. t.Errorf("table t1 c should not exist")
  94. return
  95. }
  96. tc := db.tables["t1_a"]
  97. if tc.count != 2 {
  98. t.Errorf("table t1 a should have 2 instances but got %d", tc.count)
  99. return
  100. }
  101. tc = db.tables["t2_a"]
  102. if tc.count != 1 {
  103. t.Errorf("table t2 a should have 1 instances")
  104. return
  105. }
  106. db.dropTable("t1", "a")
  107. db.dropTable("t2", "a")
  108. _, ok = db.getTable("t2", "a")
  109. if ok {
  110. t.Errorf("table t2 a should not exist")
  111. return
  112. }
  113. tc = db.tables["t1_a"]
  114. if tc.count != 1 {
  115. t.Errorf("table t1 a should have 1 instances but got %d", tc.count)
  116. return
  117. }
  118. }