test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. "fmt"
  17. "reflect"
  18. "testing"
  19. "github.com/lf-edge/ekuiper/pkg/kv"
  20. )
  21. func TestKvSetnx(ks kv.KeyValue, t *testing.T) {
  22. if err := ks.Setnx("foo", "bar"); nil != err {
  23. t.Error(err)
  24. }
  25. var val string
  26. _, _ = ks.Get("foo", &val)
  27. if val != "bar" {
  28. t.Error("expect:bar", "get:", val)
  29. }
  30. if err := ks.Setnx("foo", "bar1"); nil == err {
  31. t.Errorf("Can't overwrite an existing intem: %v", err)
  32. }
  33. }
  34. func TestKvSetGet(ks kv.KeyValue, t *testing.T) {
  35. var val string
  36. // SetNX
  37. if err := ks.Setnx("foo", "bar"); nil != err {
  38. t.Error(err)
  39. }
  40. _, _ = ks.Get("foo", &val)
  41. if val != "bar" {
  42. t.Error("expect:bar", "get:", val)
  43. }
  44. if err := ks.Setnx("foo", "bar1"); nil == err {
  45. t.Errorf("Can't overwrite an existing intem: %v", err)
  46. }
  47. _, _ = ks.Get("foo", &val)
  48. if val != "bar" {
  49. t.Error("expect:bar", "get:", val)
  50. }
  51. // Set
  52. if err := ks.Set("foo", "bar"); nil != err {
  53. t.Error(err)
  54. }
  55. _, _ = ks.Get("foo", &val)
  56. if val != "bar" {
  57. t.Error("expect:bar", "get:", val)
  58. }
  59. if err := ks.Set("foo", "bar1"); nil != err {
  60. t.Errorf("Set should overwrite an existing record")
  61. }
  62. _, _ = ks.Get("foo", &val)
  63. if val != "bar1" {
  64. t.Error("expect:bar", "get:", val)
  65. }
  66. }
  67. func TestKvSet(ks kv.KeyValue, t *testing.T) {
  68. if err := ks.Set("foo", "bar"); nil != err {
  69. t.Error(err)
  70. }
  71. if err := ks.Set("foo", "bar1"); nil != err {
  72. t.Errorf("Set should overwrite an existing record")
  73. }
  74. }
  75. func TestKvGet(ks kv.KeyValue, t *testing.T) {
  76. if err := ks.Setnx("foo", "bar"); nil != err {
  77. t.Error(err)
  78. }
  79. var v string
  80. if ok, _ := ks.Get("foo", &v); ok {
  81. if !reflect.DeepEqual("bar", v) {
  82. t.Error("expect:bar", "get:", v)
  83. }
  84. } else {
  85. t.Errorf("Should find the foo key")
  86. }
  87. }
  88. func TestKvGetKeyedState(ks kv.KeyValue, t *testing.T) {
  89. if err := ks.SetKeyedState("foo", "bar"); nil != err {
  90. t.Error(err)
  91. }
  92. if v, err := ks.GetKeyedState("foo"); err != nil {
  93. t.Errorf("Should find the foo key")
  94. } else {
  95. if !reflect.DeepEqual("bar", v) {
  96. t.Error("expect:bar", "get:", v)
  97. }
  98. }
  99. }
  100. func TestKvKeys(length int, ks kv.KeyValue, t *testing.T) {
  101. expected := make([]string, 0)
  102. for i := 0; i < length; i++ {
  103. key := fmt.Sprintf("key-%d", i)
  104. value := fmt.Sprintf("value-%d", i)
  105. if err := ks.Setnx(key, value); err != nil {
  106. t.Errorf("It should be set")
  107. }
  108. expected = append(expected, key)
  109. }
  110. var keys []string
  111. var err error
  112. if keys, err = ks.Keys(); err != nil {
  113. t.Errorf("Failed to get value: %s.", err)
  114. } else if !reflect.DeepEqual(length, len(keys)) {
  115. t.Errorf("expect: %d, got: %d", length, len(keys))
  116. }
  117. if !reflect.DeepEqual(keys, expected) {
  118. t.Errorf("Keys do not match expected %s != %s", keys, expected)
  119. }
  120. }
  121. func TestKvAll(length int, ks kv.KeyValue, t *testing.T) {
  122. expected := make(map[string]string)
  123. for i := 0; i < length; i++ {
  124. key := fmt.Sprintf("key-%d", i)
  125. value := fmt.Sprintf("value-%d", i)
  126. if err := ks.Setnx(key, value); err != nil {
  127. t.Errorf("It should be set")
  128. }
  129. expected[key] = value
  130. }
  131. var (
  132. all map[string]string
  133. err error
  134. )
  135. if all, err = ks.All(); err != nil {
  136. t.Errorf("Failed to get value: %s.", err)
  137. return
  138. } else if length != len(all) {
  139. t.Errorf("expect: %d, got: %d", length, len(all))
  140. return
  141. }
  142. if !reflect.DeepEqual(all, expected) {
  143. t.Errorf("All values do not match expected %s != %s", all, expected)
  144. }
  145. }