...

Source file src/github.com/golang/protobuf/ptypes/timestamp_test.go

Documentation: github.com/golang/protobuf/ptypes

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     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  	// The timestamp representing the Unix epoch date.
    23  	{&tspb.Timestamp{Seconds: 0, Nanos: 0}, true, utcDate(1970, 1, 1)},
    24  	// The smallest representable timestamp.
    25  	{&tspb.Timestamp{Seconds: math.MinInt64, Nanos: math.MinInt32}, false,
    26  		time.Unix(math.MinInt64, math.MinInt32).UTC()},
    27  	// The smallest representable timestamp with non-negative nanos.
    28  	{&tspb.Timestamp{Seconds: math.MinInt64, Nanos: 0}, false, time.Unix(math.MinInt64, 0).UTC()},
    29  	// The earliest valid timestamp.
    30  	{&tspb.Timestamp{Seconds: minValidSeconds, Nanos: 0}, true, utcDate(1, 1, 1)},
    31  	//"0001-01-01T00:00:00Z"},
    32  	// The largest representable timestamp.
    33  	{&tspb.Timestamp{Seconds: math.MaxInt64, Nanos: math.MaxInt32}, false,
    34  		time.Unix(math.MaxInt64, math.MaxInt32).UTC()},
    35  	// The largest representable timestamp with nanos in range.
    36  	{&tspb.Timestamp{Seconds: math.MaxInt64, Nanos: 1e9 - 1}, false,
    37  		time.Unix(math.MaxInt64, 1e9-1).UTC()},
    38  	// The largest valid timestamp.
    39  	{&tspb.Timestamp{Seconds: maxValidSeconds - 1, Nanos: 1e9 - 1}, true,
    40  		time.Date(9999, 12, 31, 23, 59, 59, 1e9-1, time.UTC)},
    41  	// The smallest invalid timestamp that is larger than the valid range.
    42  	{&tspb.Timestamp{Seconds: maxValidSeconds, Nanos: 0}, false, time.Unix(maxValidSeconds, 0).UTC()},
    43  	// A date before the epoch.
    44  	{&tspb.Timestamp{Seconds: -281836800, Nanos: 0}, true, utcDate(1961, 1, 26)},
    45  	// A date after the epoch.
    46  	{&tspb.Timestamp{Seconds: 1296000000, Nanos: 0}, true, utcDate(2011, 1, 26)},
    47  	// A date after the epoch, in the middle of the day.
    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  	// Special case: a nil Timestamp is an error, but returns the 0 Unix time.
    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  	// No corresponding special case here: no time.Time results in a nil Timestamp.
    91  }
    92  
    93  func TestTimestampString(t *testing.T) {
    94  	for _, test := range []struct {
    95  		ts   *tspb.Timestamp
    96  		want string
    97  	}{
    98  		// Not much testing needed because presumably time.Format is
    99  		// well-tested.
   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  	// Bracket the expected time.
   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