sqlKv_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 deleteIfExists(abs string) error {
  60. absPath := path.Join(abs, SDbName)
  61. if f, _ := os.Stat(absPath); f != nil {
  62. return os.Remove(absPath)
  63. }
  64. return nil
  65. }
  66. func setupSqlKv() (kv.KeyValue, definition.Database, string) {
  67. absPath, err := filepath.Abs("test")
  68. if err != nil {
  69. panic(err)
  70. }
  71. err = deleteIfExists(absPath)
  72. if err != nil {
  73. panic(err)
  74. }
  75. config := definition.Config{
  76. Type: "sqlite",
  77. Redis: definition.RedisConfig{},
  78. Sqlite: definition.SqliteConfig{
  79. Path: absPath,
  80. Name: SDbName,
  81. },
  82. }
  83. db, _ := sqlite.NewSqliteDatabase(config, "sqliteKV.db")
  84. err = db.Connect()
  85. if err != nil {
  86. panic(err)
  87. }
  88. builder := NewStoreBuilder(db.(Database))
  89. var store kv.KeyValue
  90. store, err = builder.CreateStore(STable)
  91. if err != nil {
  92. panic(err)
  93. }
  94. return store, db, absPath
  95. }
  96. func cleanSqlKv(db definition.Database, abs string) {
  97. if err := db.Disconnect(); err != nil {
  98. panic(err)
  99. }
  100. if err := deleteIfExists(abs); err != nil {
  101. panic(err)
  102. }
  103. }