ast_test.go 3.4 KB

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