Skip to content

Commit

Permalink
Improve test code
Browse files Browse the repository at this point in the history
  • Loading branch information
leeqvip committed Feb 19, 2019
1 parent 991903d commit 38bdba9
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 211 deletions.
12 changes: 2 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
language: go

sudo: false

go:
- 1.6.x
- 1.7.x
- 1.8.x
- 1.9.x
- 1.10.x
- 1.11.x
- master
- tip

services:
- redis-server
Expand All @@ -18,4 +10,4 @@ before_install:
- go get github.com/mattn/goveralls

script:
- $HOME/gopath/bin/goveralls -service=travis-ci
- $GOPATH/bin/goveralls -service=travis-ci
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
<a href="https://goreportcard.com/report/github.com/thinkoner/thinkgo">
<img src="https://goreportcard.com/badge/github.com/thinkoner/thinkgo" alt="Go Report Card">
</a>
<a href="https://codeclimate.com/github/thinkoner/thinkgo/maintainability">
<img src="https://api.codeclimate.com/v1/badges/c315fda3b07b5aef3529/maintainability" />
</a>
<a href="https://godoc.org/github.com/thinkoner/thinkgo">
<img src="https://godoc.org/github.com/thinkoner/thinkgo?status.svg" alt="GoDoc">
</a>
Expand Down Expand Up @@ -82,6 +85,7 @@ func main() {
- [View](#view)
- [HTTP Session](#http-session)
- [Logging](#logging)
- [Cache](#cache)
- [ORM](#orm)

## Routing
Expand Down Expand Up @@ -399,6 +403,46 @@ log.Alert("log with Alert")
log.Emerg("log with Emerg")
```

## Cache

ThinkGo Cache Currently supports redis, memory, and can customize the store adapter.

#### Basic Usage

```go
import (
"github.com/thinkoner/thinkgo/cache"
"time"
)


var foo string

// Create a cache with memory store
c, _ := cache.Cache(cache.NewMemoryStore("thinkgo"))

// Set the value
c.Put("foo", "thinkgo", 10 * time.Minute)

// Get the string associated with the key "foo" from the cache
c.Get("foo", &foo)

```

#### Retrieve & Store

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:

```go
var foo int

cache.Remember("foo", &a, 1*time.Minute, func() interface{} {
return "thinkgo"
})
```

refer to [ThinkGo Cache]( https://github.com/thinkoner/thinkgo/tree/master/cache )

## ORM

refer to [ThinkORM]( https://github.com/thinkoner/thinkorm )
Expand Down
115 changes: 54 additions & 61 deletions cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,86 +6,75 @@ import (
"time"

"github.com/gomodule/redigo/redis"
"github.com/stretchr/testify/assert"
)

type Foo struct {
Name string `json:"name"`
Age int `json:"age"`
}

func testCache(t *testing.T, cache *Repository) {
var a int
var b string
err := cache.Get("a", &a)
if err == nil {
t.Error("Getting A found value that shouldn't exist:", a)
}
var c Foo

err = cache.Get("b", &b)
if err == nil {
t.Error("Getting B found value that shouldn't exist:", b)
}
cache.Clear()

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

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

if a != 1 {
t.Error("Expect: ", 1)
}
assert.True(t, cache.Has("a"))
assert.True(t, cache.Has("b"))

err = cache.Get("b", &b)
if err != nil {
t.Error(err)
}
assert.NoError(t, cache.Get("a", &a))
assert.Equal(t, a, 1)
assert.NoError(t, cache.Get("b", &b))
assert.Equal(t, b, "thinkgo")

if b != "thinkgo" {
t.Error("Expect: ", "thinkgo")
}
assert.NoError(t, cache.Pull("b", &b))
assert.Equal(t, b, "thinkgo")
assert.False(t, cache.Has("b"))

testCacheRemember(t, cache)
}
assert.NoError(t, cache.Set("b", "think go", 10*time.Minute))
assert.Error(t, cache.Add("b", "think go", 10*time.Minute))

func testCacheRemember(t *testing.T, cache *Repository) {
cache.Clear()
assert.True(t, cache.Has("b"))
assert.NoError(t, cache.Forget("b"))
assert.False(t, cache.Has("b"))

var a int
assert.NoError(t, cache.Put("c", Foo{
Name: "thinkgo",
Age:100,
}, 10*time.Minute))
assert.NoError(t,cache.Get("c", &c))
fmt.Println(c)
assert.Equal(t, c.Name , "thinkgo")
assert.Equal(t, c.Age , 100)
assert.NoError(t, cache.Delete("c"))
assert.False(t, cache.Has("c"))

err := cache.Remember("a", &a, 1*time.Minute, func() interface{} {
return 1
})
_, ok := cache.GetStore().(Store)
assert.True(t, ok)

if err != nil {
t.Error(err)
}

if a != 1 {
t.Error(fmt.Sprintf("Expect: %d, Actual: %d ", 1, a))
}

err = cache.Remember("a", &a, 1*time.Minute, func() interface{} {
return 2
})

if err != nil {
t.Error(err)
}
assert.NoError(t, cache.Clear())
assert.False(t, cache.Has("a"))
assert.False(t, cache.Has("b"))

if a != 1 {
t.Error(fmt.Sprintf("Expect: %d, Actual: %d ", 1, a))
}
assert.NoError(t, cache.Remember("a", &a, 1*time.Minute, func() interface{} {
return 1000
}))

cache.Clear()
err = cache.Remember("a", &a, 1*time.Minute, func() interface{} {
return 3
})
assert.Equal(t, a, 1000)

if err != nil {
t.Error(err)
}
assert.NoError(t,cache.Remember("b", &b, 1*time.Minute, func() interface{} {
return "hello thinkgo"
}))

if a != 3 {
t.Error(fmt.Sprintf("Expect: %d, Actual: %d ", 3, a))
}
assert.Equal(t, b, "hello thinkgo")
}

func TestMemoryCache(t *testing.T) {
Expand All @@ -111,6 +100,10 @@ func TestRedisCache(t *testing.T) {
if err != nil {
return nil, err
}
// if _, err := c.Do("AUTH", "123456"); err != nil {
// c.Close()
// return nil, err
// }
return c, nil
},
}
Expand All @@ -120,4 +113,4 @@ func TestRedisCache(t *testing.T) {
t.Error(err)
}
testCache(t, cache)
}
}
61 changes: 0 additions & 61 deletions helper/utils.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package helper

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
)
Expand Down Expand Up @@ -32,61 +29,3 @@ func ParseAddr(addrs ...string) string {
}
return addr + ":" + port
}

func ListDir(dirPth string, suffix string) (files []string, err error) {
files = make([]string, 0, 10)
dir, err := ioutil.ReadDir(dirPth)
if err != nil {
return nil, err
}
suffix = strings.ToUpper(suffix)
for _, fi := range dir {
if fi.IsDir() {
continue
}
if strings.HasSuffix(strings.ToUpper(fi.Name()), suffix) {
files = append(files, strings.TrimRight(dirPth, "/")+"/"+fi.Name())
}
}
return files, nil
}

func MapGet(m map[string]interface{}, key string, parms ...interface{}) interface{} {
if value, ok := m[key]; ok {
return value
}
// database.mysql.host
s := strings.Split(key, ".")
i := 0
for _, segment := range s {
i++
if _, ok := m[segment]; !ok {
break
}

b, err := json.Marshal(m[segment])
if err != nil {
fmt.Println(err)
}

if i == len(s) {
return m[segment]
}

vv := make(map[string]interface{})
err = json.Unmarshal(b, &vv)
if err != nil {
break
}
m = vv
}
if len(parms) == 1 {
return parms[0]
}
return nil
}

func FileExists(f string) bool {
_, err := os.Stat(f)
return err == nil || os.IsExist(err)
}
70 changes: 0 additions & 70 deletions helper/value.go

This file was deleted.

Loading

0 comments on commit 38bdba9

Please sign in to comment.