-
Notifications
You must be signed in to change notification settings - Fork 8
/
remember_test.go
116 lines (88 loc) · 2.83 KB
/
remember_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
// Copyright 2018-21 PJ Engineering and Business Solutions Pty. Ltd. All rights reserved.
package remember_test
import (
"context"
"log"
"regexp"
"testing"
"time"
"github.com/rocketlaunchr/remember-go"
"github.com/rocketlaunchr/remember-go/memory"
)
type aLogger struct{}
func (l aLogger) Log(format string, args ...interface{}) {
log.Printf(format, args...)
}
func TestKeyGen(t *testing.T) {
actuals := []string{
remember.CreateKey(false, "+", "", 1, 2, 3),
remember.CreateKeyStruct(struct {
Search string
notExported string
Ignored string `json:"-"`
Omit string `json:"xxx"`
Page int
}{"search", "z", "y", "ppp", 1}),
remember.Hash("crc32-hash"),
remember.CreateKey(true, "", "", 1, 2, 3),
}
expected := []string{
"1+2+3",
`{"Page":1,"Search":"search","xxx":"ppp"}`,
"40ffd476",
`^github.com/rocketlaunchr/remember-go_test.TestKeyGen_.+?github.com/rocketlaunchr/remember-go/remember_test.go_\d+_1 2 3$`,
}
for i := range actuals {
actual := actuals[i]
if i == 3 {
match, _ := regexp.MatchString(expected[i], actual)
if !match {
t.Errorf("wrong val: expected (regex): %v actual: %v", expected[i], actual)
}
} else {
if actual != expected[i] {
t.Errorf("wrong val: expected: %v actual: %v", expected[i], actual)
}
}
}
}
func TestKeyBasicOperation(t *testing.T) {
ctx := context.Background()
var ms = memory.NewMemoryStore(10 * time.Minute)
key := "key"
exp := 10 * time.Minute
slowQuery := func(ctx context.Context) (interface{}, error) {
return "val", nil
}
actual, _, _ := remember.Cache(ctx, ms, key, exp, slowQuery, remember.Options{Logger: aLogger{}, GobRegister: true})
expected := "val"
if actual.(string) != expected {
t.Errorf("wrong val: expected: %v actual: %v", expected, actual)
}
}
func TestFetchFromCacheAndDisableCache(t *testing.T) {
ctx := context.Background()
var ms = memory.NewMemoryStore(10 * time.Minute)
key := "key"
exp := 10 * time.Minute
slowQuery := func(ctx context.Context) (interface{}, error) {
return "val", nil
}
// warm up cache
remember.Cache(ctx, ms, key, exp, slowQuery, remember.Options{Logger: aLogger{}, GobRegister: true})
// This time fetch from cache
actual, _, _ := remember.Cache(ctx, ms, key, exp, slowQuery, remember.Options{Logger: aLogger{}, GobRegister: true})
expected := "val"
if actual.(string) != expected {
t.Errorf("wrong val: expected: %v actual: %v", expected, actual)
}
// Actual is now "val", Let's change it to "val2" and disable cache usage.
slowQuery = func(ctx context.Context) (interface{}, error) {
return "val2", nil
}
actual, _, _ = remember.Cache(ctx, ms, key, exp, slowQuery, remember.Options{Logger: aLogger{}, DisableCacheUsage: true})
expected = "val2"
if actual.(string) != expected {
t.Errorf("wrong val: expected: %v actual: %v", expected, actual)
}
}