Skip to content

Commit 38bdba9

Browse files
committed
Improve test code
1 parent 991903d commit 38bdba9

File tree

8 files changed

+177
-211
lines changed

8 files changed

+177
-211
lines changed

.travis.yml

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
language: go
2-
32
sudo: false
4-
53
go:
6-
- 1.6.x
7-
- 1.7.x
8-
- 1.8.x
9-
- 1.9.x
10-
- 1.10.x
11-
- 1.11.x
12-
- master
4+
- tip
135

146
services:
157
- redis-server
@@ -18,4 +10,4 @@ before_install:
1810
- go get github.com/mattn/goveralls
1911

2012
script:
21-
- $HOME/gopath/bin/goveralls -service=travis-ci
13+
- $GOPATH/bin/goveralls -service=travis-ci

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
<a href="https://goreportcard.com/report/github.com/thinkoner/thinkgo">
1111
<img src="https://goreportcard.com/badge/github.com/thinkoner/thinkgo" alt="Go Report Card">
1212
</a>
13+
<a href="https://codeclimate.com/github/thinkoner/thinkgo/maintainability">
14+
<img src="https://api.codeclimate.com/v1/badges/c315fda3b07b5aef3529/maintainability" />
15+
</a>
1316
<a href="https://godoc.org/github.com/thinkoner/thinkgo">
1417
<img src="https://godoc.org/github.com/thinkoner/thinkgo?status.svg" alt="GoDoc">
1518
</a>
@@ -82,6 +85,7 @@ func main() {
8285
- [View](#view)
8386
- [HTTP Session](#http-session)
8487
- [Logging](#logging)
88+
- [Cache](#cache)
8589
- [ORM](#orm)
8690

8791
## Routing
@@ -399,6 +403,46 @@ log.Alert("log with Alert")
399403
log.Emerg("log with Emerg")
400404
```
401405

406+
## Cache
407+
408+
ThinkGo Cache Currently supports redis, memory, and can customize the store adapter.
409+
410+
#### Basic Usage
411+
412+
```go
413+
import (
414+
"github.com/thinkoner/thinkgo/cache"
415+
"time"
416+
)
417+
418+
419+
var foo string
420+
421+
// Create a cache with memory store
422+
c, _ := cache.Cache(cache.NewMemoryStore("thinkgo"))
423+
424+
// Set the value
425+
c.Put("foo", "thinkgo", 10 * time.Minute)
426+
427+
// Get the string associated with the key "foo" from the cache
428+
c.Get("foo", &foo)
429+
430+
```
431+
432+
#### Retrieve & Store
433+
434+
Sometimes you may wish to retrieve an item from the cache, but also store a default value if the requested item doesn't exist. For example, you may wish to retrieve all users from the cache or, if they don't exist, retrieve them from the callback and add them to the cache. You may do this using the `Remember` method:
435+
436+
```go
437+
var foo int
438+
439+
cache.Remember("foo", &a, 1*time.Minute, func() interface{} {
440+
return "thinkgo"
441+
})
442+
```
443+
444+
refer to [ThinkGo Cache]( https://github.com/thinkoner/thinkgo/tree/master/cache )
445+
402446
## ORM
403447

404448
refer to [ThinkORM]( https://github.com/thinkoner/thinkorm )

cache/cache_test.go

Lines changed: 54 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,86 +6,75 @@ import (
66
"time"
77

88
"github.com/gomodule/redigo/redis"
9+
"github.com/stretchr/testify/assert"
910
)
1011

12+
type Foo struct {
13+
Name string `json:"name"`
14+
Age int `json:"age"`
15+
}
16+
1117
func testCache(t *testing.T, cache *Repository) {
1218
var a int
1319
var b string
14-
err := cache.Get("a", &a)
15-
if err == nil {
16-
t.Error("Getting A found value that shouldn't exist:", a)
17-
}
20+
var c Foo
1821

19-
err = cache.Get("b", &b)
20-
if err == nil {
21-
t.Error("Getting B found value that shouldn't exist:", b)
22-
}
22+
cache.Clear()
2323

24-
cache.Put("a", 1, 10*time.Minute)
25-
cache.Put("b", "thinkgo", 10*time.Minute)
24+
assert.Error(t, cache.Get("a", &a))
25+
assert.Error(t, cache.Get("b", &b))
2626

27-
err = cache.Get("a", &a)
28-
if err != nil {
29-
t.Error(err)
30-
}
27+
assert.NoError(t, cache.Put("a", 1, 10*time.Minute))
28+
assert.NoError(t, cache.Put("b", "thinkgo", 10*time.Minute))
3129

32-
if a != 1 {
33-
t.Error("Expect: ", 1)
34-
}
30+
assert.True(t, cache.Has("a"))
31+
assert.True(t, cache.Has("b"))
3532

36-
err = cache.Get("b", &b)
37-
if err != nil {
38-
t.Error(err)
39-
}
33+
assert.NoError(t, cache.Get("a", &a))
34+
assert.Equal(t, a, 1)
35+
assert.NoError(t, cache.Get("b", &b))
36+
assert.Equal(t, b, "thinkgo")
4037

41-
if b != "thinkgo" {
42-
t.Error("Expect: ", "thinkgo")
43-
}
38+
assert.NoError(t, cache.Pull("b", &b))
39+
assert.Equal(t, b, "thinkgo")
40+
assert.False(t, cache.Has("b"))
4441

45-
testCacheRemember(t, cache)
46-
}
42+
assert.NoError(t, cache.Set("b", "think go", 10*time.Minute))
43+
assert.Error(t, cache.Add("b", "think go", 10*time.Minute))
4744

48-
func testCacheRemember(t *testing.T, cache *Repository) {
49-
cache.Clear()
45+
assert.True(t, cache.Has("b"))
46+
assert.NoError(t, cache.Forget("b"))
47+
assert.False(t, cache.Has("b"))
5048

51-
var a int
49+
assert.NoError(t, cache.Put("c", Foo{
50+
Name: "thinkgo",
51+
Age:100,
52+
}, 10*time.Minute))
53+
assert.NoError(t,cache.Get("c", &c))
54+
fmt.Println(c)
55+
assert.Equal(t, c.Name , "thinkgo")
56+
assert.Equal(t, c.Age , 100)
57+
assert.NoError(t, cache.Delete("c"))
58+
assert.False(t, cache.Has("c"))
5259

53-
err := cache.Remember("a", &a, 1*time.Minute, func() interface{} {
54-
return 1
55-
})
60+
_, ok := cache.GetStore().(Store)
61+
assert.True(t, ok)
5662

57-
if err != nil {
58-
t.Error(err)
59-
}
60-
61-
if a != 1 {
62-
t.Error(fmt.Sprintf("Expect: %d, Actual: %d ", 1, a))
63-
}
64-
65-
err = cache.Remember("a", &a, 1*time.Minute, func() interface{} {
66-
return 2
67-
})
68-
69-
if err != nil {
70-
t.Error(err)
71-
}
63+
assert.NoError(t, cache.Clear())
64+
assert.False(t, cache.Has("a"))
65+
assert.False(t, cache.Has("b"))
7266

73-
if a != 1 {
74-
t.Error(fmt.Sprintf("Expect: %d, Actual: %d ", 1, a))
75-
}
67+
assert.NoError(t, cache.Remember("a", &a, 1*time.Minute, func() interface{} {
68+
return 1000
69+
}))
7670

77-
cache.Clear()
78-
err = cache.Remember("a", &a, 1*time.Minute, func() interface{} {
79-
return 3
80-
})
71+
assert.Equal(t, a, 1000)
8172

82-
if err != nil {
83-
t.Error(err)
84-
}
73+
assert.NoError(t,cache.Remember("b", &b, 1*time.Minute, func() interface{} {
74+
return "hello thinkgo"
75+
}))
8576

86-
if a != 3 {
87-
t.Error(fmt.Sprintf("Expect: %d, Actual: %d ", 3, a))
88-
}
77+
assert.Equal(t, b, "hello thinkgo")
8978
}
9079

9180
func TestMemoryCache(t *testing.T) {
@@ -111,6 +100,10 @@ func TestRedisCache(t *testing.T) {
111100
if err != nil {
112101
return nil, err
113102
}
103+
// if _, err := c.Do("AUTH", "123456"); err != nil {
104+
// c.Close()
105+
// return nil, err
106+
// }
114107
return c, nil
115108
},
116109
}
@@ -120,4 +113,4 @@ func TestRedisCache(t *testing.T) {
120113
t.Error(err)
121114
}
122115
testCache(t, cache)
123-
}
116+
}

helper/utils.go

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package helper
22

33
import (
4-
"encoding/json"
5-
"fmt"
6-
"io/ioutil"
74
"os"
85
"strings"
96
)
@@ -32,61 +29,3 @@ func ParseAddr(addrs ...string) string {
3229
}
3330
return addr + ":" + port
3431
}
35-
36-
func ListDir(dirPth string, suffix string) (files []string, err error) {
37-
files = make([]string, 0, 10)
38-
dir, err := ioutil.ReadDir(dirPth)
39-
if err != nil {
40-
return nil, err
41-
}
42-
suffix = strings.ToUpper(suffix)
43-
for _, fi := range dir {
44-
if fi.IsDir() {
45-
continue
46-
}
47-
if strings.HasSuffix(strings.ToUpper(fi.Name()), suffix) {
48-
files = append(files, strings.TrimRight(dirPth, "/")+"/"+fi.Name())
49-
}
50-
}
51-
return files, nil
52-
}
53-
54-
func MapGet(m map[string]interface{}, key string, parms ...interface{}) interface{} {
55-
if value, ok := m[key]; ok {
56-
return value
57-
}
58-
// database.mysql.host
59-
s := strings.Split(key, ".")
60-
i := 0
61-
for _, segment := range s {
62-
i++
63-
if _, ok := m[segment]; !ok {
64-
break
65-
}
66-
67-
b, err := json.Marshal(m[segment])
68-
if err != nil {
69-
fmt.Println(err)
70-
}
71-
72-
if i == len(s) {
73-
return m[segment]
74-
}
75-
76-
vv := make(map[string]interface{})
77-
err = json.Unmarshal(b, &vv)
78-
if err != nil {
79-
break
80-
}
81-
m = vv
82-
}
83-
if len(parms) == 1 {
84-
return parms[0]
85-
}
86-
return nil
87-
}
88-
89-
func FileExists(f string) bool {
90-
_, err := os.Stat(f)
91-
return err == nil || os.IsExist(err)
92-
}

helper/value.go

Lines changed: 0 additions & 70 deletions
This file was deleted.

0 commit comments

Comments
 (0)