Skip to content

Commit

Permalink
test: add unit tests for dynamomq errors
Browse files Browse the repository at this point in the history
  • Loading branch information
vvatanabe committed Nov 25, 2023
1 parent 0e08a61 commit a9fc1a8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
12 changes: 6 additions & 6 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,39 @@ type ConditionalCheckFailedError struct {
}

func (e ConditionalCheckFailedError) Error() string {
return fmt.Sprintf("Condition on the 'version' attribute has failed: %v", e.Cause)
return fmt.Sprintf("Condition on the 'version' attribute has failed: %v.", e.Cause)
}

type BuildingExpressionError struct {
Cause error
}

func (e BuildingExpressionError) Error() string {
return fmt.Sprintf("Failed to build expression: %v:", e.Cause)
return fmt.Sprintf("Failed to build expression: %v.", e.Cause)
}

type DynamoDBAPIError struct {
Cause error
}

func (e DynamoDBAPIError) Error() string {
return fmt.Sprintf("Failed DynamoDB API: %v:", e.Cause)
return fmt.Sprintf("Failed DynamoDB API: %v.", e.Cause)
}

type UnmarshalingAttributeError struct {
Cause error
}

func (e UnmarshalingAttributeError) Error() string {
return fmt.Sprintf("Failed to unmarshal: %v:", e.Cause)
return fmt.Sprintf("Failed to unmarshal: %v.", e.Cause)
}

type MarshalingAttributeError struct {
Cause error
}

func (e MarshalingAttributeError) Error() string {
return fmt.Sprintf("Failed to marshal: %v:", e.Cause)
return fmt.Sprintf("Failed to marshal: %v.", e.Cause)
}

type EmptyQueueError struct{}
Expand All @@ -73,5 +73,5 @@ type InvalidStateTransitionError struct {
}

func (e InvalidStateTransitionError) Error() string {
return fmt.Sprintf("operation %s failed for status %s: %s", e.Operation, e.Current, e.Msg)
return fmt.Sprintf("operation %s failed for status %s: %s.", e.Operation, e.Current, e.Msg)
}
33 changes: 33 additions & 0 deletions error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package dynamomq_test

import (
"errors"
"testing"

. "github.com/vvatanabe/dynamomq"
)

func TestErrors(t *testing.T) {
type testCase struct {
err error
expected string
}
tests := []testCase{
{IDNotProvidedError{}, "ID was not provided."},
{IDNotFoundError{}, "Provided ID was not found in the Dynamo DB."},
{IDDuplicatedError{}, "Provided ID was duplicated."},
{ConditionalCheckFailedError{Cause: errors.New("sample cause")}, "Condition on the 'version' attribute has failed: sample cause."},
{BuildingExpressionError{Cause: errors.New("sample cause")}, "Failed to build expression: sample cause."},
{DynamoDBAPIError{Cause: errors.New("sample cause")}, "Failed DynamoDB API: sample cause."},
{UnmarshalingAttributeError{Cause: errors.New("sample cause")}, "Failed to unmarshal: sample cause."},
{MarshalingAttributeError{Cause: errors.New("sample cause")}, "Failed to marshal: sample cause."},
{EmptyQueueError{}, "Cannot proceed, queue is empty."},
{InvalidStateTransitionError{Msg: "sample message", Operation: "sample operation", Current: "sample status"}, "operation sample operation failed for status sample status: sample message."},
}
for _, tc := range tests {
if tc.err.Error() != tc.expected {
t.Errorf("Unexpected error message. Expected: %v, got: %v", tc.expected, tc.err.Error())
}
}

}

0 comments on commit a9fc1a8

Please sign in to comment.