-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathclient_test.go
83 lines (71 loc) · 2.4 KB
/
client_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
package pokeapi
import (
"encoding/json"
"testing"
"time"
"github.com/mtslzr/pokeapi-go/structs"
"github.com/stretchr/testify/assert"
)
var endpoint = "machine?offset=0&limit=1"
var mockResource structs.Resource
func TestSetCache(t *testing.T) {
_, found1 := c.Get(endpoint)
assert.Equal(t, false, found1,
"Expect to not have cached data before first call.")
_ = do(endpoint, &mockResource)
cached2, expires2, found2 := c.GetWithExpiration(endpoint)
assert.Equal(t, true, found2,
"Expect to have cached data after first call.")
_ = do(endpoint, &mockResource)
var cachedData structs.Resource
json.Unmarshal(cached2.([]byte), &cachedData)
cached3, expires3, _ := c.GetWithExpiration(endpoint)
assert.Equal(t, cachedData, mockResource,
"Expect data to match cached data.")
assert.Equal(t, cached2, cached3,
"Expect cached data to match previously-cached data.")
assert.Equal(t, expires2, expires3,
"Expect expiration times to match.")
}
func TestClearCache(t *testing.T) {
_, found := c.Get(endpoint)
if !found {
_ = do(endpoint, &mockResource)
_, found = c.Get(endpoint)
}
assert.NotEqual(t, false, found,
"Expect to start with cached data.")
ClearCache()
nocache, nofound := c.Get(endpoint)
assert.Equal(t, false, nofound,
"Expect no data found after flushing cache.")
assert.Equal(t, nil, nocache,
"Expect no data after flushing cache.")
}
func TestCustomExpiration(t *testing.T) {
ClearCache()
defaultExpire := time.Now().Add(defaultCacheSettings.MinExpire).Minute()
_ = do(endpoint, &mockResource)
_, expires1, _ := c.GetWithExpiration(endpoint)
assert.Equal(t, defaultExpire, expires1.Minute(),
"Expect expiration time to match default setting.")
ClearCache()
CacheSettings.CustomExpire = 20
customExpire := time.Now().Add(CacheSettings.CustomExpire * time.Minute).Minute()
_ = do(endpoint, &mockResource)
_, expires2, _ := c.GetWithExpiration(endpoint)
assert.Equal(t, customExpire, expires2.Minute(),
"Expect expiration time to match custom setting.")
}
func TestNoCache(t *testing.T) {
ClearCache()
_ = do(endpoint, &mockResource)
_, expires1, found1 := c.GetWithExpiration(endpoint)
assert.Equal(t, true, found1,
"Expect to have cached data after first call.")
CacheSettings.UseCache = false
_ = do(endpoint, &mockResource)
_, expires2, _ := c.GetWithExpiration(endpoint)
assert.NotEqual(t, expires1, expires2,
"Expect cache expiration not to match first call.")
}