-
Notifications
You must be signed in to change notification settings - Fork 11
/
sql_predicate_test.go
executable file
·96 lines (86 loc) · 2.66 KB
/
sql_predicate_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package dsc_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/viant/dsc"
"github.com/viant/toolbox"
)
func TestNewCriterionPredicate(t *testing.T) {
{ //Like case
parameters := toolbox.NewSliceIterator([]string{"abc%"})
predicate, err := dsc.NewSQLCriterionPredicate(&dsc.SQLCriterion{Operator: "Like", RightOperand: "?"}, parameters)
assert.Nil(t, err)
{
assert.True(t, predicate.Apply("ABC"))
assert.False(t, predicate.Apply("AB"))
}
}
{ //between case
parameters := toolbox.NewSliceIterator([]string{"1", "10"})
predicate, err := dsc.NewSQLCriterionPredicate(&dsc.SQLCriterion{Operator: "BETWEEN", RightOperands: []interface{}{"?", "?"}}, parameters)
assert.Nil(t, err)
{
assert.True(t, predicate.Apply(5))
assert.False(t, predicate.Apply(12))
}
}
{ //error no operator
parameters := toolbox.NewSliceIterator([]string{})
_, err := dsc.NewSQLCriterionPredicate(&dsc.SQLCriterion{Operator: "", RightOperands: []interface{}{"?"}}, parameters)
assert.NotNil(t, err)
}
{ //between error case
parameters := toolbox.NewSliceIterator([]string{"1"})
_, err := dsc.NewSQLCriterionPredicate(&dsc.SQLCriterion{Operator: "BETWEEN", RightOperands: []interface{}{"?", "?"}}, parameters)
assert.NotNil(t, err)
}
{ //like error case
parameters := toolbox.NewSliceIterator([]string{})
_, err := dsc.NewSQLCriterionPredicate(&dsc.SQLCriterion{Operator: "Like", RightOperand: "?"}, parameters)
assert.NotNil(t, err)
}
{ //in error case
parameters := toolbox.NewSliceIterator([]string{})
_, err := dsc.NewSQLCriterionPredicate(&dsc.SQLCriterion{Operator: "in", RightOperands: []interface{}{"?"}}, parameters)
assert.NotNil(t, err)
}
{ //default error case
parameters := toolbox.NewSliceIterator([]string{})
_, err := dsc.NewSQLCriterionPredicate(&dsc.SQLCriterion{Operator: "=", RightOperand: "?"}, parameters)
assert.NotNil(t, err)
}
}
func TestNewCriteriaPredicate(t *testing.T) {
parameters := []interface{}{"abc%", 123}
iterator := toolbox.NewSliceIterator(parameters)
predicate, err := dsc.NewSQLCriteriaPredicate(iterator,
&dsc.SQLCriteria{
LogicalOperator: "or",
Criteria: []*dsc.SQLCriterion{
{LeftOperand: "column1", Operator: "Like", RightOperand: "?"},
{LeftOperand: "column2", Operator: "=", RightOperand: "?"},
},
},
)
assert.Nil(t, err)
{
assert.False(t, predicate.Apply(
map[string]interface{}{
"column1": "AB",
"column2": 12,
},
))
assert.True(t, predicate.Apply(
map[string]interface{}{
"column1": "ABc",
"column2": 12,
},
))
assert.True(t, predicate.Apply(
map[string]interface{}{
"column1": "AB",
"column2": 123,
},
))
}
}