reply_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. "strings([v1, v2])",
  36. ve(redis.Strings([]interface{}{[]byte("v1"), []byte("v2")}, nil)),
  37. ve([]string{"v1", "v2"}, nil),
  38. },
  39. {
  40. "strings(nil)",
  41. ve(redis.Strings(nil, nil)),
  42. ve([]string(nil), redis.ErrNil),
  43. },
  44. {
  45. "values([v1, v2])",
  46. ve(redis.Values([]interface{}{[]byte("v1"), []byte("v2")}, nil)),
  47. ve([]interface{}{[]byte("v1"), []byte("v2")}, nil),
  48. },
  49. {
  50. "values(nil)",
  51. ve(redis.Values(nil, nil)),
  52. ve([]interface{}(nil), redis.ErrNil),
  53. },
  54. {
  55. "float64(1.0)",
  56. ve(redis.Float64([]byte("1.0"), nil)),
  57. ve(float64(1.0), nil),
  58. },
  59. {
  60. "float64(nil)",
  61. ve(redis.Float64(nil, nil)),
  62. ve(float64(0.0), redis.ErrNil),
  63. },
  64. {
  65. "uint64(1)",
  66. ve(redis.Uint64(int64(1), nil)),
  67. ve(uint64(1), nil),
  68. },
  69. {
  70. "uint64(-1)",
  71. ve(redis.Uint64(int64(-1), nil)),
  72. ve(uint64(0), redis.ErrNegativeInt),
  73. },
  74. }
  75. func TestReply(t *testing.T) {
  76. for _, rt := range replyTests {
  77. if rt.actual.err != rt.expected.err {
  78. t.Errorf("%s returned err %v, want %v", rt.name, rt.actual.err, rt.expected.err)
  79. continue
  80. }
  81. if !reflect.DeepEqual(rt.actual.v, rt.expected.v) {
  82. t.Errorf("%s=%+v, want %+v", rt.name, rt.actual.v, rt.expected.v)
  83. }
  84. }
  85. }
  86. // dial wraps DialTestDB() with a more suitable function name for examples.
  87. func dial() (redis.Conn, error) {
  88. return redistest.Dial()
  89. }
  90. func ExampleBool() {
  91. c, err := dial()
  92. if err != nil {
  93. panic(err)
  94. }
  95. defer c.Close()
  96. c.Do("SET", "foo", 1)
  97. exists, _ := redis.Bool(c.Do("EXISTS", "foo"))
  98. fmt.Printf("%#v\n", exists)
  99. // Output:
  100. // true
  101. }
  102. func ExampleInt() {
  103. c, err := dial()
  104. if err != nil {
  105. panic(err)
  106. }
  107. defer c.Close()
  108. c.Do("SET", "k1", 1)
  109. n, _ := redis.Int(c.Do("GET", "k1"))
  110. fmt.Printf("%#v\n", n)
  111. n, _ = redis.Int(c.Do("INCR", "k1"))
  112. fmt.Printf("%#v\n", n)
  113. // Output:
  114. // 1
  115. // 2
  116. }
  117. func ExampleString() {
  118. c, err := dial()
  119. if err != nil {
  120. panic(err)
  121. }
  122. defer c.Close()
  123. c.Do("SET", "hello", "world")
  124. s, err := redis.String(c.Do("GET", "hello"))
  125. fmt.Printf("%#v\n", s)
  126. // Output:
  127. // "world"
  128. }