-
Notifications
You must be signed in to change notification settings - Fork 6
/
milliseconds_value_test.go
58 lines (51 loc) · 1.21 KB
/
milliseconds_value_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package revcatgo
import (
"encoding/json"
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
"gopkg.in/guregu/null.v4"
)
func TestNewMilliSeconds(t *testing.T) {
cases := []struct {
in int64
expected time.Time
err error
}{
{1605526568000, time.Unix(1605526568000/1000, 0), nil},
{16055265, time.Unix(0, 0), errors.New("milliseconds should be grater than 100000000")},
{0, time.Unix(0, 0), nil},
}
for _, c := range cases {
actual, err := newMilliseconds(null.IntFrom(c.in))
if err == nil {
assert.Equal(t, c.expected, actual.DateTime())
assert.Nil(t, err)
} else {
assert.EqualError(t, err, c.err.Error())
}
}
}
func TestMilliSecondsUnMarshal(t *testing.T) {
cases := []struct {
in string
expected int64
err error
}{
{`1605526568000`, 1605526568000, nil},
{`16055265`, 0, errors.New("failed to unmarshal the value of milliseconds: milliseconds should be grater than 100000000")},
{`null`, 0, nil},
}
for _, c := range cases {
var m milliseconds
b := []byte(c.in)
err := json.Unmarshal(b, &m)
if err == nil {
assert.Equal(t, c.expected, m.Int64())
assert.Nil(t, err)
} else {
assert.EqualError(t, err, c.err.Error())
}
}
}