sqlKv_test.go 2.4 KB

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