2018-09-30 02:03:00 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2018
|
|
|
|
// Mainflux
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
|
|
|
|
package redis_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
r "github.com/go-redis/redis"
|
|
|
|
"github.com/mainflux/mainflux/things/redis"
|
|
|
|
"github.com/mainflux/mainflux/things/uuid"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestThingSave(t *testing.T) {
|
2018-11-20 19:46:33 +01:00
|
|
|
thingCache := redis.NewThingCache(redisClient)
|
2018-09-30 02:03:00 +02:00
|
|
|
key := uuid.New().ID()
|
2018-12-05 13:09:25 +01:00
|
|
|
id := "123"
|
|
|
|
id2 := "124"
|
2018-09-30 02:03:00 +02:00
|
|
|
|
|
|
|
err := thingCache.Save(key, id2)
|
|
|
|
require.Nil(t, err, fmt.Sprintf("Save thing to cache: expected nil got %s", err))
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
desc string
|
2018-12-05 13:09:25 +01:00
|
|
|
ID string
|
2018-09-30 02:03:00 +02:00
|
|
|
key string
|
|
|
|
err error
|
|
|
|
}{
|
2018-10-24 11:21:03 +02:00
|
|
|
{
|
|
|
|
desc: "Save thing to cache",
|
|
|
|
ID: id,
|
|
|
|
key: key,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Save already cached thing to cache",
|
|
|
|
ID: id2,
|
|
|
|
key: key,
|
|
|
|
err: nil,
|
|
|
|
},
|
2018-09-30 02:03:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
err := thingCache.Save(tc.key, tc.ID)
|
|
|
|
assert.Nil(t, err, fmt.Sprintf("%s: expected %s got %s", tc.desc, tc.err, err))
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestThingID(t *testing.T) {
|
2018-11-20 19:46:33 +01:00
|
|
|
thingCache := redis.NewThingCache(redisClient)
|
2018-09-30 02:03:00 +02:00
|
|
|
|
|
|
|
key := uuid.New().ID()
|
2018-12-05 13:09:25 +01:00
|
|
|
id := "123"
|
2018-09-30 02:03:00 +02:00
|
|
|
err := thingCache.Save(key, id)
|
|
|
|
require.Nil(t, err, fmt.Sprintf("Save thing to cache: expected nil got %s", err))
|
|
|
|
|
|
|
|
cases := map[string]struct {
|
2018-12-05 13:09:25 +01:00
|
|
|
ID string
|
2018-09-30 02:03:00 +02:00
|
|
|
key string
|
|
|
|
err error
|
|
|
|
}{
|
2018-10-24 11:21:03 +02:00
|
|
|
"Get ID by existing thing-key": {
|
|
|
|
ID: id,
|
|
|
|
key: key,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
"Get ID by non-existing thing-key": {
|
2018-12-05 13:09:25 +01:00
|
|
|
ID: "",
|
2018-10-24 11:21:03 +02:00
|
|
|
key: wrongValue,
|
|
|
|
err: r.Nil,
|
|
|
|
},
|
2018-09-30 02:03:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for desc, tc := range cases {
|
|
|
|
cacheID, err := thingCache.ID(tc.key)
|
2018-12-05 13:09:25 +01:00
|
|
|
assert.Equal(t, tc.ID, cacheID, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.ID, cacheID))
|
2018-09-30 02:03:00 +02:00
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestThingRemove(t *testing.T) {
|
2018-11-20 19:46:33 +01:00
|
|
|
thingCache := redis.NewThingCache(redisClient)
|
2018-09-30 02:03:00 +02:00
|
|
|
|
|
|
|
key := uuid.New().ID()
|
2018-12-05 13:09:25 +01:00
|
|
|
id := "123"
|
|
|
|
id2 := "321"
|
2018-09-30 02:03:00 +02:00
|
|
|
thingCache.Save(key, id)
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
desc string
|
2018-12-05 13:09:25 +01:00
|
|
|
ID string
|
2018-09-30 02:03:00 +02:00
|
|
|
err error
|
|
|
|
}{
|
2018-10-24 11:21:03 +02:00
|
|
|
{
|
|
|
|
desc: "Remove existing thing from cache",
|
|
|
|
ID: id,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Remove non-existing thing from cache",
|
|
|
|
ID: id2,
|
|
|
|
err: r.Nil,
|
|
|
|
},
|
2018-09-30 02:03:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
err := thingCache.Remove(tc.ID)
|
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|