marshal_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package gocql
  2. import (
  3. "bytes"
  4. "math"
  5. "reflect"
  6. "strings"
  7. "testing"
  8. "time"
  9. )
  10. var marshalTests = []struct {
  11. Info *TypeInfo
  12. Data []byte
  13. Value interface{}
  14. }{
  15. {
  16. &TypeInfo{Type: TypeVarchar},
  17. []byte("hello world"),
  18. []byte("hello world"),
  19. },
  20. {
  21. &TypeInfo{Type: TypeVarchar},
  22. []byte("hello world"),
  23. "hello world",
  24. },
  25. {
  26. &TypeInfo{Type: TypeVarchar},
  27. []byte(nil),
  28. []byte(nil),
  29. },
  30. {
  31. &TypeInfo{Type: TypeVarchar},
  32. []byte("hello world"),
  33. MyString("hello world"),
  34. },
  35. {
  36. &TypeInfo{Type: TypeVarchar},
  37. []byte("HELLO WORLD"),
  38. CustomString("hello world"),
  39. },
  40. {
  41. &TypeInfo{Type: TypeBlob},
  42. []byte("hello\x00"),
  43. []byte("hello\x00"),
  44. },
  45. {
  46. &TypeInfo{Type: TypeBlob},
  47. []byte(nil),
  48. []byte(nil),
  49. },
  50. {
  51. &TypeInfo{Type: TypeInt},
  52. []byte("\x00\x00\x00\x00"),
  53. 0,
  54. },
  55. {
  56. &TypeInfo{Type: TypeInt},
  57. []byte("\x01\x02\x03\x04"),
  58. 16909060,
  59. },
  60. {
  61. &TypeInfo{Type: TypeInt},
  62. []byte("\x80\x00\x00\x00"),
  63. int32(math.MinInt32),
  64. },
  65. {
  66. &TypeInfo{Type: TypeInt},
  67. []byte("\x7f\xff\xff\xff"),
  68. int32(math.MaxInt32),
  69. },
  70. {
  71. &TypeInfo{Type: TypeBigInt},
  72. []byte("\x00\x00\x00\x00\x00\x00\x00\x00"),
  73. 0,
  74. },
  75. {
  76. &TypeInfo{Type: TypeBigInt},
  77. []byte("\x01\x02\x03\x04\x05\x06\x07\x08"),
  78. 72623859790382856,
  79. },
  80. {
  81. &TypeInfo{Type: TypeBigInt},
  82. []byte("\x80\x00\x00\x00\x00\x00\x00\x00"),
  83. int64(math.MinInt64),
  84. },
  85. {
  86. &TypeInfo{Type: TypeBigInt},
  87. []byte("\x7f\xff\xff\xff\xff\xff\xff\xff"),
  88. int64(math.MaxInt64),
  89. },
  90. {
  91. &TypeInfo{Type: TypeBoolean},
  92. []byte("\x00"),
  93. false,
  94. },
  95. {
  96. &TypeInfo{Type: TypeBoolean},
  97. []byte("\x01"),
  98. true,
  99. },
  100. {
  101. &TypeInfo{Type: TypeFloat},
  102. []byte("\x40\x49\x0f\xdb"),
  103. float32(3.14159265),
  104. },
  105. {
  106. &TypeInfo{Type: TypeDouble},
  107. []byte("\x40\x09\x21\xfb\x53\xc8\xd4\xf1"),
  108. float64(3.14159265),
  109. },
  110. {
  111. &TypeInfo{Type: TypeTimestamp},
  112. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  113. time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC),
  114. },
  115. {
  116. &TypeInfo{Type: TypeTimestamp},
  117. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  118. int64(1376387523000),
  119. },
  120. {
  121. &TypeInfo{Type: TypeList, Elem: &TypeInfo{Type: TypeInt}},
  122. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  123. []int{1, 2},
  124. },
  125. {
  126. &TypeInfo{Type: TypeList, Elem: &TypeInfo{Type: TypeInt}},
  127. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  128. [2]int{1, 2},
  129. },
  130. {
  131. &TypeInfo{Type: TypeSet, Elem: &TypeInfo{Type: TypeInt}},
  132. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  133. []int{1, 2},
  134. },
  135. {
  136. &TypeInfo{Type: TypeMap,
  137. Key: &TypeInfo{Type: TypeVarchar},
  138. Elem: &TypeInfo{Type: TypeInt},
  139. },
  140. []byte("\x00\x01\x00\x03foo\x00\x04\x00\x00\x00\x01"),
  141. map[string]int{"foo": 1},
  142. },
  143. }
  144. func TestMarshal(t *testing.T) {
  145. for i, test := range marshalTests {
  146. data, err := Marshal(test.Info, test.Value)
  147. if err != nil {
  148. t.Errorf("marshalTest[%d]: %v", i, err)
  149. continue
  150. }
  151. if !bytes.Equal(data, test.Data) {
  152. t.Errorf("marshalTest[%d]: expected %q, got %q.", i, test.Data, data)
  153. }
  154. }
  155. }
  156. func TestUnmarshal(t *testing.T) {
  157. for i, test := range marshalTests {
  158. v := reflect.New(reflect.TypeOf(test.Value))
  159. err := Unmarshal(test.Info, test.Data, v.Interface())
  160. if err != nil {
  161. t.Errorf("marshalTest[%d]: %v", i, err)
  162. continue
  163. }
  164. if !reflect.DeepEqual(v.Elem().Interface(), test.Value) {
  165. t.Errorf("marshalTest[%d]: expected %#v, got %#v.", i, test.Value, v.Elem().Interface())
  166. }
  167. }
  168. }
  169. type CustomString string
  170. func (c CustomString) MarshalCQL(info *TypeInfo) ([]byte, error) {
  171. return []byte(strings.ToUpper(string(c))), nil
  172. }
  173. func (c *CustomString) UnmarshalCQL(info *TypeInfo, data []byte) error {
  174. *c = CustomString(strings.ToLower(string(data)))
  175. return nil
  176. }
  177. type MyString string
  178. type MyInt int