github.com/aavshr/aws-sdk-go@v1.41.3/service/dynamodb/expression/projection_test.go (about) 1 //go:build go1.7 2 // +build go1.7 3 4 package expression 5 6 import ( 7 "reflect" 8 "strings" 9 "testing" 10 ) 11 12 // projErrorMode will help with error cases and checking error types 13 type projErrorMode string 14 15 const ( 16 noProjError projErrorMode = "" 17 // invalidProjectionOperand error will occur when an invalid OperandBuilder is 18 // used as an argument 19 invalidProjectionOperand = "BuildOperand error" 20 // unsetProjection error will occur if the argument ProjectionBuilder is unset 21 unsetProjection = "unset parameter: ProjectionBuilder" 22 ) 23 24 func TestProjectionBuilder(t *testing.T) { 25 cases := []struct { 26 name string 27 input ProjectionBuilder 28 expectedNode exprNode 29 err projErrorMode 30 }{ 31 { 32 name: "names list function call", 33 input: NamesList(Name("foo"), Name("bar")), 34 expectedNode: exprNode{ 35 children: []exprNode{ 36 { 37 names: []string{"foo"}, 38 fmtExpr: "$n", 39 }, 40 { 41 names: []string{"bar"}, 42 fmtExpr: "$n", 43 }, 44 }, 45 fmtExpr: "$c, $c", 46 }, 47 }, 48 { 49 name: "names list method call", 50 input: Name("foo").NamesList(Name("bar")), 51 expectedNode: exprNode{ 52 children: []exprNode{ 53 { 54 names: []string{"foo"}, 55 fmtExpr: "$n", 56 }, 57 { 58 names: []string{"bar"}, 59 fmtExpr: "$n", 60 }, 61 }, 62 fmtExpr: "$c, $c", 63 }, 64 }, 65 { 66 name: "add name", 67 input: Name("foo").NamesList(Name("bar")).AddNames(Name("baz"), Name("qux")), 68 expectedNode: exprNode{ 69 children: []exprNode{ 70 { 71 names: []string{"foo"}, 72 fmtExpr: "$n", 73 }, 74 { 75 names: []string{"bar"}, 76 fmtExpr: "$n", 77 }, 78 { 79 names: []string{"baz"}, 80 fmtExpr: "$n", 81 }, { 82 names: []string{"qux"}, 83 fmtExpr: "$n", 84 }, 85 }, 86 fmtExpr: "$c, $c, $c, $c", 87 }, 88 }, 89 { 90 name: "invalid operand", 91 input: NamesList(Name("")), 92 err: invalidProjectionOperand, 93 }, 94 } 95 for _, c := range cases { 96 t.Run(c.name, func(t *testing.T) { 97 actual, err := c.input.buildTree() 98 if c.err != noProjError { 99 if err == nil { 100 t.Errorf("expect error %q, got no error", c.err) 101 } else { 102 if e, a := string(c.err), err.Error(); !strings.Contains(a, e) { 103 t.Errorf("expect %q error message to be in %q", e, a) 104 } 105 } 106 } else { 107 if err != nil { 108 t.Errorf("expect no error, got unexpected Error %q", err) 109 } 110 if e, a := c.expectedNode, actual; !reflect.DeepEqual(a, e) { 111 t.Errorf("expect %v, got %v", e, a) 112 } 113 } 114 }) 115 } 116 } 117 118 func TestBuildProjection(t *testing.T) { 119 cases := []struct { 120 name string 121 input ProjectionBuilder 122 expected string 123 err projErrorMode 124 }{ 125 { 126 name: "build projection 3", 127 input: NamesList(Name("foo"), Name("bar"), Name("baz")), 128 expected: "$c, $c, $c", 129 }, 130 { 131 name: "build projection 5", 132 input: NamesList(Name("foo"), Name("bar"), Name("baz")).AddNames(Name("qux"), Name("quux")), 133 expected: "$c, $c, $c, $c, $c", 134 }, 135 { 136 name: "empty ProjectionBuilder", 137 input: ProjectionBuilder{}, 138 err: unsetProjection, 139 }, 140 } 141 for _, c := range cases { 142 t.Run(c.name, func(t *testing.T) { 143 actual, err := c.input.buildTree() 144 if c.err != noProjError { 145 if err == nil { 146 t.Errorf("expect error %q, got no error", c.err) 147 } else { 148 if e, a := string(c.err), err.Error(); !strings.Contains(a, e) { 149 t.Errorf("expect %q error message to be in %q", e, a) 150 } 151 } 152 } else { 153 if err != nil { 154 t.Errorf("expect no error, got unexpected Error %q", err) 155 } 156 if e, a := c.expected, actual.fmtExpr; !reflect.DeepEqual(a, e) { 157 t.Errorf("expect %v, got %v", e, a) 158 } 159 } 160 }) 161 } 162 } 163 164 func TestBuildProjectionChildNodes(t *testing.T) { 165 cases := []struct { 166 name string 167 input ProjectionBuilder 168 expected []exprNode 169 err projErrorMode 170 }{ 171 { 172 name: "build child nodes", 173 input: NamesList(Name("foo"), Name("bar"), Name("baz")), 174 expected: []exprNode{ 175 { 176 names: []string{"foo"}, 177 fmtExpr: "$n", 178 }, 179 { 180 names: []string{"bar"}, 181 fmtExpr: "$n", 182 }, 183 { 184 names: []string{"baz"}, 185 fmtExpr: "$n", 186 }, 187 }, 188 }, 189 { 190 name: "operand error", 191 input: NamesList(Name("")), 192 err: invalidProjectionOperand, 193 }, 194 } 195 for _, c := range cases { 196 t.Run(c.name, func(t *testing.T) { 197 actual, err := c.input.buildTree() 198 if c.err != noProjError { 199 if err == nil { 200 t.Errorf("expect error %q, got no error", c.err) 201 } else { 202 if e, a := string(c.err), err.Error(); !strings.Contains(a, e) { 203 t.Errorf("expect %q error message to be in %q", e, a) 204 } 205 } 206 } else { 207 if err != nil { 208 t.Errorf("expect no error, got unexpected Error %q", err) 209 } 210 if e, a := c.expected, actual.children; !reflect.DeepEqual(a, e) { 211 t.Errorf("expect %v, got %v", e, a) 212 } 213 } 214 }) 215 } 216 }