gitee.com/woood2/luca@v1.0.4/internal/cache/redis_test.go (about)

     1  package cache
     2  
     3  import (
     4  	"gitee.com/woood2/luca/internal/util"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestRedisMiss(t *testing.T) {
    10  	util.SkipCI(t)
    11  	c := Redis()
    12  	var tests = []struct {
    13  		in string // input
    14  	}{
    15  		{"县"},
    16  		{"市"},
    17  		{"省"},
    18  	}
    19  	for _, tt := range tests {
    20  		var v interface{}
    21  		if e := c.Get(tt.in, &v); e != c.Miss() {
    22  			t.Errorf("expected e=cache.Miss; actual e=%v", e)
    23  		}
    24  	}
    25  }
    26  
    27  func TestRedisExpire(t *testing.T) {
    28  	util.SkipCI(t)
    29  	c := Redis()
    30  	var tests = []struct {
    31  		in string // input
    32  	}{
    33  		{"孺子牛"},
    34  		{"拓荒牛"},
    35  		{"老黄牛"},
    36  	}
    37  	for _, tt := range tests {
    38  		p := &person{
    39  			Name: tt.in,
    40  		}
    41  		c.Set(tt.in, p, 1*time.Second)
    42  	}
    43  	for _, tt := range tests {
    44  		var v interface{}
    45  		if e := c.Get(tt.in, &v); e != nil {
    46  			t.Errorf("expected e=nil; actual e=%v", e)
    47  		}
    48  	}
    49  	time.Sleep(2 * time.Second)
    50  	for _, tt := range tests {
    51  		var v interface{}
    52  		if e := c.Get(tt.in, &v); e != c.Miss() {
    53  			t.Errorf("expected e=cache.Miss; actual e=%v", e)
    54  		}
    55  	}
    56  }
    57  
    58  func TestRedisNormal(t *testing.T) {
    59  	util.SkipCI(t)
    60  	c := Redis()
    61  	var tests = []struct {
    62  		in       string // input
    63  		expected string // expected result
    64  	}{
    65  		{"wmd", "wmd"},
    66  		{"吴孟达", "吴孟达"},
    67  		{"周星驰", "周星驰"},
    68  	}
    69  	for _, tt := range tests {
    70  		p1 := &person{
    71  			Name: tt.in,
    72  		}
    73  		c.Set(tt.in, p1, 1*time.Second)
    74  		var p *person
    75  		if e := c.Get(tt.in, &p); e != nil {
    76  			t.Errorf("expected e=nil; actual e=%v", e)
    77  		} else {
    78  			if p.Name != tt.expected {
    79  				t.Errorf("set %s; get %s; expected %s", tt.in, p.Name, tt.expected)
    80  			}
    81  		}
    82  	}
    83  }
    84  
    85  func TestRedisTypes(t *testing.T) {
    86  	util.SkipCI(t)
    87  	c := Redis()
    88  
    89  	sl := []string{"wa", "ha", "ha"}
    90  	m := make(map[string]int)
    91  	m["name"] = 120
    92  	m["age"] = 23
    93  	var p1 *person //nil
    94  	p2 := &person{
    95  		Name: "2d",
    96  		Age:  30,
    97  	}
    98  
    99  	var tests = []struct {
   100  		k string
   101  		v interface{}
   102  	}{
   103  		{"rabbit", "rabbit"},
   104  		{"", ""},
   105  
   106  		{"911", 911},
   107  
   108  		{"true", true},
   109  		{"false", false},
   110  
   111  		{"slice", sl},
   112  		{"map", m},
   113  		{"p1", p1},
   114  		{"p2", p2},
   115  		{"nil", nil},
   116  	}
   117  	for _, tt := range tests {
   118  		c.Set(tt.k, tt.v, 1*time.Second)
   119  		switch tt.k {
   120  		case "rabbit":
   121  			s := ""
   122  			if e := c.Get(tt.k, &s); e != nil {
   123  				t.Errorf("expected e=nil; actual e=%v", e)
   124  			}
   125  			if s != tt.v {
   126  				t.Errorf("expected %v; actual %v", tt.v, s)
   127  			}
   128  		case "":
   129  			s := "hi"
   130  			if e := c.Get(tt.k, &s); e != nil {
   131  				t.Errorf("expected e=nil; actual e=%v", e)
   132  			}
   133  			if s != tt.v {
   134  				t.Errorf("expected %v; actual %v", tt.v, s)
   135  			}
   136  		case "911":
   137  			i := 0
   138  			if e := c.Get(tt.k, &i); e != nil {
   139  				t.Errorf("expected e=nil; actual e=%v", e)
   140  			}
   141  			if i != tt.v {
   142  				t.Errorf("expected %v; actual %v", tt.v, i)
   143  			}
   144  		case "true":
   145  			b := false
   146  			if e := c.Get(tt.k, &b); e != nil {
   147  				t.Errorf("expected e=nil; actual e=%v", e)
   148  			}
   149  			if b != tt.v {
   150  				t.Errorf("expected %v; actual %v", tt.v, b)
   151  			}
   152  		case "false":
   153  			b := true
   154  			if e := c.Get(tt.k, &b); e != nil {
   155  				t.Errorf("expected e=nil; actual e=%v", e)
   156  			}
   157  			if b != tt.v {
   158  				t.Errorf("expected %v; actual %v", tt.v, b)
   159  			}
   160  		case "slice":
   161  			var sl2 []string
   162  			if e := c.Get(tt.k, &sl2); e != nil {
   163  				t.Errorf("expected e=nil; actual e=%v", e)
   164  			}
   165  			if len(sl2) != len(sl) {
   166  				t.Errorf("expected %v; actual %v", sl, sl2)
   167  			}
   168  		case "map":
   169  			var m2 map[string]int
   170  			if e := c.Get(tt.k, &m2); e != nil {
   171  				t.Errorf("expected e=nil; actual e=%v", e)
   172  			}
   173  			if m2["name"] != m["name"] {
   174  				t.Errorf("expected %v; actual %v", m, m2)
   175  			}
   176  		case "p1":
   177  			var p12 *person
   178  			if e := c.Get(tt.k, &p12); e != nil {
   179  				t.Errorf("expected e=nil; actual e=%v", e)
   180  			}
   181  			if p12 != nil {
   182  				t.Errorf("expected nil; actual %v", p12)
   183  			}
   184  		case "p2":
   185  			var p22 *person
   186  			if e := c.Get(tt.k, &p22); e != nil {
   187  				t.Errorf("expected e=nil; actual e=%v", e)
   188  			}
   189  			if p22.Name != p2.Name {
   190  				t.Errorf("expected %v; actual %v", p2, p22)
   191  			}
   192  		case "nil":
   193  			var kong interface{}
   194  			if e := c.Get(tt.k, &kong); e != nil {
   195  				t.Errorf("expected e=nil; actual e=%v", e)
   196  			}
   197  			if kong != nil {
   198  				t.Errorf("expected nil; actual %v", kong)
   199  			}
   200  		}
   201  	}
   202  }