gitlab.com/postgres-ai/database-lab/v3@v3.0.3/pkg/util/time_test.go (about)

     1  /*
     2  2019 © Postgres.ai
     3  */
     4  
     5  package util
     6  
     7  import (
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  func TestSecondsAgo(t *testing.T) {
    13  	ts1 := time.Date(2010, time.December, 10, 10, 10, 10, 0, time.UTC)
    14  	if SecondsAgo(ts1) == 0 {
    15  		t.FailNow()
    16  	}
    17  
    18  	ts2 := time.Date(292277020000, time.January, 1, 1, 0, 0, 0, time.UTC)
    19  	if SecondsAgo(ts2) != 0 {
    20  		t.FailNow()
    21  	}
    22  }
    23  
    24  func TestDurationToString(t *testing.T) {
    25  	tests := []struct {
    26  		input    string
    27  		expected string
    28  	}{
    29  		{
    30  			input:    "10µs",
    31  			expected: "0.010 ms",
    32  		},
    33  		{
    34  			input:    "1m",
    35  			expected: "1.000 min",
    36  		},
    37  		{
    38  			input:    "1h25m10s",
    39  			expected: "85.167 min",
    40  		},
    41  	}
    42  
    43  	for i, test := range tests {
    44  		d, err := time.ParseDuration(test.input)
    45  		if err != nil {
    46  			t.Errorf("Incorrect input in %d test case.", i)
    47  			t.FailNow()
    48  		}
    49  
    50  		actual := DurationToString(d)
    51  		if actual != test.expected {
    52  			t.Errorf("(%d) got different result than expected: \n%s\n",
    53  				i, diff(test.expected, actual))
    54  			t.FailNow()
    55  		}
    56  	}
    57  }
    58  
    59  func TestMillisecondsToString(t *testing.T) {
    60  	tests := []struct {
    61  		input    float64
    62  		expected string
    63  	}{
    64  		{
    65  			input:    100.0,
    66  			expected: "100.000 ms",
    67  		},
    68  		{
    69  			input:    1000.0,
    70  			expected: "1.000 s",
    71  		},
    72  		{
    73  			input:    10000.0,
    74  			expected: "10.000 s",
    75  		},
    76  	}
    77  
    78  	for i, test := range tests {
    79  		actual := MillisecondsToString(test.input)
    80  		if actual != test.expected {
    81  			t.Errorf("(%d) got different result than expected: \n%s\n",
    82  				i, diff(test.expected, actual))
    83  			t.FailNow()
    84  		}
    85  	}
    86  }
    87  
    88  func TestFormatTime(t *testing.T) {
    89  	ts1 := time.Date(2019, time.December, 10, 23, 0, 10, 0, time.UTC)
    90  	actual := FormatTime(ts1)
    91  	expected := "2019-12-10 23:00:10 UTC"
    92  	if actual != expected {
    93  		t.Errorf("Got different result than expected: \n%s\n", diff(expected, actual))
    94  		t.FailNow()
    95  	}
    96  }