From a9fc1a84c22bec06ef203b4595b7c39b80549557 Mon Sep 17 00:00:00 2001 From: vvatanabe Date: Sun, 26 Nov 2023 07:06:34 +0900 Subject: [PATCH] test: add unit tests for dynamomq errors --- error.go | 12 ++++++------ error_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 error_test.go diff --git a/error.go b/error.go index b5fd329..fe728c4 100644 --- a/error.go +++ b/error.go @@ -25,7 +25,7 @@ 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 { @@ -33,7 +33,7 @@ type BuildingExpressionError struct { } 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 { @@ -41,7 +41,7 @@ type DynamoDBAPIError struct { } 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 { @@ -49,7 +49,7 @@ type UnmarshalingAttributeError struct { } 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 { @@ -57,7 +57,7 @@ type MarshalingAttributeError struct { } 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{} @@ -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) } diff --git a/error_test.go b/error_test.go new file mode 100644 index 0000000..531e6a3 --- /dev/null +++ b/error_test.go @@ -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()) + } + } + +}