sqlKv_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2021 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 store
  15. import (
  16. "github.com/lf-edge/ekuiper/internal/pkg/db/sql/sqlite"
  17. sb "github.com/lf-edge/ekuiper/internal/pkg/store/sql"
  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 DbName = "sqliteKV.db"
  26. const Table = "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, DbName)
  50. if f, _ := os.Stat(absPath); f != nil {
  51. return os.Remove(absPath)
  52. }
  53. return nil
  54. }
  55. func setupSqlKv() (kv.KeyValue, *sqlite.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 := sqlite.Config{
  65. Path: absPath,
  66. Name: DbName,
  67. }
  68. _, db := sqlite.NewSqliteDatabase(config)
  69. err = db.Connect()
  70. if err != nil {
  71. panic(err)
  72. }
  73. builder := sb.NewStoreBuilder(db)
  74. var store kv.KeyValue
  75. store, err = builder.CreateStore(Table)
  76. if err != nil {
  77. panic(err)
  78. }
  79. return store, db, absPath
  80. }
  81. func cleanSqlKv(db *sqlite.Database, abs string) {
  82. if err := db.Disconnect(); err != nil {
  83. panic(err)
  84. }
  85. if err := deleteIfExists(abs); err != nil {
  86. panic(err)
  87. }
  88. }