marshal_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. package gocql
  2. import (
  3. "bytes"
  4. "math"
  5. "math/big"
  6. "reflect"
  7. "strings"
  8. "testing"
  9. "time"
  10. "speter.net/go/exp/math/dec/inf"
  11. )
  12. var marshalTests = []struct {
  13. Info *TypeInfo
  14. Data []byte
  15. Value interface{}
  16. }{
  17. {
  18. &TypeInfo{Type: TypeVarchar},
  19. []byte("hello world"),
  20. []byte("hello world"),
  21. },
  22. {
  23. &TypeInfo{Type: TypeVarchar},
  24. []byte("hello world"),
  25. "hello world",
  26. },
  27. {
  28. &TypeInfo{Type: TypeVarchar},
  29. []byte(nil),
  30. []byte(nil),
  31. },
  32. {
  33. &TypeInfo{Type: TypeVarchar},
  34. []byte("hello world"),
  35. MyString("hello world"),
  36. },
  37. {
  38. &TypeInfo{Type: TypeVarchar},
  39. []byte("HELLO WORLD"),
  40. CustomString("hello world"),
  41. },
  42. {
  43. &TypeInfo{Type: TypeBlob},
  44. []byte("hello\x00"),
  45. []byte("hello\x00"),
  46. },
  47. {
  48. &TypeInfo{Type: TypeBlob},
  49. []byte(nil),
  50. []byte(nil),
  51. },
  52. {
  53. &TypeInfo{Type: TypeTimeUUID},
  54. []byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
  55. func() UUID {
  56. x, _ := UUIDFromBytes([]byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0})
  57. return x
  58. }(),
  59. },
  60. {
  61. &TypeInfo{Type: TypeInt},
  62. []byte("\x00\x00\x00\x00"),
  63. 0,
  64. },
  65. {
  66. &TypeInfo{Type: TypeInt},
  67. []byte("\x01\x02\x03\x04"),
  68. int(16909060),
  69. },
  70. {
  71. &TypeInfo{Type: TypeInt},
  72. []byte("\x80\x00\x00\x00"),
  73. int32(math.MinInt32),
  74. },
  75. {
  76. &TypeInfo{Type: TypeInt},
  77. []byte("\x7f\xff\xff\xff"),
  78. int32(math.MaxInt32),
  79. },
  80. {
  81. &TypeInfo{Type: TypeBigInt},
  82. []byte("\x00\x00\x00\x00\x00\x00\x00\x00"),
  83. 0,
  84. },
  85. {
  86. &TypeInfo{Type: TypeBigInt},
  87. []byte("\x01\x02\x03\x04\x05\x06\x07\x08"),
  88. 72623859790382856,
  89. },
  90. {
  91. &TypeInfo{Type: TypeBigInt},
  92. []byte("\x80\x00\x00\x00\x00\x00\x00\x00"),
  93. int64(math.MinInt64),
  94. },
  95. {
  96. &TypeInfo{Type: TypeBigInt},
  97. []byte("\x7f\xff\xff\xff\xff\xff\xff\xff"),
  98. int64(math.MaxInt64),
  99. },
  100. {
  101. &TypeInfo{Type: TypeBoolean},
  102. []byte("\x00"),
  103. false,
  104. },
  105. {
  106. &TypeInfo{Type: TypeBoolean},
  107. []byte("\x01"),
  108. true,
  109. },
  110. {
  111. &TypeInfo{Type: TypeFloat},
  112. []byte("\x40\x49\x0f\xdb"),
  113. float32(3.14159265),
  114. },
  115. {
  116. &TypeInfo{Type: TypeDouble},
  117. []byte("\x40\x09\x21\xfb\x53\xc8\xd4\xf1"),
  118. float64(3.14159265),
  119. },
  120. {
  121. &TypeInfo{Type: TypeDecimal},
  122. []byte("\x00\x00\x00\x00\x00"),
  123. inf.NewDec(0, 0),
  124. },
  125. {
  126. &TypeInfo{Type: TypeDecimal},
  127. []byte("\x00\x00\x00\x00\x64"),
  128. inf.NewDec(100, 0),
  129. },
  130. {
  131. &TypeInfo{Type: TypeDecimal},
  132. []byte("\x00\x00\x00\x02\x19"),
  133. decimalize("0.25"),
  134. },
  135. {
  136. &TypeInfo{Type: TypeDecimal},
  137. []byte("\x00\x00\x00\x13\xD5\a;\x20\x14\xA2\x91"),
  138. decimalize("-0.0012095473475870063"), // From the iconara/cql-rb test suite
  139. },
  140. {
  141. &TypeInfo{Type: TypeDecimal},
  142. []byte("\x00\x00\x00\x13*\xF8\xC4\xDF\xEB]o"),
  143. decimalize("0.0012095473475870063"), // From the iconara/cql-rb test suite
  144. },
  145. {
  146. &TypeInfo{Type: TypeDecimal},
  147. []byte("\x00\x00\x00\x12\xF2\xD8\x02\xB6R\x7F\x99\xEE\x98#\x99\xA9V"),
  148. decimalize("-1042342234234.123423435647768234"), // From the iconara/cql-rb test suite
  149. },
  150. {
  151. &TypeInfo{Type: TypeDecimal},
  152. []byte("\x00\x00\x00\r\nJ\x04\"^\x91\x04\x8a\xb1\x18\xfe"),
  153. decimalize("1243878957943.1234124191998"), // From the datastax/python-driver test suite
  154. },
  155. {
  156. &TypeInfo{Type: TypeDecimal},
  157. []byte("\x00\x00\x00\x06\xe5\xde]\x98Y"),
  158. decimalize("-112233.441191"), // From the datastax/python-driver test suite
  159. },
  160. {
  161. &TypeInfo{Type: TypeDecimal},
  162. []byte("\x00\x00\x00\x14\x00\xfa\xce"),
  163. decimalize("0.00000000000000064206"), // From the datastax/python-driver test suite
  164. },
  165. {
  166. &TypeInfo{Type: TypeDecimal},
  167. []byte("\x00\x00\x00\x14\xff\x052"),
  168. decimalize("-0.00000000000000064206"), // From the datastax/python-driver test suite
  169. },
  170. {
  171. &TypeInfo{Type: TypeDecimal},
  172. []byte("\xff\xff\xff\x9c\x00\xfa\xce"),
  173. inf.NewDec(64206, -100), // From the datastax/python-driver test suite
  174. },
  175. {
  176. &TypeInfo{Type: TypeTimestamp},
  177. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  178. time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC),
  179. },
  180. {
  181. &TypeInfo{Type: TypeTimestamp},
  182. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  183. int64(1376387523000),
  184. },
  185. {
  186. &TypeInfo{Type: TypeList, Elem: &TypeInfo{Type: TypeInt}},
  187. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  188. []int{1, 2},
  189. },
  190. {
  191. &TypeInfo{Type: TypeList, Elem: &TypeInfo{Type: TypeInt}},
  192. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  193. [2]int{1, 2},
  194. },
  195. {
  196. &TypeInfo{Type: TypeSet, Elem: &TypeInfo{Type: TypeInt}},
  197. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  198. []int{1, 2},
  199. },
  200. {
  201. &TypeInfo{Type: TypeSet, Elem: &TypeInfo{Type: TypeInt}},
  202. []byte(nil),
  203. []int(nil),
  204. },
  205. {
  206. &TypeInfo{Type: TypeMap,
  207. Key: &TypeInfo{Type: TypeVarchar},
  208. Elem: &TypeInfo{Type: TypeInt},
  209. },
  210. []byte("\x00\x01\x00\x03foo\x00\x04\x00\x00\x00\x01"),
  211. map[string]int{"foo": 1},
  212. },
  213. {
  214. &TypeInfo{Type: TypeMap,
  215. Key: &TypeInfo{Type: TypeVarchar},
  216. Elem: &TypeInfo{Type: TypeInt},
  217. },
  218. []byte(nil),
  219. map[string]int(nil),
  220. },
  221. {
  222. &TypeInfo{Type: TypeList, Elem: &TypeInfo{Type: TypeVarchar}},
  223. bytes.Join([][]byte{
  224. []byte("\x00\x01\xFF\xFF"),
  225. bytes.Repeat([]byte("X"), 65535)}, []byte("")),
  226. []string{strings.Repeat("X", 65535)},
  227. },
  228. {
  229. &TypeInfo{Type: TypeMap,
  230. Key: &TypeInfo{Type: TypeVarchar},
  231. Elem: &TypeInfo{Type: TypeVarchar},
  232. },
  233. bytes.Join([][]byte{
  234. []byte("\x00\x01\xFF\xFF"),
  235. bytes.Repeat([]byte("X"), 65535),
  236. []byte("\xFF\xFF"),
  237. bytes.Repeat([]byte("Y"), 65535)}, []byte("")),
  238. map[string]string{
  239. strings.Repeat("X", 65535): strings.Repeat("Y", 65535),
  240. },
  241. },
  242. {
  243. &TypeInfo{Type: TypeVarint},
  244. []byte("\x00"),
  245. 0,
  246. },
  247. {
  248. &TypeInfo{Type: TypeVarint},
  249. []byte("\x37\xE2\x3C\xEC"),
  250. int32(937573612),
  251. },
  252. {
  253. &TypeInfo{Type: TypeVarint},
  254. []byte("\x37\xE2\x3C\xEC"),
  255. big.NewInt(937573612),
  256. },
  257. {
  258. &TypeInfo{Type: TypeVarint},
  259. []byte("\x03\x9EV \x15\f\x03\x9DK\x18\xCDI\\$?\a["),
  260. bigintize("1231312312331283012830129382342342412123"), // From the iconara/cql-rb test suite
  261. },
  262. {
  263. &TypeInfo{Type: TypeVarint},
  264. []byte("\xC9v\x8D:\x86"),
  265. big.NewInt(-234234234234), // From the iconara/cql-rb test suite
  266. },
  267. {
  268. &TypeInfo{Type: TypeVarint},
  269. []byte("f\x1e\xfd\xf2\xe3\xb1\x9f|\x04_\x15"),
  270. bigintize("123456789123456789123456789"), // From the datastax/python-driver test suite
  271. },
  272. }
  273. func decimalize(s string) *inf.Dec {
  274. i, _ := new(inf.Dec).SetString(s)
  275. return i
  276. }
  277. func bigintize(s string) *big.Int {
  278. i, _ := new(big.Int).SetString(s, 10)
  279. return i
  280. }
  281. func TestMarshal(t *testing.T) {
  282. for i, test := range marshalTests {
  283. data, err := Marshal(test.Info, test.Value)
  284. if err != nil {
  285. t.Errorf("marshalTest[%d]: %v", i, err)
  286. continue
  287. }
  288. if !bytes.Equal(data, test.Data) {
  289. t.Errorf("marshalTest[%d]: expected %q, got %q.", i, test.Data, data)
  290. }
  291. }
  292. }
  293. func TestMarshalNil(t *testing.T) {
  294. data, err := Marshal(&TypeInfo{Type: TypeInt}, nil)
  295. if err != nil {
  296. t.Errorf("failed to marshal nil with err: %v", err)
  297. }
  298. if data != nil {
  299. t.Errorf("expected nil, got %v", data)
  300. }
  301. }
  302. func TestUnmarshal(t *testing.T) {
  303. for i, test := range marshalTests {
  304. v := reflect.New(reflect.TypeOf(test.Value))
  305. err := Unmarshal(test.Info, test.Data, v.Interface())
  306. if err != nil {
  307. t.Errorf("marshalTest[%d]: %v", i, err)
  308. continue
  309. }
  310. if !reflect.DeepEqual(v.Elem().Interface(), test.Value) {
  311. t.Errorf("marshalTest[%d]: expected %#v, got %#v.", i, test.Value, v.Elem().Interface())
  312. }
  313. }
  314. }
  315. func TestMarshalVarint(t *testing.T) {
  316. varintTests := []struct {
  317. Value interface{}
  318. Marshaled []byte
  319. Unmarshaled *big.Int
  320. }{
  321. {
  322. Value: int8(0),
  323. Marshaled: []byte("\x00"),
  324. Unmarshaled: big.NewInt(0),
  325. },
  326. {
  327. Value: uint8(255),
  328. Marshaled: []byte("\x00\xFF"),
  329. Unmarshaled: big.NewInt(255),
  330. },
  331. {
  332. Value: int8(-1),
  333. Marshaled: []byte("\xFF"),
  334. Unmarshaled: big.NewInt(-1),
  335. },
  336. {
  337. Value: big.NewInt(math.MaxInt32),
  338. Marshaled: []byte("\x7F\xFF\xFF\xFF"),
  339. Unmarshaled: big.NewInt(math.MaxInt32),
  340. },
  341. {
  342. Value: big.NewInt(int64(math.MaxInt32) + 1),
  343. Marshaled: []byte("\x00\x80\x00\x00\x00"),
  344. Unmarshaled: big.NewInt(int64(math.MaxInt32) + 1),
  345. },
  346. {
  347. Value: big.NewInt(math.MinInt32),
  348. Marshaled: []byte("\x80\x00\x00\x00"),
  349. Unmarshaled: big.NewInt(math.MinInt32),
  350. },
  351. {
  352. Value: big.NewInt(int64(math.MinInt32) - 1),
  353. Marshaled: []byte("\xFF\x7F\xFF\xFF\xFF"),
  354. Unmarshaled: big.NewInt(int64(math.MinInt32) - 1),
  355. },
  356. {
  357. Value: math.MinInt64,
  358. Marshaled: []byte("\x80\x00\x00\x00\x00\x00\x00\x00"),
  359. Unmarshaled: big.NewInt(math.MinInt64),
  360. },
  361. {
  362. Value: uint64(math.MaxInt64) + 1,
  363. Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00"),
  364. Unmarshaled: bigintize("9223372036854775808"),
  365. },
  366. {
  367. Value: bigintize("2361183241434822606848"), // 2**71
  368. Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00"),
  369. Unmarshaled: bigintize("2361183241434822606848"),
  370. },
  371. {
  372. Value: bigintize("-9223372036854775809"), // -2**63 - 1
  373. Marshaled: []byte("\xFF\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF"),
  374. Unmarshaled: bigintize("-9223372036854775809"),
  375. },
  376. }
  377. for i, test := range varintTests {
  378. data, err := Marshal(&TypeInfo{Type: TypeVarint}, test.Value)
  379. if err != nil {
  380. t.Errorf("error marshaling varint: %v (test #%d)", err, i)
  381. }
  382. if !bytes.Equal(test.Marshaled, data) {
  383. t.Errorf("marshaled varint mismatch: expected %v, got %v (test #%d)", test.Marshaled, data, i)
  384. }
  385. binder := new(big.Int)
  386. err = Unmarshal(&TypeInfo{Type: TypeVarint}, test.Marshaled, binder)
  387. if err != nil {
  388. t.Errorf("error unmarshaling varint: %v (test #%d)", err, i)
  389. }
  390. if test.Unmarshaled.Cmp(binder) != 0 {
  391. t.Errorf("unmarshaled varint mismatch: expected %v, got %v (test #%d)", test.Unmarshaled, binder, i)
  392. }
  393. }
  394. }
  395. type CustomString string
  396. func (c CustomString) MarshalCQL(info *TypeInfo) ([]byte, error) {
  397. return []byte(strings.ToUpper(string(c))), nil
  398. }
  399. func (c *CustomString) UnmarshalCQL(info *TypeInfo, data []byte) error {
  400. *c = CustomString(strings.ToLower(string(data)))
  401. return nil
  402. }
  403. type MyString string
  404. type MyInt int
  405. var typeLookupTest = []struct {
  406. TypeName string
  407. ExpectedType Type
  408. }{
  409. {"AsciiType", TypeAscii},
  410. {"LongType", TypeBigInt},
  411. {"BytesType", TypeBlob},
  412. {"BooleanType", TypeBoolean},
  413. {"CounterColumnType", TypeCounter},
  414. {"DecimalType", TypeDecimal},
  415. {"DoubleType", TypeDouble},
  416. {"FloatType", TypeFloat},
  417. {"Int32Type", TypeInt},
  418. {"DateType", TypeTimestamp},
  419. {"UUIDType", TypeUUID},
  420. {"UTF8Type", TypeVarchar},
  421. {"IntegerType", TypeVarint},
  422. {"TimeUUIDType", TypeTimeUUID},
  423. {"InetAddressType", TypeInet},
  424. {"MapType", TypeMap},
  425. {"ListType", TypeInet},
  426. {"SetType", TypeInet},
  427. {"unknown", TypeCustom},
  428. }
  429. func testType(t *testing.T, cassType string, expectedType Type) {
  430. if computedType := getApacheCassandraType(apacheCassandraTypePrefix + cassType); computedType != expectedType {
  431. t.Errorf("Cassandra custom type lookup for %s failed. Expected %s, got %s.", cassType, expectedType.String(), computedType.String())
  432. }
  433. }
  434. func TestLookupCassType(t *testing.T) {
  435. for _, lookupTest := range typeLookupTest {
  436. testType(t, lookupTest.TypeName, lookupTest.ExpectedType)
  437. }
  438. }