Skip to content

Commit

Permalink
feat: 1.remove assert package, 2.rewrite unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
fanjindong committed Jun 29, 2022
1 parent d4a91dd commit 26557c5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 29 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ go 1.12
require (
github.com/go-redis/redis/v8 v8.11.4
github.com/kr/pretty v0.1.0 // indirect
github.com/stretchr/testify v1.5.1
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
)
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
Expand All @@ -29,7 +26,6 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand All @@ -47,10 +43,8 @@ github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7J
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/onsi/gomega v1.16.0 h1:6gjqkI8iiRHMvdccRJM8rVKjCWk6ZIm6FTm3ddIe4/c=
github.com/onsi/gomega v1.16.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down Expand Up @@ -104,12 +98,10 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
Expand Down
58 changes: 38 additions & 20 deletions rede_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package go_rede

import (
"context"
"github.com/stretchr/testify/assert"
"os"
"reflect"
"testing"
"time"
)
Expand All @@ -16,9 +16,7 @@ var (
func TestMain(m *testing.M) {
rede = NewClient(&Options{
Namespaces: "rede",
Addr: "ml5pub.tsht3.mc.ops:6379",
Password: "",
DB: 0,
Addr: os.Getenv("REDIS_ADDR"),
})
exitCode := m.Run()
os.Exit(exitCode)
Expand All @@ -31,8 +29,9 @@ func TestClient_Push(t *testing.T) {
ttl time.Duration
}
tests := []struct {
input input
want float64
input input
want float64
wantErr bool
}{
{input: input{member: "a", ttl: 1 * time.Second}, want: now + 1},
{input: input{member: "b", ttl: 2 * time.Second}, want: now + 2},
Expand All @@ -43,10 +42,18 @@ func TestClient_Push(t *testing.T) {

for _, ts := range tests {
_, err := rede.Push(ctx, ts.input.member, ts.input.ttl)
assert.NoError(t, err)
if (err != nil) != ts.wantErr {
t.Errorf("Push() error = %v, wantErr %v", err, ts.wantErr)
return
}
got, err := rede.ZScore(ctx, rede.Namespaces, ts.input.member).Result()
assert.NoError(t, err)
assert.Equal(t, int64(ts.want*1e6), int64(got*1e6))
if (err != nil) != ts.wantErr {
t.Errorf("ZScore() error = %v, wantErr %v", err, ts.wantErr)
return
}
if !reflect.DeepEqual(int64(ts.want*1e6), int64(got*1e6)) {
t.Errorf("ZScore() got = %v, want %v", int64(got*1e6), int64(ts.want*1e6))
}
}
}

Expand All @@ -56,8 +63,9 @@ func TestClient_Look(t *testing.T) {
ttl time.Duration
}
tests := []struct {
input input
want float64
input input
want float64
wantErr bool
}{
{input: input{member: "a", ttl: 1 * time.Second}, want: 1},
{input: input{member: "b", ttl: 2 * time.Second}, want: 2},
Expand All @@ -68,8 +76,13 @@ func TestClient_Look(t *testing.T) {
for _, ts := range tests {
_, _ = rede.Push(ctx, ts.input.member, ts.input.ttl)
got, err := rede.Look(ctx, ts.input.member)
assert.NoError(t, err)
assert.Equal(t, got, ts.want)
if (err != nil) != ts.wantErr {
t.Errorf("Look() error = %v, wantErr %v", err, ts.wantErr)
return
}
if !reflect.DeepEqual(got, ts.want) {
t.Errorf("ZScore() got = %v, want %v", got, ts.want)
}
}
}

Expand All @@ -84,9 +97,10 @@ func TestClient_Poll(t *testing.T) {
ttl time.Duration
}
tests := struct {
input []input
sleep time.Duration
want []string
input []input
sleep time.Duration
want []string
wantErr bool
}{
input: []input{
{member: "a", ttl: 1 * time.Second},
Expand All @@ -99,8 +113,7 @@ func TestClient_Poll(t *testing.T) {
}
rede.Del(ctx, rede.Namespaces)
for _, ts := range tests.input {
_, err := rede.Push(ctx, ts.member, ts.ttl)
assert.NoError(t, err)
_, _ = rede.Push(ctx, ts.member, ts.ttl)
}

time.Sleep(tests.sleep)
Expand All @@ -110,8 +123,13 @@ func TestClient_Poll(t *testing.T) {
for cur.Next() {
got, err := cur.Get()
t.Log(got, err)
assert.NoError(t, err)
assert.Equal(t, tests.want[i], got)
if (err != nil) != tests.wantErr {
t.Errorf("Poll() error = %v, wantErr %v", err, tests.wantErr)
return
}
if !reflect.DeepEqual(tests.want[i], got) {
t.Errorf("Next() got = %v, want %v", got, tests.want[i])
}
i++
}
}

0 comments on commit 26557c5

Please sign in to comment.