scan_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. "github.com/garyburd/redigo/redis"
  18. "math"
  19. "reflect"
  20. "testing"
  21. )
  22. var scanConversionTests = []struct {
  23. src interface{}
  24. dest interface{}
  25. }{
  26. {[]byte("-inf"), math.Inf(-1)},
  27. {[]byte("+inf"), math.Inf(1)},
  28. {[]byte("0"), float64(0)},
  29. {[]byte("3.14159"), float64(3.14159)},
  30. {[]byte("3.14"), float32(3.14)},
  31. {[]byte("-100"), int(-100)},
  32. {[]byte("101"), int(101)},
  33. {int64(102), int(102)},
  34. {[]byte("103"), uint(103)},
  35. {int64(104), uint(104)},
  36. {[]byte("105"), int8(105)},
  37. {int64(106), int8(106)},
  38. {[]byte("107"), uint8(107)},
  39. {int64(108), uint8(108)},
  40. {[]byte("0"), false},
  41. {int64(0), false},
  42. {[]byte("f"), false},
  43. {[]byte("1"), true},
  44. {int64(1), true},
  45. {[]byte("t"), true},
  46. {[]byte("hello"), "hello"},
  47. {[]byte("world"), []byte("world")},
  48. }
  49. var scanConversionErrorTests = []struct {
  50. src interface{}
  51. dest interface{}
  52. }{
  53. {[]byte("1234"), byte(0)},
  54. {int64(1234), byte(0)},
  55. {[]byte("-1"), byte(0)},
  56. {int64(-1), byte(0)},
  57. {[]byte("junk"), false},
  58. }
  59. func TestScanConversion(t *testing.T) {
  60. for _, tt := range scanConversionTests {
  61. multiBulk := []interface{}{tt.src}
  62. dest := reflect.New(reflect.TypeOf(tt.dest))
  63. multiBulk, err := redis.Scan(multiBulk, dest.Interface())
  64. if err != nil {
  65. t.Errorf("Scan(%v) returned error %v", tt, err)
  66. continue
  67. }
  68. if !reflect.DeepEqual(tt.dest, dest.Elem().Interface()) {
  69. t.Errorf("Scan(%v) returned %v, want %v", tt, dest.Elem().Interface(), tt.dest)
  70. }
  71. }
  72. }
  73. func TestScanConversionError(t *testing.T) {
  74. for _, tt := range scanConversionErrorTests {
  75. multiBulk := []interface{}{tt.src}
  76. dest := reflect.New(reflect.TypeOf(tt.dest))
  77. multiBulk, err := redis.Scan(multiBulk, dest.Interface())
  78. if err == nil {
  79. t.Errorf("Scan(%v) did not return error", tt)
  80. }
  81. }
  82. }
  83. func ExampleScan() {
  84. c, err := dial()
  85. if err != nil {
  86. panic(err)
  87. }
  88. defer c.Close()
  89. c.Send("HMSET", "album:1", "title", "Red", "rating", 5)
  90. c.Send("HMSET", "album:2", "title", "Earthbound", "rating", 1)
  91. c.Send("HMSET", "album:3", "title", "Beat")
  92. c.Send("LPUSH", "albums", "1")
  93. c.Send("LPUSH", "albums", "2")
  94. c.Send("LPUSH", "albums", "3")
  95. multiBulk, err := redis.MultiBulk(c.Do("SORT", "albums",
  96. "BY", "album:*->rating",
  97. "GET", "album:*->title",
  98. "GET", "album:*->rating"))
  99. if err != nil {
  100. panic(err)
  101. }
  102. for len(multiBulk) > 0 {
  103. var title string
  104. rating := -1 // initialize to illegal value to detect nil.
  105. multiBulk, err = redis.Scan(multiBulk, &title, &rating)
  106. if err != nil {
  107. panic(err)
  108. }
  109. if rating == -1 {
  110. fmt.Println(title, "not-rated")
  111. } else {
  112. fmt.Println(title, rating)
  113. }
  114. }
  115. // Output:
  116. // Beat not-rated
  117. // Earthbound 1
  118. // Red 5
  119. }