forked from prebid/prebid-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_test.go
More file actions
98 lines (90 loc) · 2.38 KB
/
memory_test.go
File metadata and controls
98 lines (90 loc) · 2.38 KB
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
package backends
import (
"context"
"testing"
"github.com/prebid/prebid-cache/utils"
"github.com/stretchr/testify/assert"
)
func TestMemoryBackend(t *testing.T) {
type testExpectedValues struct {
value string
err error
}
type aTest struct {
desc string
backend *MemoryBackend
setup func(b *MemoryBackend)
run func(b *MemoryBackend) (string, error)
expected testExpectedValues
}
testGroups := []struct {
desc string
testCases []aTest
}{
{
"Put tests",
[]aTest{
{
desc: "succesful put",
backend: NewMemoryBackend(),
setup: func(b *MemoryBackend) {},
run: func(b *MemoryBackend) (string, error) {
err := b.Put(context.Background(), "someKey", "someValye", 0)
return "", err
},
expected: testExpectedValues{err: nil},
},
{
desc: "Put returns a RecordExistsError",
backend: NewMemoryBackend(),
setup: func(b *MemoryBackend) {
b.Put(context.Background(), "someKey", "someValue", 0)
},
run: func(b *MemoryBackend) (string, error) {
err := b.Put(context.Background(), "someKey", "someValye", 0)
return "", err
},
expected: testExpectedValues{"", utils.NewPBCError(utils.RECORD_EXISTS)},
},
},
},
{
"Get tests",
[]aTest{
{
desc: "succesful get",
backend: NewMemoryBackend(),
setup: func(b *MemoryBackend) {
b.Put(context.Background(), "someKey", "someValue", 0)
},
run: func(b *MemoryBackend) (string, error) {
return b.Get(context.Background(), "someKey")
},
expected: testExpectedValues{"someValue", nil},
},
{
desc: "Get returns a Key not found error",
backend: NewMemoryBackend(),
setup: func(b *MemoryBackend) {
b.Put(context.Background(), "someKey", "someValue", 0)
},
run: func(b *MemoryBackend) (string, error) {
return b.Get(context.Background(), "anotherKey")
},
expected: testExpectedValues{"", utils.NewPBCError(utils.KEY_NOT_FOUND)},
},
},
},
}
for _, group := range testGroups {
for _, tc := range group.testCases {
// Setup
tc.setup(tc.backend)
//Run
resultingValue, resultingError := tc.run(tc.backend)
//Assert
assert.Equal(t, tc.expected.value, resultingValue, "%s - %s", group.desc, tc.desc)
assert.Equal(t, tc.expected.err, resultingError, "%s - %s", group.desc, tc.desc)
}
}
}