Giter Site home page Giter Site logo

go-dynamock-v2's Introduction

go-dynamock-v2

GoDoc Go Report Card Build Status Codecov

Amazon DynamoDB mock for unit testing, fully compatible with SDK.

Visit GoDoc for public API documentation.

Thanks to gusaul for the first version of package go-dynamock.

Requirements

Usage

To use mock you should depend in your code on ClientAPI interface, instead of dependency on specific DynamoDB instance.

package main

import (
    "github.com/aws/aws-sdk-go-v2/service/dynamodb/dynamodbiface"
)

type Service struct {
    DynamoDB dynamodbiface.ClientAPI
}

func NewService (dynamo dynamodbiface.ClientAPI) *Service {
    return &Service{
        DynamoDB: dynamo,
    }
}

Function you want to test

package main

import (
    "context"
    "strconv"
    
    "github.com/aws/aws-sdk-go-v2/aws"
    "github.com/aws/aws-sdk-go-v2/service/dynamodb"
    "github.com/aws/aws-sdk-go-v2/service/dynamodb/dynamodbattribute"
)

func GetNameByID(ID int) (*string, error) {
	param := &dynamodb.GetItemInput{
		Key: map[string]dynamodb.AttributeValue{
			"id": {
				N: aws.String(strconv.Itoa(ID)),
			},
		},
		TableName: aws.String("employee"),
	}

	req := Fake.DB.GetItemRequest(param)
	if req.Error != nil {
		return nil, req.Error
	}

	var value *string
	output, err := req.Send(context.Background())
	if err != nil {
		return nil, err
	}

	if v, ok := output.Item["name"]; ok {
		err := dynamodbattribute.Unmarshal(&v, &value)
		if err != nil {
			return value, err
		}
	}

	return value, nil
}

Test

package examples

import (
	"strconv"
	"testing"

	dynamock "github.com/groovili/go-dynamock-v2"

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

func init() {
	Fake = new(FakeDynamo)
	Fake.DB, Mock = dynamock.New()
}

func TestGetItem(t *testing.T) {
	ID := 123
	expectKey := map[string]dynamodb.AttributeValue{
		"id": {
			N: aws.String(strconv.Itoa(ID)),
		},
	}

	expectedResult := "rick sanchez"
	result := dynamodb.GetItemResponse{
		GetItemOutput: &dynamodb.GetItemOutput{
			Item: map[string]dynamodb.AttributeValue{
				"id": {
					N: aws.String(strconv.Itoa(ID)),
				},
				"name": {
					S: aws.String(expectedResult),
				},
			},
		},
	}

	Mock.ExpectGetItem().Table("employee").WithKeys(expectKey).WillReturn(result)

	actualResult, err := GetNameByID(ID)
	if err != nil {
		t.Fatal(err)
	}

	if aws.StringValue(actualResult) != expectedResult {
		t.Fatalf("Fail: expected: %s, got: %s", expectedResult, aws.StringValue(actualResult))
	}
}

Currently Supported Functions

GetItemRequest(*dynamodb.GetItemInput) dynamodb.GetItemRequest
PutItemRequest(*dynamodb.PutItemInput) dynamodb.PutItemRequest
UpdateItemRequest(*dynamodb.UpdateItemInput) dynamodb.UpdateItemRequest
DeleteItemRequest(*dynamodb.DeleteItemInput) dynamodb.DeleteItemRequest
BatchGetItemRequest(*dynamodb.BatchGetItemInput) dynamodb.BatchGetItemRequest
BatchWriteItemRequest(*dynamodb.BatchWriteItemInput) dynamodb.BatchWriteItemRequest
ScanRequest(*dynamodb.ScanInput) dynamodb.ScanRequest
QueryRequest(*dynamodb.QueryInput) dynamodb.QueryRequest
CreateTableRequest(*dynamodb.CreateTableInput) dynamodb.CreateTableRequest
DescribeTableRequest(*dynamodb.DescribeTableInput) dynamodb.DescribeTableRequest
WaitUntilTableExists(context.Context, *dynamodb.DescribeTableInput, ...aws.WaiterOption) error

Contributions

Feel free to open a pull request.

License

The MIT License

go-dynamock-v2's People

Contributors

aultokped avatar enicho avatar groovili avatar gusaul avatar mspiegel avatar zmalik avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.