-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements Query() and changes how PutItem() works
- Loading branch information
Showing
8 changed files
with
293 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package mockdynamodb | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/request" | ||
"github.com/aws/aws-sdk-go/service/dynamodb" | ||
) | ||
|
||
// Query is implemented. | ||
// Use AddReturnQueryOutput() set QueryOutput for Query() to return. | ||
// Query() will return QueryOutputs in the same order in which AddReturnQueryOutput() is called. | ||
// If a nil value is given to AddReturnQueryOutput() then Query() will return an error. | ||
// Use ReceivedQueryInputs() retrieve the inputs given to Query(), use for asserting. | ||
func (db *DynamoDb) Query(input *dynamodb.QueryInput) (*dynamodb.QueryOutput, error) { | ||
db.Lock() | ||
defer db.Unlock() | ||
|
||
return db.query(input) | ||
} | ||
|
||
// QueryWithContext is not implemented. It will panic in all cases. | ||
func (db *DynamoDb) QueryWithContext(aws.Context, *dynamodb.QueryInput, ...request.Option) (*dynamodb.QueryOutput, error) { | ||
panic("QueryWithContext is not implemented") | ||
} | ||
|
||
// QueryRequest is not implemented. It will panic in all cases. | ||
func (db *DynamoDb) QueryRequest(*dynamodb.QueryInput) (*request.Request, *dynamodb.QueryOutput) { | ||
panic("QueryRequest is not implemented") | ||
} | ||
|
||
// QueryPages is not implemented. It will panic in all cases. | ||
func (db *DynamoDb) QueryPages(*dynamodb.QueryInput, func(*dynamodb.QueryOutput, bool) bool) error { | ||
panic("QueryPages is not implemented") | ||
} | ||
|
||
// QueryPagesWithContext is not implemented. It will panic in all cases. | ||
func (db *DynamoDb) QueryPagesWithContext(aws.Context, *dynamodb.QueryInput, func(*dynamodb.QueryOutput, bool) bool, ...request.Option) error { | ||
panic("QueryPagesWithContext is not implemented") | ||
} | ||
|
||
func (db *DynamoDb) query(input *dynamodb.QueryInput) (*dynamodb.QueryOutput, error) { | ||
err := checkRequiredFields(map[string]interface{}{ | ||
"QueryInput.TableName": input.TableName, | ||
}) | ||
if err != nil { | ||
return &dynamodb.QueryOutput{}, err | ||
} | ||
|
||
if table := db.GetTable(*input.TableName); table != nil { | ||
table.receivedQueryInputs = append(table.receivedQueryInputs, *input) | ||
if len(table.returnQueryOutputs) > 0 { | ||
o := table.popReturnQueryOutput() | ||
if o != nil { | ||
return o, nil | ||
} | ||
return nil, errors.New("nil QueryOutput") | ||
} | ||
|
||
return nil, errors.New("no QueryOutputs to return") | ||
} | ||
|
||
return &dynamodb.QueryOutput{}, errorNonExistentTable() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package mockdynamodb_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/service/dynamodb" | ||
|
||
"github.com/ebh/mockdynamodb" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDynamoDb_Query(t *testing.T) { | ||
t.Run("TableNameNotSet", func(t *testing.T) { | ||
tableName := "mockTable" | ||
db := mockdynamodb.NewWithTables([]string{tableName}) | ||
|
||
_, err := db.Query(&dynamodb.QueryInput{}) | ||
|
||
assert.EqualError(t, err, "InvalidParameter: 1 validation error(s) found.\n- missing required field, QueryInput.TableName.\n") | ||
}) | ||
|
||
t.Run("NonExistentTable", func(t *testing.T) { | ||
tableName := "mockTable" | ||
db := mockdynamodb.NewWithTables([]string{"anotherMockTable"}) | ||
|
||
_, err := db.Query(&dynamodb.QueryInput{TableName: &tableName}) | ||
|
||
assertRegexpError(t, err, "AWS.DynamoDB.NonExistentTable: The specified table does not exist for this wsdl version.\\n\\tstatus code: 400, request id: "+uuidRegexp+"$") | ||
}) | ||
|
||
t.Run("QueryInputRecorded", func(t *testing.T) { | ||
tableName := "mockTable" | ||
QueryInput := dynamodb.QueryInput{TableName: &tableName} | ||
QueryOutput := dynamodb.QueryOutput{} | ||
|
||
db := mockdynamodb.NewWithTables([]string{tableName}) | ||
db.GetTable(tableName).AddReturnQueryOutput(&QueryOutput) | ||
|
||
output, err := db.Query(&QueryInput) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, &[]dynamodb.QueryInput{QueryInput}, db.GetTable(tableName).ReceivedQueryInputs()) | ||
assert.Equal(t, &QueryOutput, output) | ||
}) | ||
|
||
t.Run("QueryOutputIsNull", func(t *testing.T) { | ||
tableName := "mockTable" | ||
Query := dynamodb.QueryInput{TableName: &tableName} | ||
|
||
db := mockdynamodb.NewWithTables([]string{tableName}) | ||
db.GetTable(tableName).AddReturnQueryOutput(nil) | ||
|
||
_, err := db.Query(&Query) | ||
|
||
assert.Error(t, err) | ||
assert.Equal(t, &[]dynamodb.QueryInput{Query}, db.GetTable(tableName).ReceivedQueryInputs()) | ||
}) | ||
|
||
t.Run("QueryOutputEmpty", func(t *testing.T) { | ||
tableName := "mockTable" | ||
Query := dynamodb.QueryInput{TableName: &tableName} | ||
|
||
db := mockdynamodb.NewWithTables([]string{tableName}) | ||
|
||
_, err := db.Query(&Query) | ||
|
||
assert.Error(t, err) | ||
assert.Equal(t, &[]dynamodb.QueryInput{Query}, db.GetTable(tableName).ReceivedQueryInputs()) | ||
}) | ||
} | ||
|
||
func TestDynamoDb_QueryWithContext(t *testing.T) { | ||
assert.PanicsWithValue(t, "QueryWithContext is not implemented", func() { | ||
_, err := mockdynamodb.New().QueryWithContext(context.TODO(), nil) | ||
require.NoError(t, err) | ||
}) | ||
} | ||
|
||
func TestDynamoDb_QueryRequest(t *testing.T) { | ||
assert.PanicsWithValue(t, "QueryRequest is not implemented", func() { | ||
mockdynamodb.New().QueryRequest(nil) | ||
}) | ||
} | ||
|
||
func TestDynamoDb_QueryPages(t *testing.T) { | ||
assert.PanicsWithValue(t, "QueryPages is not implemented", func() { | ||
err := mockdynamodb.New().QueryPages(nil, nil) | ||
require.NoError(t, err) | ||
}) | ||
} | ||
|
||
func TestDynamoDb_QueryPagesWithContext(t *testing.T) { | ||
assert.PanicsWithValue(t, "QueryPagesWithContext is not implemented", func() { | ||
err := mockdynamodb.New().QueryPagesWithContext(context.TODO(), nil, nil) | ||
require.NoError(t, err) | ||
}) | ||
} |
Oops, something went wrong.