github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/util/time/time_test.go (about)

     1  /*
     2  Copyright 2021 The Skaffold 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 time
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/GoogleContainerTools/skaffold/testutil"
    24  )
    25  
    26  func TestLessThan(t *testing.T) {
    27  	tests := []struct {
    28  		description string
    29  		date        string
    30  		duration    time.Duration
    31  		expected    bool
    32  	}{
    33  		{
    34  			description: "date is less than 10 days from now",
    35  			date:        time.Now().AddDate(0, 0, -5).Format(time.RFC3339),
    36  			duration:    10 * 24 * time.Hour,
    37  			expected:    true,
    38  		},
    39  		{
    40  			description: "date is not less than 10 days from now",
    41  			date:        time.Now().AddDate(0, 0, -11).Format(time.RFC3339),
    42  			duration:    10 * 24 * time.Hour,
    43  		},
    44  		{
    45  			description: "date is not right format",
    46  			date:        "01-19=20129",
    47  			expected:    false,
    48  		},
    49  	}
    50  	for _, test := range tests {
    51  		testutil.Run(t, test.description, func(t *testutil.T) {
    52  			t.CheckDeepEqual(test.expected, LessThan(test.date, test.duration))
    53  		})
    54  	}
    55  }
    56  
    57  func TestHumanize(t *testing.T) {
    58  	duration1, err := time.ParseDuration("1h58m30.918273645s")
    59  	if err != nil {
    60  		t.Errorf("%s", err)
    61  	}
    62  	duration2, err := time.ParseDuration("5.23494327s")
    63  	if err != nil {
    64  		t.Errorf("%s", err)
    65  	}
    66  	tests := []struct {
    67  		description string
    68  		value       time.Duration
    69  		expected    string
    70  	}{
    71  		{
    72  			description: "Case for 1h58m30.918273645s",
    73  			value:       duration1,
    74  			expected:    "1 hour 58 minutes 30.918 seconds",
    75  		},
    76  		{
    77  			description: "Case for 5.23494327s",
    78  			value:       duration2,
    79  			expected:    "5.234 seconds",
    80  		},
    81  	}
    82  	for _, test := range tests {
    83  		testutil.Run(t, test.description, func(t *testutil.T) {
    84  			humanizedValue := Humanize(test.value)
    85  			t.CheckDeepEqual(test.expected, humanizedValue)
    86  		})
    87  	}
    88  }