redisTs_test.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 redis
  15. import (
  16. "github.com/alicebob/miniredis/v2"
  17. "github.com/lf-edge/ekuiper/internal/pkg/db/redis"
  18. "github.com/lf-edge/ekuiper/internal/pkg/ts/test/common"
  19. ts2 "github.com/lf-edge/ekuiper/pkg/kv"
  20. "strconv"
  21. "testing"
  22. )
  23. func TestRedisTsSet(t *testing.T) {
  24. ks, db, minRedis := setupRedisKv()
  25. defer cleanRedisKv(db, minRedis)
  26. common.TestTsSet(ks, t)
  27. }
  28. func TestRedisTsLast(t *testing.T) {
  29. ks, db, minRedis := setupRedisKv()
  30. defer cleanRedisKv(db, minRedis)
  31. common.TestTsLast(ks, t)
  32. }
  33. func TestRedisTsGet(t *testing.T) {
  34. ks, db, minRedis := setupRedisKv()
  35. defer cleanRedisKv(db, minRedis)
  36. common.TestTsGet(ks, t)
  37. }
  38. func TestRedisTsDelete(t *testing.T) {
  39. ks, db, minRedis := setupRedisKv()
  40. defer cleanRedisKv(db, minRedis)
  41. common.TestTsDelete(ks, t)
  42. }
  43. func TestRedisTsDeleteBefore(t *testing.T) {
  44. ks, db, minRedis := setupRedisKv()
  45. defer cleanRedisKv(db, minRedis)
  46. common.TestTsDeleteBefore(ks, t)
  47. }
  48. func setupRedisKv() (ts2.Tskv, *redis.Instance, *miniredis.Miniredis) {
  49. minRedis, err := miniredis.Run()
  50. if err != nil {
  51. panic(err)
  52. }
  53. redisDB := redis.NewRedis("localhost", stringToInt(minRedis.Port()))
  54. err = redisDB.Connect()
  55. if err != nil {
  56. panic(err)
  57. }
  58. builder := NewTsBuilder(redisDB)
  59. var ks ts2.Tskv
  60. err, ks = builder.CreateTs("test")
  61. if err != nil {
  62. panic(err)
  63. }
  64. return ks, &redisDB, minRedis
  65. }
  66. func cleanRedisKv(instance *redis.Instance, minRedis *miniredis.Miniredis) {
  67. instance.Disconnect()
  68. minRedis.Close()
  69. }
  70. func stringToInt(svalue string) int {
  71. ivalue, err := strconv.Atoi(svalue)
  72. if err != nil {
  73. panic(err)
  74. }
  75. return ivalue
  76. }