sqlTs_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2021 INTECH Process Automation 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 ts
  15. import (
  16. "github.com/lf-edge/ekuiper/internal/pkg/db/sql/sqlite"
  17. sb "github.com/lf-edge/ekuiper/internal/pkg/ts/sql"
  18. "github.com/lf-edge/ekuiper/internal/pkg/ts/test/common"
  19. "github.com/lf-edge/ekuiper/pkg/kv/stores"
  20. "os"
  21. "path"
  22. "path/filepath"
  23. "testing"
  24. )
  25. const (
  26. DbName = "sqliteTS.db"
  27. Table = "test"
  28. )
  29. func TestSqlTsSet(t *testing.T) {
  30. ks, db, abs := setupSqlKv()
  31. defer cleanSqlKv(db, abs)
  32. common.TestTsSet(ks, t)
  33. }
  34. func TestSqlTsLast(t *testing.T) {
  35. ks, db, abs := setupSqlKv()
  36. defer cleanSqlKv(db, abs)
  37. common.TestTsLast(ks, t)
  38. }
  39. func TestSqlTsGet(t *testing.T) {
  40. ks, db, abs := setupSqlKv()
  41. defer cleanSqlKv(db, abs)
  42. common.TestTsGet(ks, t)
  43. }
  44. func TestSqlTsDelete(t *testing.T) {
  45. ks, db, abs := setupSqlKv()
  46. defer cleanSqlKv(db, abs)
  47. common.TestTsDelete(ks, t)
  48. }
  49. func TestSqlTsDeleteBefore(t *testing.T) {
  50. ks, db, abs := setupSqlKv()
  51. defer cleanSqlKv(db, abs)
  52. common.TestTsDeleteBefore(ks, t)
  53. }
  54. func deleteIfExists(abs string) error {
  55. absPath := path.Join(abs, DbName)
  56. if f, _ := os.Stat(absPath); f != nil {
  57. return os.Remove(absPath)
  58. }
  59. return nil
  60. }
  61. func setupSqlKv() (stores.Tskv, *sqlite.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 := sqlite.Config{
  71. Path: absPath,
  72. Name: DbName,
  73. }
  74. _, db := sqlite.NewSqliteDatabase(config)
  75. err = db.Connect()
  76. if err != nil {
  77. panic(err)
  78. }
  79. builder := sb.NewTsBuilder(db)
  80. if err != nil {
  81. panic(err)
  82. }
  83. var store stores.Tskv
  84. err, store = builder.CreateTs(Table)
  85. if err != nil {
  86. panic(err)
  87. }
  88. return store, db, absPath
  89. }
  90. func cleanSqlKv(db *sqlite.Database, abs string) {
  91. if err := db.Disconnect(); err != nil {
  92. panic(err)
  93. }
  94. if err := deleteIfExists(abs); err != nil {
  95. panic(err)
  96. }
  97. }