reply_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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/internal/redistest"
  20. "github.com/garyburd/redigo/redis"
  21. )
  22. type valueError struct {
  23. v interface{}
  24. err error
  25. }
  26. func ve(v interface{}, err error) valueError {
  27. return valueError{v, err}
  28. }
  29. var replyTests = []struct {
  30. name interface{}
  31. actual valueError
  32. expected valueError
  33. }{
  34. {
  35. "ints([v1, v2])",
  36. ve(redis.Ints([]interface{}{[]byte("4"), []byte("5")}, nil)),
  37. ve([]int{4, 5}, nil),
  38. },
  39. {
  40. "ints(nil)",
  41. ve(redis.Ints(nil, nil)),
  42. ve([]int(nil), redis.ErrNil),
  43. },
  44. {
  45. "strings([v1, v2])",
  46. ve(redis.Strings([]interface{}{[]byte("v1"), []byte("v2")}, nil)),
  47. ve([]string{"v1", "v2"}, nil),
  48. },
  49. {
  50. "strings(nil)",
  51. ve(redis.Strings(nil, nil)),
  52. ve([]string(nil), redis.ErrNil),
  53. },
  54. {
  55. "values([v1, v2])",
  56. ve(redis.Values([]interface{}{[]byte("v1"), []byte("v2")}, nil)),
  57. ve([]interface{}{[]byte("v1"), []byte("v2")}, nil),
  58. },
  59. {
  60. "values(nil)",
  61. ve(redis.Values(nil, nil)),
  62. ve([]interface{}(nil), redis.ErrNil),
  63. },
  64. {
  65. "float64(1.0)",
  66. ve(redis.Float64([]byte("1.0"), nil)),
  67. ve(float64(1.0), nil),
  68. },
  69. {
  70. "float64(nil)",
  71. ve(redis.Float64(nil, nil)),
  72. ve(float64(0.0), redis.ErrNil),
  73. },
  74. {
  75. "uint64(1)",
  76. ve(redis.Uint64(int64(1), nil)),
  77. ve(uint64(1), nil),
  78. },
  79. {
  80. "uint64(-1)",
  81. ve(redis.Uint64(int64(-1), nil)),
  82. ve(uint64(0), redis.ErrNegativeInt),
  83. },
  84. }
  85. func TestReply(t *testing.T) {
  86. for _, rt := range replyTests {
  87. if rt.actual.err != rt.expected.err {
  88. t.Errorf("%s returned err %v, want %v", rt.name, rt.actual.err, rt.expected.err)
  89. continue
  90. }
  91. if !reflect.DeepEqual(rt.actual.v, rt.expected.v) {
  92. t.Errorf("%s=%+v, want %+v", rt.name, rt.actual.v, rt.expected.v)
  93. }
  94. }
  95. }
  96. // dial wraps DialTestDB() with a more suitable function name for examples.
  97. func dial() (redis.Conn, error) {
  98. return redistest.Dial()
  99. }
  100. func ExampleBool() {
  101. c, err := dial()
  102. if err != nil {
  103. panic(err)
  104. }
  105. defer c.Close()
  106. c.Do("SET", "foo", 1)
  107. exists, _ := redis.Bool(c.Do("EXISTS", "foo"))
  108. fmt.Printf("%#v\n", exists)
  109. // Output:
  110. // true
  111. }
  112. func ExampleInt() {
  113. c, err := dial()
  114. if err != nil {
  115. panic(err)
  116. }
  117. defer c.Close()
  118. c.Do("SET", "k1", 1)
  119. n, _ := redis.Int(c.Do("GET", "k1"))
  120. fmt.Printf("%#v\n", n)
  121. n, _ = redis.Int(c.Do("INCR", "k1"))
  122. fmt.Printf("%#v\n", n)
  123. // Output:
  124. // 1
  125. // 2
  126. }
  127. func ExampleInts() {
  128. c, err := dial()
  129. if err != nil {
  130. panic(err)
  131. }
  132. defer c.Close()
  133. c.Do("SADD", "set_with_integers", 4, 5, 6)
  134. ints, _ := redis.Ints(c.Do("SMEMBERS", "set_with_integers"))
  135. fmt.Printf("%#v\n", ints)
  136. // Output:
  137. // []int{4, 5, 6}
  138. }
  139. func ExampleString() {
  140. c, err := dial()
  141. if err != nil {
  142. panic(err)
  143. }
  144. defer c.Close()
  145. c.Do("SET", "hello", "world")
  146. s, err := redis.String(c.Do("GET", "hello"))
  147. fmt.Printf("%#v\n", s)
  148. // Output:
  149. // "world"
  150. }