reply_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright 2012 Gary Burd
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. package redis_test
  15. import (
  16. "fmt"
  17. "reflect"
  18. "testing"
  19. "github.com/garyburd/redigo/redis"
  20. )
  21. type valueError struct {
  22. v interface{}
  23. err error
  24. }
  25. func ve(v interface{}, err error) valueError {
  26. return valueError{v, err}
  27. }
  28. var replyTests = []struct {
  29. name interface{}
  30. actual valueError
  31. expected valueError
  32. }{
  33. {
  34. "ints([v1, v2])",
  35. ve(redis.Ints([]interface{}{[]byte("4"), []byte("5")}, nil)),
  36. ve([]int{4, 5}, nil),
  37. },
  38. {
  39. "ints([v1, nil, v2])",
  40. ve(redis.Ints([]interface{}{[]byte("4"), nil, []byte("5")}, nil)),
  41. ve([]int{4, 0, 5}, nil),
  42. },
  43. {
  44. "ints([v1, nil, v2])",
  45. ve(redis.Ints([]interface{}{[]byte("4"), nil, []byte("5")}, nil)),
  46. ve([]int{4, 0, 5}, nil),
  47. },
  48. {
  49. "ints(nil)",
  50. ve(redis.Ints(nil, nil)),
  51. ve([]int(nil), redis.ErrNil),
  52. },
  53. {
  54. "int64s([v1, v2])",
  55. ve(redis.Int64s([]interface{}{[]byte("4"), []byte("5")}, nil)),
  56. ve([]int64{4, 5}, nil),
  57. },
  58. {
  59. "strings([v1, v2])",
  60. ve(redis.Strings([]interface{}{[]byte("v1"), []byte("v2")}, nil)),
  61. ve([]string{"v1", "v2"}, nil),
  62. },
  63. {
  64. "byteslices([v1, v2])",
  65. ve(redis.ByteSlices([]interface{}{[]byte("v1"), []byte("v2")}, nil)),
  66. ve([][]byte{[]byte("v1"), []byte("v2")}, nil),
  67. },
  68. {
  69. "float64s([v1, v2])",
  70. ve(redis.Float64s([]interface{}{[]byte("1.234"), []byte("5.678")}, nil)),
  71. ve([]float64{1.234, 5.678}, nil),
  72. },
  73. {
  74. "values([v1, v2])",
  75. ve(redis.Values([]interface{}{[]byte("v1"), []byte("v2")}, nil)),
  76. ve([]interface{}{[]byte("v1"), []byte("v2")}, nil),
  77. },
  78. {
  79. "values(nil)",
  80. ve(redis.Values(nil, nil)),
  81. ve([]interface{}(nil), redis.ErrNil),
  82. },
  83. {
  84. "float64(1.0)",
  85. ve(redis.Float64([]byte("1.0"), nil)),
  86. ve(float64(1.0), nil),
  87. },
  88. {
  89. "float64(nil)",
  90. ve(redis.Float64(nil, nil)),
  91. ve(float64(0.0), redis.ErrNil),
  92. },
  93. {
  94. "uint64(1)",
  95. ve(redis.Uint64(int64(1), nil)),
  96. ve(uint64(1), nil),
  97. },
  98. {
  99. "uint64(-1)",
  100. ve(redis.Uint64(int64(-1), nil)),
  101. ve(uint64(0), redis.ErrNegativeInt),
  102. },
  103. {
  104. "positions([[1, 2], nil, [3, 4]])",
  105. ve(redis.Positions([]interface{}{[]interface{}{[]byte("1"), []byte("2")}, nil, []interface{}{[]byte("3"), []byte("4")}}, nil)),
  106. ve([]*[2]float64{{1.0, 2.0}, nil, {3.0, 4.0}}, nil),
  107. },
  108. }
  109. func TestReply(t *testing.T) {
  110. for _, rt := range replyTests {
  111. if rt.actual.err != rt.expected.err {
  112. t.Errorf("%s returned err %v, want %v", rt.name, rt.actual.err, rt.expected.err)
  113. continue
  114. }
  115. if !reflect.DeepEqual(rt.actual.v, rt.expected.v) {
  116. t.Errorf("%s=%+v, want %+v", rt.name, rt.actual.v, rt.expected.v)
  117. }
  118. }
  119. }
  120. // dial wraps DialDefaultServer() with a more suitable function name for examples.
  121. func dial() (redis.Conn, error) {
  122. return redis.DialDefaultServer()
  123. }
  124. func ExampleBool() {
  125. c, err := dial()
  126. if err != nil {
  127. fmt.Println(err)
  128. return
  129. }
  130. defer c.Close()
  131. c.Do("SET", "foo", 1)
  132. exists, _ := redis.Bool(c.Do("EXISTS", "foo"))
  133. fmt.Printf("%#v\n", exists)
  134. // Output:
  135. // true
  136. }
  137. func ExampleInt() {
  138. c, err := dial()
  139. if err != nil {
  140. fmt.Println(err)
  141. return
  142. }
  143. defer c.Close()
  144. c.Do("SET", "k1", 1)
  145. n, _ := redis.Int(c.Do("GET", "k1"))
  146. fmt.Printf("%#v\n", n)
  147. n, _ = redis.Int(c.Do("INCR", "k1"))
  148. fmt.Printf("%#v\n", n)
  149. // Output:
  150. // 1
  151. // 2
  152. }
  153. func ExampleInts() {
  154. c, err := dial()
  155. if err != nil {
  156. fmt.Println(err)
  157. return
  158. }
  159. defer c.Close()
  160. c.Do("SADD", "set_with_integers", 4, 5, 6)
  161. ints, _ := redis.Ints(c.Do("SMEMBERS", "set_with_integers"))
  162. fmt.Printf("%#v\n", ints)
  163. // Output:
  164. // []int{4, 5, 6}
  165. }
  166. func ExampleString() {
  167. c, err := dial()
  168. if err != nil {
  169. fmt.Println(err)
  170. return
  171. }
  172. defer c.Close()
  173. c.Do("SET", "hello", "world")
  174. s, err := redis.String(c.Do("GET", "hello"))
  175. fmt.Printf("%#v\n", s)
  176. // Output:
  177. // "world"
  178. }