1
2
3
4
5 package ptypes
6
7 import (
8 "math"
9 "testing"
10 "time"
11
12 "github.com/golang/protobuf/proto"
13
14 tspb "github.com/golang/protobuf/ptypes/timestamp"
15 )
16
17 var tests = []struct {
18 ts *tspb.Timestamp
19 valid bool
20 t time.Time
21 }{
22
23 {&tspb.Timestamp{Seconds: 0, Nanos: 0}, true, utcDate(1970, 1, 1)},
24
25 {&tspb.Timestamp{Seconds: math.MinInt64, Nanos: math.MinInt32}, false,
26 time.Unix(math.MinInt64, math.MinInt32).UTC()},
27
28 {&tspb.Timestamp{Seconds: math.MinInt64, Nanos: 0}, false, time.Unix(math.MinInt64, 0).UTC()},
29
30 {&tspb.Timestamp{Seconds: minValidSeconds, Nanos: 0}, true, utcDate(1, 1, 1)},
31
32
33 {&tspb.Timestamp{Seconds: math.MaxInt64, Nanos: math.MaxInt32}, false,
34 time.Unix(math.MaxInt64, math.MaxInt32).UTC()},
35
36 {&tspb.Timestamp{Seconds: math.MaxInt64, Nanos: 1e9 - 1}, false,
37 time.Unix(math.MaxInt64, 1e9-1).UTC()},
38
39 {&tspb.Timestamp{Seconds: maxValidSeconds - 1, Nanos: 1e9 - 1}, true,
40 time.Date(9999, 12, 31, 23, 59, 59, 1e9-1, time.UTC)},
41
42 {&tspb.Timestamp{Seconds: maxValidSeconds, Nanos: 0}, false, time.Unix(maxValidSeconds, 0).UTC()},
43
44 {&tspb.Timestamp{Seconds: -281836800, Nanos: 0}, true, utcDate(1961, 1, 26)},
45
46 {&tspb.Timestamp{Seconds: 1296000000, Nanos: 0}, true, utcDate(2011, 1, 26)},
47
48 {&tspb.Timestamp{Seconds: 1296012345, Nanos: 940483}, true,
49 time.Date(2011, 1, 26, 3, 25, 45, 940483, time.UTC)},
50 }
51
52 func TestValidateTimestamp(t *testing.T) {
53 for _, s := range tests {
54 got := validateTimestamp(s.ts)
55 if (got == nil) != s.valid {
56 t.Errorf("validateTimestamp(%v) = %v, want %v", s.ts, got, s.valid)
57 }
58 }
59 }
60
61 func TestTimestamp(t *testing.T) {
62 for _, s := range tests {
63 got, err := Timestamp(s.ts)
64 if (err == nil) != s.valid {
65 t.Errorf("Timestamp(%v) error = %v, but valid = %t", s.ts, err, s.valid)
66 } else if s.valid && got != s.t {
67 t.Errorf("Timestamp(%v) = %v, want %v", s.ts, got, s.t)
68 }
69 }
70
71 got, err := Timestamp(nil)
72 want := time.Unix(0, 0).UTC()
73 if got != want {
74 t.Errorf("Timestamp(nil) = %v, want %v", got, want)
75 }
76 if err == nil {
77 t.Errorf("Timestamp(nil) error = nil, expected error")
78 }
79 }
80
81 func TestTimestampProto(t *testing.T) {
82 for _, s := range tests {
83 got, err := TimestampProto(s.t)
84 if (err == nil) != s.valid {
85 t.Errorf("TimestampProto(%v) error = %v, but valid = %t", s.t, err, s.valid)
86 } else if s.valid && !proto.Equal(got, s.ts) {
87 t.Errorf("TimestampProto(%v) = %v, want %v", s.t, got, s.ts)
88 }
89 }
90
91 }
92
93 func TestTimestampString(t *testing.T) {
94 for _, test := range []struct {
95 ts *tspb.Timestamp
96 want string
97 }{
98
99
100 {&tspb.Timestamp{Seconds: 0, Nanos: 0}, "1970-01-01T00:00:00Z"},
101 } {
102 got := TimestampString(test.ts)
103 if got != test.want {
104 t.Errorf("TimestampString(%v) = %q, want %q", test.ts, got, test.want)
105 }
106 }
107 }
108
109 func utcDate(year, month, day int) time.Time {
110 return time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC)
111 }
112
113 func TestTimestampNow(t *testing.T) {
114
115 before := time.Now()
116 ts := TimestampNow()
117 after := time.Now()
118
119 tm, err := Timestamp(ts)
120 if err != nil {
121 t.Errorf("between %v and %v\nTimestampNow() = %v\nwhich is invalid (%v)", before, after, ts, err)
122 }
123 if tm.Before(before) || tm.After(after) {
124 t.Errorf("between %v and %v\nTimestamp(TimestampNow()) = %v", before, after, tm)
125 }
126 }
127
View as plain text