github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/apis/meta/v1/micro_time_test.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package v1
    18  
    19  import (
    20  	"encoding/json"
    21  	"reflect"
    22  	"testing"
    23  	"time"
    24  
    25  	"sigs.k8s.io/yaml"
    26  )
    27  
    28  type MicroTimeHolder struct {
    29  	T MicroTime `json:"t"`
    30  }
    31  
    32  func TestMicroTimeMarshalYAML(t *testing.T) {
    33  	cases := []struct {
    34  		input  MicroTime
    35  		result string
    36  	}{
    37  		{MicroTime{}, "t: null\n"},
    38  		{DateMicro(1998, time.May, 5, 1, 5, 5, 50, time.FixedZone("test", -4*60*60)), "t: \"1998-05-05T05:05:05.000000Z\"\n"},
    39  		{DateMicro(1998, time.May, 5, 5, 5, 5, 0, time.UTC), "t: \"1998-05-05T05:05:05.000000Z\"\n"},
    40  	}
    41  
    42  	for _, c := range cases {
    43  		input := MicroTimeHolder{c.input}
    44  		result, err := yaml.Marshal(&input)
    45  		if err != nil {
    46  			t.Errorf("Failed to marshal input: '%v': %v", input, err)
    47  		}
    48  		if string(result) != c.result {
    49  			t.Errorf("Failed to marshal input: '%v': expected %+v, got %q", input, c.result, string(result))
    50  		}
    51  	}
    52  }
    53  
    54  func TestMicroTimeUnmarshalYAML(t *testing.T) {
    55  	cases := []struct {
    56  		input  string
    57  		result MicroTime
    58  	}{
    59  		{"t: null\n", MicroTime{}},
    60  		{"t: 1998-05-05T05:05:05.000000Z\n", MicroTime{Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC).Local()}},
    61  	}
    62  
    63  	for _, c := range cases {
    64  		var result MicroTimeHolder
    65  		if err := yaml.Unmarshal([]byte(c.input), &result); err != nil {
    66  			t.Errorf("Failed to unmarshal input '%v': %v", c.input, err)
    67  		}
    68  		if result.T != c.result {
    69  			t.Errorf("Failed to unmarshal input '%v': expected %+v, got %+v", c.input, c.result, result)
    70  		}
    71  	}
    72  }
    73  
    74  func TestMicroTimeMarshalJSON(t *testing.T) {
    75  	cases := []struct {
    76  		input  MicroTime
    77  		result string
    78  	}{
    79  		{MicroTime{}, "{\"t\":null}"},
    80  		{DateMicro(1998, time.May, 5, 5, 5, 5, 50, time.UTC), "{\"t\":\"1998-05-05T05:05:05.000000Z\"}"},
    81  		{DateMicro(1998, time.May, 5, 5, 5, 5, 0, time.UTC), "{\"t\":\"1998-05-05T05:05:05.000000Z\"}"},
    82  	}
    83  
    84  	for _, c := range cases {
    85  		input := MicroTimeHolder{c.input}
    86  		result, err := json.Marshal(&input)
    87  		if err != nil {
    88  			t.Errorf("Failed to marshal input: '%v': %v", input, err)
    89  		}
    90  		if string(result) != c.result {
    91  			t.Errorf("Failed to marshal input: '%v': expected %+v, got %q", input, c.result, string(result))
    92  		}
    93  	}
    94  }
    95  
    96  func TestMicroTimeUnmarshalJSON(t *testing.T) {
    97  	cases := []struct {
    98  		input  string
    99  		result MicroTime
   100  	}{
   101  		{"{\"t\":null}", MicroTime{}},
   102  		{"{\"t\":\"1998-05-05T05:05:05.000000Z\"}", MicroTime{Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC).Local()}},
   103  	}
   104  
   105  	for _, c := range cases {
   106  		var result MicroTimeHolder
   107  		if err := json.Unmarshal([]byte(c.input), &result); err != nil {
   108  			t.Errorf("Failed to unmarshal input '%v': %v", c.input, err)
   109  		}
   110  		if result.T != c.result {
   111  			t.Errorf("Failed to unmarshal input '%v': expected %+v, got %+v", c.input, c.result, result)
   112  		}
   113  	}
   114  }
   115  
   116  func TestMicroTimeProto(t *testing.T) {
   117  	cases := []struct {
   118  		input MicroTime
   119  	}{
   120  		{MicroTime{}},
   121  		{DateMicro(1998, time.May, 5, 1, 5, 5, 1000, time.Local)},
   122  		{DateMicro(1998, time.May, 5, 5, 5, 5, 0, time.Local)},
   123  	}
   124  
   125  	for _, c := range cases {
   126  		input := c.input
   127  		data, err := input.Marshal()
   128  		if err != nil {
   129  			t.Fatalf("Failed to marshal input: '%v': %v", input, err)
   130  		}
   131  		time := MicroTime{}
   132  		if err := time.Unmarshal(data); err != nil {
   133  			t.Fatalf("Failed to unmarshal output: '%v': %v", input, err)
   134  		}
   135  		if !reflect.DeepEqual(input, time) {
   136  			t.Errorf("Marshal->Unmarshal is not idempotent: '%v' vs '%v'", input, time)
   137  		}
   138  	}
   139  }
   140  
   141  func TestMicroTimeEqual(t *testing.T) {
   142  	t1 := NewMicroTime(time.Now())
   143  	cases := []struct {
   144  		name   string
   145  		x      *MicroTime
   146  		y      *MicroTime
   147  		result bool
   148  	}{
   149  		{"nil =? nil", nil, nil, true},
   150  		{"!nil =? !nil", &t1, &t1, true},
   151  		{"nil =? !nil", nil, &t1, false},
   152  		{"!nil =? nil", &t1, nil, false},
   153  	}
   154  
   155  	for _, c := range cases {
   156  		t.Run(c.name, func(t *testing.T) {
   157  			result := c.x.Equal(c.y)
   158  			if result != c.result {
   159  				t.Errorf("Failed equality test for '%v', '%v': expected %+v, got %+v", c.x, c.y, c.result, result)
   160  			}
   161  		})
   162  	}
   163  }
   164  
   165  func TestMicroTimeEqualTime(t *testing.T) {
   166  	t1 := NewMicroTime(time.Now())
   167  	t2 := NewTime(t1.Time)
   168  	cases := []struct {
   169  		name   string
   170  		x      *MicroTime
   171  		y      *Time
   172  		result bool
   173  	}{
   174  		{"nil =? nil", nil, nil, true},
   175  		{"!nil =? !nil", &t1, &t2, true},
   176  		{"nil =? !nil", nil, &t2, false},
   177  		{"!nil =? nil", &t1, nil, false},
   178  	}
   179  
   180  	for _, c := range cases {
   181  		t.Run(c.name, func(t *testing.T) {
   182  			result := c.x.EqualTime(c.y)
   183  			if result != c.result {
   184  				t.Errorf("Failed equality test for '%v', '%v': expected %+v, got %+v", c.x, c.y, c.result, result)
   185  			}
   186  		})
   187  	}
   188  }
   189  
   190  func TestMicroTimeBefore(t *testing.T) {
   191  	t1 := NewMicroTime(time.Now())
   192  	cases := []struct {
   193  		name string
   194  		x    *MicroTime
   195  		y    *MicroTime
   196  	}{
   197  		{"nil <? nil", nil, nil},
   198  		{"!nil <? !nil", &t1, &t1},
   199  		{"nil <? !nil", nil, &t1},
   200  		{"!nil <? nil", &t1, nil},
   201  	}
   202  
   203  	for _, c := range cases {
   204  		t.Run(c.name, func(t *testing.T) {
   205  			result := c.x.Before(c.y)
   206  			if result {
   207  				t.Errorf("Failed before test for '%v', '%v': expected false, got %+v", c.x, c.y, result)
   208  			}
   209  		})
   210  	}
   211  }
   212  func TestMicroTimeBeforeTime(t *testing.T) {
   213  	t1 := NewMicroTime(time.Now())
   214  	t2 := NewTime(t1.Time)
   215  	cases := []struct {
   216  		name string
   217  		x    *MicroTime
   218  		y    *Time
   219  	}{
   220  		{"nil <? nil", nil, nil},
   221  		{"!nil <? !nil", &t1, &t2},
   222  		{"nil <? !nil", nil, &t2},
   223  		{"!nil <? nil", &t1, nil},
   224  	}
   225  
   226  	for _, c := range cases {
   227  		t.Run(c.name, func(t *testing.T) {
   228  			result := c.x.BeforeTime(c.y)
   229  			if result {
   230  				t.Errorf("Failed before test for '%v', '%v': expected false, got %+v", c.x, c.y, result)
   231  			}
   232  		})
   233  	}
   234  }
   235  
   236  func TestMicroTimeIsZero(t *testing.T) {
   237  	t1 := NewMicroTime(time.Now())
   238  	cases := []struct {
   239  		name   string
   240  		x      *MicroTime
   241  		result bool
   242  	}{
   243  		{"nil =? 0", nil, true},
   244  		{"!nil =? 0", &t1, false},
   245  	}
   246  
   247  	for _, c := range cases {
   248  		t.Run(c.name, func(t *testing.T) {
   249  			result := c.x.IsZero()
   250  			if result != c.result {
   251  				t.Errorf("Failed equality test for '%v': expected %+v, got %+v", c.x, c.result, result)
   252  			}
   253  		})
   254  	}
   255  }
   256  
   257  func TestMicroTimeUnmarshalJSONAndProtoEqual(t *testing.T) {
   258  	cases := []struct {
   259  		name   string
   260  		input  MicroTime
   261  		result bool
   262  	}{
   263  		{"nanosecond level precision", UnixMicro(123, 123123123), true},
   264  		{"microsecond level precision", UnixMicro(123, 123123000), true},
   265  	}
   266  
   267  	for _, c := range cases {
   268  		t.Run(c.name, func(t *testing.T) {
   269  			jsonData, err := c.input.MarshalJSON()
   270  			if err != nil {
   271  				t.Fatalf("Failed to marshal input to JSON: '%v': %v", c.input, err)
   272  			}
   273  
   274  			protoData, err := c.input.Marshal()
   275  			if err != nil {
   276  				t.Fatalf("Failed to marshal input to proto: '%v': %v", c.input, err)
   277  			}
   278  
   279  			var tJSON, tProto MicroTime
   280  			if err = tJSON.UnmarshalJSON(jsonData); err != nil {
   281  				t.Fatalf("Failed to unmarshal JSON: '%v': %v", jsonData, err)
   282  			}
   283  			if err = tProto.Unmarshal(protoData); err != nil {
   284  				t.Fatalf("Failed to unmarshal proto: '%v': %v", protoData, err)
   285  			}
   286  
   287  			result := tJSON.Equal(&tProto)
   288  			if result != c.result {
   289  				t.Errorf("Failed equality test for '%v': expected %+v, got %+v", c.input, c.result, result)
   290  			}
   291  		})
   292  	}
   293  }
   294  
   295  func TestMicroTimeProtoUnmarshalRaw(t *testing.T) {
   296  	cases := []struct {
   297  		name     string
   298  		input    []byte
   299  		expected MicroTime
   300  	}{
   301  		// input is generated by Timestamp{123, 123123123}.Marshal()
   302  		{"nanosecond level precision", []byte{8, 123, 16, 179, 235, 218, 58}, UnixMicro(123, 123123000)},
   303  		// input is generated by Timestamp{123, 123123000}.Marshal()
   304  		{"microsecond level precision", []byte{8, 123, 16, 184, 234, 218, 58}, UnixMicro(123, 123123000)},
   305  	}
   306  
   307  	for _, c := range cases {
   308  		t.Run(c.name, func(t *testing.T) {
   309  			var actual MicroTime
   310  			if err := actual.Unmarshal(c.input); err != nil {
   311  				t.Fatalf("Failed to unmarshal proto: '%v': %v", c.input, err)
   312  			}
   313  
   314  			if !actual.Equal(&c.expected) {
   315  				t.Errorf("Failed unmarshal from nanosecond-precise raw for '%v': expected %+v, got %+v", c.input, c.expected, actual)
   316  			}
   317  		})
   318  	}
   319  
   320  }