github.com/vipernet-xyz/tm@v0.34.24/libs/pubsub/query/parser_test.go (about)

     1  package query_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/vipernet-xyz/tm/libs/pubsub/query"
     9  )
    10  
    11  // TODO: fuzzy testing?
    12  func TestParser(t *testing.T) {
    13  	cases := []struct {
    14  		query string
    15  		valid bool
    16  	}{
    17  		{"tm.events.type='NewBlock'", true},
    18  		{"tm.events.type = 'NewBlock'", true},
    19  		{"tm.events.name = ''", true},
    20  		{"tm.events.type='TIME'", true},
    21  		{"tm.events.type='DATE'", true},
    22  		{"tm.events.type='='", true},
    23  		{"tm.events.type='TIME", false},
    24  		{"tm.events.type=TIME'", false},
    25  		{"tm.events.type==", false},
    26  		{"tm.events.type=NewBlock", false},
    27  		{">==", false},
    28  		{"tm.events.type 'NewBlock' =", false},
    29  		{"tm.events.type>'NewBlock'", false},
    30  		{"", false},
    31  		{"=", false},
    32  		{"='NewBlock'", false},
    33  		{"tm.events.type=", false},
    34  
    35  		{"tm.events.typeNewBlock", false},
    36  		{"tm.events.type'NewBlock'", false},
    37  		{"'NewBlock'", false},
    38  		{"NewBlock", false},
    39  		{"", false},
    40  
    41  		{"tm.events.type='NewBlock' AND abci.account.name='Igor'", true},
    42  		{"tm.events.type='NewBlock' AND", false},
    43  		{"tm.events.type='NewBlock' AN", false},
    44  		{"tm.events.type='NewBlock' AN tm.events.type='NewBlockHeader'", false},
    45  		{"AND tm.events.type='NewBlock' ", false},
    46  
    47  		{"abci.account.name CONTAINS 'Igor'", true},
    48  
    49  		{"tx.date > DATE 2013-05-03", true},
    50  		{"tx.date < DATE 2013-05-03", true},
    51  		{"tx.date <= DATE 2013-05-03", true},
    52  		{"tx.date >= DATE 2013-05-03", true},
    53  		{"tx.date >= DAT 2013-05-03", false},
    54  		{"tx.date <= DATE2013-05-03", false},
    55  		{"tx.date <= DATE -05-03", false},
    56  		{"tx.date >= DATE 20130503", false},
    57  		{"tx.date >= DATE 2013+01-03", false},
    58  		// incorrect year, month, day
    59  		{"tx.date >= DATE 0013-01-03", false},
    60  		{"tx.date >= DATE 2013-31-03", false},
    61  		{"tx.date >= DATE 2013-01-83", false},
    62  
    63  		{"tx.date > TIME 2013-05-03T14:45:00+07:00", true},
    64  		{"tx.date < TIME 2013-05-03T14:45:00-02:00", true},
    65  		{"tx.date <= TIME 2013-05-03T14:45:00Z", true},
    66  		{"tx.date >= TIME 2013-05-03T14:45:00Z", true},
    67  		{"tx.date >= TIME2013-05-03T14:45:00Z", false},
    68  		{"tx.date = IME 2013-05-03T14:45:00Z", false},
    69  		{"tx.date = TIME 2013-05-:45:00Z", false},
    70  		{"tx.date >= TIME 2013-05-03T14:45:00", false},
    71  		{"tx.date >= TIME 0013-00-00T14:45:00Z", false},
    72  		{"tx.date >= TIME 2013+05=03T14:45:00Z", false},
    73  
    74  		{"account.balance=100", true},
    75  		{"account.balance >= 200", true},
    76  		{"account.balance >= -300", false},
    77  		{"account.balance >>= 400", false},
    78  		{"account.balance=33.22.1", false},
    79  
    80  		{"slashing.amount EXISTS", true},
    81  		{"slashing.amount EXISTS AND account.balance=100", true},
    82  		{"account.balance=100 AND slashing.amount EXISTS", true},
    83  		{"slashing EXISTS", true},
    84  
    85  		{"hash='136E18F7E4C348B780CF873A0BF43922E5BAFA63'", true},
    86  		{"hash=136E18F7E4C348B780CF873A0BF43922E5BAFA63", false},
    87  	}
    88  
    89  	for _, c := range cases {
    90  		_, err := query.New(c.query)
    91  		if c.valid {
    92  			assert.NoErrorf(t, err, "Query was '%s'", c.query)
    93  		} else {
    94  			assert.Errorf(t, err, "Query was '%s'", c.query)
    95  		}
    96  	}
    97  }