1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-28 13:48:49 +08:00
Mainflux.mainflux/things/redis/things_test.go
Ivan Milošević d5f0d7d225 MF-384 - Add test for Redis cache (#405)
* Add test

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* Add test

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* Add test

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* Add test

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* Add test

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* MF-384 - Add test for Redis cache

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* Add copyright headers

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* Fix copyright header

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* Fix test setup

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* handling errors and put test cases in maps instead of slices

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* Add test case if thing already exists

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* Fix TestThingSave to use require instead of assert

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* Add test case for removing non-existing thing from cache

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* Use table of test cases for Remove and test Connect for every case

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* Use table of test cases for Save

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* Test cases in slice instead of map for testing Remove

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* Make test-cases independent, use asserts instead of requires

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* Use slice and map where appropriate

Signed-off-by: Ivan Milošević <iva@blokovi.com>
2018-09-30 02:03:00 +02:00

95 lines
2.2 KiB
Go

//
// 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) {
thingCache := redis.NewThingCache(cacheClient)
key := uuid.New().ID()
id := uint64(123)
id2 := uint64(124)
err := thingCache.Save(key, id2)
require.Nil(t, err, fmt.Sprintf("Save thing to cache: expected nil got %s", err))
cases := []struct {
desc string
ID uint64
key string
err error
}{
{desc: "Save thing to cache", ID: id, key: key, err: nil},
{desc: "Save already cached thing to cache", ID: id2, key: key, err: nil},
}
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) {
thingCache := redis.NewThingCache(cacheClient)
key := uuid.New().ID()
id := uint64(123)
err := thingCache.Save(key, id)
require.Nil(t, err, fmt.Sprintf("Save thing to cache: expected nil got %s", err))
cases := map[string]struct {
ID uint64
key string
err error
}{
"Get ID by existing thing-key": {ID: id, key: key, err: nil},
"Get ID by non-existing thing-key": {ID: 0, key: wrongValue, err: r.Nil},
}
for desc, tc := range cases {
cacheID, err := thingCache.ID(tc.key)
assert.Equal(t, tc.ID, cacheID, fmt.Sprintf("%s: expected %d got %d\n", desc, tc.ID, cacheID))
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
}
}
func TestThingRemove(t *testing.T) {
thingCache := redis.NewThingCache(cacheClient)
key := uuid.New().ID()
id := uint64(123)
id2 := uint64(321)
thingCache.Save(key, id)
cases := []struct {
desc string
ID uint64
err error
}{
{desc: "Remove existing thing from cache", ID: id, err: nil},
{desc: "Remove non-existing thing from cache", ID: id2, err: r.Nil},
}
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))
}
}