sqlKv_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 sql
  15. import (
  16. "github.com/lf-edge/ekuiper/internal/pkg/store/definition"
  17. "github.com/lf-edge/ekuiper/internal/pkg/store/sql/sqlite"
  18. "github.com/lf-edge/ekuiper/internal/pkg/store/test/common"
  19. "github.com/lf-edge/ekuiper/pkg/kv"
  20. "os"
  21. "path"
  22. "path/filepath"
  23. "testing"
  24. )
  25. const SDbName = "sqliteKV.db"
  26. const STable = "test"
  27. func TestSqlKvSetnx(t *testing.T) {
  28. ks, db, abs := setupSqlKv()
  29. defer cleanSqlKv(db, abs)
  30. common.TestKvSetnx(ks, t)
  31. }
  32. func TestSqlKvSet(t *testing.T) {
  33. ks, db, abs := setupSqlKv()
  34. defer cleanSqlKv(db, abs)
  35. common.TestKvSet(ks, t)
  36. }
  37. func TestSqlKvGet(t *testing.T) {
  38. ks, db, abs := setupSqlKv()
  39. defer cleanSqlKv(db, abs)
  40. common.TestKvGet(ks, t)
  41. }
  42. func TestSqlKvSetGet(t *testing.T) {
  43. ks, db, abs := setupSqlKv()
  44. defer cleanSqlKv(db, abs)
  45. common.TestKvSetGet(ks, t)
  46. }
  47. func TestSqlKvKeys(t *testing.T) {
  48. ks, db, abs := setupSqlKv()
  49. defer cleanSqlKv(db, abs)
  50. length := 10
  51. common.TestKvKeys(length, ks, t)
  52. }
  53. func TestSqlKvAll(t *testing.T) {
  54. ks, db, abs := setupSqlKv()
  55. defer cleanSqlKv(db, abs)
  56. length := 10
  57. common.TestKvAll(length, ks, t)
  58. }
  59. func TestSqlKvGetKeyedState(t *testing.T) {
  60. ks, db, abs := setupSqlKv()
  61. defer cleanSqlKv(db, abs)
  62. common.TestKvGetKeyedState(ks, t)
  63. }
  64. func deleteIfExists(abs string) error {
  65. absPath := path.Join(abs, SDbName)
  66. if f, _ := os.Stat(absPath); f != nil {
  67. return os.Remove(absPath)
  68. }
  69. return nil
  70. }
  71. func setupSqlKv() (kv.KeyValue, definition.Database, string) {
  72. absPath, err := filepath.Abs("test")
  73. if err != nil {
  74. panic(err)
  75. }
  76. err = deleteIfExists(absPath)
  77. if err != nil {
  78. panic(err)
  79. }
  80. config := definition.Config{
  81. Type: "sqlite",
  82. Redis: definition.RedisConfig{},
  83. Sqlite: definition.SqliteConfig{
  84. Path: absPath,
  85. Name: SDbName,
  86. },
  87. }
  88. db, _ := sqlite.NewSqliteDatabase(config, "sqliteKV.db")
  89. err = db.Connect()
  90. if err != nil {
  91. panic(err)
  92. }
  93. builder := NewStoreBuilder(db.(Database))
  94. var store kv.KeyValue
  95. store, err = builder.CreateStore(STable)
  96. if err != nil {
  97. panic(err)
  98. }
  99. return store, db, absPath
  100. }
  101. func cleanSqlKv(db definition.Database, abs string) {
  102. if err := db.Disconnect(); err != nil {
  103. panic(err)
  104. }
  105. if err := deleteIfExists(abs); err != nil {
  106. panic(err)
  107. }
  108. }