Skip to content

Commit

Permalink
discard redis tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyofx committed Nov 10, 2021
1 parent 0ab5173 commit e1c3528
Show file tree
Hide file tree
Showing 3 changed files with 597 additions and 703 deletions.
245 changes: 123 additions & 122 deletions tests/goredis_test.go
Original file line number Diff line number Diff line change
@@ -1,124 +1,125 @@
package tests

import (
"context"
"fmt"
"github.com/go-redis/redis/v8"
"github.com/stretchr/testify/assert"
_ "github.com/stretchr/testify/assert"
cache "github.com/yoyofx/yoyogo/pkg/cache/redis"

"testing"
"time"
)

func newClient() *redis.Client {
client := redis.NewClient(&redis.Options{
Addr: "62.234.6.120:31379",
Password: "",
DB: 0,
})
return client
}

var ctx = context.Background()

func TestRedisConn(t *testing.T) {
client := newClient()
//defer client.Close()

//ping
pong, err := client.Ping(ctx).Result()
if err != nil {
fmt.Println("ping error", err.Error())
return
}
assert.Equal(t, pong, "PONG")
}

type Doctor struct {
Name string
Age int
}

func TestRedisStringValue(t *testing.T) {
client := newClient()
defer client.Close()

// string
client.Set(ctx, "yoyogo:version", "v1.6.1", 15*time.Minute)
version, _ := client.Get(ctx, "yoyogo:version").Result()
assert.Equal(t, version, "v1.6.1")

// json 序列化
doctor := Doctor{Name: "钟南山", Age: 83}

serializer := cache.JsonSerializer{}

doctorJson, _ := serializer.Serialization(doctor)
client.Set(ctx, "doctor2", doctorJson, time.Hour)
var doctor2 Doctor
doctorResult, _ := client.Get(ctx, "doctor2").Bytes()
_ = serializer.Deserialization(doctorResult, &doctor2)
assert.Equal(t, doctor2.Name, "钟南山")
assert.Equal(t, doctor2.Age, 83)

//client.SetNX()
}

func TestRedisList(t *testing.T) {
client := newClient()
defer client.Close()
listKey := "go2list"
_, _ = client.Del(ctx, listKey).Result()

client.RPush(ctx, listKey, 1, 2, 3)

first, _ := client.LPop(ctx, listKey).Int()
assert.Equal(t, first, 1)

i1, _ := client.LIndex(ctx, listKey, 1).Int64()
assert.Equal(t, i1, int64(3))

values, _ := client.LRange(ctx, listKey, 0, 1).Result()
assert.Equal(t, len(values), 2)
}

func TestAddTodoList(t *testing.T) {
client := newClient()
defer client.Close()

json := `[{
"id": 0,
"status": "STATUS_TODO",
"content": "每周七天阅读五次,每次阅读完要做100字的读书笔记",
"editable": false
}, {
"id": 1,
"status": "STATUS_TODO",
"content": "每周七天健身4次,每次健身时间需要大于20分钟",
"editable": false
}, {
"id": 2,
"status": "STATUS_TODO",
"content": "单词*100",
"editable": false
}, {
"id": 3,
"status": "STATUS_TODO",
"content": "单词*150",
"editable": false
}, {
"id": 4,
"status": "STATUS_TODO",
"content": "单词*200",
"editable": false
}, {
"id": 5,
"status": "STATUS_TODO",
"content": "单词*250",
"editable": false
}]`
//client.Set(ctx, "yoyogo:todolist", json, 0)
_ = json
}
//
//import (
// "context"
// "fmt"
// "github.com/go-redis/redis/v8"
// "github.com/stretchr/testify/assert"
// _ "github.com/stretchr/testify/assert"
// cache "github.com/yoyofx/yoyogo/pkg/cache/redis"
//
// "testing"
// "time"
//)
//
//func newClient() *redis.Client {
// client := redis.NewClient(&redis.Options{
// Addr: "m.yoyogo.run:6379",
// Password: "123.com",
// DB: 0,
// })
// return client
//}
//
//var ctx = context.Background()
//
//func TestRedisConn(t *testing.T) {
// client := newClient()
// //defer client.Close()
//
// //ping
// pong, err := client.Ping(ctx).Result()
// if err != nil {
// fmt.Println("ping error", err.Error())
// return
// }
// assert.Equal(t, pong, "PONG")
//}
//
//type Doctor struct {
// Name string
// Age int
//}
//
//func TestRedisStringValue(t *testing.T) {
// client := newClient()
// defer client.Close()
//
// // string
// client.Set(ctx, "yoyogo:version", "v1.6.1", 15*time.Minute)
// version, _ := client.Get(ctx, "yoyogo:version").Result()
// assert.Equal(t, version, "v1.6.1")
//
// // json 序列化
// doctor := Doctor{Name: "钟南山", Age: 83}
//
// serializer := cache.JsonSerializer{}
//
// doctorJson, _ := serializer.Serialization(doctor)
// client.Set(ctx, "doctor2", doctorJson, time.Hour)
// var doctor2 Doctor
// doctorResult, _ := client.Get(ctx, "doctor2").Bytes()
// _ = serializer.Deserialization(doctorResult, &doctor2)
// assert.Equal(t, doctor2.Name, "钟南山")
// assert.Equal(t, doctor2.Age, 83)
//
// //client.SetNX()
//}
//
//func TestRedisList(t *testing.T) {
// client := newClient()
// defer client.Close()
// listKey := "go2list"
// _, _ = client.Del(ctx, listKey).Result()
//
// client.RPush(ctx, listKey, 1, 2, 3)
//
// first, _ := client.LPop(ctx, listKey).Int()
// assert.Equal(t, first, 1)
//
// i1, _ := client.LIndex(ctx, listKey, 1).Int64()
// assert.Equal(t, i1, int64(3))
//
// values, _ := client.LRange(ctx, listKey, 0, 1).Result()
// assert.Equal(t, len(values), 2)
//}
//
//func TestAddTodoList(t *testing.T) {
// client := newClient()
// defer client.Close()
//
// json := `[{
// "id": 0,
// "status": "STATUS_TODO",
// "content": "每周七天阅读五次,每次阅读完要做100字的读书笔记",
// "editable": false
//}, {
// "id": 1,
// "status": "STATUS_TODO",
// "content": "每周七天健身4次,每次健身时间需要大于20分钟",
// "editable": false
//}, {
// "id": 2,
// "status": "STATUS_TODO",
// "content": "单词*100",
// "editable": false
//}, {
// "id": 3,
// "status": "STATUS_TODO",
// "content": "单词*150",
// "editable": false
//}, {
// "id": 4,
// "status": "STATUS_TODO",
// "content": "单词*200",
// "editable": false
//}, {
// "id": 5,
// "status": "STATUS_TODO",
// "content": "单词*250",
// "editable": false
//}]`
// //client.Set(ctx, "yoyogo:todolist", json, 0)
// _ = json
//}
40 changes: 20 additions & 20 deletions tests/httpclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,26 +98,26 @@ func TestHttpCleintFactoryCreateServiceDiscoveryCleint(t *testing.T) {
assert.Equal(t, string(res.Body), "ok")
}

func TestHttpClientFactoryBaseUrl(t *testing.T) {
httpServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
_, _ = w.Write([]byte("ok"))
}))
fmt.Print(httpServer.URL)
//httpServer.URL = "http://127.0.0.1:8080"
defer httpServer.Close()
factory := httpclient.NewFactory()
client, err := factory.Create(httpServer.URL)
if err != nil {
panic(err)
}
req := httpclient.WithRequest()
req.GET("")
req.SetTimeout(10)
res, err := client.Do(req)
fmt.Print(string(res.Body))
assert.Equal(t, string(res.Body), "ok")
}
//func TestHttpClientFactoryBaseUrl(t *testing.T) {
// httpServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// w.WriteHeader(200)
// _, _ = w.Write([]byte("ok"))
// }))
// fmt.Print(httpServer.URL)
// //httpServer.URL = "http://127.0.0.1:8080"
// defer httpServer.Close()
// factory := httpclient.NewFactory()
// client, err := factory.Create(httpServer.URL)
// if err != nil {
// panic(err)
// }
// req := httpclient.WithRequest()
// req.GET("")
// req.SetTimeout(10)
// res, err := client.Do(req)
// fmt.Print(string(res.Body))
// assert.Equal(t, string(res.Body), "ok")
//}

func TestBaseUrlSplicingUrl(t *testing.T) {
factory := httpclient.NewFactory()
Expand Down
Loading

0 comments on commit e1c3528

Please sign in to comment.