sqlKv_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 TestSqlKvKeys(t *testing.T) {
  43. ks, db, abs := setupSqlKv()
  44. defer cleanSqlKv(db, abs)
  45. length := 10
  46. common.TestKvKeys(length, ks, t)
  47. }
  48. func TestSqlKvAll(t *testing.T) {
  49. ks, db, abs := setupSqlKv()
  50. defer cleanSqlKv(db, abs)
  51. length := 10
  52. common.TestKvAll(length, ks, t)
  53. }
  54. func deleteIfExists(abs string) error {
  55. absPath := path.Join(abs, SDbName)
  56. if f, _ := os.Stat(absPath); f != nil {
  57. return os.Remove(absPath)
  58. }
  59. return nil
  60. }
  61. func setupSqlKv() (kv.KeyValue, definition.Database, string) {
  62. absPath, err := filepath.Abs("test")
  63. if err != nil {
  64. panic(err)
  65. }
  66. err = deleteIfExists(absPath)
  67. if err != nil {
  68. panic(err)
  69. }
  70. config := definition.Config{
  71. Type: "sqlite",
  72. Redis: definition.RedisConfig{},
  73. Sqlite: definition.SqliteConfig{
  74. Path: absPath,
  75. Name: SDbName,
  76. },
  77. }
  78. db, _ := sqlite.NewSqliteDatabase(config, "sqliteKV.db")
  79. err = db.Connect()
  80. if err != nil {
  81. panic(err)
  82. }
  83. builder := NewStoreBuilder(db.(Database))
  84. var store kv.KeyValue
  85. store, err = builder.CreateStore(STable)
  86. if err != nil {
  87. panic(err)
  88. }
  89. return store, db, absPath
  90. }
  91. func cleanSqlKv(db definition.Database, abs string) {
  92. if err := db.Disconnect(); err != nil {
  93. panic(err)
  94. }
  95. if err := deleteIfExists(abs); err != nil {
  96. panic(err)
  97. }
  98. }