-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimestamp_test.go
More file actions
56 lines (46 loc) · 1.33 KB
/
timestamp_test.go
File metadata and controls
56 lines (46 loc) · 1.33 KB
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
package proto
import (
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestTimestampNow(t *testing.T) {
tsNanos := TimestampNow().Time().UnixNano()
now := time.Now()
assert.True(t, tsNanos-now.UnixNano() < 1000)
}
func TestTimestampTime(t *testing.T) {
when := time.Now().Add(-time.Hour * 5)
ts := TimestampTime(when)
assert.Equal(t, when.UnixNano(), ts.Time().UnixNano())
}
func TestTimestamp_Time_Nanos(t *testing.T) {
ts := &Timestamp{Unit: &Timestamp_Nanoseconds{Nanoseconds: 100}}
nanos := ts.Time().UnixNano()
assert.Equal(t, int64(100*time.Nanosecond), nanos)
}
func TestTimestamp_Time_Millis(t *testing.T) {
ts := &Timestamp{Unit: &Timestamp_Milliseconds{Milliseconds: 100}}
nanos := ts.Time().UnixNano()
assert.Equal(t, int64(100*time.Millisecond), nanos)
}
func TestTimestamp_Time_Seconds(t *testing.T) {
ts := &Timestamp{Unit: &Timestamp_Seconds{Seconds: 100}}
nanos := ts.Time().UnixNano()
assert.Equal(t, int64(100*time.Second), nanos)
}
func TestTimestamp_Time_Nil(t *testing.T) {
ts := &Timestamp{Unit: nil}
nanos := ts.Time().UnixNano()
assert.Equal(t, int64(0), nanos)
}
type unknownTime struct{}
func (u *unknownTime) isTimestamp_Unit() {}
func TestTimestamp_Time_unknown(t *testing.T) {
defer func() {
err := recover()
assert.NotNil(t, err)
}()
pb := &Timestamp{Unit: &unknownTime{}}
pb.Time()
}