You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
5.4 KiB

  1. package syntax_test
  2. import (
  3. "io"
  4. "reflect"
  5. "strings"
  6. "testing"
  7. "github.com/tendermint/tendermint/internal/pubsub/query/syntax"
  8. )
  9. func TestScanner(t *testing.T) {
  10. tests := []struct {
  11. input string
  12. want []syntax.Token
  13. }{
  14. // Empty inputs
  15. {"", nil},
  16. {" ", nil},
  17. {"\t\n ", nil},
  18. // Numbers
  19. {`0 123`, []syntax.Token{syntax.TNumber, syntax.TNumber}},
  20. {`0.32 3.14`, []syntax.Token{syntax.TNumber, syntax.TNumber}},
  21. // Tags
  22. {`foo foo.bar`, []syntax.Token{syntax.TTag, syntax.TTag}},
  23. // Strings (values)
  24. {` '' x 'x' 'x y'`, []syntax.Token{syntax.TString, syntax.TTag, syntax.TString, syntax.TString}},
  25. {` 'you are not your job' `, []syntax.Token{syntax.TString}},
  26. // Comparison operators
  27. {`< <= = > >=`, []syntax.Token{
  28. syntax.TLt, syntax.TLeq, syntax.TEq, syntax.TGt, syntax.TGeq,
  29. }},
  30. // Mixed values of various kinds.
  31. {`x AND y`, []syntax.Token{syntax.TTag, syntax.TAnd, syntax.TTag}},
  32. {`x.y CONTAINS 'z'`, []syntax.Token{syntax.TTag, syntax.TContains, syntax.TString}},
  33. {`foo EXISTS`, []syntax.Token{syntax.TTag, syntax.TExists}},
  34. {`and AND`, []syntax.Token{syntax.TTag, syntax.TAnd}},
  35. // Timestamp
  36. {`TIME 2021-11-23T15:16:17Z`, []syntax.Token{syntax.TTime}},
  37. // Datestamp
  38. {`DATE 2021-11-23`, []syntax.Token{syntax.TDate}},
  39. }
  40. for _, test := range tests {
  41. s := syntax.NewScanner(strings.NewReader(test.input))
  42. var got []syntax.Token
  43. for s.Next() == nil {
  44. got = append(got, s.Token())
  45. }
  46. if err := s.Err(); err != io.EOF {
  47. t.Errorf("Next: unexpected error: %w", err)
  48. }
  49. if !reflect.DeepEqual(got, test.want) {
  50. t.Logf("Scanner input: %q", test.input)
  51. t.Errorf("Wrong tokens:\ngot: %+v\nwant: %+v", got, test.want)
  52. }
  53. }
  54. }
  55. func TestScannerErrors(t *testing.T) {
  56. tests := []struct {
  57. input string
  58. }{
  59. {`'incomplete string`},
  60. {`-23`},
  61. {`&`},
  62. {`DATE xyz-pdq`},
  63. {`DATE xyzp-dq-zv`},
  64. {`DATE 0000-00-00`},
  65. {`DATE 0000-00-000`},
  66. {`DATE 2021-01-99`},
  67. {`TIME 2021-01-01T34:56:78Z`},
  68. {`TIME 2021-01-99T14:56:08Z`},
  69. {`TIME 2021-01-99T34:56:08`},
  70. {`TIME 2021-01-99T34:56:11+3`},
  71. }
  72. for _, test := range tests {
  73. s := syntax.NewScanner(strings.NewReader(test.input))
  74. if err := s.Next(); err == nil {
  75. t.Errorf("Next: got %v (%#q), want error", s.Token(), s.Text())
  76. }
  77. }
  78. }
  79. // These parser tests were copied from the original implementation of the query
  80. // parser, and are preserved here as a compatibility check.
  81. func TestParseValid(t *testing.T) {
  82. tests := []struct {
  83. input string
  84. valid bool
  85. }{
  86. {"tm.events.type='NewBlock'", true},
  87. {"tm.events.type = 'NewBlock'", true},
  88. {"tm.events.name = ''", true},
  89. {"tm.events.type='TIME'", true},
  90. {"tm.events.type='DATE'", true},
  91. {"tm.events.type='='", true},
  92. {"tm.events.type='TIME", false},
  93. {"tm.events.type=TIME'", false},
  94. {"tm.events.type==", false},
  95. {"tm.events.type=NewBlock", false},
  96. {">==", false},
  97. {"tm.events.type 'NewBlock' =", false},
  98. {"tm.events.type>'NewBlock'", false},
  99. {"", false},
  100. {"=", false},
  101. {"='NewBlock'", false},
  102. {"tm.events.type=", false},
  103. {"tm.events.typeNewBlock", false},
  104. {"tm.events.type'NewBlock'", false},
  105. {"'NewBlock'", false},
  106. {"NewBlock", false},
  107. {"", false},
  108. {"tm.events.type='NewBlock' AND abci.account.name='Igor'", true},
  109. {"tm.events.type='NewBlock' AND", false},
  110. {"tm.events.type='NewBlock' AN", false},
  111. {"tm.events.type='NewBlock' AN tm.events.type='NewBlockHeader'", false},
  112. {"AND tm.events.type='NewBlock' ", false},
  113. {"abci.account.name CONTAINS 'Igor'", true},
  114. {"tx.date > DATE 2013-05-03", true},
  115. {"tx.date < DATE 2013-05-03", true},
  116. {"tx.date <= DATE 2013-05-03", true},
  117. {"tx.date >= DATE 2013-05-03", true},
  118. {"tx.date >= DAT 2013-05-03", false},
  119. {"tx.date <= DATE2013-05-03", false},
  120. {"tx.date <= DATE -05-03", false},
  121. {"tx.date >= DATE 20130503", false},
  122. {"tx.date >= DATE 2013+01-03", false},
  123. // incorrect year, month, day
  124. {"tx.date >= DATE 0013-01-03", false},
  125. {"tx.date >= DATE 2013-31-03", false},
  126. {"tx.date >= DATE 2013-01-83", false},
  127. {"tx.date > TIME 2013-05-03T14:45:00+07:00", true},
  128. {"tx.date < TIME 2013-05-03T14:45:00-02:00", true},
  129. {"tx.date <= TIME 2013-05-03T14:45:00Z", true},
  130. {"tx.date >= TIME 2013-05-03T14:45:00Z", true},
  131. {"tx.date >= TIME2013-05-03T14:45:00Z", false},
  132. {"tx.date = IME 2013-05-03T14:45:00Z", false},
  133. {"tx.date = TIME 2013-05-:45:00Z", false},
  134. {"tx.date >= TIME 2013-05-03T14:45:00", false},
  135. {"tx.date >= TIME 0013-00-00T14:45:00Z", false},
  136. {"tx.date >= TIME 2013+05=03T14:45:00Z", false},
  137. {"account.balance=100", true},
  138. {"account.balance >= 200", true},
  139. {"account.balance >= -300", false},
  140. {"account.balance >>= 400", false},
  141. {"account.balance=33.22.1", false},
  142. {"slashing.amount EXISTS", true},
  143. {"slashing.amount EXISTS AND account.balance=100", true},
  144. {"account.balance=100 AND slashing.amount EXISTS", true},
  145. {"slashing EXISTS", true},
  146. {"hash='136E18F7E4C348B780CF873A0BF43922E5BAFA63'", true},
  147. {"hash=136E18F7E4C348B780CF873A0BF43922E5BAFA63", false},
  148. }
  149. for _, test := range tests {
  150. q, err := syntax.Parse(test.input)
  151. if test.valid != (err == nil) {
  152. t.Errorf("Parse %#q: valid %v got err=%v", test.input, test.valid, err)
  153. }
  154. // For valid queries, check that the query round-trips.
  155. if test.valid {
  156. qstr := q.String()
  157. r, err := syntax.Parse(qstr)
  158. if err != nil {
  159. t.Errorf("Reparse %#q failed: %v", qstr, err)
  160. }
  161. if rstr := r.String(); rstr != qstr {
  162. t.Errorf("Reparse diff\nold: %#q\nnew: %#q", qstr, rstr)
  163. }
  164. }
  165. }
  166. }