Skip to content

Commit

Permalink
add integration tests using ExpressionLiteral
Browse files Browse the repository at this point in the history
  • Loading branch information
guregu committed Mar 8, 2022
1 parent a4c76da commit 5734816
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 16 deletions.
28 changes: 26 additions & 2 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
)

func TestGetAllCount(t *testing.T) {
Expand All @@ -30,15 +31,38 @@ func TestGetAllCount(t *testing.T) {
t.Error("unexpected error:", err)
}

lit := ExpressionLiteral{
Expression: "#meta.#foo = :bar",
AttributeNames: aws.StringMap(map[string]string{
"#meta": "Meta",
"#foo": "foo",
}),
AttributeValues: map[string]*dynamodb.AttributeValue{
":bar": {S: aws.String("bar")},
},
}

// now check if get all and count return the same amount of items
var result []widget
var cc1, cc2 ConsumedCapacity
err = table.Get("UserID", 42).Consistent(true).Filter("Msg = ?", item.Msg).Filter("StrPtr = ?", "").ConsumedCapacity(&cc1).All(&result)
err = table.Get("UserID", 42).
Consistent(true).
Filter("Msg = ?", item.Msg).
Filter("StrPtr = ?", "").
Filter("?", lit).
ConsumedCapacity(&cc1).
All(&result)
if err != nil {
t.Error("unexpected error:", err)
}

ct, err := table.Get("UserID", 42).Consistent(true).Filter("Msg = ?", item.Msg).Filter("StrPtr = ?", "").ConsumedCapacity(&cc2).Count()
ct, err := table.Get("UserID", 42).
Consistent(true).
Filter("Msg = ?", item.Msg).
Filter("StrPtr = ?", "").
Filter("$", lit). // both $ and ? are OK for literals
ConsumedCapacity(&cc2).
Count()
if err != nil {
t.Error("unexpected error:", err)
}
Expand Down
13 changes: 0 additions & 13 deletions substitute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,6 @@ func TestSubMerge(t *testing.T) {
t.Error("merged value mismatch. want:", v, "got:", got)
}
}

t.Run("wrap", func(t *testing.T) {
s := subber{}
lit := lit.Wrap()
rewrite, err := s.subExpr("$", lit)
if err != nil {
t.Fatal(err)
}
want := "(contains(#x_a, :x_v) AND (#x_abc.#x_abcdef = :x_v0))"
if rewrite != want {
t.Error("bad rewrite. want:", want, "got:", rewrite)
}
})
}

func BenchmarkSubExpr(b *testing.B) {
Expand Down
37 changes: 36 additions & 1 deletion update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"reflect"
"testing"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
)

func TestUpdate(t *testing.T) {
Expand All @@ -30,6 +33,7 @@ func TestUpdate(t *testing.T) {
"foo": "bar",
"keep": "untouched",
"nope": "痛",
"haha": "555 no jam",
},
},
MySet1: []string{"one", "deleteme"},
Expand All @@ -41,23 +45,53 @@ func TestUpdate(t *testing.T) {
t.Error("unexpected error:", err)
}

setLit := ExpressionLiteral{
Expression: "#meta.#pet = :cat",
AttributeNames: aws.StringMap(map[string]string{
"#meta": "Meta",
"#pet": "pet",
}),
AttributeValues: map[string]*dynamodb.AttributeValue{
":cat": {S: aws.String("猫")},
},
}
rmLit := ExpressionLiteral{
Expression: "#meta.#haha",
AttributeNames: aws.StringMap(map[string]string{
"#meta": "Meta",
"#haha": "haha",
}),
}
ifLit := ExpressionLiteral{
Expression: "#msg = :hi",
AttributeNames: aws.StringMap(map[string]string{
"#msg": "Msg",
}),
AttributeValues: map[string]*dynamodb.AttributeValue{
":hi": {S: aws.String("hello")},
},
}

// change it a bit and check the result
var result widget2
var cc ConsumedCapacity
err = table.Update("UserID", item.UserID).
Range("Time", item.Time).
Set("Msg", "changed").
SetExpr("Meta.$ = ?", "foo", "baz").
SetExpr("$", setLit).
Add("Count", 1).
Add("Test", []string{"A", "B"}).
RemoveExpr("Meta.$", "nope").
RemoveExpr("$", rmLit).
If("('Count' = ?) OR attribute_not_exists('Count')", 0).
If("'Msg' = ?", "hello").
If("?", ifLit).
DeleteFromSet("MySet1", "deleteme").
DeleteFromSet("MySet2", []string{"bad1", "bad2"}).
DeleteFromSet("MySet3", map[int64]struct{}{999: {}, 555: {}}).
ConsumedCapacity(&cc).
Value(&result)

expected := widget2{
widget: widget{
UserID: item.UserID,
Expand All @@ -67,6 +101,7 @@ func TestUpdate(t *testing.T) {
Meta: map[string]string{
"foo": "baz",
"keep": "untouched",
"pet": "猫",
},
},
MySet1: []string{"one"},
Expand Down

0 comments on commit 5734816

Please sign in to comment.