From 367a2d675220818121a6279ba27bd9c0cb653855 Mon Sep 17 00:00:00 2001 From: Jacob Alberty Date: Mon, 21 Feb 2022 11:16:21 -0600 Subject: [PATCH 1/3] Add shallow item comparison for PutItemExpectation --- put_item.go | 21 +++++++++++++++++++++ types.go | 7 ++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/put_item.go b/put_item.go index b8c25c6..b1b0758 100644 --- a/put_item.go +++ b/put_item.go @@ -21,6 +21,11 @@ func (e *PutItemExpectation) WithItems(item map[string]*dynamodb.AttributeValue) return e } +func (e *PutItemExpectation) WithShallowItems(item map[string]*dynamodb.AttributeValue) *PutItemExpectation { + e.shallowitem = item + return e +} + // WillReturns - method for set desired result func (e *PutItemExpectation) WillReturns(res dynamodb.PutItemOutput) *PutItemExpectation { e.output = &res @@ -44,6 +49,22 @@ func (e *MockDynamoDB) PutItem(input *dynamodb.PutItemInput) (*dynamodb.PutItemO } } + if x.shallowitem != nil { + mmLeft := make(map[string]*dynamodb.AttributeValue) + mmRight := make(map[string]*dynamodb.AttributeValue) + + for k, v := range x.shallowitem { + if !reflect.DeepEqual(v, input.Item[k]) { + mmLeft[k] = v + mmRight[k] = input.Item[k] + } + } + + if len(mmLeft) > 0 { + return &dynamodb.PutItemOutput{}, fmt.Errorf("Expect item %+v but found item %+v", mmLeft, mmRight) + } + } + // delete first element of expectation e.dynaMock.PutItemExpect = append(e.dynaMock.PutItemExpect[:0], e.dynaMock.PutItemExpect[1:]...) diff --git a/types.go b/types.go index f898888..9827b8a 100644 --- a/types.go +++ b/types.go @@ -51,9 +51,10 @@ type ( // PutItemExpectation struct hold expectation field, err, and result PutItemExpectation struct { - item map[string]*dynamodb.AttributeValue - table *string - output *dynamodb.PutItemOutput + item map[string]*dynamodb.AttributeValue + shallowitem map[string]*dynamodb.AttributeValue + table *string + output *dynamodb.PutItemOutput } // DeleteItemExpectation struct hold expectation field, err, and result From a006d7f81c6b5f3169290c75fe1d208add2e16ea Mon Sep 17 00:00:00 2001 From: Jacob Alberty Date: Mon, 21 Feb 2022 11:28:38 -0600 Subject: [PATCH 2/3] Add document for WithShallowItems --- put_item.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/put_item.go b/put_item.go index b1b0758..55618ec 100644 --- a/put_item.go +++ b/put_item.go @@ -21,6 +21,8 @@ func (e *PutItemExpectation) WithItems(item map[string]*dynamodb.AttributeValue) return e } +// WithShalowItems - method to set a shallow Items expectation +// This will only compare the items in the expectation and not throw an error on missing items func (e *PutItemExpectation) WithShallowItems(item map[string]*dynamodb.AttributeValue) *PutItemExpectation { e.shallowitem = item return e From 1ba26f49c85b7cae5e5d96d4b31f3ebd79b40149 Mon Sep 17 00:00:00 2001 From: Jacob Alberty Date: Mon, 21 Feb 2022 11:45:46 -0600 Subject: [PATCH 3/3] Add shallow check to PutItemWithContext as well --- put_item.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/put_item.go b/put_item.go index 55618ec..e34246b 100644 --- a/put_item.go +++ b/put_item.go @@ -93,6 +93,22 @@ func (e *MockDynamoDB) PutItemWithContext(ctx aws.Context, input *dynamodb.PutIt } } + if x.shallowitem != nil { + mmLeft := make(map[string]*dynamodb.AttributeValue) + mmRight := make(map[string]*dynamodb.AttributeValue) + + for k, v := range x.shallowitem { + if !reflect.DeepEqual(v, input.Item[k]) { + mmLeft[k] = v + mmRight[k] = input.Item[k] + } + } + + if len(mmLeft) > 0 { + return &dynamodb.PutItemOutput{}, fmt.Errorf("Expect item %+v but found item %+v", mmLeft, mmRight) + } + } + // delete first element of expectation e.dynaMock.PutItemExpect = append(e.dynaMock.PutItemExpect[:0], e.dynaMock.PutItemExpect[1:]...)