summaryrefslogtreecommitdiffstats
path: root/models/actions/utils_test.go
blob: 98c048d4ef899470d92957351b066b3bf8e269ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package actions

import (
	"math"
	"testing"
	"time"

	"code.gitea.io/gitea/modules/timeutil"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestLogIndexes_ToDB(t *testing.T) {
	tests := []struct {
		indexes LogIndexes
	}{
		{
			indexes: []int64{1, 2, 0, -1, -2, math.MaxInt64, math.MinInt64},
		},
	}
	for _, tt := range tests {
		t.Run("", func(t *testing.T) {
			got, err := tt.indexes.ToDB()
			require.NoError(t, err)

			indexes := LogIndexes{}
			require.NoError(t, indexes.FromDB(got))

			assert.Equal(t, tt.indexes, indexes)
		})
	}
}

func Test_calculateDuration(t *testing.T) {
	oldTimeSince := timeSince
	defer func() {
		timeSince = oldTimeSince
	}()

	timeSince = func(t time.Time) time.Duration {
		return timeutil.TimeStamp(1000).AsTime().Sub(t)
	}
	type args struct {
		started timeutil.TimeStamp
		stopped timeutil.TimeStamp
		status  Status
	}
	tests := []struct {
		name string
		args args
		want time.Duration
	}{
		{
			name: "unknown",
			args: args{
				started: 0,
				stopped: 0,
				status:  StatusUnknown,
			},
			want: 0,
		},
		{
			name: "running",
			args: args{
				started: 500,
				stopped: 0,
				status:  StatusRunning,
			},
			want: 500 * time.Second,
		},
		{
			name: "done",
			args: args{
				started: 500,
				stopped: 600,
				status:  StatusSuccess,
			},
			want: 100 * time.Second,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			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)
		})
	}
}