123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- package redis
- import (
- "testing"
- "git.i2edu.net/i2/go-zero/core/stringx"
- "github.com/stretchr/testify/assert"
- )
- func TestRedisConf(t *testing.T) {
- tests := []struct {
- name string
- RedisConf
- ok bool
- }{
- {
- name: "missing host",
- RedisConf: RedisConf{
- Host: "",
- Type: NodeType,
- Pass: "",
- },
- ok: false,
- },
- {
- name: "missing type",
- RedisConf: RedisConf{
- Host: "localhost:6379",
- Type: "",
- Pass: "",
- },
- ok: false,
- },
- {
- name: "ok",
- RedisConf: RedisConf{
- Host: "localhost:6379",
- Type: NodeType,
- Pass: "",
- },
- ok: true,
- },
- }
- for _, test := range tests {
- t.Run(stringx.RandId(), func(t *testing.T) {
- if test.ok {
- assert.Nil(t, test.RedisConf.Validate())
- assert.NotNil(t, test.RedisConf.NewRedis())
- } else {
- assert.NotNil(t, test.RedisConf.Validate())
- }
- })
- }
- }
- func TestRedisKeyConf(t *testing.T) {
- tests := []struct {
- name string
- RedisKeyConf
- ok bool
- }{
- {
- name: "missing host",
- RedisKeyConf: RedisKeyConf{
- RedisConf: RedisConf{
- Host: "",
- Type: NodeType,
- Pass: "",
- },
- Key: "foo",
- },
- ok: false,
- },
- {
- name: "missing key",
- RedisKeyConf: RedisKeyConf{
- RedisConf: RedisConf{
- Host: "localhost:6379",
- Type: NodeType,
- Pass: "",
- },
- Key: "",
- },
- ok: false,
- },
- {
- name: "ok",
- RedisKeyConf: RedisKeyConf{
- RedisConf: RedisConf{
- Host: "localhost:6379",
- Type: NodeType,
- Pass: "",
- },
- Key: "foo",
- },
- ok: true,
- },
- }
- for _, test := range tests {
- t.Run(test.name, func(t *testing.T) {
- if test.ok {
- assert.Nil(t, test.RedisKeyConf.Validate())
- } else {
- assert.NotNil(t, test.RedisKeyConf.Validate())
- }
- })
- }
- }
|