Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shallow item comparison for PutItemExpectation #46

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions put_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ 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
}

// WillReturns - method for set desired result
func (e *PutItemExpectation) WillReturns(res dynamodb.PutItemOutput) *PutItemExpectation {
e.output = &res
Expand All @@ -44,6 +51,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:]...)

Expand All @@ -70,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:]...)

Expand Down
7 changes: 4 additions & 3 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down