util_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package xsql
  2. import (
  3. "fmt"
  4. "reflect"
  5. "testing"
  6. )
  7. func TestLowercaseKeyMap(t *testing.T) {
  8. var tests = []struct {
  9. src map[string]interface{}
  10. dest map[string]interface{}
  11. }{
  12. {
  13. src: map[string]interface{}{
  14. "Key1": "value1",
  15. "key2": "value2",
  16. },
  17. dest: map[string]interface{}{
  18. "key1": "value1",
  19. "key2": "value2",
  20. },
  21. },
  22. {
  23. src: map[string]interface{}{
  24. "Key1": "value1",
  25. "Complex": map[string]interface{}{
  26. "Sub1": "sub_value1",
  27. },
  28. },
  29. dest: map[string]interface{}{
  30. "key1": "value1",
  31. "complex": map[string]interface{}{
  32. "sub1": "sub_value1",
  33. },
  34. },
  35. },
  36. {
  37. src: map[string]interface{}{
  38. "Key1": "value1",
  39. "Complex": map[string]interface{}{
  40. "Sub1": "sub_value1",
  41. "Sub1_2": map[string]interface{}{
  42. "Sub2": "sub2",
  43. },
  44. },
  45. },
  46. dest: map[string]interface{}{
  47. "key1": "value1",
  48. "complex": map[string]interface{}{
  49. "sub1": "sub_value1",
  50. "sub1_2": map[string]interface{}{
  51. "sub2": "sub2",
  52. },
  53. },
  54. },
  55. },
  56. }
  57. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  58. for i, tt := range tests {
  59. result := LowercaseKeyMap(tt.src)
  60. if !reflect.DeepEqual(tt.dest, result) {
  61. t.Errorf("%d. \nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.dest, result)
  62. }
  63. }
  64. }
  65. func TestPrintFieldType(t *testing.T) {
  66. var tests = []struct {
  67. ft FieldType
  68. printed string
  69. }{{
  70. ft: &RecType{
  71. StreamFields: []StreamField{
  72. {Name: "STREET_NAME", FieldType: &BasicType{Type: STRINGS}},
  73. {Name: "NUMBER", FieldType: &BasicType{Type: BIGINT}},
  74. },
  75. },
  76. printed: `{"Type":"struct","Fields":[{"FieldType":"string","Name":"STREET_NAME"},{"FieldType":"bigint","Name":"NUMBER"}]}`,
  77. }, {
  78. ft: &ArrayType{
  79. Type: STRUCT,
  80. FieldType: &RecType{
  81. StreamFields: []StreamField{
  82. {Name: "STREET_NAME", FieldType: &BasicType{Type: STRINGS}},
  83. {Name: "NUMBER", FieldType: &BasicType{Type: BIGINT}},
  84. },
  85. },
  86. },
  87. printed: `{"Type":"array","ElementType":{"Type":"struct","Fields":[{"FieldType":"string","Name":"STREET_NAME"},{"FieldType":"bigint","Name":"NUMBER"}]}}`,
  88. }, {
  89. ft: &ArrayType{
  90. Type: STRUCT,
  91. FieldType: &BasicType{Type: STRINGS},
  92. },
  93. printed: `{"Type":"array","ElementType":"string"}`,
  94. }, {
  95. ft: &BasicType{
  96. Type: STRINGS,
  97. },
  98. printed: `string`,
  99. }}
  100. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  101. for i, tt := range tests {
  102. //fmt.Printf("Parsing SQL %q.\n",tt.s)
  103. result, _ := doPrintFieldTypeForJson(tt.ft)
  104. if !reflect.DeepEqual(tt.printed, result) {
  105. t.Errorf("%d. \nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.printed, result)
  106. }
  107. }
  108. }