@@ -2,6 +2,7 @@ package query_test
2
2
3
3
import (
4
4
"fmt"
5
+ "math/big"
5
6
"testing"
6
7
"time"
7
8
@@ -11,6 +12,57 @@ import (
11
12
"github.com/tendermint/tendermint/libs/pubsub/query"
12
13
)
13
14
15
+ func TestBigNumbers (t * testing.T ) {
16
+ bigInt := "10000000000000000000"
17
+ bigIntAsFloat := "10000000000000000000.0"
18
+ bigFloat := "10000000000000000000.6"
19
+ bigFloatLowerRounding := "10000000000000000000.1"
20
+ doubleBigInt := "20000000000000000000"
21
+
22
+ testCases := []struct {
23
+ s string
24
+ events map [string ][]string
25
+ err bool
26
+ matches bool
27
+ matchErr bool
28
+ }{
29
+
30
+ {"account.balance <= " + bigInt , map [string ][]string {"account.balance" : {bigInt }}, false , true , false },
31
+ {"account.balance <= " + bigInt , map [string ][]string {"account.balance" : {bigIntAsFloat }}, false , true , false },
32
+ {"account.balance <= " + doubleBigInt , map [string ][]string {"account.balance" : {bigInt }}, false , true , false },
33
+ {"account.balance <= " + bigInt , map [string ][]string {"account.balance" : {"10000000000000000001" }}, false , false , false },
34
+ {"account.balance <= " + doubleBigInt , map [string ][]string {"account.balance" : {bigFloat }}, false , true , false },
35
+ // To maintain compatibility with the old implementation which did a simple cast of float to int64, we do not round the float
36
+ // Thus both 10000000000000000000.6 and "10000000000000000000.1 are equal to 10000000000000000000
37
+ // and the test does not find a match
38
+ {"account.balance > " + bigInt , map [string ][]string {"account.balance" : {bigFloat }}, false , false , false },
39
+ {"account.balance > " + bigInt , map [string ][]string {"account.balance" : {bigFloatLowerRounding }}, true , false , false },
40
+ // This test should also find a match, but floats that are too big cannot be properly converted, thus
41
+ // 10000000000000000000.6 gets rounded to 10000000000000000000
42
+ {"account.balance > " + bigIntAsFloat , map [string ][]string {"account.balance" : {bigFloat }}, false , false , false },
43
+ {"account.balance > 11234.0" , map [string ][]string {"account.balance" : {"11234.6" }}, false , true , false },
44
+ {"account.balance <= " + bigInt , map [string ][]string {"account.balance" : {"1000.45" }}, false , true , false },
45
+ }
46
+
47
+ for _ , tc := range testCases {
48
+ q , err := query .New (tc .s )
49
+ if ! tc .err {
50
+ require .Nil (t , err )
51
+ }
52
+ require .NotNil (t , q , "Query '%s' should not be nil" , tc .s )
53
+
54
+ if tc .matches {
55
+ match , err := q .Matches (tc .events )
56
+ assert .Nil (t , err , "Query '%s' should not error on match %v" , tc .s , tc .events )
57
+ assert .True (t , match , "Query '%s' should match %v" , tc .s , tc .events )
58
+ } else {
59
+ match , err := q .Matches (tc .events )
60
+ assert .Equal (t , tc .matchErr , err != nil , "Unexpected error for query '%s' match %v" , tc .s , tc .events )
61
+ assert .False (t , match , "Query '%s' should not match %v" , tc .s , tc .events )
62
+ }
63
+ }
64
+ }
65
+
14
66
func TestMatches (t * testing.T ) {
15
67
var (
16
68
txDate = "2017-01-01"
@@ -180,6 +232,10 @@ func TestConditions(t *testing.T) {
180
232
txTime , err := time .Parse (time .RFC3339 , "2013-05-03T14:45:00Z" )
181
233
require .NoError (t , err )
182
234
235
+ bigInt := new (big.Int )
236
+ bigInt , ok := bigInt .SetString ("10000000000000000000" , 10 )
237
+ require .True (t , ok )
238
+
183
239
testCases := []struct {
184
240
s string
185
241
conditions []query.Condition
@@ -193,8 +249,24 @@ func TestConditions(t *testing.T) {
193
249
{
194
250
s : "tx.gas > 7 AND tx.gas < 9" ,
195
251
conditions : []query.Condition {
196
- {CompositeKey : "tx.gas" , Op : query .OpGreater , Operand : int64 (7 )},
197
- {CompositeKey : "tx.gas" , Op : query .OpLess , Operand : int64 (9 )},
252
+ {CompositeKey : "tx.gas" , Op : query .OpGreater , Operand : big .NewInt (7 )},
253
+ {CompositeKey : "tx.gas" , Op : query .OpLess , Operand : big .NewInt (9 )},
254
+ },
255
+ },
256
+ {
257
+
258
+ s : "tx.gas > 7.5 AND tx.gas < 9" ,
259
+ conditions : []query.Condition {
260
+ {CompositeKey : "tx.gas" , Op : query .OpGreater , Operand : 7.5 },
261
+ {CompositeKey : "tx.gas" , Op : query .OpLess , Operand : big .NewInt (9 )},
262
+ },
263
+ },
264
+ {
265
+
266
+ s : "tx.gas > " + bigInt .String () + " AND tx.gas < 9" ,
267
+ conditions : []query.Condition {
268
+ {CompositeKey : "tx.gas" , Op : query .OpGreater , Operand : bigInt },
269
+ {CompositeKey : "tx.gas" , Op : query .OpLess , Operand : big .NewInt (9 )},
198
270
},
199
271
},
200
272
{
0 commit comments