7 "github.com/stretchr/testify/assert"
8 "github.com/stretchr/testify/require"
9 "github.com/tendermint/tmlibs/pubsub/query"
12 func TestMatches(t *testing.T) {
13 const shortForm = "2006-Jan-02"
14 txDate, err := time.Parse(shortForm, "2017-Jan-01")
15 require.NoError(t, err)
16 txTime, err := time.Parse(time.RFC3339, "2018-05-03T14:45:00Z")
17 require.NoError(t, err)
19 testCases := []struct {
21 tags map[string]interface{}
25 {"tm.events.type='NewBlock'", map[string]interface{}{"tm.events.type": "NewBlock"}, false, true},
27 {"tx.gas > 7", map[string]interface{}{"tx.gas": 8}, false, true},
28 {"tx.gas > 7 AND tx.gas < 9", map[string]interface{}{"tx.gas": 8}, false, true},
29 {"body.weight >= 3.5", map[string]interface{}{"body.weight": 3.5}, false, true},
30 {"account.balance < 1000.0", map[string]interface{}{"account.balance": 900}, false, true},
31 {"apples.kg <= 4", map[string]interface{}{"apples.kg": 4.0}, false, true},
32 {"body.weight >= 4.5", map[string]interface{}{"body.weight": float32(4.5)}, false, true},
33 {"oranges.kg < 4 AND watermellons.kg > 10", map[string]interface{}{"oranges.kg": 3, "watermellons.kg": 12}, false, true},
34 {"peaches.kg < 4", map[string]interface{}{"peaches.kg": 5}, false, false},
36 {"tx.date > DATE 2017-01-01", map[string]interface{}{"tx.date": time.Now()}, false, true},
37 {"tx.date = DATE 2017-01-01", map[string]interface{}{"tx.date": txDate}, false, true},
38 {"tx.date = DATE 2018-01-01", map[string]interface{}{"tx.date": txDate}, false, false},
40 {"tx.time >= TIME 2013-05-03T14:45:00Z", map[string]interface{}{"tx.time": time.Now()}, false, true},
41 {"tx.time = TIME 2013-05-03T14:45:00Z", map[string]interface{}{"tx.time": txTime}, false, false},
43 {"abci.owner.name CONTAINS 'Igor'", map[string]interface{}{"abci.owner.name": "Igor,Ivan"}, false, true},
44 {"abci.owner.name CONTAINS 'Igor'", map[string]interface{}{"abci.owner.name": "Pavel,Ivan"}, false, false},
47 for _, tc := range testCases {
48 query, err := query.New(tc.s)
54 assert.True(t, query.Matches(tc.tags), "Query '%s' should match %v", tc.s, tc.tags)
56 assert.False(t, query.Matches(tc.tags), "Query '%s' should not match %v", tc.s, tc.tags)
61 func TestMustParse(t *testing.T) {
62 assert.Panics(t, func() { query.MustParse("=") })
63 assert.NotPanics(t, func() { query.MustParse("tm.events.type='NewBlock'") })