sourceStmt_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. "reflect"
  17. "testing"
  18. )
  19. func TestPrintFieldType(t *testing.T) {
  20. tests := []struct {
  21. ft FieldType
  22. printed string
  23. }{{
  24. ft: &RecType{
  25. StreamFields: []StreamField{
  26. {Name: "STREET_NAME", FieldType: &BasicType{Type: STRINGS}},
  27. {Name: "NUMBER", FieldType: &BasicType{Type: BIGINT}},
  28. },
  29. },
  30. printed: `{"Type":"struct","Fields":[{"FieldType":"string","Name":"STREET_NAME"},{"FieldType":"bigint","Name":"NUMBER"}]}`,
  31. }, {
  32. ft: &ArrayType{
  33. Type: STRUCT,
  34. FieldType: &RecType{
  35. StreamFields: []StreamField{
  36. {Name: "STREET_NAME", FieldType: &BasicType{Type: STRINGS}},
  37. {Name: "NUMBER", FieldType: &BasicType{Type: BIGINT}},
  38. },
  39. },
  40. },
  41. printed: `{"Type":"array","ElementType":{"Type":"struct","Fields":[{"FieldType":"string","Name":"STREET_NAME"},{"FieldType":"bigint","Name":"NUMBER"}]}}`,
  42. }, {
  43. ft: &ArrayType{
  44. Type: STRUCT,
  45. FieldType: &BasicType{Type: STRINGS},
  46. },
  47. printed: `{"Type":"array","ElementType":"string"}`,
  48. }, {
  49. ft: &BasicType{
  50. Type: STRINGS,
  51. },
  52. printed: `string`,
  53. }}
  54. t.Logf("The test bucket size is %d.", len(tests))
  55. for i, tt := range tests {
  56. // t.Logf("Parsing SQL %q.",tt.s)
  57. result, _ := doPrintFieldTypeForJson(tt.ft)
  58. if !reflect.DeepEqual(tt.printed, result) {
  59. t.Errorf("%d. \nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.printed, result)
  60. }
  61. }
  62. }
  63. func TestToJsonFields(t *testing.T) {
  64. tests := []struct {
  65. input StreamFields
  66. output map[string]*JsonStreamField
  67. }{
  68. {
  69. input: StreamFields{
  70. {Name: "STREET_NAME", FieldType: &BasicType{Type: STRINGS}},
  71. },
  72. output: map[string]*JsonStreamField{
  73. "STREET_NAME": {
  74. Type: "string",
  75. },
  76. },
  77. }, {
  78. input: []StreamField{
  79. {Name: "USERID", FieldType: &BasicType{Type: BIGINT}},
  80. {Name: "FIRST_NAME", FieldType: &BasicType{Type: STRINGS}},
  81. {Name: "LAST_NAME", FieldType: &BasicType{Type: STRINGS}},
  82. {Name: "NICKNAMES", FieldType: &ArrayType{Type: STRINGS}},
  83. {Name: "data", FieldType: &BasicType{Type: BYTEA}},
  84. {Name: "Gender", FieldType: &BasicType{Type: BOOLEAN}},
  85. {Name: "ADDRESS", FieldType: &RecType{
  86. StreamFields: []StreamField{
  87. {Name: "STREET_NAME", FieldType: &BasicType{Type: STRINGS}},
  88. {Name: "NUMBER", FieldType: &BasicType{Type: BIGINT}},
  89. },
  90. }},
  91. },
  92. output: map[string]*JsonStreamField{
  93. "USERID": {Type: "bigint"},
  94. "FIRST_NAME": {Type: "string"},
  95. "LAST_NAME": {Type: "string"},
  96. "NICKNAMES": {Type: "array", Items: &JsonStreamField{Type: "string"}},
  97. "data": {Type: "bytea"},
  98. "Gender": {Type: "boolean"},
  99. "ADDRESS": {Type: "struct", Properties: map[string]*JsonStreamField{
  100. "STREET_NAME": {Type: "string"},
  101. "NUMBER": {Type: "bigint"},
  102. }},
  103. },
  104. }, {
  105. input: []StreamField{
  106. {Name: "ADDRESSES", FieldType: &ArrayType{
  107. Type: STRUCT,
  108. FieldType: &RecType{
  109. StreamFields: []StreamField{
  110. {Name: "STREET_NAME", FieldType: &BasicType{Type: STRINGS}},
  111. {Name: "NUMBER", FieldType: &BasicType{Type: BIGINT}},
  112. },
  113. },
  114. }},
  115. },
  116. output: map[string]*JsonStreamField{
  117. "ADDRESSES": {
  118. 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. }
  128. t.Logf("The test bucket size is %d.", len(tests))
  129. for i, tt := range tests {
  130. result := tt.input.ToJsonSchema()
  131. if !reflect.DeepEqual(tt.output, result) {
  132. t.Errorf("%d. \nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.output, result)
  133. }
  134. }
  135. }