tstest.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2021-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 common
  15. import (
  16. "github.com/lf-edge/ekuiper/pkg/kv"
  17. "reflect"
  18. "testing"
  19. )
  20. var (
  21. Keys = []int64{1000, 1500, 2000, 3000}
  22. Values = []string{"bar1", "bar15", "bar2", "bar3"}
  23. )
  24. func TestTsSet(ks kv.Tskv, t *testing.T) {
  25. load(ks, t)
  26. if ok, err := ks.Set(2500, "bar25"); nil != err {
  27. t.Error(err)
  28. } else if ok {
  29. t.Errorf("should deny key 2500 while last one is 3000")
  30. }
  31. }
  32. func TestTsLast(ks kv.Tskv, t *testing.T) {
  33. load(ks, t)
  34. var v string
  35. if k, err := ks.Last(&v); err != nil {
  36. t.Error(err)
  37. } else if k != 3000 || v != "bar3" {
  38. t.Errorf("Last expect 3000/bar3 but got %d/%s", k, v)
  39. }
  40. }
  41. func TestTsGet(ks kv.Tskv, t *testing.T) {
  42. load(ks, t)
  43. var value string
  44. if ok, _ := ks.Get(2000, &value); ok {
  45. if !reflect.DeepEqual("bar2", value) {
  46. t.Error("expect:bar", "get:", value)
  47. }
  48. } else {
  49. t.Errorf("Should find key 2000.")
  50. }
  51. }
  52. func TestTsDelete(ks kv.Tskv, t *testing.T) {
  53. load(ks, t)
  54. if err := ks.Delete(1500); nil != err {
  55. t.Error(err)
  56. }
  57. var value string
  58. if ok, _ := ks.Get(1500, &value); ok {
  59. t.Errorf("Should not find deleted key 1500.")
  60. }
  61. }
  62. func TestTsDeleteBefore(ks kv.Tskv, t *testing.T) {
  63. load(ks, t)
  64. if ok, err := ks.Set(3500, "bar35"); nil != err {
  65. t.Error(err)
  66. } else if !ok {
  67. t.Error("should allow key 3500")
  68. }
  69. if err := ks.DeleteBefore(2999); nil != err {
  70. t.Error(err)
  71. }
  72. var value string
  73. if ok, _ := ks.Get(1000, &value); ok {
  74. t.Errorf("Should not find deleted key 1000.")
  75. }
  76. if ok, _ := ks.Get(2000, &value); ok {
  77. t.Errorf("Should not find deleted key 2000.")
  78. }
  79. if ok, _ := ks.Get(3000, &value); ok {
  80. if !reflect.DeepEqual("bar3", value) {
  81. t.Error("expect:bar3", "get:", value)
  82. }
  83. } else {
  84. t.Errorf("Should find key 3000.")
  85. }
  86. if ok, _ := ks.Get(3500, &value); ok {
  87. if !reflect.DeepEqual("bar35", value) {
  88. t.Error("expect:bar35", "get:", value)
  89. }
  90. } else {
  91. t.Errorf("Should find key 3500.")
  92. }
  93. }
  94. func load(ks kv.Tskv, t *testing.T) {
  95. for i := 0; i < len(Keys); i++ {
  96. k := Keys[i]
  97. v := Values[i]
  98. if ok, err := ks.Set(k, v); nil != err {
  99. t.Error(err)
  100. } else if !ok {
  101. t.Errorf("should allow key %d", k)
  102. }
  103. }
  104. }