marshal_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. // +build all unit
  2. package gocql
  3. import (
  4. "bytes"
  5. "math"
  6. "math/big"
  7. "net"
  8. "reflect"
  9. "strings"
  10. "testing"
  11. "time"
  12. "speter.net/go/exp/math/dec/inf"
  13. )
  14. var marshalTests = []struct {
  15. Info *TypeInfo
  16. Data []byte
  17. Value interface{}
  18. }{
  19. {
  20. &TypeInfo{Type: TypeVarchar},
  21. []byte("hello world"),
  22. []byte("hello world"),
  23. },
  24. {
  25. &TypeInfo{Type: TypeVarchar},
  26. []byte("hello world"),
  27. "hello world",
  28. },
  29. {
  30. &TypeInfo{Type: TypeVarchar},
  31. []byte(nil),
  32. []byte(nil),
  33. },
  34. {
  35. &TypeInfo{Type: TypeVarchar},
  36. []byte("hello world"),
  37. MyString("hello world"),
  38. },
  39. {
  40. &TypeInfo{Type: TypeVarchar},
  41. []byte("HELLO WORLD"),
  42. CustomString("hello world"),
  43. },
  44. {
  45. &TypeInfo{Type: TypeBlob},
  46. []byte("hello\x00"),
  47. []byte("hello\x00"),
  48. },
  49. {
  50. &TypeInfo{Type: TypeBlob},
  51. []byte(nil),
  52. []byte(nil),
  53. },
  54. {
  55. &TypeInfo{Type: TypeTimeUUID},
  56. []byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
  57. func() UUID {
  58. x, _ := UUIDFromBytes([]byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0})
  59. return x
  60. }(),
  61. },
  62. {
  63. &TypeInfo{Type: TypeInt},
  64. []byte("\x00\x00\x00\x00"),
  65. 0,
  66. },
  67. {
  68. &TypeInfo{Type: TypeInt},
  69. []byte("\x01\x02\x03\x04"),
  70. int(16909060),
  71. },
  72. {
  73. &TypeInfo{Type: TypeInt},
  74. []byte("\x80\x00\x00\x00"),
  75. int32(math.MinInt32),
  76. },
  77. {
  78. &TypeInfo{Type: TypeInt},
  79. []byte("\x7f\xff\xff\xff"),
  80. int32(math.MaxInt32),
  81. },
  82. {
  83. &TypeInfo{Type: TypeBigInt},
  84. []byte("\x00\x00\x00\x00\x00\x00\x00\x00"),
  85. 0,
  86. },
  87. {
  88. &TypeInfo{Type: TypeBigInt},
  89. []byte("\x01\x02\x03\x04\x05\x06\x07\x08"),
  90. 72623859790382856,
  91. },
  92. {
  93. &TypeInfo{Type: TypeBigInt},
  94. []byte("\x80\x00\x00\x00\x00\x00\x00\x00"),
  95. int64(math.MinInt64),
  96. },
  97. {
  98. &TypeInfo{Type: TypeBigInt},
  99. []byte("\x7f\xff\xff\xff\xff\xff\xff\xff"),
  100. int64(math.MaxInt64),
  101. },
  102. {
  103. &TypeInfo{Type: TypeBoolean},
  104. []byte("\x00"),
  105. false,
  106. },
  107. {
  108. &TypeInfo{Type: TypeBoolean},
  109. []byte("\x01"),
  110. true,
  111. },
  112. {
  113. &TypeInfo{Type: TypeFloat},
  114. []byte("\x40\x49\x0f\xdb"),
  115. float32(3.14159265),
  116. },
  117. {
  118. &TypeInfo{Type: TypeDouble},
  119. []byte("\x40\x09\x21\xfb\x53\xc8\xd4\xf1"),
  120. float64(3.14159265),
  121. },
  122. {
  123. &TypeInfo{Type: TypeDecimal},
  124. []byte("\x00\x00\x00\x00\x00"),
  125. inf.NewDec(0, 0),
  126. },
  127. {
  128. &TypeInfo{Type: TypeDecimal},
  129. []byte("\x00\x00\x00\x00\x64"),
  130. inf.NewDec(100, 0),
  131. },
  132. {
  133. &TypeInfo{Type: TypeDecimal},
  134. []byte("\x00\x00\x00\x02\x19"),
  135. decimalize("0.25"),
  136. },
  137. {
  138. &TypeInfo{Type: TypeDecimal},
  139. []byte("\x00\x00\x00\x13\xD5\a;\x20\x14\xA2\x91"),
  140. decimalize("-0.0012095473475870063"), // From the iconara/cql-rb test suite
  141. },
  142. {
  143. &TypeInfo{Type: TypeDecimal},
  144. []byte("\x00\x00\x00\x13*\xF8\xC4\xDF\xEB]o"),
  145. decimalize("0.0012095473475870063"), // From the iconara/cql-rb test suite
  146. },
  147. {
  148. &TypeInfo{Type: TypeDecimal},
  149. []byte("\x00\x00\x00\x12\xF2\xD8\x02\xB6R\x7F\x99\xEE\x98#\x99\xA9V"),
  150. decimalize("-1042342234234.123423435647768234"), // From the iconara/cql-rb test suite
  151. },
  152. {
  153. &TypeInfo{Type: TypeDecimal},
  154. []byte("\x00\x00\x00\r\nJ\x04\"^\x91\x04\x8a\xb1\x18\xfe"),
  155. decimalize("1243878957943.1234124191998"), // From the datastax/python-driver test suite
  156. },
  157. {
  158. &TypeInfo{Type: TypeDecimal},
  159. []byte("\x00\x00\x00\x06\xe5\xde]\x98Y"),
  160. decimalize("-112233.441191"), // From the datastax/python-driver test suite
  161. },
  162. {
  163. &TypeInfo{Type: TypeDecimal},
  164. []byte("\x00\x00\x00\x14\x00\xfa\xce"),
  165. decimalize("0.00000000000000064206"), // From the datastax/python-driver test suite
  166. },
  167. {
  168. &TypeInfo{Type: TypeDecimal},
  169. []byte("\x00\x00\x00\x14\xff\x052"),
  170. decimalize("-0.00000000000000064206"), // From the datastax/python-driver test suite
  171. },
  172. {
  173. &TypeInfo{Type: TypeDecimal},
  174. []byte("\xff\xff\xff\x9c\x00\xfa\xce"),
  175. inf.NewDec(64206, -100), // From the datastax/python-driver test suite
  176. },
  177. {
  178. &TypeInfo{Type: TypeTimestamp},
  179. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  180. time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC),
  181. },
  182. {
  183. &TypeInfo{Type: TypeTimestamp},
  184. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  185. int64(1376387523000),
  186. },
  187. {
  188. &TypeInfo{Type: TypeList, Elem: &TypeInfo{Type: TypeInt}},
  189. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  190. []int{1, 2},
  191. },
  192. {
  193. &TypeInfo{Type: TypeList, Elem: &TypeInfo{Type: TypeInt}},
  194. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  195. [2]int{1, 2},
  196. },
  197. {
  198. &TypeInfo{Type: TypeSet, Elem: &TypeInfo{Type: TypeInt}},
  199. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  200. []int{1, 2},
  201. },
  202. {
  203. &TypeInfo{Type: TypeSet, Elem: &TypeInfo{Type: TypeInt}},
  204. []byte(nil),
  205. []int(nil),
  206. },
  207. {
  208. &TypeInfo{Type: TypeMap,
  209. Key: &TypeInfo{Type: TypeVarchar},
  210. Elem: &TypeInfo{Type: TypeInt},
  211. },
  212. []byte("\x00\x01\x00\x03foo\x00\x04\x00\x00\x00\x01"),
  213. map[string]int{"foo": 1},
  214. },
  215. {
  216. &TypeInfo{Type: TypeMap,
  217. Key: &TypeInfo{Type: TypeVarchar},
  218. Elem: &TypeInfo{Type: TypeInt},
  219. },
  220. []byte(nil),
  221. map[string]int(nil),
  222. },
  223. {
  224. &TypeInfo{Type: TypeList, Elem: &TypeInfo{Type: TypeVarchar}},
  225. bytes.Join([][]byte{
  226. []byte("\x00\x01\xFF\xFF"),
  227. bytes.Repeat([]byte("X"), 65535)}, []byte("")),
  228. []string{strings.Repeat("X", 65535)},
  229. },
  230. {
  231. &TypeInfo{Type: TypeMap,
  232. Key: &TypeInfo{Type: TypeVarchar},
  233. Elem: &TypeInfo{Type: TypeVarchar},
  234. },
  235. bytes.Join([][]byte{
  236. []byte("\x00\x01\xFF\xFF"),
  237. bytes.Repeat([]byte("X"), 65535),
  238. []byte("\xFF\xFF"),
  239. bytes.Repeat([]byte("Y"), 65535)}, []byte("")),
  240. map[string]string{
  241. strings.Repeat("X", 65535): strings.Repeat("Y", 65535),
  242. },
  243. },
  244. {
  245. &TypeInfo{Type: TypeVarint},
  246. []byte("\x00"),
  247. 0,
  248. },
  249. {
  250. &TypeInfo{Type: TypeVarint},
  251. []byte("\x37\xE2\x3C\xEC"),
  252. int32(937573612),
  253. },
  254. {
  255. &TypeInfo{Type: TypeVarint},
  256. []byte("\x37\xE2\x3C\xEC"),
  257. big.NewInt(937573612),
  258. },
  259. {
  260. &TypeInfo{Type: TypeVarint},
  261. []byte("\x03\x9EV \x15\f\x03\x9DK\x18\xCDI\\$?\a["),
  262. bigintize("1231312312331283012830129382342342412123"), // From the iconara/cql-rb test suite
  263. },
  264. {
  265. &TypeInfo{Type: TypeVarint},
  266. []byte("\xC9v\x8D:\x86"),
  267. big.NewInt(-234234234234), // From the iconara/cql-rb test suite
  268. },
  269. {
  270. &TypeInfo{Type: TypeVarint},
  271. []byte("f\x1e\xfd\xf2\xe3\xb1\x9f|\x04_\x15"),
  272. bigintize("123456789123456789123456789"), // From the datastax/python-driver test suite
  273. },
  274. {
  275. &TypeInfo{Type: TypeInet},
  276. []byte("\x7F\x00\x00\x01"),
  277. net.ParseIP("127.0.0.1").To4(),
  278. },
  279. {
  280. &TypeInfo{Type: TypeInet},
  281. []byte("\xFF\xFF\xFF\xFF"),
  282. net.ParseIP("255.255.255.255").To4(),
  283. },
  284. {
  285. &TypeInfo{Type: TypeInet},
  286. []byte("\x7F\x00\x00\x01"),
  287. "127.0.0.1",
  288. },
  289. {
  290. &TypeInfo{Type: TypeInet},
  291. []byte("\xFF\xFF\xFF\xFF"),
  292. "255.255.255.255",
  293. },
  294. {
  295. &TypeInfo{Type: TypeInet},
  296. []byte("\x21\xDA\x00\xd3\x00\x00\x2f\x3b\x02\xaa\x00\xff\xfe\x28\x9c\x5a"),
  297. "21da:d3:0:2f3b:2aa:ff:fe28:9c5a",
  298. },
  299. {
  300. &TypeInfo{Type: TypeInet},
  301. []byte("\xfe\x80\x00\x00\x00\x00\x00\x00\x02\x02\xb3\xff\xfe\x1e\x83\x29"),
  302. "fe80::202:b3ff:fe1e:8329",
  303. },
  304. {
  305. &TypeInfo{Type: TypeInet},
  306. []byte("\x21\xDA\x00\xd3\x00\x00\x2f\x3b\x02\xaa\x00\xff\xfe\x28\x9c\x5a"),
  307. net.ParseIP("21da:d3:0:2f3b:2aa:ff:fe28:9c5a"),
  308. },
  309. {
  310. &TypeInfo{Type: TypeInet},
  311. []byte("\xfe\x80\x00\x00\x00\x00\x00\x00\x02\x02\xb3\xff\xfe\x1e\x83\x29"),
  312. net.ParseIP("fe80::202:b3ff:fe1e:8329"),
  313. },
  314. }
  315. var marshalNilTests = []struct {
  316. Info *TypeInfo
  317. Data []byte
  318. Value interface{}
  319. }{
  320. {
  321. &TypeInfo{Type: TypeInt},
  322. []byte(nil),
  323. nil,
  324. },
  325. {
  326. &TypeInfo{Type: TypeVarchar},
  327. []byte("nullable string"),
  328. func() *string {
  329. value := "nullable string"
  330. return &value
  331. }(),
  332. },
  333. {
  334. &TypeInfo{Type: TypeVarchar},
  335. []byte{},
  336. (*string)(nil),
  337. },
  338. {
  339. &TypeInfo{Type: TypeInt},
  340. []byte("\x7f\xff\xff\xff"),
  341. func() *int {
  342. var value int = math.MaxInt32
  343. return &value
  344. }(),
  345. },
  346. {
  347. &TypeInfo{Type: TypeInt},
  348. []byte(nil),
  349. (*int)(nil),
  350. },
  351. {
  352. &TypeInfo{Type: TypeTimeUUID},
  353. []byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
  354. &UUID{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
  355. },
  356. {
  357. &TypeInfo{Type: TypeTimeUUID},
  358. []byte{},
  359. (*UUID)(nil),
  360. },
  361. {
  362. &TypeInfo{Type: TypeTimestamp},
  363. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  364. func() *time.Time {
  365. t := time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC)
  366. return &t
  367. }(),
  368. },
  369. {
  370. &TypeInfo{Type: TypeTimestamp},
  371. []byte(nil),
  372. (*time.Time)(nil),
  373. },
  374. {
  375. &TypeInfo{Type: TypeBoolean},
  376. []byte("\x00"),
  377. func() *bool {
  378. b := false
  379. return &b
  380. }(),
  381. },
  382. {
  383. &TypeInfo{Type: TypeBoolean},
  384. []byte("\x01"),
  385. func() *bool {
  386. b := true
  387. return &b
  388. }(),
  389. },
  390. {
  391. &TypeInfo{Type: TypeBoolean},
  392. []byte(nil),
  393. (*bool)(nil),
  394. },
  395. {
  396. &TypeInfo{Type: TypeFloat},
  397. []byte("\x40\x49\x0f\xdb"),
  398. func() *float32 {
  399. f := float32(3.14159265)
  400. return &f
  401. }(),
  402. },
  403. {
  404. &TypeInfo{Type: TypeFloat},
  405. []byte(nil),
  406. (*float32)(nil),
  407. },
  408. {
  409. &TypeInfo{Type: TypeDouble},
  410. []byte("\x40\x09\x21\xfb\x53\xc8\xd4\xf1"),
  411. func() *float64 {
  412. d := float64(3.14159265)
  413. return &d
  414. }(),
  415. },
  416. {
  417. &TypeInfo{Type: TypeDouble},
  418. []byte(nil),
  419. (*float64)(nil),
  420. },
  421. {
  422. &TypeInfo{Type: TypeInet},
  423. []byte("\x7F\x00\x00\x01"),
  424. func() *net.IP {
  425. ip := net.ParseIP("127.0.0.1").To4()
  426. return &ip
  427. }(),
  428. },
  429. {
  430. &TypeInfo{Type: TypeInet},
  431. []byte(nil),
  432. (*net.IP)(nil),
  433. },
  434. {
  435. &TypeInfo{Type: TypeList, Elem: &TypeInfo{Type: TypeInt}},
  436. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  437. func() *[]int {
  438. l := []int{1, 2}
  439. return &l
  440. }(),
  441. },
  442. {
  443. &TypeInfo{Type: TypeList, Elem: &TypeInfo{Type: TypeInt}},
  444. []byte(nil),
  445. (*[]int)(nil),
  446. },
  447. {
  448. &TypeInfo{Type: TypeMap,
  449. Key: &TypeInfo{Type: TypeVarchar},
  450. Elem: &TypeInfo{Type: TypeInt},
  451. },
  452. []byte("\x00\x01\x00\x03foo\x00\x04\x00\x00\x00\x01"),
  453. func() *map[string]int {
  454. m := map[string]int{"foo": 1}
  455. return &m
  456. }(),
  457. },
  458. {
  459. &TypeInfo{Type: TypeMap,
  460. Key: &TypeInfo{Type: TypeVarchar},
  461. Elem: &TypeInfo{Type: TypeInt},
  462. },
  463. []byte(nil),
  464. (*map[string]int)(nil),
  465. },
  466. }
  467. func decimalize(s string) *inf.Dec {
  468. i, _ := new(inf.Dec).SetString(s)
  469. return i
  470. }
  471. func bigintize(s string) *big.Int {
  472. i, _ := new(big.Int).SetString(s, 10)
  473. return i
  474. }
  475. func TestMarshal(t *testing.T) {
  476. for i, test := range marshalTests {
  477. data, err := Marshal(test.Info, test.Value)
  478. if err != nil {
  479. t.Errorf("marshalTest[%d]: %v", i, err)
  480. continue
  481. }
  482. if !bytes.Equal(data, test.Data) {
  483. t.Errorf("marshalTest[%d]: expected %q, got %q (%#v)", i, test.Data, data, test.Value)
  484. }
  485. }
  486. }
  487. func TestMarshalNil(t *testing.T) {
  488. for i, test := range marshalNilTests {
  489. data, err := Marshal(test.Info, test.Value)
  490. if err != nil {
  491. t.Errorf("marshalNilTest[%d]: %v", i, err)
  492. continue
  493. }
  494. if !bytes.Equal(data, test.Data) {
  495. t.Errorf("marshalNilTest[%d]: expected %q, got %q.(%#v)", i, test.Data, data, test.Value)
  496. }
  497. }
  498. }
  499. func TestUnmarshal(t *testing.T) {
  500. for i, test := range marshalTests {
  501. v := reflect.New(reflect.TypeOf(test.Value))
  502. err := Unmarshal(test.Info, test.Data, v.Interface())
  503. if err != nil {
  504. t.Errorf("marshalTest[%d]: %v", i, err)
  505. continue
  506. }
  507. if !reflect.DeepEqual(v.Elem().Interface(), test.Value) {
  508. t.Errorf("marshalTest[%d]: expected %#v, got %#v.", i, test.Value, v.Elem().Interface())
  509. }
  510. }
  511. }
  512. func TestMarshalVarint(t *testing.T) {
  513. varintTests := []struct {
  514. Value interface{}
  515. Marshaled []byte
  516. Unmarshaled *big.Int
  517. }{
  518. {
  519. Value: int8(0),
  520. Marshaled: []byte("\x00"),
  521. Unmarshaled: big.NewInt(0),
  522. },
  523. {
  524. Value: uint8(255),
  525. Marshaled: []byte("\x00\xFF"),
  526. Unmarshaled: big.NewInt(255),
  527. },
  528. {
  529. Value: int8(-1),
  530. Marshaled: []byte("\xFF"),
  531. Unmarshaled: big.NewInt(-1),
  532. },
  533. {
  534. Value: big.NewInt(math.MaxInt32),
  535. Marshaled: []byte("\x7F\xFF\xFF\xFF"),
  536. Unmarshaled: big.NewInt(math.MaxInt32),
  537. },
  538. {
  539. Value: big.NewInt(int64(math.MaxInt32) + 1),
  540. Marshaled: []byte("\x00\x80\x00\x00\x00"),
  541. Unmarshaled: big.NewInt(int64(math.MaxInt32) + 1),
  542. },
  543. {
  544. Value: big.NewInt(math.MinInt32),
  545. Marshaled: []byte("\x80\x00\x00\x00"),
  546. Unmarshaled: big.NewInt(math.MinInt32),
  547. },
  548. {
  549. Value: big.NewInt(int64(math.MinInt32) - 1),
  550. Marshaled: []byte("\xFF\x7F\xFF\xFF\xFF"),
  551. Unmarshaled: big.NewInt(int64(math.MinInt32) - 1),
  552. },
  553. {
  554. Value: math.MinInt64,
  555. Marshaled: []byte("\x80\x00\x00\x00\x00\x00\x00\x00"),
  556. Unmarshaled: big.NewInt(math.MinInt64),
  557. },
  558. {
  559. Value: uint64(math.MaxInt64) + 1,
  560. Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00"),
  561. Unmarshaled: bigintize("9223372036854775808"),
  562. },
  563. {
  564. Value: bigintize("2361183241434822606848"), // 2**71
  565. Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00"),
  566. Unmarshaled: bigintize("2361183241434822606848"),
  567. },
  568. {
  569. Value: bigintize("-9223372036854775809"), // -2**63 - 1
  570. Marshaled: []byte("\xFF\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF"),
  571. Unmarshaled: bigintize("-9223372036854775809"),
  572. },
  573. }
  574. for i, test := range varintTests {
  575. data, err := Marshal(&TypeInfo{Type: TypeVarint}, test.Value)
  576. if err != nil {
  577. t.Errorf("error marshaling varint: %v (test #%d)", err, i)
  578. }
  579. if !bytes.Equal(test.Marshaled, data) {
  580. t.Errorf("marshaled varint mismatch: expected %v, got %v (test #%d)", test.Marshaled, data, i)
  581. }
  582. binder := new(big.Int)
  583. err = Unmarshal(&TypeInfo{Type: TypeVarint}, test.Marshaled, binder)
  584. if err != nil {
  585. t.Errorf("error unmarshaling varint: %v (test #%d)", err, i)
  586. }
  587. if test.Unmarshaled.Cmp(binder) != 0 {
  588. t.Errorf("unmarshaled varint mismatch: expected %v, got %v (test #%d)", test.Unmarshaled, binder, i)
  589. }
  590. }
  591. }
  592. type CustomString string
  593. func (c CustomString) MarshalCQL(info *TypeInfo) ([]byte, error) {
  594. return []byte(strings.ToUpper(string(c))), nil
  595. }
  596. func (c *CustomString) UnmarshalCQL(info *TypeInfo, data []byte) error {
  597. *c = CustomString(strings.ToLower(string(data)))
  598. return nil
  599. }
  600. type MyString string
  601. type MyInt int
  602. var typeLookupTest = []struct {
  603. TypeName string
  604. ExpectedType Type
  605. }{
  606. {"AsciiType", TypeAscii},
  607. {"LongType", TypeBigInt},
  608. {"BytesType", TypeBlob},
  609. {"BooleanType", TypeBoolean},
  610. {"CounterColumnType", TypeCounter},
  611. {"DecimalType", TypeDecimal},
  612. {"DoubleType", TypeDouble},
  613. {"FloatType", TypeFloat},
  614. {"Int32Type", TypeInt},
  615. {"DateType", TypeTimestamp},
  616. {"UUIDType", TypeUUID},
  617. {"UTF8Type", TypeVarchar},
  618. {"IntegerType", TypeVarint},
  619. {"TimeUUIDType", TypeTimeUUID},
  620. {"InetAddressType", TypeInet},
  621. {"MapType", TypeMap},
  622. {"ListType", TypeInet},
  623. {"SetType", TypeInet},
  624. {"unknown", TypeCustom},
  625. }
  626. func testType(t *testing.T, cassType string, expectedType Type) {
  627. if computedType := getApacheCassandraType(apacheCassandraTypePrefix + cassType); computedType != expectedType {
  628. t.Errorf("Cassandra custom type lookup for %s failed. Expected %s, got %s.", cassType, expectedType.String(), computedType.String())
  629. }
  630. }
  631. func TestLookupCassType(t *testing.T) {
  632. for _, lookupTest := range typeLookupTest {
  633. testType(t, lookupTest.TypeName, lookupTest.ExpectedType)
  634. }
  635. }