github.com/datachainlab/burrow@v0.25.0/event/query/query_test.go (about)

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