1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-05-04 22:17:59 +08:00
Mainflux.mainflux/things/redis/things_test.go
Aleksandar Novaković ad5c66fad2 NOISSUE - Refactor SDK and things service (#420)
* Refactor Mainflux go SDK

Add structures instead of string parameters. Add offset and limit
parameters to things and channels methods. Add better configuration
support.

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Add new public errors with better error handling

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Update SDK to use uint instread of string id

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Update cli to use new SDK API

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Remove TLS termination from nginx configuration

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Update SDK documentation and structures

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Refactor things service

Decouple HTTP layer from business logic. Remove ID number validation
check. Remove models from HTTP requests and responses.

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Reformat tests for things service

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Increase test coverage for things service

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2018-10-24 10:21:03 +01:00

121 lines
2.3 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))
}
}