2019-12-27 13:30:58 -07:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2017-09-11 23:48:13 -07:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2019-12-27 13:30:58 -07:00
|
|
|
"time"
|
2017-09-11 23:48:13 -07:00
|
|
|
|
2021-11-12 07:36:47 -07:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 02:49:20 -07:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-11-17 05:34:35 -07:00
|
|
|
|
2017-09-11 23:48:13 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAddTime(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-09-11 23:48:13 -07:00
|
|
|
|
2021-11-24 02:49:20 -07:00
|
|
|
user3, err := user_model.GetUserByID(3)
|
2017-09-11 23:48:13 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
issue1, err := GetIssueByID(1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-03-14 11:52:12 -07:00
|
|
|
// 3661 = 1h 1min 1s
|
2019-12-27 13:30:58 -07:00
|
|
|
trackedTime, err := AddTime(user3, issue1, 3661, time.Now())
|
2017-09-11 23:48:13 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(3), trackedTime.UserID)
|
|
|
|
assert.Equal(t, int64(1), trackedTime.IssueID)
|
|
|
|
assert.Equal(t, int64(3661), trackedTime.Time)
|
|
|
|
|
2021-11-16 01:53:21 -07:00
|
|
|
tt := unittest.AssertExistsAndLoadBean(t, &TrackedTime{UserID: 3, IssueID: 1}).(*TrackedTime)
|
2019-12-27 13:30:58 -07:00
|
|
|
assert.Equal(t, int64(3661), tt.Time)
|
2017-09-11 23:48:13 -07:00
|
|
|
|
2021-11-16 01:53:21 -07:00
|
|
|
comment := unittest.AssertExistsAndLoadBean(t, &Comment{Type: CommentTypeAddTimeManual, PosterID: 3, IssueID: 1}).(*Comment)
|
2017-09-11 23:48:13 -07:00
|
|
|
assert.Equal(t, comment.Content, "1h 1min 1s")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetTrackedTimes(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-09-11 23:48:13 -07:00
|
|
|
|
|
|
|
// by Issue
|
2021-08-12 05:43:08 -07:00
|
|
|
times, err := GetTrackedTimes(&FindTrackedTimesOptions{IssueID: 1})
|
2017-09-11 23:48:13 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, times, 1)
|
2019-12-27 13:30:58 -07:00
|
|
|
assert.Equal(t, int64(400), times[0].Time)
|
2017-09-11 23:48:13 -07:00
|
|
|
|
2021-08-12 05:43:08 -07:00
|
|
|
times, err = GetTrackedTimes(&FindTrackedTimesOptions{IssueID: -1})
|
2017-09-11 23:48:13 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, times, 0)
|
|
|
|
|
|
|
|
// by User
|
2021-08-12 05:43:08 -07:00
|
|
|
times, err = GetTrackedTimes(&FindTrackedTimesOptions{UserID: 1})
|
2017-09-11 23:48:13 -07:00
|
|
|
assert.NoError(t, err)
|
2019-12-27 13:30:58 -07:00
|
|
|
assert.Len(t, times, 3)
|
|
|
|
assert.Equal(t, int64(400), times[0].Time)
|
2017-09-11 23:48:13 -07:00
|
|
|
|
2021-08-12 05:43:08 -07:00
|
|
|
times, err = GetTrackedTimes(&FindTrackedTimesOptions{UserID: 3})
|
2017-09-11 23:48:13 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, times, 0)
|
|
|
|
|
|
|
|
// by Repo
|
2021-08-12 05:43:08 -07:00
|
|
|
times, err = GetTrackedTimes(&FindTrackedTimesOptions{RepositoryID: 2})
|
2017-09-11 23:48:13 -07:00
|
|
|
assert.NoError(t, err)
|
2019-12-27 13:30:58 -07:00
|
|
|
assert.Len(t, times, 3)
|
|
|
|
assert.Equal(t, int64(1), times[0].Time)
|
2017-09-11 23:48:13 -07:00
|
|
|
issue, err := GetIssueByID(times[0].IssueID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, issue.RepoID, int64(2))
|
|
|
|
|
2021-08-12 05:43:08 -07:00
|
|
|
times, err = GetTrackedTimes(&FindTrackedTimesOptions{RepositoryID: 1})
|
2017-09-11 23:48:13 -07:00
|
|
|
assert.NoError(t, err)
|
2019-12-27 13:30:58 -07:00
|
|
|
assert.Len(t, times, 5)
|
2017-09-11 23:48:13 -07:00
|
|
|
|
2021-08-12 05:43:08 -07:00
|
|
|
times, err = GetTrackedTimes(&FindTrackedTimesOptions{RepositoryID: 10})
|
2017-09-11 23:48:13 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, times, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTotalTimes(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-09-11 23:48:13 -07:00
|
|
|
|
2021-08-12 05:43:08 -07:00
|
|
|
total, err := TotalTimes(&FindTrackedTimesOptions{IssueID: 1})
|
2017-09-11 23:48:13 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, total, 1)
|
|
|
|
for user, time := range total {
|
|
|
|
assert.Equal(t, int64(1), user.ID)
|
|
|
|
assert.Equal(t, "6min 40s", time)
|
|
|
|
}
|
|
|
|
|
2021-08-12 05:43:08 -07:00
|
|
|
total, err = TotalTimes(&FindTrackedTimesOptions{IssueID: 2})
|
2017-09-11 23:48:13 -07:00
|
|
|
assert.NoError(t, err)
|
2019-12-27 13:30:58 -07:00
|
|
|
assert.Len(t, total, 2)
|
2017-09-11 23:48:13 -07:00
|
|
|
for user, time := range total {
|
2019-12-27 13:30:58 -07:00
|
|
|
if user.ID == 2 {
|
|
|
|
assert.Equal(t, "1h 1min 2s", time)
|
|
|
|
} else if user.ID == 1 {
|
|
|
|
assert.Equal(t, "20s", time)
|
|
|
|
} else {
|
|
|
|
assert.Error(t, assert.AnError)
|
|
|
|
}
|
2017-09-11 23:48:13 -07:00
|
|
|
}
|
|
|
|
|
2021-08-12 05:43:08 -07:00
|
|
|
total, err = TotalTimes(&FindTrackedTimesOptions{IssueID: 5})
|
2017-09-11 23:48:13 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, total, 1)
|
|
|
|
for user, time := range total {
|
|
|
|
assert.Equal(t, int64(2), user.ID)
|
|
|
|
assert.Equal(t, "1s", time)
|
|
|
|
}
|
|
|
|
|
2021-08-12 05:43:08 -07:00
|
|
|
total, err = TotalTimes(&FindTrackedTimesOptions{IssueID: 4})
|
2017-09-11 23:48:13 -07:00
|
|
|
assert.NoError(t, err)
|
2019-12-27 13:30:58 -07:00
|
|
|
assert.Len(t, total, 2)
|
2017-09-11 23:48:13 -07:00
|
|
|
}
|