-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathredis_test.go
64 lines (53 loc) · 1.44 KB
/
redis_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
package cache
import (
"net/http"
"os"
"testing"
"github.com/adhocore/urlsh/model"
"github.com/adhocore/urlsh/util"
)
func TestConnection(t *testing.T) {
key := "APP_CACHE_HOST"
old := os.Getenv(key)
t.Run("nil", func(t *testing.T) {
_ = os.Setenv(key, "")
t.Run("not nil", func(t *testing.T) {
_ = os.Setenv(key, old)
if nil == Connection() {
t.Errorf("connection should not be nil")
}
})
})
}
func TestDeactivateURL(t *testing.T) {
t.Run("deactivate", func(t *testing.T) {
urlModel := model.URL{ShortCode: "testCode", OriginURL: "http://local-host/url"}
DeactivateURL(urlModel)
if !hasURL(urlModel) {
t.Errorf("deactivated url should be in cache")
}
})
}
func TestLookupURL(t *testing.T) {
t.Run("save + lookup", func(t *testing.T) {
urlModel := model.URL{ShortCode: util.RandomString(8), OriginURL: "http://local-host/abc/def/ghi"}
t.Run("save", func(t *testing.T) {
SavePopularURL(urlModel, true)
if !hasURL(urlModel) {
t.Errorf("saved url should be in cache")
}
t.Run("lookup", func(t *testing.T) {
cachedModel, status := LookupURL(urlModel.ShortCode)
if status != http.StatusGone {
t.Errorf("inactive url should be 410 gone")
}
if cachedModel.OriginURL != urlModel.OriginURL {
t.Errorf("cached origin_url should be same as input")
}
if cachedModel.ShortCode != urlModel.ShortCode {
t.Errorf("cached short_code should be same as input")
}
})
})
})
}