marshal_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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. &TypeInfo{Type: TypeInt},
  316. []byte(nil),
  317. nil,
  318. },
  319. {
  320. &TypeInfo{Type: TypeVarchar},
  321. []byte("nullable string"),
  322. func() *string {
  323. value := "nullable string"
  324. return &value
  325. }(),
  326. },
  327. {
  328. &TypeInfo{Type: TypeVarchar},
  329. []byte{},
  330. (*string)(nil),
  331. },
  332. {
  333. &TypeInfo{Type: TypeInt},
  334. []byte("\x7f\xff\xff\xff"),
  335. func() *int {
  336. var value int = math.MaxInt32
  337. return &value
  338. }(),
  339. },
  340. {
  341. &TypeInfo{Type: TypeInt},
  342. []byte(nil),
  343. (*int)(nil),
  344. },
  345. {
  346. &TypeInfo{Type: TypeTimeUUID},
  347. []byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
  348. &UUID{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
  349. },
  350. {
  351. &TypeInfo{Type: TypeTimeUUID},
  352. []byte{},
  353. (*UUID)(nil),
  354. },
  355. {
  356. &TypeInfo{Type: TypeTimestamp},
  357. []byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
  358. func() *time.Time {
  359. t := time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC)
  360. return &t
  361. }(),
  362. },
  363. {
  364. &TypeInfo{Type: TypeTimestamp},
  365. []byte(nil),
  366. (*time.Time)(nil),
  367. },
  368. {
  369. &TypeInfo{Type: TypeBoolean},
  370. []byte("\x00"),
  371. func() *bool {
  372. b := false
  373. return &b
  374. }(),
  375. },
  376. {
  377. &TypeInfo{Type: TypeBoolean},
  378. []byte("\x01"),
  379. func() *bool {
  380. b := true
  381. return &b
  382. }(),
  383. },
  384. {
  385. &TypeInfo{Type: TypeBoolean},
  386. []byte(nil),
  387. (*bool)(nil),
  388. },
  389. {
  390. &TypeInfo{Type: TypeFloat},
  391. []byte("\x40\x49\x0f\xdb"),
  392. func() *float32 {
  393. f := float32(3.14159265)
  394. return &f
  395. }(),
  396. },
  397. {
  398. &TypeInfo{Type: TypeFloat},
  399. []byte(nil),
  400. (*float32)(nil),
  401. },
  402. {
  403. &TypeInfo{Type: TypeDouble},
  404. []byte("\x40\x09\x21\xfb\x53\xc8\xd4\xf1"),
  405. func() *float64 {
  406. d := float64(3.14159265)
  407. return &d
  408. }(),
  409. },
  410. {
  411. &TypeInfo{Type: TypeDouble},
  412. []byte(nil),
  413. (*float64)(nil),
  414. },
  415. {
  416. &TypeInfo{Type: TypeInet},
  417. []byte("\x7F\x00\x00\x01"),
  418. func() *net.IP {
  419. ip := net.ParseIP("127.0.0.1").To4()
  420. return &ip
  421. }(),
  422. },
  423. {
  424. &TypeInfo{Type: TypeInet},
  425. []byte(nil),
  426. (*net.IP)(nil),
  427. },
  428. {
  429. &TypeInfo{Type: TypeList, Elem: &TypeInfo{Type: TypeInt}},
  430. []byte("\x00\x02\x00\x04\x00\x00\x00\x01\x00\x04\x00\x00\x00\x02"),
  431. func() *[]int {
  432. l := []int{1, 2}
  433. return &l
  434. }(),
  435. },
  436. {
  437. &TypeInfo{Type: TypeList, Elem: &TypeInfo{Type: TypeInt}},
  438. []byte(nil),
  439. (*[]int)(nil),
  440. },
  441. {
  442. &TypeInfo{Type: TypeMap,
  443. Key: &TypeInfo{Type: TypeVarchar},
  444. Elem: &TypeInfo{Type: TypeInt},
  445. },
  446. []byte("\x00\x01\x00\x03foo\x00\x04\x00\x00\x00\x01"),
  447. func() *map[string]int {
  448. m := map[string]int{"foo": 1}
  449. return &m
  450. }(),
  451. },
  452. {
  453. &TypeInfo{Type: TypeMap,
  454. Key: &TypeInfo{Type: TypeVarchar},
  455. Elem: &TypeInfo{Type: TypeInt},
  456. },
  457. []byte(nil),
  458. (*map[string]int)(nil),
  459. },
  460. }
  461. func decimalize(s string) *inf.Dec {
  462. i, _ := new(inf.Dec).SetString(s)
  463. return i
  464. }
  465. func bigintize(s string) *big.Int {
  466. i, _ := new(big.Int).SetString(s, 10)
  467. return i
  468. }
  469. func TestMarshal(t *testing.T) {
  470. for i, test := range marshalTests {
  471. data, err := Marshal(test.Info, test.Value)
  472. if err != nil {
  473. t.Errorf("marshalTest[%d]: %v", i, err)
  474. continue
  475. }
  476. if !bytes.Equal(data, test.Data) {
  477. t.Errorf("marshalTest[%d]: expected %q, got %q (%#v)", i, test.Data, data, test.Value)
  478. }
  479. }
  480. }
  481. func TestUnmarshal(t *testing.T) {
  482. for i, test := range marshalTests {
  483. if test.Value != nil {
  484. v := reflect.New(reflect.TypeOf(test.Value))
  485. err := Unmarshal(test.Info, test.Data, v.Interface())
  486. if err != nil {
  487. t.Errorf("unmarshalTest[%d]: %v", i, err)
  488. continue
  489. }
  490. if !reflect.DeepEqual(v.Elem().Interface(), test.Value) {
  491. t.Errorf("unmarshalTest[%d]: expected %#v, got %#v.", i, test.Value, v.Elem().Interface())
  492. }
  493. } else {
  494. if err := Unmarshal(test.Info, test.Data, test.Value); nil == err {
  495. t.Errorf("unmarshalTest[%d]: %#v not return error.", i, test.Value)
  496. }
  497. }
  498. }
  499. }
  500. func TestMarshalVarint(t *testing.T) {
  501. varintTests := []struct {
  502. Value interface{}
  503. Marshaled []byte
  504. Unmarshaled *big.Int
  505. }{
  506. {
  507. Value: int8(0),
  508. Marshaled: []byte("\x00"),
  509. Unmarshaled: big.NewInt(0),
  510. },
  511. {
  512. Value: uint8(255),
  513. Marshaled: []byte("\x00\xFF"),
  514. Unmarshaled: big.NewInt(255),
  515. },
  516. {
  517. Value: int8(-1),
  518. Marshaled: []byte("\xFF"),
  519. Unmarshaled: big.NewInt(-1),
  520. },
  521. {
  522. Value: big.NewInt(math.MaxInt32),
  523. Marshaled: []byte("\x7F\xFF\xFF\xFF"),
  524. Unmarshaled: big.NewInt(math.MaxInt32),
  525. },
  526. {
  527. Value: big.NewInt(int64(math.MaxInt32) + 1),
  528. Marshaled: []byte("\x00\x80\x00\x00\x00"),
  529. Unmarshaled: big.NewInt(int64(math.MaxInt32) + 1),
  530. },
  531. {
  532. Value: big.NewInt(math.MinInt32),
  533. Marshaled: []byte("\x80\x00\x00\x00"),
  534. Unmarshaled: big.NewInt(math.MinInt32),
  535. },
  536. {
  537. Value: big.NewInt(int64(math.MinInt32) - 1),
  538. Marshaled: []byte("\xFF\x7F\xFF\xFF\xFF"),
  539. Unmarshaled: big.NewInt(int64(math.MinInt32) - 1),
  540. },
  541. {
  542. Value: math.MinInt64,
  543. Marshaled: []byte("\x80\x00\x00\x00\x00\x00\x00\x00"),
  544. Unmarshaled: big.NewInt(math.MinInt64),
  545. },
  546. {
  547. Value: uint64(math.MaxInt64) + 1,
  548. Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00"),
  549. Unmarshaled: bigintize("9223372036854775808"),
  550. },
  551. {
  552. Value: bigintize("2361183241434822606848"), // 2**71
  553. Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00"),
  554. Unmarshaled: bigintize("2361183241434822606848"),
  555. },
  556. {
  557. Value: bigintize("-9223372036854775809"), // -2**63 - 1
  558. Marshaled: []byte("\xFF\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF"),
  559. Unmarshaled: bigintize("-9223372036854775809"),
  560. },
  561. }
  562. for i, test := range varintTests {
  563. data, err := Marshal(&TypeInfo{Type: TypeVarint}, test.Value)
  564. if err != nil {
  565. t.Errorf("error marshaling varint: %v (test #%d)", err, i)
  566. }
  567. if !bytes.Equal(test.Marshaled, data) {
  568. t.Errorf("marshaled varint mismatch: expected %v, got %v (test #%d)", test.Marshaled, data, i)
  569. }
  570. binder := new(big.Int)
  571. err = Unmarshal(&TypeInfo{Type: TypeVarint}, test.Marshaled, binder)
  572. if err != nil {
  573. t.Errorf("error unmarshaling varint: %v (test #%d)", err, i)
  574. }
  575. if test.Unmarshaled.Cmp(binder) != 0 {
  576. t.Errorf("unmarshaled varint mismatch: expected %v, got %v (test #%d)", test.Unmarshaled, binder, i)
  577. }
  578. }
  579. }
  580. type CustomString string
  581. func (c CustomString) MarshalCQL(info *TypeInfo) ([]byte, error) {
  582. return []byte(strings.ToUpper(string(c))), nil
  583. }
  584. func (c *CustomString) UnmarshalCQL(info *TypeInfo, data []byte) error {
  585. *c = CustomString(strings.ToLower(string(data)))
  586. return nil
  587. }
  588. type MyString string
  589. type MyInt int
  590. var typeLookupTest = []struct {
  591. TypeName string
  592. ExpectedType Type
  593. }{
  594. {"AsciiType", TypeAscii},
  595. {"LongType", TypeBigInt},
  596. {"BytesType", TypeBlob},
  597. {"BooleanType", TypeBoolean},
  598. {"CounterColumnType", TypeCounter},
  599. {"DecimalType", TypeDecimal},
  600. {"DoubleType", TypeDouble},
  601. {"FloatType", TypeFloat},
  602. {"Int32Type", TypeInt},
  603. {"DateType", TypeTimestamp},
  604. {"UUIDType", TypeUUID},
  605. {"UTF8Type", TypeVarchar},
  606. {"IntegerType", TypeVarint},
  607. {"TimeUUIDType", TypeTimeUUID},
  608. {"InetAddressType", TypeInet},
  609. {"MapType", TypeMap},
  610. {"ListType", TypeInet},
  611. {"SetType", TypeInet},
  612. {"unknown", TypeCustom},
  613. }
  614. func testType(t *testing.T, cassType string, expectedType Type) {
  615. if computedType := getApacheCassandraType(apacheCassandraTypePrefix + cassType); computedType != expectedType {
  616. t.Errorf("Cassandra custom type lookup for %s failed. Expected %s, got %s.", cassType, expectedType.String(), computedType.String())
  617. }
  618. }
  619. func TestLookupCassType(t *testing.T) {
  620. for _, lookupTest := range typeLookupTest {
  621. testType(t, lookupTest.TypeName, lookupTest.ExpectedType)
  622. }
  623. }