ast_test.go 4.1 KB

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