marshal_test.go 4.7 KB

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