sourceStmt_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2021 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. }