marshal_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. package gocql
  2. import (
  3. "bytes"
  4. "math"
  5. "reflect"
  6. "speter.net/go/exp/math/dec/inf"
  7. "strings"
  8. "testing"
  9. "time"
  10. )
  11. var marshalTests = []struct {
  12. Info *TypeInfo
  13. Data []byte
  14. Value interface{}
  15. }{
  16. {
  17. &TypeInfo{Type: TypeVarchar},
  18. []byte("hello world"),
  19. []byte("hello world"),
  20. },
  21. {
  22. &TypeInfo{Type: TypeVarchar},
  23. []byte("hello world"),
  24. "hello world",
  25. },
  26. {
  27. &TypeInfo{Type: TypeVarchar},
  28. []byte(nil),
  29. []byte(nil),
  30. },
  31. {
  32. &TypeInfo{Type: TypeVarchar},
  33. []byte("hello world"),
  34. MyString("hello world"),
  35. },
  36. {
  37. &TypeInfo{Type: TypeVarchar},
  38. []byte("HELLO WORLD"),
  39. CustomString("hello world"),
  40. },
  41. {
  42. &TypeInfo{Type: TypeBlob},
  43. []byte("hello\x00"),
  44. []byte("hello\x00"),
  45. },
  46. {
  47. &TypeInfo{Type: TypeBlob},
  48. []byte(nil),
  49. []byte(nil),
  50. },
  51. {
  52. &TypeInfo{Type: TypeTimeUUID},
  53. []byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
  54. func() UUID {
  55. x, _ := UUIDFromBytes([]byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0})
  56. return x
  57. }(),
  58. },
  59. {
  60. &TypeInfo{Type: TypeInt},
  61. []byte("\x00\x00\x00\x00"),
  62. 0,
  63. },
  64. {
  65. &TypeInfo{Type: TypeInt},
  66. []byte("\x01\x02\x03\x04"),
  67. 16909060,
  68. },
  69. {
  70. &TypeInfo{Type: TypeInt},
  71. []byte("\x80\x00\x00\x00"),
  72. int32(math.MinInt32),
  73. },
  74. {
  75. &TypeInfo{Type: TypeInt},
  76. []byte("\x7f\xff\xff\xff"),
  77. int32(math.MaxInt32),
  78. },
  79. {
  80. &TypeInfo{Type: TypeBigInt},
  81. []byte("\x00\x00\x00\x00\x00\x00\x00\x00"),
  82. 0,
  83. },
  84. {
  85. &TypeInfo{Type: TypeBigInt},
  86. []byte("\x01\x02\x03\x04\x05\x06\x07\x08"),
  87. 72623859790382856,
  88. },
  89. {
  90. &TypeInfo{Type: TypeBigInt},
  91. []byte("\x80\x00\x00\x00\x00\x00\x00\x00"),
  92. int64(math.MinInt64),
  93. },
  94. {
  95. &TypeInfo{Type: TypeBigInt},
  96. []byte("\x7f\xff\xff\xff\xff\xff\xff\xff"),
  97. int64(math.MaxInt64),
  98. },
  99. {
  100. &TypeInfo{Type: TypeBoolean},
  101. []byte("\x00"),
  102. false,
  103. },
  104. {
  105. &TypeInfo{Type: TypeBoolean},
  106. []byte("\x01"),
  107. true,
  108. },
  109. {
  110. &TypeInfo{Type: TypeFloat},
  111. []byte("\x40\x49\x0f\xdb"),
  112. float32(3.14159265),
  113. },
  114. {
  115. &TypeInfo{Type: TypeDouble},
  116. []byte("\x40\x09\x21\xfb\x53\xc8\xd4\xf1"),
  117. float64(3.14159265),
  118. },
  119. {
  120. &TypeInfo{Type: TypeDecimal},
  121. []byte("\x00\x00\x00\x00\x00"),
  122. inf.NewDec(0, 0),
  123. },
  124. {
  125. &TypeInfo{Type: TypeDecimal},
  126. []byte("\x00\x00\x00\x00\x64"),
  127. inf.NewDec(100, 0),
  128. },
  129. {
  130. &TypeInfo{Type: TypeDecimal},
  131. []byte("\x00\x00\x00\x02\x19"),
  132. decimalize("0.25"),
  133. },
  134. {
  135. &TypeInfo{Type: TypeDecimal},
  136. []byte("\x00\x00\x00\x13\xD5\a;\x20\x14\xA2\x91"),
  137. decimalize("-0.0012095473475870063"), // From the iconara/cql-rb test suite
  138. },
  139. {
  140. &TypeInfo{Type: TypeDecimal},
  141. []byte("\x00\x00\x00\x13*\xF8\xC4\xDF\xEB]o"),
  142. decimalize("0.0012095473475870063"), // From the iconara/cql-rb test suite
  143. },
  144. {
  145. &TypeInfo{Type: TypeDecimal},
  146. []byte("\x00\x00\x00\x12\xF2\xD8\x02\xB6R\x7F\x99\xEE\x98#\x99\xA9V"),
  147. decimalize("-1042342234234.123423435647768234"), // From the iconara/cql-rb test suite
  148. },
  149. {
  150. &TypeInfo{Type: TypeDecimal},
  151. []byte("\x00\x00\x00\r\nJ\x04\"^\x91\x04\x8a\xb1\x18\xfe"),
  152. decimalize("1243878957943.1234124191998"), // From the datastax/python-driver test suite
  153. },
  154. {
  155. &TypeInfo{Type: TypeDecimal},
  156. []byte("\x00\x00\x00\x06\xe5\xde]\x98Y"),
  157. decimalize("-112233.441191"), // From the datastax/python-driver test suite
  158. },
  159. {
  160. &TypeInfo{Type: TypeDecimal},
  161. []byte("\x00\x00\x00\x14\x00\xfa\xce"),
  162. decimalize("0.00000000000000064206"), // From the datastax/python-driver test suite
  163. },
  164. {
  165. &TypeInfo{Type: TypeDecimal},
  166. []byte("\x00\x00\x00\x14\xff\x052"),
  167. decimalize("-0.00000000000000064206"), // From the datastax/python-driver test suite
  168. },
  169. {
  170. &TypeInfo{Type: TypeDecimal},
  171. []byte("\xff\xff\xff\x9c\x00\xfa\xce"),
  172. inf.NewDec(64206, -100), // From the datastax/python-driver test suite
  173. },
  174. {
  175. &TypeInfo{Type: TypeTimestamp},
  176. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  177. time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC),
  178. },
  179. {
  180. &TypeInfo{Type: TypeTimestamp},
  181. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  182. int64(1376387523000),
  183. },
  184. {
  185. &TypeInfo{Type: TypeList, Elem: &TypeInfo{Type: TypeInt}},
  186. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  187. []int{1, 2},
  188. },
  189. {
  190. &TypeInfo{Type: TypeList, Elem: &TypeInfo{Type: TypeInt}},
  191. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  192. [2]int{1, 2},
  193. },
  194. {
  195. &TypeInfo{Type: TypeSet, Elem: &TypeInfo{Type: TypeInt}},
  196. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  197. []int{1, 2},
  198. },
  199. {
  200. &TypeInfo{Type: TypeSet, Elem: &TypeInfo{Type: TypeInt}},
  201. []byte(nil),
  202. []int(nil),
  203. },
  204. {
  205. &TypeInfo{Type: TypeMap,
  206. Key: &TypeInfo{Type: TypeVarchar},
  207. Elem: &TypeInfo{Type: TypeInt},
  208. },
  209. []byte("\x00\x01\x00\x03foo\x00\x04\x00\x00\x00\x01"),
  210. map[string]int{"foo": 1},
  211. },
  212. {
  213. &TypeInfo{Type: TypeMap,
  214. Key: &TypeInfo{Type: TypeVarchar},
  215. Elem: &TypeInfo{Type: TypeInt},
  216. },
  217. []byte(nil),
  218. map[string]int(nil),
  219. },
  220. {
  221. &TypeInfo{Type: TypeList, Elem: &TypeInfo{Type: TypeVarchar}},
  222. bytes.Join([][]byte{
  223. []byte("\x00\x01\xFF\xFF"),
  224. bytes.Repeat([]byte("X"), 65535)}, []byte("")),
  225. []string{strings.Repeat("X", 65535)},
  226. },
  227. {
  228. &TypeInfo{Type: TypeMap,
  229. Key: &TypeInfo{Type: TypeVarchar},
  230. Elem: &TypeInfo{Type: TypeVarchar},
  231. },
  232. bytes.Join([][]byte{
  233. []byte("\x00\x01\xFF\xFF"),
  234. bytes.Repeat([]byte("X"), 65535),
  235. []byte("\xFF\xFF"),
  236. bytes.Repeat([]byte("Y"), 65535)}, []byte("")),
  237. map[string]string{
  238. strings.Repeat("X", 65535): strings.Repeat("Y", 65535),
  239. },
  240. },
  241. }
  242. func decimalize(s string) *inf.Dec {
  243. i, _ := new(inf.Dec).SetString(s)
  244. return i
  245. }
  246. func TestMarshal(t *testing.T) {
  247. for i, test := range marshalTests {
  248. data, err := Marshal(test.Info, test.Value)
  249. if err != nil {
  250. t.Errorf("marshalTest[%d]: %v", i, err)
  251. continue
  252. }
  253. if !bytes.Equal(data, test.Data) {
  254. t.Errorf("marshalTest[%d]: expected %q, got %q.", i, test.Data, data)
  255. }
  256. }
  257. }
  258. func TestUnmarshal(t *testing.T) {
  259. for i, test := range marshalTests {
  260. v := reflect.New(reflect.TypeOf(test.Value))
  261. err := Unmarshal(test.Info, test.Data, v.Interface())
  262. if err != nil {
  263. t.Errorf("marshalTest[%d]: %v", i, err)
  264. continue
  265. }
  266. if !reflect.DeepEqual(v.Elem().Interface(), test.Value) {
  267. t.Errorf("marshalTest[%d]: expected %#v, got %#v.", i, test.Value, v.Elem().Interface())
  268. }
  269. }
  270. }
  271. type CustomString string
  272. func (c CustomString) MarshalCQL(info *TypeInfo) ([]byte, error) {
  273. return []byte(strings.ToUpper(string(c))), nil
  274. }
  275. func (c *CustomString) UnmarshalCQL(info *TypeInfo, data []byte) error {
  276. *c = CustomString(strings.ToLower(string(data)))
  277. return nil
  278. }
  279. type MyString string
  280. type MyInt int