OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / tendermint / tmlibs / pubsub / query / query_test.go
1 package query_test
2
3 import (
4         "testing"
5         "time"
6
7         "github.com/stretchr/testify/assert"
8         "github.com/stretchr/testify/require"
9         "github.com/tendermint/tmlibs/pubsub/query"
10 )
11
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)
18
19         testCases := []struct {
20                 s       string
21                 tags    map[string]interface{}
22                 err     bool
23                 matches bool
24         }{
25                 {"tm.events.type='NewBlock'", map[string]interface{}{"tm.events.type": "NewBlock"}, false, true},
26
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},
35
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},
39
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},
42
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},
45         }
46
47         for _, tc := range testCases {
48                 query, err := query.New(tc.s)
49                 if !tc.err {
50                         require.Nil(t, err)
51                 }
52
53                 if tc.matches {
54                         assert.True(t, query.Matches(tc.tags), "Query '%s' should match %v", tc.s, tc.tags)
55                 } else {
56                         assert.False(t, query.Matches(tc.tags), "Query '%s' should not match %v", tc.s, tc.tags)
57                 }
58         }
59 }
60
61 func TestMustParse(t *testing.T) {
62         assert.Panics(t, func() { query.MustParse("=") })
63         assert.NotPanics(t, func() { query.MustParse("tm.events.type='NewBlock'") })
64 }