-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcache_test.go
47 lines (37 loc) · 1.13 KB
/
cache_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
package dinero
import (
"encoding/json"
"strings"
"testing"
"time"
. "github.com/onsi/gomega"
)
// TestCache will test that our in-memory cache of forex results is working.
func TestCache(t *testing.T) {
// Register the test.
NewWithT(t)
// Init dinero client.
client := NewClient(appID, "AUD", 1*time.Minute)
// Get latest forex rates.
response1, err := client.Rates.List()
if err != nil {
if strings.HasPrefix(err.Error(), setBaseNotAllowedResponsePrefix) {
t.Skipf("skipping test, unsuitable app ID: %s", err)
}
t.Fatalf("Unexpected error running client.Rates.List(): %s", err.Error())
}
// Fetch results again
response2, ok := client.Cache.Get("AUD", time.Now())
if !ok {
t.Fatalf("Expected response when fetching from cache for base currency AUD, got: %v", response2)
}
first, _ := json.Marshal(response1)
second, _ := json.Marshal(response2)
Expect(first).To(MatchJSON(second))
// Expire the cache
client.Cache.Expire("AUD", time.Now())
// Fetch results again (from the cache), now it's cleared.
response2, _ = client.Cache.Get("AUD", time.Now())
// Should be nothing.
Expect(response2).Should(BeNil())
}