github.com/aavshr/aws-sdk-go@v1.41.3/example/service/dynamodb/unitTest/README.md (about)

     1  # Example
     2  You can instantiate `*dynamodb.DynamoDB` and pass that as a parameter to all
     3  methods connecting to DynamoDB, or as `unitTest` demonstrates, create your own
     4  `type` and pass it along as a field.
     5  
     6  ## Test-compatible DynamoDB field
     7  If you use `*dynamodb.DynamoDB` as a field, you will be unable to unit test it,
     8  as documented in #88. Cast it instead as `dynamodbiface.DynamoDBAPI`:
     9  
    10  ```go
    11  type ItemGetter struct {
    12  		DynamoDB dynamodbiface.DynamoDBAPI
    13  }
    14  ```
    15  
    16  ## Querying actual DynamoDB
    17  You'll need an `*aws.Config` and `*session.Session` for these to work correctly:
    18  
    19  ```go
    20  // Setup
    21  var getter = new(ItemGetter)
    22  var config *aws.Config = &aws.Config{Region: aws.String("us-west-2"),}
    23  var sess *session.Session = session.NewSession(config)
    24  var svc *dynamodb.DynamoDB = dynamodb.New()
    25  getter.DynamoDB = dynamodbiface.DynamoDBAPI(svc)
    26  // Finally
    27  getter.DynamoDB.GetItem(/* ... */)
    28  ```
    29  
    30  ## Querying in tests
    31  Construct a `fakeDynamoDB` and add the necessary methods for each of those
    32  structs (custom ones for `ItemGetter` and [whatever methods you're using for
    33  DynamoDB](https://github.com/aavshr/aws-sdk-go/blob/main/service/dynamodb/dynamodbiface/interface.go)),
    34  and you're good to go!
    35  
    36  ```go
    37  type fakeDynamoDB struct {
    38  		dynamodbiface.DynamoDBAPI
    39  }
    40  var getter = new(ItemGetter)
    41  getter.DynamoDB = &fakeDynamoDB{}
    42  // And to run it (assuming you've mocked fakeDynamoDB.GetItem)
    43  getter.DynamoDB.GetItem(/* ... */)
    44  ```
    45  
    46  ## Output
    47  ```
    48  $ go test -tags example -cover
    49  PASS
    50  coverage: 100.0% of statements
    51  ok		_/Users/shatil/workspace/aws-sdk-go/example/service/dynamodb/unitTest	0.008s
    52  ```