mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 23:16:45 +00:00
* tag optional sonic and allow full library conversion * Add workflow and disallow arm and darwin usage * Add basic hotswap benchmark * linter: fix * use bash * linter: fix? * Fix whoopsie, add to make file, also add mention in features list. * test enforcement * actually read documentation see if this works * linter: fix * linter: fix * sonic: bump tagged version * encoding/json: drop build tag arch and os filters * encoding/json: consolidate tests * encoding/json: log build tag usage * rm superfluous builds * glorious/nits: add template change and regen docs * glorious/nits: update commentary on nolint directive * glorious/nits: rm init func and log results in main.go * Test to actually pull flag in * linter: fix * thrasher: nits * gk: nits 4 goflags goooooooooo! * gk: nits rn * make sonic default json implementation * screen 386 * linter: fix * Add commentary * glorious: nits Makefile not working * gk: nits * gk: nits whoops * whoopsirino * mention 32bit systems won't be sonic * gk: super-duper nit of extremes --------- Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
90 lines
3.0 KiB
Go
90 lines
3.0 KiB
Go
package types
|
|
|
|
import (
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"github.com/thrasher-corp/gocryptotrader/encoding/json"
|
|
)
|
|
|
|
func TestUnmarshalJSON(t *testing.T) {
|
|
t.Parallel()
|
|
var testTime Time
|
|
|
|
require.NoError(t, json.Unmarshal([]byte(`null`), &testTime))
|
|
assert.Equal(t, time.Time{}, testTime.Time())
|
|
|
|
require.NoError(t, json.Unmarshal([]byte(`0`), &testTime))
|
|
assert.Equal(t, time.Time{}, testTime.Time())
|
|
|
|
require.NoError(t, json.Unmarshal([]byte(`""`), &testTime))
|
|
assert.Equal(t, time.Time{}, testTime.Time())
|
|
|
|
require.NoError(t, json.Unmarshal([]byte(`"0"`), &testTime))
|
|
assert.Equal(t, time.Time{}, testTime.Time())
|
|
|
|
// seconds
|
|
require.NoError(t, json.Unmarshal([]byte(`"1628736847"`), &testTime))
|
|
assert.Equal(t, time.Unix(1628736847, 0), testTime.Time())
|
|
|
|
// milliseconds
|
|
require.NoError(t, json.Unmarshal([]byte(`"1726104395.5"`), &testTime))
|
|
assert.Equal(t, time.UnixMilli(1726104395500), testTime.Time())
|
|
|
|
require.NoError(t, json.Unmarshal([]byte(`"1726104395.56"`), &testTime))
|
|
assert.Equal(t, time.UnixMilli(1726104395560), testTime.Time())
|
|
|
|
require.NoError(t, json.Unmarshal([]byte(`"1628736847325"`), &testTime))
|
|
assert.Equal(t, time.UnixMilli(1628736847325), testTime.Time())
|
|
|
|
// microseconds
|
|
require.NoError(t, json.Unmarshal([]byte(`"1628736847325123"`), &testTime))
|
|
assert.Equal(t, time.UnixMicro(1628736847325123), testTime.Time())
|
|
|
|
require.NoError(t, json.Unmarshal([]byte(`"1726106210903.0"`), &testTime))
|
|
assert.Equal(t, time.UnixMicro(1726106210903000), testTime.Time())
|
|
|
|
// nanoseconds
|
|
require.NoError(t, json.Unmarshal([]byte(`"1606292218213.4578"`), &testTime))
|
|
assert.Equal(t, time.Unix(0, 1606292218213457800), testTime.Time())
|
|
|
|
require.NoError(t, json.Unmarshal([]byte(`"1606292218213457800"`), &testTime))
|
|
assert.Equal(t, time.Unix(0, 1606292218213457800), testTime.Time())
|
|
|
|
require.ErrorIs(t, json.Unmarshal([]byte(`"blurp"`), &testTime), strconv.ErrSyntax)
|
|
require.Error(t, json.Unmarshal([]byte(`"123456"`), &testTime))
|
|
|
|
// Captures bad syntax when type should be time.Time (RFC3339)
|
|
require.ErrorIs(t, json.Unmarshal([]byte(`"2025-03-28T08:00:00Z"`), &testTime), strconv.ErrSyntax)
|
|
// parse int failure
|
|
require.ErrorIs(t, json.Unmarshal([]byte(`"1606292218213.45.8"`), &testTime), strconv.ErrSyntax)
|
|
}
|
|
|
|
// 5030734 240.1 ns/op 168 B/op 2 allocs/op (current)
|
|
// 2716176 441.9 ns/op 352 B/op 6 allocs/op (previous)
|
|
func BenchmarkUnmarshalJSON(b *testing.B) {
|
|
var testTime Time
|
|
for i := 0; i < b.N; i++ {
|
|
err := json.Unmarshal([]byte(`"1691122380942.173000"`), &testTime)
|
|
require.NoError(b, err)
|
|
}
|
|
}
|
|
|
|
func TestTime(t *testing.T) {
|
|
t.Parallel()
|
|
testTime := Time(time.Time{})
|
|
assert.Equal(t, time.Time{}, testTime.Time())
|
|
assert.Equal(t, "0001-01-01 00:00:00 +0000 UTC", testTime.String())
|
|
}
|
|
|
|
func TestTime_MarshalJSON(t *testing.T) {
|
|
t.Parallel()
|
|
testTime := Time(time.Time{})
|
|
data, err := testTime.MarshalJSON()
|
|
require.NoError(t, err)
|
|
assert.Equal(t, `"0001-01-01T00:00:00Z"`, string(data))
|
|
}
|