1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-05-01 13:48:56 +08:00
Mainflux.mainflux/things/redis/channels_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

119 lines
3.3 KiB
Go

//
// Copyright (c) 2018
// Mainflux
//
// SPDX-License-Identifier: Apache-2.0
//
package redis_test
import (
"fmt"
"testing"
"github.com/mainflux/mainflux/things/redis"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestConnect(t *testing.T) {
channelCache := redis.NewChannelCache(cacheClient)
cid := uint64(123)
tid := uint64(321)
cases := []struct {
desc string
cid uint64
tid uint64
}{
{desc: "connect thing to channel", cid: cid, tid: tid},
{desc: "connect already connected thing to channel", cid: cid, tid: tid},
}
for _, tc := range cases {
err := channelCache.Connect(cid, tid)
assert.Nil(t, err, fmt.Sprintf("%s: fail to connect due to: %s\n", tc.desc, err))
}
}
func TestHasThing(t *testing.T) {
channelCache := redis.NewChannelCache(cacheClient)
cid := uint64(123)
tid := uint64(321)
err := channelCache.Connect(cid, tid)
require.Nil(t, err, fmt.Sprintf("connect thing to channel: fail to connect due to: %s\n", err))
cases := map[string]struct {
cid uint64
tid uint64
hasAccess bool
}{
"access check for thing that has access": {cid: cid, tid: tid, hasAccess: true},
"access check for thing without access": {cid: cid, tid: cid, hasAccess: false},
"access check for non-existing channel": {cid: tid, tid: tid, hasAccess: false},
}
for desc, tc := range cases {
hasAccess := channelCache.HasThing(tc.cid, tc.tid)
assert.Equal(t, tc.hasAccess, hasAccess, fmt.Sprintf("%s: expected %t got %t\n", desc, tc.hasAccess, hasAccess))
}
}
func TestDisconnect(t *testing.T) {
channelCache := redis.NewChannelCache(cacheClient)
cid := uint64(123)
tid := uint64(321)
tid2 := uint64(322)
err := channelCache.Connect(cid, tid)
require.Nil(t, err, fmt.Sprintf("connect thing to channel: fail to connect due to: %s\n", err))
cases := []struct {
desc string
cid uint64
tid uint64
hasAccess bool
}{
{desc: "disconnecting connected thing", cid: cid, tid: tid, hasAccess: false},
{desc: "disconnecting non-connected thing", cid: cid, tid: tid2, hasAccess: false},
}
for _, tc := range cases {
err := channelCache.Disconnect(tc.cid, tc.tid)
assert.Nil(t, err, fmt.Sprintf("%s: fail due to: %s\n", tc.desc, err))
hasAccess := channelCache.HasThing(tc.cid, tc.tid)
assert.Equal(t, tc.hasAccess, hasAccess, fmt.Sprintf("access check after %s: expected %t got %t\n", tc.desc, tc.hasAccess, hasAccess))
}
}
func TestRemove(t *testing.T) {
channelCache := redis.NewChannelCache(cacheClient)
cid := uint64(123)
cid2 := uint64(124)
tid := uint64(321)
err := channelCache.Connect(cid, tid)
require.Nil(t, err, fmt.Sprintf("connect thing to channel: fail to connect due to: %s\n", err))
cases := []struct {
desc string
cid uint64
tid uint64
err error
hasAccess bool
}{
{desc: "Remove channel from cache", cid: cid, tid: tid, err: nil, hasAccess: false},
{desc: "Remove non-cached channel from cache", cid: cid2, tid: tid, err: nil, hasAccess: false},
}
for _, tc := range cases {
err := channelCache.Remove(tc.cid)
assert.Nil(t, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
hasAcces := channelCache.HasThing(tc.cid, tc.tid)
assert.Equal(t, tc.hasAccess, hasAcces, "%s - check access after removing channel: expected %t got %t\n", tc.desc, tc.hasAccess, hasAcces)
}
}