Created
December 11, 2020 23:36
-
-
Save diyan/065a83d27ef9a2e5647fa505a1c5555d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package loose | |
import ( | |
"encoding/json" | |
"testing" | |
"time" | |
"github.com/mitchellh/mapstructure" | |
"github.com/pkg/errors" | |
"github.com/stretchr/testify/assert" | |
"github.com/stretchr/testify/require" | |
) | |
type TimeSample struct { | |
Value time.Time `in:"value"` | |
} | |
func parseSample(t *testing.T, input string) TimeSample { | |
noError := require.New(t).NoError | |
inputMap := map[string]interface{}{} | |
err := json.Unmarshal([]byte(input), &inputMap) | |
noError(errors.Wrapf(err, "input is not a valid JSON")) | |
result := TimeSample{} | |
config := mapstructure.DecoderConfig{ | |
DecodeHook: TimeDecodeHook, | |
TagName: "in", | |
Result: &result, | |
} | |
decoder, err := mapstructure.NewDecoder(&config) | |
noError(errors.Wrapf(err, "can not init mapstructure decoder")) | |
err = decoder.Decode(inputMap) | |
noError(errors.Wrapf(err, "can not decode sample")) | |
return result | |
} | |
func Test_Date_T_Time_9_digit_fraction_of_second_Z_as_timezone(t *testing.T) { | |
res := parseSample(t, `{ "value": "2020-12-31T23:59:59.999999999Z" }`) | |
expect, _ := time.Parse(time.RFC3339, "2020-12-31T23:59:59.999999999Z") | |
assert.Equal(t, expect, res.Value) | |
} | |
func Test_Date_space_Time_9_digit_fraction_of_second_Z_as_timezone(t *testing.T) { | |
res := parseSample(t, `{ "value": "2020-12-31 23:59:59.999999999Z" }`) | |
expect, _ := time.Parse(time.RFC3339, "2020-12-31T23:59:59.999999999Z") | |
assert.Equal(t, expect, res.Value) | |
} | |
func Test_Date_space_Time_1_digit_fraction_of_second_Z_as_timezone(t *testing.T) { | |
res := parseSample(t, `{ "value": "2020-12-31 23:59:59.9Z" }`) | |
expect, _ := time.Parse(time.RFC3339, "2020-12-31T23:59:59.9Z") | |
assert.Equal(t, expect, res.Value) | |
} | |
func Test_Date_space_Time_no_fraction_of_second_Z_as_timezone(t *testing.T) { | |
res := parseSample(t, `{ "value": "2020-12-31 23:59:59Z" }`) | |
expect, _ := time.Parse(time.RFC3339, "2020-12-31T23:59:59Z") | |
assert.Equal(t, expect, res.Value) | |
} | |
func Test_Date_space_Time_9_digit_fraction_of_second_no_timezone(t *testing.T) { | |
res := parseSample(t, `{ "value": "2020-12-31 23:59:59.999999999" }`) | |
expect, _ := time.Parse(time.RFC3339, "2020-12-31T23:59:59.999999999Z") | |
assert.Equal(t, expect, res.Value) | |
} | |
func Test_Date_space_Time_1_digit_fraction_of_second_no_timezone(t *testing.T) { | |
res := parseSample(t, `{ "value": "2020-12-31 23:59:59.9" }`) | |
expect, _ := time.Parse(time.RFC3339, "2020-12-31T23:59:59.9Z") | |
assert.Equal(t, expect, res.Value) | |
} | |
func Test_Date_space_Time_no_fraction_of_second_no_timezone(t *testing.T) { | |
res := parseSample(t, `{ "value": "2020-12-31 23:59:59" }`) | |
expect, _ := time.Parse(time.RFC3339, "2020-12-31T23:59:59Z") | |
assert.Equal(t, expect, res.Value) | |
} | |
func Test_Null_value(t *testing.T) { | |
res := parseSample(t, `{ "value": null }`) | |
assert.True(t, res.Value.IsZero()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment