This repository has been archived by the owner on Mar 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgotcha_test.go
104 lines (91 loc) · 2.22 KB
/
gotcha_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
package gotcha_test
import (
"testing"
"github.com/bxcodec/gotcha"
)
func TestGotcha(t *testing.T) {
t.Run("set", func(t *testing.T) {
err := gotcha.Set("name", "John Snow")
if err != nil {
t.Fatalf("expected: %v, got %v", nil, err)
}
err = gotcha.Set("kingdom", "North Kingdom")
if err != nil {
t.Fatalf("expected: %v, got %v", nil, err)
}
})
t.Run("get", func(t *testing.T) {
val, err := gotcha.Get("name")
if err != nil {
t.Fatalf("expected: %v, got %v", nil, err)
}
if val.(string) != "John Snow" {
t.Fatalf("expected: %v, got %v", "John Snow", val)
}
})
t.Run("get-keys", func(t *testing.T) {
keys, err := gotcha.GetKeys()
if err != nil {
t.Fatalf("expected: %v, got %v", nil, err)
}
var contains = func(keys []string, k string) bool {
for _, item := range keys {
if item == k {
return true
}
}
return false
}
expectedKeys := []string{"name", "kingdom"}
for _, k := range expectedKeys {
if !contains(keys, k) {
t.Fatalf("expected: %v, got: %v", true, false)
}
}
})
t.Run("delete", func(t *testing.T) {
// Ensure the key is exists
val, err := gotcha.Get("kingdom")
if err != nil {
t.Fatalf("expected: %v, got %v", nil, err)
}
if val.(string) != "North Kingdom" {
t.Fatalf("expected: %v, got %v", "John Snow", val)
}
// Delete the Keys
err = gotcha.Delete("kingdom")
if err != nil {
t.Fatalf("expected: %v, got %v", nil, err)
}
// Re-Ensure the keys is deleted
val, err = gotcha.Get("kingdom")
if err == nil {
t.Fatalf("expected: %v, got %v", "error", err)
}
if val != nil {
t.Fatalf("expected: %v, got %v", nil, val)
}
})
t.Run("clear-cache", func(t *testing.T) {
// Ensure the cache is still contains item
keys, err := gotcha.GetKeys()
if err != nil {
t.Fatalf("expected: %v, got %v", nil, err)
}
if len(keys) == 0 {
t.Fatalf("expected: %v, got %v", "not zero", len(keys))
}
err = gotcha.ClearCache()
if err != nil {
t.Fatalf("expected: %v, got %v", nil, err)
}
// Re-Ensure the cache already cleared
keys, err = gotcha.GetKeys()
if err != nil {
t.Fatalf("expected: %v, got %v", nil, err)
}
if len(keys) != 0 {
t.Fatalf("expected: %v, got %v", "zero", len(keys))
}
})
}