Skip to content

Commit

Permalink
Added some new unit tests for datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronsky committed Sep 1, 2020
1 parent 38f723e commit e258b21
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions asc/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,54 @@ func TestDateUnmarshalInvalidDate(t *testing.T) {
assert.Error(t, err)
}

type dateTimeContainer struct {
Field DateTime `json:"time"`
}

func newDateTimeContainer(year int, month time.Month, date int, hour int, min int, sec int, nsec int) dateTimeContainer {
return dateTimeContainer{
DateTime{
time.Date(year, month, date, hour, min, sec, nsec, time.UTC),
},
}
}

func dateTimeContainerJSON(dateTime string) string {
return fmt.Sprintf(`{"time":"%s"}`, dateTime)
}

func TestDateTimeMarshal(t *testing.T) {
want := dateTimeContainerJSON("2020-04-01T05:16:48.915+0000")
b := newDateTimeContainer(2020, 4, 1, 5, 16, 48, 915000000)
got, err := json.Marshal(b)
assert.NoError(t, err)
assert.JSONEq(t, want, string(got))
}

func TestDateTimeUnmarshal(t *testing.T) {
time.Local = time.UTC
want := time.Date(2020, 4, 1, 5, 16, 48, 915000000, time.Local)
jsonStr := dateTimeContainerJSON("2020-04-01T05:16:48.915+0000")
var b dateTimeContainer
err := json.Unmarshal([]byte(jsonStr), &b)
assert.NoError(t, err)
assert.Equal(t, b.Field.Time, want)
}

func TestDateTimeUnmarshalWrongType(t *testing.T) {
jsonStr := `{"time":-1}`
var b dateTimeContainer
err := json.Unmarshal([]byte(jsonStr), &b)
assert.Error(t, err)
}

func TestDateTimeUnmarshalInvalidDate(t *testing.T) {
jsonStr := dateTimeContainerJSON("TEST")
var b dateTimeContainer
err := json.Unmarshal([]byte(jsonStr), &b)
assert.Error(t, err)
}

type emailContainer struct {
Field Email `json:"email"`
}
Expand Down

0 comments on commit e258b21

Please sign in to comment.