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.

87 lines
3.2 KiB

  1. package query_test
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. "github.com/tendermint/tendermint/libs/pubsub"
  9. "github.com/tendermint/tendermint/libs/pubsub/query"
  10. )
  11. func TestMatches(t *testing.T) {
  12. var (
  13. txDate = "2017-01-01"
  14. txTime = "2018-05-03T14:45:00Z"
  15. )
  16. testCases := []struct {
  17. s string
  18. tags map[string]string
  19. err bool
  20. matches bool
  21. }{
  22. {"tm.events.type='NewBlock'", map[string]string{"tm.events.type": "NewBlock"}, false, true},
  23. {"tx.gas > 7", map[string]string{"tx.gas": "8"}, false, true},
  24. {"tx.gas > 7 AND tx.gas < 9", map[string]string{"tx.gas": "8"}, false, true},
  25. {"body.weight >= 3.5", map[string]string{"body.weight": "3.5"}, false, true},
  26. {"account.balance < 1000.0", map[string]string{"account.balance": "900"}, false, true},
  27. {"apples.kg <= 4", map[string]string{"apples.kg": "4.0"}, false, true},
  28. {"body.weight >= 4.5", map[string]string{"body.weight": fmt.Sprintf("%v", float32(4.5))}, false, true},
  29. {"oranges.kg < 4 AND watermellons.kg > 10", map[string]string{"oranges.kg": "3", "watermellons.kg": "12"}, false, true},
  30. {"peaches.kg < 4", map[string]string{"peaches.kg": "5"}, false, false},
  31. {"tx.date > DATE 2017-01-01", map[string]string{"tx.date": time.Now().Format(query.DateLayout)}, false, true},
  32. {"tx.date = DATE 2017-01-01", map[string]string{"tx.date": txDate}, false, true},
  33. {"tx.date = DATE 2018-01-01", map[string]string{"tx.date": txDate}, false, false},
  34. {"tx.time >= TIME 2013-05-03T14:45:00Z", map[string]string{"tx.time": time.Now().Format(query.TimeLayout)}, false, true},
  35. {"tx.time = TIME 2013-05-03T14:45:00Z", map[string]string{"tx.time": txTime}, false, false},
  36. {"abci.owner.name CONTAINS 'Igor'", map[string]string{"abci.owner.name": "Igor,Ivan"}, false, true},
  37. {"abci.owner.name CONTAINS 'Igor'", map[string]string{"abci.owner.name": "Pavel,Ivan"}, false, false},
  38. }
  39. for _, tc := range testCases {
  40. q, err := query.New(tc.s)
  41. if !tc.err {
  42. require.Nil(t, err)
  43. }
  44. if tc.matches {
  45. assert.True(t, q.Matches(pubsub.NewTagMap(tc.tags)), "Query '%s' should match %v", tc.s, tc.tags)
  46. } else {
  47. assert.False(t, q.Matches(pubsub.NewTagMap(tc.tags)), "Query '%s' should not match %v", tc.s, tc.tags)
  48. }
  49. }
  50. }
  51. func TestMustParse(t *testing.T) {
  52. assert.Panics(t, func() { query.MustParse("=") })
  53. assert.NotPanics(t, func() { query.MustParse("tm.events.type='NewBlock'") })
  54. }
  55. func TestConditions(t *testing.T) {
  56. txTime, err := time.Parse(time.RFC3339, "2013-05-03T14:45:00Z")
  57. require.NoError(t, err)
  58. testCases := []struct {
  59. s string
  60. conditions []query.Condition
  61. }{
  62. {s: "tm.events.type='NewBlock'", conditions: []query.Condition{query.Condition{Tag: "tm.events.type", Op: query.OpEqual, Operand: "NewBlock"}}},
  63. {s: "tx.gas > 7 AND tx.gas < 9", conditions: []query.Condition{query.Condition{Tag: "tx.gas", Op: query.OpGreater, Operand: int64(7)}, query.Condition{Tag: "tx.gas", Op: query.OpLess, Operand: int64(9)}}},
  64. {s: "tx.time >= TIME 2013-05-03T14:45:00Z", conditions: []query.Condition{query.Condition{Tag: "tx.time", Op: query.OpGreaterEqual, Operand: txTime}}},
  65. }
  66. for _, tc := range testCases {
  67. q, err := query.New(tc.s)
  68. require.Nil(t, err)
  69. assert.Equal(t, tc.conditions, q.Conditions())
  70. }
  71. }