code.gitea.io/gitea@v1.21.7/models/actions/utils_test.go (about)

     1  // Copyright 2022 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package actions
     5  
     6  import (
     7  	"math"
     8  	"testing"
     9  	"time"
    10  
    11  	"code.gitea.io/gitea/modules/timeutil"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func TestLogIndexes_ToDB(t *testing.T) {
    18  	tests := []struct {
    19  		indexes LogIndexes
    20  	}{
    21  		{
    22  			indexes: []int64{1, 2, 0, -1, -2, math.MaxInt64, math.MinInt64},
    23  		},
    24  	}
    25  	for _, tt := range tests {
    26  		t.Run("", func(t *testing.T) {
    27  			got, err := tt.indexes.ToDB()
    28  			require.NoError(t, err)
    29  
    30  			indexes := LogIndexes{}
    31  			require.NoError(t, indexes.FromDB(got))
    32  
    33  			assert.Equal(t, tt.indexes, indexes)
    34  		})
    35  	}
    36  }
    37  
    38  func Test_calculateDuration(t *testing.T) {
    39  	oldTimeSince := timeSince
    40  	defer func() {
    41  		timeSince = oldTimeSince
    42  	}()
    43  
    44  	timeSince = func(t time.Time) time.Duration {
    45  		return timeutil.TimeStamp(1000).AsTime().Sub(t)
    46  	}
    47  	type args struct {
    48  		started timeutil.TimeStamp
    49  		stopped timeutil.TimeStamp
    50  		status  Status
    51  	}
    52  	tests := []struct {
    53  		name string
    54  		args args
    55  		want time.Duration
    56  	}{
    57  		{
    58  			name: "unknown",
    59  			args: args{
    60  				started: 0,
    61  				stopped: 0,
    62  				status:  StatusUnknown,
    63  			},
    64  			want: 0,
    65  		},
    66  		{
    67  			name: "running",
    68  			args: args{
    69  				started: 500,
    70  				stopped: 0,
    71  				status:  StatusRunning,
    72  			},
    73  			want: 500 * time.Second,
    74  		},
    75  		{
    76  			name: "done",
    77  			args: args{
    78  				started: 500,
    79  				stopped: 600,
    80  				status:  StatusSuccess,
    81  			},
    82  			want: 100 * time.Second,
    83  		},
    84  	}
    85  	for _, tt := range tests {
    86  		t.Run(tt.name, func(t *testing.T) {
    87  			assert.Equalf(t, tt.want, calculateDuration(tt.args.started, tt.args.stopped, tt.args.status), "calculateDuration(%v, %v, %v)", tt.args.started, tt.args.stopped, tt.args.status)
    88  		})
    89  	}
    90  }