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 132 133 134 135 136 137 138 139 140 141 142 143 144
|
// +build go1.7
package expression
import (
"reflect"
"strings"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
)
// opeErrorMode will help with error cases and checking error types
type opeErrorMode string
const (
noOperandError opeErrorMode = ""
// unsetName error will occur if an empty string is passed into NameBuilder
unsetName = "unset parameter: NameBuilder"
// invalidName error will occur if a nested name has an empty intermediary
// attribute name (i.e. foo.bar..baz)
invalidName = "invalid parameter: NameBuilder"
// unsetKey error will occur if an empty string is passed into KeyBuilder
unsetKey = "unset parameter: KeyBuilder"
)
func TestBuildOperand(t *testing.T) {
cases := []struct {
name string
input OperandBuilder
expected exprNode
err opeErrorMode
}{
{
name: "basic name",
input: Name("foo"),
expected: exprNode{
names: []string{"foo"},
fmtExpr: "$n",
},
},
{
name: "duplicate name name",
input: Name("foo.foo"),
expected: exprNode{
names: []string{"foo", "foo"},
fmtExpr: "$n.$n",
},
},
{
name: "basic value",
input: Value(5),
expected: exprNode{
values: []dynamodb.AttributeValue{
{
N: aws.String("5"),
},
},
fmtExpr: "$v",
},
},
{
name: "nested name",
input: Name("foo.bar"),
expected: exprNode{
names: []string{"foo", "bar"},
fmtExpr: "$n.$n",
},
},
{
name: "nested name with index",
input: Name("foo.bar[0].baz"),
expected: exprNode{
names: []string{"foo", "bar", "baz"},
fmtExpr: "$n.$n[0].$n",
},
},
{
name: "basic size",
input: Name("foo").Size(),
expected: exprNode{
names: []string{"foo"},
fmtExpr: "size ($n)",
},
},
{
name: "key",
input: Key("foo"),
expected: exprNode{
names: []string{"foo"},
fmtExpr: "$n",
},
},
{
name: "unset key error",
input: Key(""),
expected: exprNode{},
err: unsetKey,
},
{
name: "empty name error",
input: Name(""),
expected: exprNode{},
err: unsetName,
},
{
name: "invalid name",
input: Name("foo..bar"),
expected: exprNode{},
err: invalidName,
},
{
name: "invalid index",
input: Name("[foo]"),
expected: exprNode{},
err: invalidName,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
operand, err := c.input.BuildOperand()
if c.err != noOperandError {
if err == nil {
t.Errorf("expect error %q, got no error", c.err)
} else {
if e, a := string(c.err), err.Error(); !strings.Contains(a, e) {
t.Errorf("expect %q error message to be in %q", e, a)
}
}
} else {
if err != nil {
t.Errorf("expect no error, got unexpected Error %q", err)
}
if e, a := c.expected, operand.exprNode; !reflect.DeepEqual(a, e) {
t.Errorf("expect %v, got %v", e, a)
}
}
})
}
}
|