timestamp_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2016 The Go Authors. All rights reserved.
  4. // https://github.com/golang/protobuf
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. package ptypes
  32. import (
  33. "math"
  34. "testing"
  35. "time"
  36. "github.com/golang/protobuf/proto"
  37. tspb "github.com/golang/protobuf/ptypes/timestamp"
  38. )
  39. var tests = []struct {
  40. ts *tspb.Timestamp
  41. valid bool
  42. t time.Time
  43. }{
  44. // The timestamp representing the Unix epoch date.
  45. {&tspb.Timestamp{Seconds: 0, Nanos: 0}, true, utcDate(1970, 1, 1)},
  46. // The smallest representable timestamp.
  47. {&tspb.Timestamp{Seconds: math.MinInt64, Nanos: math.MinInt32}, false,
  48. time.Unix(math.MinInt64, math.MinInt32).UTC()},
  49. // The smallest representable timestamp with non-negative nanos.
  50. {&tspb.Timestamp{Seconds: math.MinInt64, Nanos: 0}, false, time.Unix(math.MinInt64, 0).UTC()},
  51. // The earliest valid timestamp.
  52. {&tspb.Timestamp{Seconds: minValidSeconds, Nanos: 0}, true, utcDate(1, 1, 1)},
  53. //"0001-01-01T00:00:00Z"},
  54. // The largest representable timestamp.
  55. {&tspb.Timestamp{Seconds: math.MaxInt64, Nanos: math.MaxInt32}, false,
  56. time.Unix(math.MaxInt64, math.MaxInt32).UTC()},
  57. // The largest representable timestamp with nanos in range.
  58. {&tspb.Timestamp{Seconds: math.MaxInt64, Nanos: 1e9 - 1}, false,
  59. time.Unix(math.MaxInt64, 1e9-1).UTC()},
  60. // The largest valid timestamp.
  61. {&tspb.Timestamp{Seconds: maxValidSeconds - 1, Nanos: 1e9 - 1}, true,
  62. time.Date(9999, 12, 31, 23, 59, 59, 1e9-1, time.UTC)},
  63. // The smallest invalid timestamp that is larger than the valid range.
  64. {&tspb.Timestamp{Seconds: maxValidSeconds, Nanos: 0}, false, time.Unix(maxValidSeconds, 0).UTC()},
  65. // A date before the epoch.
  66. {&tspb.Timestamp{Seconds: -281836800, Nanos: 0}, true, utcDate(1961, 1, 26)},
  67. // A date after the epoch.
  68. {&tspb.Timestamp{Seconds: 1296000000, Nanos: 0}, true, utcDate(2011, 1, 26)},
  69. // A date after the epoch, in the middle of the day.
  70. {&tspb.Timestamp{Seconds: 1296012345, Nanos: 940483}, true,
  71. time.Date(2011, 1, 26, 3, 25, 45, 940483, time.UTC)},
  72. }
  73. func TestValidateTimestamp(t *testing.T) {
  74. for _, s := range tests {
  75. got := validateTimestamp(s.ts)
  76. if (got == nil) != s.valid {
  77. t.Errorf("validateTimestamp(%v) = %v, want %v", s.ts, got, s.valid)
  78. }
  79. }
  80. }
  81. func TestTimestamp(t *testing.T) {
  82. for _, s := range tests {
  83. got, err := Timestamp(s.ts)
  84. if (err == nil) != s.valid {
  85. t.Errorf("Timestamp(%v) error = %v, but valid = %t", s.ts, err, s.valid)
  86. } else if s.valid && got != s.t {
  87. t.Errorf("Timestamp(%v) = %v, want %v", s.ts, got, s.t)
  88. }
  89. }
  90. // Special case: a nil Timestamp is an error, but returns the 0 Unix time.
  91. got, err := Timestamp(nil)
  92. want := time.Unix(0, 0).UTC()
  93. if got != want {
  94. t.Errorf("Timestamp(nil) = %v, want %v", got, want)
  95. }
  96. if err == nil {
  97. t.Errorf("Timestamp(nil) error = nil, expected error")
  98. }
  99. }
  100. func TestTimestampProto(t *testing.T) {
  101. for _, s := range tests {
  102. got, err := TimestampProto(s.t)
  103. if (err == nil) != s.valid {
  104. t.Errorf("TimestampProto(%v) error = %v, but valid = %t", s.t, err, s.valid)
  105. } else if s.valid && !proto.Equal(got, s.ts) {
  106. t.Errorf("TimestampProto(%v) = %v, want %v", s.t, got, s.ts)
  107. }
  108. }
  109. // No corresponding special case here: no time.Time results in a nil Timestamp.
  110. }
  111. func TestTimestampString(t *testing.T) {
  112. for _, test := range []struct {
  113. ts *tspb.Timestamp
  114. want string
  115. }{
  116. // Not much testing needed because presumably time.Format is
  117. // well-tested.
  118. {&tspb.Timestamp{Seconds: 0, Nanos: 0}, "1970-01-01T00:00:00Z"},
  119. {&tspb.Timestamp{Seconds: minValidSeconds - 1, Nanos: 0}, "(timestamp: seconds:-62135596801 before 0001-01-01)"},
  120. } {
  121. got := TimestampString(test.ts)
  122. if got != test.want {
  123. t.Errorf("TimestampString(%v) = %q, want %q", test.ts, got, test.want)
  124. }
  125. }
  126. }
  127. func utcDate(year, month, day int) time.Time {
  128. return time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC)
  129. }
  130. func TestTimestampNow(t *testing.T) {
  131. // Bracket the expected time.
  132. before := time.Now()
  133. ts := TimestampNow()
  134. after := time.Now()
  135. tm, err := Timestamp(ts)
  136. if err != nil {
  137. t.Errorf("between %v and %v\nTimestampNow() = %v\nwhich is invalid (%v)", before, after, ts, err)
  138. }
  139. if tm.Before(before) || tm.After(after) {
  140. t.Errorf("between %v and %v\nTimestamp(TimestampNow()) = %v", before, after, tm)
  141. }
  142. }