ast_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 xsql
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "github.com/lf-edge/ekuiper/pkg/ast"
  19. "reflect"
  20. "testing"
  21. )
  22. func Test_MessageValTest(t *testing.T) {
  23. var tests = []struct {
  24. key string
  25. message Message
  26. exptV interface{}
  27. exptOk bool
  28. }{
  29. {
  30. key: "key1",
  31. message: Message{
  32. "key1": "val1",
  33. "key2": "val2",
  34. },
  35. exptV: "val1",
  36. exptOk: true,
  37. },
  38. {
  39. key: "key0",
  40. message: Message{
  41. "key1": "val1",
  42. "key2": "val2",
  43. },
  44. exptV: nil,
  45. exptOk: false,
  46. },
  47. {
  48. key: "key1",
  49. message: Message{
  50. "Key1": "val1",
  51. "key2": "val2",
  52. },
  53. exptV: "val1",
  54. exptOk: true,
  55. },
  56. {
  57. key: "key1" + ast.COLUMN_SEPARATOR + "subkey",
  58. message: Message{
  59. "Key1": "val1",
  60. "subkey": "subval",
  61. },
  62. exptV: "subval",
  63. exptOk: true,
  64. },
  65. {
  66. key: "192.168.0.1",
  67. message: Message{
  68. "Key1": "val1",
  69. "192.168.0.1": "000",
  70. },
  71. exptV: "000",
  72. exptOk: true,
  73. },
  74. {
  75. key: "parent" + ast.COLUMN_SEPARATOR + "child",
  76. message: Message{
  77. "key1": "val1",
  78. "child": "child_val",
  79. "parent.child": "demo",
  80. },
  81. exptV: "child_val",
  82. exptOk: true,
  83. },
  84. {
  85. key: "parent.child",
  86. message: Message{
  87. "key1": "val1",
  88. "child": "child_val",
  89. "parent.child": "demo",
  90. },
  91. exptV: "demo",
  92. exptOk: true,
  93. },
  94. {
  95. key: "parent.Child",
  96. message: Message{
  97. "key1": "val1",
  98. "child": "child_val",
  99. "parent.child": "demo",
  100. },
  101. exptV: "demo",
  102. exptOk: true,
  103. },
  104. }
  105. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  106. for i, tt := range tests {
  107. //fmt.Printf("Parsing SQL %q.\n", tt.s)
  108. v, ok := tt.message.Value(tt.key)
  109. if tt.exptOk != ok {
  110. t.Errorf("%d. error mismatch:\n exp=%t\n got=%t\n\n", i, tt.exptOk, ok)
  111. } else if tt.exptOk && !reflect.DeepEqual(tt.exptV, v) {
  112. t.Errorf("%d. \n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.exptV, v)
  113. }
  114. }
  115. }
  116. func Test_StreamFieldsMarshall(t *testing.T) {
  117. var tests = []struct {
  118. sf ast.StreamFields
  119. r string
  120. }{{
  121. sf: []ast.StreamField{
  122. {Name: "USERID", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  123. {Name: "FIRST_NAME", FieldType: &ast.BasicType{Type: ast.STRINGS}},
  124. {Name: "LAST_NAME", FieldType: &ast.BasicType{Type: ast.STRINGS}},
  125. {Name: "NICKNAMES", FieldType: &ast.ArrayType{Type: ast.STRINGS}},
  126. {Name: "Gender", FieldType: &ast.BasicType{Type: ast.BOOLEAN}},
  127. {Name: "ADDRESS", FieldType: &ast.RecType{
  128. StreamFields: []ast.StreamField{
  129. {Name: "STREET_NAME", FieldType: &ast.BasicType{Type: ast.STRINGS}},
  130. {Name: "NUMBER", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  131. },
  132. }},
  133. },
  134. r: `[{"FieldType":"bigint","Name":"USERID"},{"FieldType":"string","Name":"FIRST_NAME"},{"FieldType":"string","Name":"LAST_NAME"},{"FieldType":{"Type":"array","ElementType":"string"},"Name":"NICKNAMES"},{"FieldType":"boolean","Name":"Gender"},{"FieldType":{"Type":"struct","Fields":[{"FieldType":"string","Name":"STREET_NAME"},{"FieldType":"bigint","Name":"NUMBER"}]},"Name":"ADDRESS"}]`,
  135. }, {
  136. sf: []ast.StreamField{
  137. {Name: "USERID", FieldType: &ast.BasicType{Type: ast.BIGINT}},
  138. },
  139. r: `[{"FieldType":"bigint","Name":"USERID"}]`,
  140. }}
  141. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  142. for i, tt := range tests {
  143. r, err := json.Marshal(tt.sf)
  144. if err != nil {
  145. t.Errorf("%d. \nmarshall error: %v", i, err)
  146. t.FailNow()
  147. }
  148. result := string(r)
  149. if !reflect.DeepEqual(tt.r, result) {
  150. t.Errorf("%d. \nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.r, result)
  151. }
  152. }
  153. }