marshal_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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: TypeTimeUUID},
  52. []byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
  53. func() UUID {
  54. x, _ := UUIDFromBytes([]byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0})
  55. return x
  56. }(),
  57. },
  58. {
  59. &TypeInfo{Type: TypeInt},
  60. []byte("\x00\x00\x00\x00"),
  61. 0,
  62. },
  63. {
  64. &TypeInfo{Type: TypeInt},
  65. []byte("\x01\x02\x03\x04"),
  66. 16909060,
  67. },
  68. {
  69. &TypeInfo{Type: TypeInt},
  70. []byte("\x80\x00\x00\x00"),
  71. int32(math.MinInt32),
  72. },
  73. {
  74. &TypeInfo{Type: TypeInt},
  75. []byte("\x7f\xff\xff\xff"),
  76. int32(math.MaxInt32),
  77. },
  78. {
  79. &TypeInfo{Type: TypeBigInt},
  80. []byte("\x00\x00\x00\x00\x00\x00\x00\x00"),
  81. 0,
  82. },
  83. {
  84. &TypeInfo{Type: TypeBigInt},
  85. []byte("\x01\x02\x03\x04\x05\x06\x07\x08"),
  86. 72623859790382856,
  87. },
  88. {
  89. &TypeInfo{Type: TypeBigInt},
  90. []byte("\x80\x00\x00\x00\x00\x00\x00\x00"),
  91. int64(math.MinInt64),
  92. },
  93. {
  94. &TypeInfo{Type: TypeBigInt},
  95. []byte("\x7f\xff\xff\xff\xff\xff\xff\xff"),
  96. int64(math.MaxInt64),
  97. },
  98. {
  99. &TypeInfo{Type: TypeBoolean},
  100. []byte("\x00"),
  101. false,
  102. },
  103. {
  104. &TypeInfo{Type: TypeBoolean},
  105. []byte("\x01"),
  106. true,
  107. },
  108. {
  109. &TypeInfo{Type: TypeFloat},
  110. []byte("\x40\x49\x0f\xdb"),
  111. float32(3.14159265),
  112. },
  113. {
  114. &TypeInfo{Type: TypeDouble},
  115. []byte("\x40\x09\x21\xfb\x53\xc8\xd4\xf1"),
  116. float64(3.14159265),
  117. },
  118. {
  119. &TypeInfo{Type: TypeTimestamp},
  120. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  121. time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC),
  122. },
  123. {
  124. &TypeInfo{Type: TypeTimestamp},
  125. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  126. int64(1376387523000),
  127. },
  128. {
  129. &TypeInfo{Type: TypeList, Elem: &TypeInfo{Type: TypeInt}},
  130. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  131. []int{1, 2},
  132. },
  133. {
  134. &TypeInfo{Type: TypeList, Elem: &TypeInfo{Type: TypeInt}},
  135. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  136. [2]int{1, 2},
  137. },
  138. {
  139. &TypeInfo{Type: TypeSet, Elem: &TypeInfo{Type: TypeInt}},
  140. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  141. []int{1, 2},
  142. },
  143. {
  144. &TypeInfo{Type: TypeSet, Elem: &TypeInfo{Type: TypeInt}},
  145. []byte(nil),
  146. []int(nil),
  147. },
  148. {
  149. &TypeInfo{Type: TypeMap,
  150. Key: &TypeInfo{Type: TypeVarchar},
  151. Elem: &TypeInfo{Type: TypeInt},
  152. },
  153. []byte("\x00\x01\x00\x03foo\x00\x04\x00\x00\x00\x01"),
  154. map[string]int{"foo": 1},
  155. },
  156. {
  157. &TypeInfo{Type: TypeMap,
  158. Key: &TypeInfo{Type: TypeVarchar},
  159. Elem: &TypeInfo{Type: TypeInt},
  160. },
  161. []byte(nil),
  162. map[string]int(nil),
  163. },
  164. {
  165. &TypeInfo{Type: TypeList, Elem: &TypeInfo{Type: TypeVarchar}},
  166. bytes.Join([][]byte{
  167. []byte("\x00\x01\xFF\xFF"),
  168. bytes.Repeat([]byte("X"), 65535)}, []byte("")),
  169. []string{strings.Repeat("X", 65535)},
  170. },
  171. {
  172. &TypeInfo{Type: TypeMap,
  173. Key: &TypeInfo{Type: TypeVarchar},
  174. Elem: &TypeInfo{Type: TypeVarchar},
  175. },
  176. bytes.Join([][]byte{
  177. []byte("\x00\x01\xFF\xFF"),
  178. bytes.Repeat([]byte("X"), 65535),
  179. []byte("\xFF\xFF"),
  180. bytes.Repeat([]byte("Y"), 65535)}, []byte("")),
  181. map[string]string{
  182. strings.Repeat("X", 65535): strings.Repeat("Y", 65535),
  183. },
  184. },
  185. }
  186. func TestMarshal(t *testing.T) {
  187. for i, test := range marshalTests {
  188. data, err := Marshal(test.Info, test.Value)
  189. if err != nil {
  190. t.Errorf("marshalTest[%d]: %v", i, err)
  191. continue
  192. }
  193. if !bytes.Equal(data, test.Data) {
  194. t.Errorf("marshalTest[%d]: expected %q, got %q.", i, test.Data, data)
  195. }
  196. }
  197. }
  198. func TestUnmarshal(t *testing.T) {
  199. for i, test := range marshalTests {
  200. v := reflect.New(reflect.TypeOf(test.Value))
  201. err := Unmarshal(test.Info, test.Data, v.Interface())
  202. if err != nil {
  203. t.Errorf("marshalTest[%d]: %v", i, err)
  204. continue
  205. }
  206. if !reflect.DeepEqual(v.Elem().Interface(), test.Value) {
  207. t.Errorf("marshalTest[%d]: expected %#v, got %#v.", i, test.Value, v.Elem().Interface())
  208. }
  209. }
  210. }
  211. type CustomString string
  212. func (c CustomString) MarshalCQL(info *TypeInfo) ([]byte, error) {
  213. return []byte(strings.ToUpper(string(c))), nil
  214. }
  215. func (c *CustomString) UnmarshalCQL(info *TypeInfo, data []byte) error {
  216. *c = CustomString(strings.ToLower(string(data)))
  217. return nil
  218. }
  219. type MyString string
  220. type MyInt int