-
Notifications
You must be signed in to change notification settings - Fork 6
/
expr_test.go
131 lines (122 loc) · 3.93 KB
/
expr_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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package mql
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Test_root will focus on error conditions
func Test_root(t *testing.T) {
t.Parallel()
t.Run("missing-expr", func(t *testing.T) {
e, err := root(nil, "raw")
require.Error(t, err)
assert.Empty(t, e)
assert.ErrorIs(t, err, ErrInvalidParameter)
assert.ErrorContains(t, err, "invalid parameter (missing expression)")
})
t.Run("missing-left-expr", func(t *testing.T) {
e, err := root(&logicalExpr{
leftExpr: nil,
logicalOp: "",
rightExpr: &comparisonExpr{},
}, "raw")
require.Error(t, err)
assert.Empty(t, e)
assert.ErrorIs(t, err, ErrMissingExpr)
assert.ErrorContains(t, err, "missing expression nil in: \"raw\"")
})
}
// Test_newComparison will focus on error conditions
func Test_newLogicalOp(t *testing.T) {
t.Parallel()
t.Run("invalid-comp-op", func(t *testing.T) {
op, err := newLogicalOp("not-valid")
require.Error(t, err)
assert.Empty(t, op)
assert.ErrorIs(t, err, ErrInvalidLogicalOp)
assert.ErrorContains(t, err, `invalid logical operator "not-valid"`)
})
}
// Test_newComparisonOp will focus on error conditions
func Test_newComparisonOp(t *testing.T) {
t.Parallel()
t.Run("invalid-comp-op", func(t *testing.T) {
op, err := newComparisonOp("not-valid")
require.Error(t, err)
assert.Empty(t, op)
assert.ErrorIs(t, err, ErrInvalidComparisonOp)
assert.ErrorContains(t, err, `invalid comparison operator "not-valid"`)
})
}
func Test_comparisonExprString(t *testing.T) {
t.Run("nil-value", func(t *testing.T) {
e := &comparisonExpr{
column: "name",
comparisonOp: "=",
value: nil,
}
assert.Equal(t, "(comparisonExpr: name = nil)", e.String())
})
}
func Test_logicalExprString(t *testing.T) {
t.Run("String", func(t *testing.T) {
e := &logicalExpr{
leftExpr: &comparisonExpr{
column: "name",
comparisonOp: "=",
value: pointer("alice"),
},
logicalOp: andOp,
rightExpr: &comparisonExpr{
column: "name",
comparisonOp: "=",
value: pointer("alice"),
},
}
assert.Equal(t, "(logicalExpr: (comparisonExpr: name = alice) and (comparisonExpr: name = alice))", e.String())
})
}
// Test_defaultValidateConvert will focus on error conditions
func Test_defaultValidateConvert(t *testing.T) {
t.Parallel()
fValidators, err := fieldValidators(reflect.ValueOf(testModel{}))
require.NoError(t, err)
t.Run("missing-column", func(t *testing.T) {
e, err := defaultValidateConvert("", EqualOp, pointer("alice"), fValidators["name"])
require.Error(t, err)
assert.Empty(t, e)
assert.ErrorIs(t, err, ErrMissingColumn)
assert.ErrorContains(t, err, "missing column")
})
t.Run("missing-comparison-op", func(t *testing.T) {
e, err := defaultValidateConvert("name", "", pointer("alice"), fValidators["name"])
require.Error(t, err)
assert.Empty(t, e)
assert.ErrorIs(t, err, ErrMissingComparisonOp)
assert.ErrorContains(t, err, "missing comparison operator")
})
t.Run("missing-value", func(t *testing.T) {
e, err := defaultValidateConvert("name", EqualOp, nil, fValidators["name"])
require.Error(t, err)
assert.Empty(t, e)
assert.ErrorIs(t, err, ErrMissingComparisonValue)
assert.ErrorContains(t, err, "missing comparison value")
})
t.Run("missing-validator-func", func(t *testing.T) {
e, err := defaultValidateConvert("name", EqualOp, pointer("alice"), validator{typ: "string"})
require.Error(t, err)
assert.Empty(t, e)
assert.ErrorIs(t, err, ErrInvalidParameter)
assert.ErrorContains(t, err, "missing validator function")
})
t.Run("missing-validator-typ", func(t *testing.T) {
e, err := defaultValidateConvert("name", EqualOp, pointer("alice"), validator{fn: fValidators["name"].fn})
require.Error(t, err)
assert.Empty(t, e)
assert.ErrorIs(t, err, ErrInvalidParameter)
assert.ErrorContains(t, err, "missing validator type")
})
}