Skip to content

Commit

Permalink
Refine UnixMilli tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lippserd committed May 7, 2024
1 parent 44f1a5f commit 2c8b5bf
Showing 1 changed file with 112 additions and 65 deletions.
177 changes: 112 additions & 65 deletions pkg/types/unix_milli_test.go
Original file line number Diff line number Diff line change
@@ -1,96 +1,143 @@
package types

import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
"time"
"unicode/utf8"
)

func TestUnixMilli_MarshalJSON(t *testing.T) {
subtests := []struct {
name string
input UnixMilli
output string
}{
{"zero", UnixMilli{}, "null"},
{"epoch", UnixMilli(time.Unix(0, 0)), "0"},
{"nonzero", UnixMilli(time.Unix(1234567890, 62000000)), "1234567890062"},
func TestUnixMilli(t *testing.T) {
type testCase struct {
v UnixMilli
json string
text string
}

tests := map[string]testCase{
"Zero": {UnixMilli{}, "null", ""},
"Non-zero": {UnixMilli(time.Unix(1234567890, 0)), "1234567890000", "1234567890000"},
"Epoch": {UnixMilli(time.Unix(0, 0)), "0", "0"},
"With milliseconds": {UnixMilli(time.Unix(1234567890, 62000000)), "1234567890062", "1234567890062"},
}

for _, st := range subtests {
t.Run(st.name, func(t *testing.T) {
actual, err := st.input.MarshalJSON()
var runTests = func(t *testing.T, f func(*testing.T, testCase)) {
for name, test := range tests {
t.Run(name, func(t *testing.T) {
f(t, test)
})
}
}

t.Run("MarshalJSON", func(t *testing.T) {
runTests(t, func(t *testing.T, test testCase) {
actual, err := test.v.MarshalJSON()
require.NoError(t, err)
require.True(t, utf8.Valid(actual))
require.Equal(t, st.output, string(actual))
require.Equal(t, test.json, string(actual))
})
}
}
})

func TestUnixMilli_UnmarshalJSON(t *testing.T) {
subtests := []struct {
name string
input UnixMilli
output string
}{
{"zero", UnixMilli{}, "null"},
{"epoch", UnixMilli(time.Unix(0, 0)), "0"},
{"nonzero", UnixMilli(time.Unix(1234567890, 62000000)), "1234567890062"},
}
t.Run("UnmarshalJSON", func(t *testing.T) {
runTests(t, func(t *testing.T, test testCase) {
var actual UnixMilli
err := actual.UnmarshalJSON([]byte(test.json))
require.NoError(t, err)
require.Equal(t, test.v, actual)
})
})

for _, st := range subtests {
t.Run(st.name, func(t *testing.T) {
var s UnixMilli
err := s.UnmarshalJSON([]byte(st.output))
t.Run("MarshalText", func(t *testing.T) {
runTests(t, func(t *testing.T, test testCase) {
actual, err := test.v.MarshalText()
require.NoError(t, err)
require.True(t, utf8.Valid(actual))
require.Equal(t, test.text, string(actual))
})
})

t.Run("UnmarshalText", func(t *testing.T) {
runTests(t, func(t *testing.T, test testCase) {
var actual UnixMilli
err := actual.UnmarshalText([]byte(test.text))
require.NoError(t, err)
require.Equal(t, st.input, s)
require.Equal(t, test.v, actual)
})
}
})
}

func TestUnixMilli_MarshalText(t *testing.T) {
subtests := []struct {
name string
input UnixMilli
output string
func TestUnixMilli_Scan(t *testing.T) {
tests := []struct {
name string
v any
expected UnixMilli
expectErr bool
}{
{"zero", UnixMilli{}, ""},
{"epoch", UnixMilli(time.Unix(0, 0)), "0"},
{"nonzero", UnixMilli(time.Unix(1234567890, 62000000)), "1234567890062"},
{
name: "Nil",
v: nil,
expected: UnixMilli{},
},
{
name: "Epoch",
v: int64(0),
expected: UnixMilli(time.Unix(0, 0)),
},
{
name: "bytes",
v: []byte("1234567890062"),
expected: UnixMilli(time.Unix(1234567890, 62000000)),
},
{
name: "Invalid bytes",
v: []byte("invalid"),
expectErr: true,
},
{
name: "int64",
v: int64(1234567890062),
expected: UnixMilli(time.Unix(1234567890, 62000000)),
},
{
name: "uint64",
v: uint64(1234567890062),
expected: UnixMilli(time.Unix(1234567890, 62000000)),
},
{
name: "Invalid type",
v: "invalid",
expectErr: true,
},
}

for _, st := range subtests {
t.Run(st.name, func(t *testing.T) {
actual, err := st.input.MarshalText()

require.NoError(t, err)
require.True(t, utf8.Valid(actual))
require.Equal(t, st.output, string(actual))
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var actual UnixMilli
err := actual.Scan(test.v)
if test.expectErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, test.expected, actual)
}
})
}
}

func TestUnixMilli_UnmarshalText(t *testing.T) {
subtests := []struct {
name string
input UnixMilli
output string
}{
{"zero", UnixMilli{}, ""},
{"epoch", UnixMilli(time.Unix(0, 0)), "0"},
{"nonzero", UnixMilli(time.Unix(1234567890, 62000000)), "1234567890062"},
}

for _, st := range subtests {
t.Run(st.name, func(t *testing.T) {
var s UnixMilli
err := s.UnmarshalText([]byte(st.output))
func TestUnixMilli_Value(t *testing.T) {
t.Run("Zero", func(t *testing.T) {
var zero UnixMilli
actual, err := zero.Value()
require.NoError(t, err)
require.Nil(t, actual)
})

require.NoError(t, err)
require.Equal(t, st.input, s)
})
}
t.Run("Non-zero", func(t *testing.T) {
withMilliseconds := time.Unix(1234567890, 62000000)
expected := withMilliseconds.UnixMilli()
actual, err := UnixMilli(withMilliseconds).Value()
assert.NoError(t, err)
assert.Equal(t, expected, actual)
})
}

0 comments on commit 2c8b5bf

Please sign in to comment.