-
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.
Currently only IsMalformed is being stored in Dynamo, as it only uses named fields. Since a shared.Date where IsMalformed is true will give an error to whatever is creating it I have chosen to Marshal as a date string.
- Loading branch information
Showing
4 changed files
with
84 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,73 @@ | ||
package shared | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue" | ||
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestUnmarshalDate(t *testing.T) { | ||
testCases := []struct { | ||
testCases := map[string]struct { | ||
name string | ||
in string | ||
expectFormatted string | ||
expectIsMalformed bool | ||
}{ | ||
{ | ||
name: "ok", | ||
"ok": { | ||
in: `"1930-10-31"`, | ||
expectFormatted: "31 October 1930", | ||
expectIsMalformed: false, | ||
}, | ||
{ | ||
name: "out of bounds", | ||
"out of bounds": { | ||
in: `"1930-11-31"`, | ||
expectFormatted: "1 January 0001", | ||
expectIsMalformed: true, | ||
}, | ||
{ | ||
name: "invalid string", | ||
"invalid string": { | ||
in: `"31 October 1930"`, | ||
expectFormatted: "1 January 0001", | ||
expectIsMalformed: true, | ||
}, | ||
{ | ||
name: "number", | ||
"number": { | ||
in: `1700240133`, | ||
expectFormatted: "1 January 0001", | ||
expectIsMalformed: true, | ||
}, | ||
"empty string": { | ||
in: `""`, | ||
expectFormatted: "1 January 0001", | ||
expectIsMalformed: false, | ||
}, | ||
"space": { | ||
in: `" "`, | ||
expectFormatted: "1 January 0001", | ||
expectIsMalformed: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
date := Date{} | ||
date.UnmarshalJSON([]byte(tc.in)) | ||
for name, tc := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
var date Date | ||
err := json.Unmarshal([]byte(tc.in), &date) | ||
|
||
assert.Nil(t, err) | ||
assert.Equal(t, tc.expectFormatted, date.Time.Format("2 January 2006")) | ||
assert.Equal(t, tc.expectIsMalformed, date.IsMalformed) | ||
}) | ||
} | ||
} | ||
|
||
func TestDateDynamoDB(t *testing.T) { | ||
in := &types.AttributeValueMemberS{Value: "2000-01-02"} | ||
|
||
var v Date | ||
attributevalue.Unmarshal(in, &v) | ||
assert.Equal(t, Date{Time: time.Date(2000, time.January, 2, 0, 0, 0, 0, time.UTC)}, v) | ||
|
||
out, _ := attributevalue.Marshal(v) | ||
assert.Equal(t, in, out) | ||
} |