sourceStmt_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2021-2022 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package ast
  15. import (
  16. "fmt"
  17. "reflect"
  18. "testing"
  19. )
  20. func TestPrintFieldType(t *testing.T) {
  21. var tests = []struct {
  22. ft FieldType
  23. printed string
  24. }{{
  25. ft: &RecType{
  26. StreamFields: []StreamField{
  27. {Name: "STREET_NAME", FieldType: &BasicType{Type: STRINGS}},
  28. {Name: "NUMBER", FieldType: &BasicType{Type: BIGINT}},
  29. },
  30. },
  31. printed: `{"Type":"struct","Fields":[{"FieldType":"string","Name":"STREET_NAME"},{"FieldType":"bigint","Name":"NUMBER"}]}`,
  32. }, {
  33. ft: &ArrayType{
  34. Type: STRUCT,
  35. FieldType: &RecType{
  36. StreamFields: []StreamField{
  37. {Name: "STREET_NAME", FieldType: &BasicType{Type: STRINGS}},
  38. {Name: "NUMBER", FieldType: &BasicType{Type: BIGINT}},
  39. },
  40. },
  41. },
  42. printed: `{"Type":"array","ElementType":{"Type":"struct","Fields":[{"FieldType":"string","Name":"STREET_NAME"},{"FieldType":"bigint","Name":"NUMBER"}]}}`,
  43. }, {
  44. ft: &ArrayType{
  45. Type: STRUCT,
  46. FieldType: &BasicType{Type: STRINGS},
  47. },
  48. printed: `{"Type":"array","ElementType":"string"}`,
  49. }, {
  50. ft: &BasicType{
  51. Type: STRINGS,
  52. },
  53. printed: `string`,
  54. }}
  55. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  56. for i, tt := range tests {
  57. //fmt.Printf("Parsing SQL %q.\n",tt.s)
  58. result, _ := doPrintFieldTypeForJson(tt.ft)
  59. if !reflect.DeepEqual(tt.printed, result) {
  60. t.Errorf("%d. \nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.printed, result)
  61. }
  62. }
  63. }
  64. func TestToJsonFields(t *testing.T) {
  65. var tests = []struct {
  66. input StreamFields
  67. output map[string]*JsonStreamField
  68. }{
  69. {
  70. input: StreamFields{
  71. {Name: "STREET_NAME", FieldType: &BasicType{Type: STRINGS}},
  72. },
  73. output: map[string]*JsonStreamField{
  74. "STREET_NAME": {
  75. Type: "string",
  76. },
  77. },
  78. }, {
  79. input: []StreamField{
  80. {Name: "USERID", FieldType: &BasicType{Type: BIGINT}},
  81. {Name: "FIRST_NAME", FieldType: &BasicType{Type: STRINGS}},
  82. {Name: "LAST_NAME", FieldType: &BasicType{Type: STRINGS}},
  83. {Name: "NICKNAMES", FieldType: &ArrayType{Type: STRINGS}},
  84. {Name: "data", FieldType: &BasicType{Type: BYTEA}},
  85. {Name: "Gender", FieldType: &BasicType{Type: BOOLEAN}},
  86. {Name: "ADDRESS", FieldType: &RecType{
  87. StreamFields: []StreamField{
  88. {Name: "STREET_NAME", FieldType: &BasicType{Type: STRINGS}},
  89. {Name: "NUMBER", FieldType: &BasicType{Type: BIGINT}},
  90. },
  91. }},
  92. },
  93. output: map[string]*JsonStreamField{
  94. "USERID": {Type: "bigint"},
  95. "FIRST_NAME": {Type: "string"},
  96. "LAST_NAME": {Type: "string"},
  97. "NICKNAMES": {Type: "array", Items: &JsonStreamField{Type: "string"}},
  98. "data": {Type: "bytea"},
  99. "Gender": {Type: "boolean"},
  100. "ADDRESS": {Type: "struct", Properties: map[string]*JsonStreamField{
  101. "STREET_NAME": {Type: "string"},
  102. "NUMBER": {Type: "bigint"},
  103. }},
  104. },
  105. }, {
  106. input: []StreamField{
  107. {Name: "ADDRESSES", FieldType: &ArrayType{
  108. Type: STRUCT,
  109. FieldType: &RecType{
  110. StreamFields: []StreamField{
  111. {Name: "STREET_NAME", FieldType: &BasicType{Type: STRINGS}},
  112. {Name: "NUMBER", FieldType: &BasicType{Type: BIGINT}},
  113. },
  114. },
  115. }},
  116. },
  117. output: map[string]*JsonStreamField{
  118. "ADDRESSES": {Type: "array", Items: &JsonStreamField{
  119. Type: "struct", Properties: map[string]*JsonStreamField{
  120. "STREET_NAME": {Type: "string"},
  121. "NUMBER": {Type: "bigint"},
  122. }},
  123. },
  124. },
  125. },
  126. }
  127. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  128. for i, tt := range tests {
  129. result := tt.input.ToJsonSchema()
  130. if !reflect.DeepEqual(tt.output, result) {
  131. t.Errorf("%d. \nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.output, result)
  132. }
  133. }
  134. }