forked from absmach/supermq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththings_test.go
120 lines (103 loc) · 2.67 KB
/
things_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package redis_test
import (
"context"
"fmt"
"testing"
r "github.com/go-redis/redis"
"github.com/mainflux/mainflux/errors"
"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(redisClient)
key, err := uuid.New().ID()
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))
id := "123"
id2 := "124"
err = thingCache.Save(context.Background(), key, id2)
require.Nil(t, err, fmt.Sprintf("Save thing to cache: expected nil got %s", err))
cases := []struct {
desc string
ID string
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(context.Background(), 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(redisClient)
key, err := uuid.New().ID()
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))
id := "123"
err = thingCache.Save(context.Background(), key, id)
require.Nil(t, err, fmt.Sprintf("Save thing to cache: expected nil got %s", err))
cases := map[string]struct {
ID string
key string
err error
}{
"Get ID by existing thing-key": {
ID: id,
key: key,
err: nil,
},
"Get ID by non-existing thing-key": {
ID: "",
key: wrongValue,
err: r.Nil,
},
}
for desc, tc := range cases {
cacheID, err := thingCache.ID(context.Background(), tc.key)
assert.Equal(t, tc.ID, cacheID, fmt.Sprintf("%s: expected %s got %s\n", desc, tc.ID, cacheID))
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", desc, tc.err, err))
}
}
func TestThingRemove(t *testing.T) {
thingCache := redis.NewThingCache(redisClient)
key, err := uuid.New().ID()
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))
id := "123"
id2 := "321"
thingCache.Save(context.Background(), key, id)
cases := []struct {
desc string
ID string
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(context.Background(), tc.ID)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
}
}