chore: 恢复旧布局基线(迁移改动丢失后重建,apps/packages 布局暂存)
This commit is contained in:
1 parent
a9510235bb
commit
456fa6954b
203 files changed
+18787
-48
No files matched your search
@@ -0,0 +1,90 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/alicebob/miniredis/v2"
|
||||
)
|
||||
|
||||
func TestListGetDeleteRedisKeys(t *testing.T) {
|
||||
mr, err := miniredis.Run()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer mr.Close()
|
||||
|
||||
_ = mr.Set("str:a", "hello")
|
||||
mr.HSet("hash:b", "f1", "v1")
|
||||
mr.Push("list:c", "x", "y")
|
||||
mr.SAdd("set:d", "m1", "m2")
|
||||
_, _ = mr.ZAdd("zset:e", 1.5, "zm")
|
||||
|
||||
host, portStr, err := net.SplitHostPort(mr.Addr())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
port, _ := strconv.Atoi(portStr)
|
||||
base := ConnReq{Driver: "redis", Host: host, Port: port, Database: "0"}
|
||||
|
||||
list := ListRedisKeys(context.Background(), base)
|
||||
if list.Error != "" {
|
||||
t.Fatal(list.Error)
|
||||
}
|
||||
if len(list.Keys) < 5 {
|
||||
t.Fatalf("want >=5 keys, got %d %#v", len(list.Keys), list.Keys)
|
||||
}
|
||||
byName := map[string]RedisKeyInfo{}
|
||||
for _, k := range list.Keys {
|
||||
byName[k.Name] = k
|
||||
}
|
||||
if byName["str:a"].Type != "string" {
|
||||
t.Fatalf("str type %s", byName["str:a"].Type)
|
||||
}
|
||||
if byName["hash:b"].Type != "hash" {
|
||||
t.Fatalf("hash type %s", byName["hash:b"].Type)
|
||||
}
|
||||
// Real Redis returns -1 (no expire); miniredis may report 0.
|
||||
if byName["str:a"].TTL < -1 {
|
||||
t.Fatalf("unexpected ttl %d", byName["str:a"].TTL)
|
||||
}
|
||||
|
||||
gv := GetRedisKey(context.Background(), ConnReq{Driver: "redis", Host: host, Port: port, Database: "0", Key: "str:a"})
|
||||
if gv.Error != "" || gv.RowCount != 1 || gv.Rows[0]["value"] != "hello" {
|
||||
t.Fatalf("get string %#v", gv)
|
||||
}
|
||||
gh := GetRedisKey(context.Background(), ConnReq{Driver: "redis", Host: host, Port: port, Database: "0", Key: "hash:b"})
|
||||
if gh.Error != "" || gh.RowCount < 1 || gh.Columns[0] != "field" {
|
||||
t.Fatalf("get hash %#v", gh)
|
||||
}
|
||||
if gh.Rows[0]["field"] != "f1" {
|
||||
t.Fatalf("hash fields should be sorted, got %#v", gh.Rows)
|
||||
}
|
||||
gl := GetRedisKey(context.Background(), ConnReq{Driver: "redis", Host: host, Port: port, Database: "0", Key: "list:c"})
|
||||
if gl.Error != "" || gl.RowCount < 2 {
|
||||
t.Fatalf("get list %#v", gl)
|
||||
}
|
||||
gs := GetRedisKey(context.Background(), ConnReq{Driver: "redis", Host: host, Port: port, Database: "0", Key: "set:d"})
|
||||
if gs.Error != "" || gs.RowCount < 2 {
|
||||
t.Fatalf("get set %#v", gs)
|
||||
}
|
||||
gz := GetRedisKey(context.Background(), ConnReq{Driver: "redis", Host: host, Port: port, Database: "0", Key: "zset:e"})
|
||||
if gz.Error != "" || gz.RowCount != 1 {
|
||||
t.Fatalf("get zset %#v", gz)
|
||||
}
|
||||
|
||||
pat := ListRedisKeys(context.Background(), ConnReq{Driver: "redis", Host: host, Port: port, Database: "0", Pattern: "str:*"})
|
||||
if pat.Error != "" || len(pat.Keys) != 1 || pat.Keys[0].Name != "str:a" {
|
||||
t.Fatalf("pattern %#v", pat)
|
||||
}
|
||||
|
||||
del := DeleteRedisKey(context.Background(), ConnReq{Driver: "redis", Host: host, Port: port, Database: "0", Key: "str:a"})
|
||||
if del.Error != "" {
|
||||
t.Fatal(del.Error)
|
||||
}
|
||||
if mr.Exists("str:a") {
|
||||
t.Fatal("key should be deleted")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user