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