helpers_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package gocql
  2. import (
  3. "reflect"
  4. "testing"
  5. )
  6. func TestGetCassandraType_Set(t *testing.T) {
  7. typ := getCassandraType("set<text>")
  8. set, ok := typ.(CollectionType)
  9. if !ok {
  10. t.Fatalf("expected CollectionType got %T", typ)
  11. } else if set.typ != TypeSet {
  12. t.Fatalf("expected type %v got %v", TypeSet, set.typ)
  13. }
  14. inner, ok := set.Elem.(NativeType)
  15. if !ok {
  16. t.Fatalf("expected to get NativeType got %T", set.Elem)
  17. } else if inner.typ != TypeText {
  18. t.Fatalf("expected to get %v got %v for set value", TypeText, set.typ)
  19. }
  20. }
  21. func TestGetCassandraType(t *testing.T) {
  22. tests := []struct {
  23. input string
  24. exp TypeInfo
  25. }{
  26. {
  27. "set<text>", CollectionType{
  28. NativeType: NativeType{typ: TypeSet},
  29. Elem: NativeType{typ: TypeText},
  30. },
  31. },
  32. {
  33. "map<text, varchar>", CollectionType{
  34. NativeType: NativeType{typ: TypeMap},
  35. Key: NativeType{typ: TypeText},
  36. Elem: NativeType{typ: TypeVarchar},
  37. },
  38. },
  39. {
  40. "list<int>", CollectionType{
  41. NativeType: NativeType{typ: TypeList},
  42. Elem: NativeType{typ: TypeInt},
  43. },
  44. },
  45. {
  46. "tuple<int, int, text>", TupleTypeInfo{
  47. NativeType: NativeType{typ: TypeTuple},
  48. Elems: []TypeInfo{
  49. NativeType{typ: TypeInt},
  50. NativeType{typ: TypeInt},
  51. NativeType{typ: TypeText},
  52. },
  53. },
  54. },
  55. {
  56. "frozen<map<text, frozen<list<frozen<tuple<int, int>>>>>>", CollectionType{
  57. NativeType: NativeType{typ: TypeMap},
  58. Key: NativeType{typ: TypeText},
  59. Elem: CollectionType{
  60. NativeType: NativeType{typ: TypeList},
  61. Elem: TupleTypeInfo{
  62. NativeType: NativeType{typ: TypeTuple},
  63. Elems: []TypeInfo{
  64. NativeType{typ: TypeInt},
  65. NativeType{typ: TypeInt},
  66. },
  67. },
  68. },
  69. },
  70. },
  71. }
  72. for _, test := range tests {
  73. t.Run(test.input, func(t *testing.T) {
  74. got := getCassandraType(test.input)
  75. // TODO(zariel): define an equal method on the types?
  76. if !reflect.DeepEqual(got, test.exp) {
  77. t.Fatalf("expected %v got %v", test.exp, got)
  78. }
  79. })
  80. }
  81. }