| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771 |
- // +build go1.7
- package expression
- import (
- "reflect"
- "strings"
- "testing"
- "github.com/aws/aws-sdk-go/aws"
- "github.com/aws/aws-sdk-go/service/dynamodb"
- )
- // updateErrorMode will help with error cases and checking error types
- type updateErrorMode string
- const (
- noUpdateError updateErrorMode = ""
- invalidUpdateOperand = "BuildOperand error"
- unsetSetValue = "unset parameter: SetValueBuilder"
- unsetUpdate = "unset parameter: UpdateBuilder"
- emptyOperationBuilderList = "operationBuilder list is empty"
- )
- func TestBuildOperation(t *testing.T) {
- cases := []struct {
- name string
- input operationBuilder
- expected exprNode
- err updateErrorMode
- }{
- {
- name: "set operation",
- input: operationBuilder{
- name: Name("foo"),
- value: Value(5),
- mode: setOperation,
- },
- expected: exprNode{
- children: []exprNode{
- {
- names: []string{"foo"},
- fmtExpr: "$n",
- },
- {
- values: []dynamodb.AttributeValue{
- {
- N: aws.String("5"),
- },
- },
- fmtExpr: "$v",
- },
- },
- fmtExpr: "$c = $c",
- },
- },
- {
- name: "add operation",
- input: operationBuilder{
- name: Name("foo"),
- value: Value(5),
- mode: addOperation,
- },
- expected: exprNode{
- children: []exprNode{
- {
- names: []string{"foo"},
- fmtExpr: "$n",
- },
- {
- values: []dynamodb.AttributeValue{
- {
- N: aws.String("5"),
- },
- },
- fmtExpr: "$v",
- },
- },
- fmtExpr: "$c $c",
- },
- },
- {
- name: "remove operation",
- input: operationBuilder{
- name: Name("foo"),
- mode: removeOperation,
- },
- expected: exprNode{
- children: []exprNode{
- {
- names: []string{"foo"},
- fmtExpr: "$n",
- },
- },
- fmtExpr: "$c",
- },
- },
- {
- name: "invalid operand",
- input: operationBuilder{
- name: Name(""),
- mode: removeOperation,
- },
- err: invalidUpdateOperand,
- },
- }
- for _, c := range cases {
- t.Run(c.name, func(t *testing.T) {
- actual, err := c.input.buildOperation()
- if c.err != noUpdateError {
- 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, actual; !reflect.DeepEqual(a, e) {
- t.Errorf("expect %v, got %v", e, a)
- }
- }
- })
- }
- }
- func TestUpdateTree(t *testing.T) {
- cases := []struct {
- name string
- input UpdateBuilder
- expectedNode exprNode
- err updateErrorMode
- }{
- {
- name: "set update",
- input: Set(Name("foo"), Value(5)),
- expectedNode: exprNode{
- children: []exprNode{
- {
- children: []exprNode{
- {
- children: []exprNode{
- {
- names: []string{"foo"},
- fmtExpr: "$n",
- },
- {
- values: []dynamodb.AttributeValue{
- {
- N: aws.String("5"),
- },
- },
- fmtExpr: "$v",
- },
- },
- fmtExpr: "$c = $c",
- },
- },
- fmtExpr: "$c",
- },
- },
- fmtExpr: "SET $c\n",
- },
- },
- {
- name: "remove update",
- input: Remove(Name("foo")),
- expectedNode: exprNode{
- children: []exprNode{
- {
- children: []exprNode{
- {
- children: []exprNode{
- {
- names: []string{"foo"},
- fmtExpr: "$n",
- },
- },
- fmtExpr: "$c",
- },
- },
- fmtExpr: "$c",
- },
- },
- fmtExpr: "REMOVE $c\n",
- },
- },
- {
- name: "add update",
- input: Add(Name("foo"), Value(5)),
- expectedNode: exprNode{
- children: []exprNode{
- {
- children: []exprNode{
- {
- children: []exprNode{
- {
- names: []string{"foo"},
- fmtExpr: "$n",
- },
- {
- values: []dynamodb.AttributeValue{
- {
- N: aws.String("5"),
- },
- },
- fmtExpr: "$v",
- },
- },
- fmtExpr: "$c $c",
- },
- },
- fmtExpr: "$c",
- },
- },
- fmtExpr: "ADD $c\n",
- },
- },
- {
- name: "delete update",
- input: Delete(Name("foo"), Value(5)),
- expectedNode: exprNode{
- children: []exprNode{
- {
- children: []exprNode{
- {
- children: []exprNode{
- {
- names: []string{"foo"},
- fmtExpr: "$n",
- },
- {
- values: []dynamodb.AttributeValue{
- {
- N: aws.String("5"),
- },
- },
- fmtExpr: "$v",
- },
- },
- fmtExpr: "$c $c",
- },
- },
- fmtExpr: "$c",
- },
- },
- fmtExpr: "DELETE $c\n",
- },
- },
- {
- name: "multiple sets",
- input: Set(Name("foo"), Value(5)).Set(Name("bar"), Value(6)).Set(Name("baz"), Name("qux")),
- expectedNode: exprNode{
- fmtExpr: "SET $c\n",
- children: []exprNode{
- {
- fmtExpr: "$c, $c, $c",
- children: []exprNode{
- {
- fmtExpr: "$c = $c",
- children: []exprNode{
- {
- fmtExpr: "$n",
- names: []string{"foo"},
- },
- {
- fmtExpr: "$v",
- values: []dynamodb.AttributeValue{
- {
- N: aws.String("5"),
- },
- },
- },
- },
- },
- {
- fmtExpr: "$c = $c",
- children: []exprNode{
- {
- fmtExpr: "$n",
- names: []string{"bar"},
- },
- {
- fmtExpr: "$v",
- values: []dynamodb.AttributeValue{
- {
- N: aws.String("6"),
- },
- },
- },
- },
- },
- {
- fmtExpr: "$c = $c",
- children: []exprNode{
- {
- fmtExpr: "$n",
- names: []string{"baz"},
- },
- {
- fmtExpr: "$n",
- names: []string{"qux"},
- },
- },
- },
- },
- },
- },
- },
- },
- {
- name: "compound update",
- input: Add(Name("foo"), Value(5)).Set(Name("foo"), Value(5)).Delete(Name("foo"), Value(5)).Remove(Name("foo")),
- expectedNode: exprNode{
- children: []exprNode{
- {
- children: []exprNode{
- {
- children: []exprNode{
- {
- names: []string{"foo"},
- fmtExpr: "$n",
- },
- {
- values: []dynamodb.AttributeValue{
- {
- N: aws.String("5"),
- },
- },
- fmtExpr: "$v",
- },
- },
- fmtExpr: "$c $c",
- },
- },
- fmtExpr: "$c",
- },
- {
- children: []exprNode{
- {
- children: []exprNode{
- {
- names: []string{"foo"},
- fmtExpr: "$n",
- },
- {
- values: []dynamodb.AttributeValue{
- {
- N: aws.String("5"),
- },
- },
- fmtExpr: "$v",
- },
- },
- fmtExpr: "$c $c",
- },
- },
- fmtExpr: "$c",
- },
- {
- children: []exprNode{
- {
- children: []exprNode{
- {
- names: []string{"foo"},
- fmtExpr: "$n",
- },
- },
- fmtExpr: "$c",
- },
- },
- fmtExpr: "$c",
- },
- {
- children: []exprNode{
- {
- children: []exprNode{
- {
- names: []string{"foo"},
- fmtExpr: "$n",
- },
- {
- values: []dynamodb.AttributeValue{
- {
- N: aws.String("5"),
- },
- },
- fmtExpr: "$v",
- },
- },
- fmtExpr: "$c = $c",
- },
- },
- fmtExpr: "$c",
- },
- },
- fmtExpr: "ADD $c\nDELETE $c\nREMOVE $c\nSET $c\n",
- },
- },
- {
- name: "empty UpdateBuilder",
- input: UpdateBuilder{},
- err: unsetUpdate,
- },
- }
- for _, c := range cases {
- t.Run(c.name, func(t *testing.T) {
- actual, err := c.input.buildTree()
- if c.err != noUpdateError {
- 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.expectedNode, actual; !reflect.DeepEqual(a, e) {
- t.Errorf("expect %v, got %v", e, a)
- }
- }
- })
- }
- }
- func TestSetValueBuilder(t *testing.T) {
- cases := []struct {
- name string
- input SetValueBuilder
- expected exprNode
- err updateErrorMode
- }{
- {
- name: "name plus name",
- input: Name("foo").Plus(Name("bar")),
- expected: exprNode{
- children: []exprNode{
- {
- names: []string{"foo"},
- fmtExpr: "$n",
- },
- {
- names: []string{"bar"},
- fmtExpr: "$n",
- },
- },
- fmtExpr: "$c + $c",
- },
- },
- {
- name: "name minus name",
- input: Name("foo").Minus(Name("bar")),
- expected: exprNode{
- children: []exprNode{
- {
- names: []string{"foo"},
- fmtExpr: "$n",
- },
- {
- names: []string{"bar"},
- fmtExpr: "$n",
- },
- },
- fmtExpr: "$c - $c",
- },
- },
- {
- name: "list append name and name",
- input: Name("foo").ListAppend(Name("bar")),
- expected: exprNode{
- children: []exprNode{
- {
- names: []string{"foo"},
- fmtExpr: "$n",
- },
- {
- names: []string{"bar"},
- fmtExpr: "$n",
- },
- },
- fmtExpr: "list_append($c, $c)",
- },
- },
- {
- name: "if not exists name and name",
- input: Name("foo").IfNotExists(Name("bar")),
- expected: exprNode{
- children: []exprNode{
- {
- names: []string{"foo"},
- fmtExpr: "$n",
- },
- {
- names: []string{"bar"},
- fmtExpr: "$n",
- },
- },
- fmtExpr: "if_not_exists($c, $c)",
- },
- },
- {
- name: "value plus name",
- input: Value(5).Plus(Name("bar")),
- expected: exprNode{
- children: []exprNode{
- {
- values: []dynamodb.AttributeValue{
- {
- N: aws.String("5"),
- },
- },
- fmtExpr: "$v",
- },
- {
- names: []string{"bar"},
- fmtExpr: "$n",
- },
- },
- fmtExpr: "$c + $c",
- },
- },
- {
- name: "value minus name",
- input: Value(5).Minus(Name("bar")),
- expected: exprNode{
- children: []exprNode{
- {
- values: []dynamodb.AttributeValue{
- {
- N: aws.String("5"),
- },
- },
- fmtExpr: "$v",
- },
- {
- names: []string{"bar"},
- fmtExpr: "$n",
- },
- },
- fmtExpr: "$c - $c",
- },
- },
- {
- name: "list append list and name",
- input: Value([]int{1, 2, 3}).ListAppend(Name("bar")),
- expected: exprNode{
- children: []exprNode{
- {
- values: []dynamodb.AttributeValue{
- {
- L: []*dynamodb.AttributeValue{
- {
- N: aws.String("1"),
- },
- {
- N: aws.String("2"),
- },
- {
- N: aws.String("3"),
- },
- },
- },
- },
- fmtExpr: "$v",
- },
- {
- names: []string{"bar"},
- fmtExpr: "$n",
- },
- },
- fmtExpr: "list_append($c, $c)",
- },
- },
- {
- name: "unset SetValueBuilder",
- input: SetValueBuilder{},
- err: unsetSetValue,
- },
- {
- name: "invalid operand error",
- input: Name("").Plus(Name("foo")),
- err: invalidUpdateOperand,
- },
- {
- name: "invalid operand error",
- input: Name("foo").Plus(Name("")),
- err: invalidUpdateOperand,
- },
- }
- for _, c := range cases {
- t.Run(c.name, func(t *testing.T) {
- actual, err := c.input.BuildOperand()
- if c.err != noUpdateError {
- 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, actual.exprNode; !reflect.DeepEqual(a, e) {
- t.Errorf("expect %v, got %v", e, a)
- }
- }
- })
- }
- }
- func TestUpdateBuildChildNodes(t *testing.T) {
- cases := []struct {
- name string
- input []operationBuilder
- expected exprNode
- err updateErrorMode
- }{
- {
- name: "set operand builder",
- input: []operationBuilder{
- {
- mode: setOperation,
- name: NameBuilder{
- name: "foo",
- },
- value: ValueBuilder{
- value: 5,
- },
- },
- {
- mode: setOperation,
- name: NameBuilder{
- name: "bar",
- },
- value: ValueBuilder{
- value: 6,
- },
- },
- {
- mode: setOperation,
- name: NameBuilder{
- name: "baz",
- },
- value: ValueBuilder{
- value: 7,
- },
- },
- {
- mode: setOperation,
- name: NameBuilder{
- name: "qux",
- },
- value: ValueBuilder{
- value: 8,
- },
- },
- },
- expected: exprNode{
- fmtExpr: "$c, $c, $c, $c",
- children: []exprNode{
- {
- fmtExpr: "$c = $c",
- children: []exprNode{
- {
- fmtExpr: "$n",
- names: []string{"foo"},
- },
- {
- fmtExpr: "$v",
- values: []dynamodb.AttributeValue{
- {
- N: aws.String("5"),
- },
- },
- },
- },
- },
- {
- fmtExpr: "$c = $c",
- children: []exprNode{
- {
- fmtExpr: "$n",
- names: []string{"bar"},
- },
- {
- fmtExpr: "$v",
- values: []dynamodb.AttributeValue{
- {
- N: aws.String("6"),
- },
- },
- },
- },
- },
- {
- fmtExpr: "$c = $c",
- children: []exprNode{
- {
- fmtExpr: "$n",
- names: []string{"baz"},
- },
- {
- fmtExpr: "$v",
- values: []dynamodb.AttributeValue{
- {
- N: aws.String("7"),
- },
- },
- },
- },
- },
- {
- fmtExpr: "$c = $c",
- children: []exprNode{
- {
- fmtExpr: "$n",
- names: []string{"qux"},
- },
- {
- fmtExpr: "$v",
- values: []dynamodb.AttributeValue{
- {
- N: aws.String("8"),
- },
- },
- },
- },
- },
- },
- },
- },
- {
- name: "empty operationBuilder list",
- input: []operationBuilder{},
- err: emptyOperationBuilderList,
- },
- }
- for _, c := range cases {
- t.Run(c.name, func(t *testing.T) {
- actual, err := buildChildNodes(c.input)
- if c.err != noUpdateError {
- 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, actual; !reflect.DeepEqual(a, e) {
- t.Errorf("expect %v, got %v", e, a)
- }
- }
- })
- }
- }
|