2019-12-28 04:30:58 +08:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-12-28 04:30:58 +08:00
|
|
|
|
2022-06-13 17:37:59 +08:00
|
|
|
package issues_test
|
2017-09-12 14:48:13 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2019-12-28 04:30:58 +08:00
|
|
|
"time"
|
2017-09-12 14:48:13 +08:00
|
|
|
|
2022-05-20 22:08:52 +08:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-13 17:37:59 +08:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-11-12 22:36:47 +08:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 17:49:20 +08:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-11-17 20:34:35 +08:00
|
|
|
|
2017-09-12 14:48:13 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAddTime(t *testing.T) {
|
2021-11-12 22:36:47 +08:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-09-12 14:48:13 +08:00
|
|
|
|
2023-09-14 10:59:53 +08:00
|
|
|
org3, err := user_model.GetUserByID(db.DefaultContext, 3)
|
2017-09-12 14:48:13 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-06-13 17:37:59 +08:00
|
|
|
issue1, err := issues_model.GetIssueByID(db.DefaultContext, 1)
|
2017-09-12 14:48:13 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-03-15 02:52:12 +08:00
|
|
|
// 3661 = 1h 1min 1s
|
2023-09-14 10:59:53 +08:00
|
|
|
trackedTime, err := issues_model.AddTime(db.DefaultContext, org3, issue1, 3661, time.Now())
|
2017-09-12 14:48:13 +08: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)
|
|
|
|
|
2022-08-16 10:22:25 +08:00
|
|
|
tt := unittest.AssertExistsAndLoadBean(t, &issues_model.TrackedTime{UserID: 3, IssueID: 1})
|
2019-12-28 04:30:58 +08:00
|
|
|
assert.Equal(t, int64(3661), tt.Time)
|
2017-09-12 14:48:13 +08:00
|
|
|
|
2022-08-16 10:22:25 +08:00
|
|
|
comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{Type: issues_model.CommentTypeAddTimeManual, PosterID: 3, IssueID: 1})
|
2023-06-23 20:12:39 +08:00
|
|
|
assert.Equal(t, "|3661", comment.Content)
|
2017-09-12 14:48:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetTrackedTimes(t *testing.T) {
|
2021-11-12 22:36:47 +08:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-09-12 14:48:13 +08:00
|
|
|
|
|
|
|
// by Issue
|
2022-06-13 17:37:59 +08:00
|
|
|
times, err := issues_model.GetTrackedTimes(db.DefaultContext, &issues_model.FindTrackedTimesOptions{IssueID: 1})
|
2017-09-12 14:48:13 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, times, 1)
|
2019-12-28 04:30:58 +08:00
|
|
|
assert.Equal(t, int64(400), times[0].Time)
|
2017-09-12 14:48:13 +08:00
|
|
|
|
2022-06-13 17:37:59 +08:00
|
|
|
times, err = issues_model.GetTrackedTimes(db.DefaultContext, &issues_model.FindTrackedTimesOptions{IssueID: -1})
|
2017-09-12 14:48:13 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, times, 0)
|
|
|
|
|
|
|
|
// by User
|
2022-06-13 17:37:59 +08:00
|
|
|
times, err = issues_model.GetTrackedTimes(db.DefaultContext, &issues_model.FindTrackedTimesOptions{UserID: 1})
|
2017-09-12 14:48:13 +08:00
|
|
|
assert.NoError(t, err)
|
2019-12-28 04:30:58 +08:00
|
|
|
assert.Len(t, times, 3)
|
|
|
|
assert.Equal(t, int64(400), times[0].Time)
|
2017-09-12 14:48:13 +08:00
|
|
|
|
2022-06-13 17:37:59 +08:00
|
|
|
times, err = issues_model.GetTrackedTimes(db.DefaultContext, &issues_model.FindTrackedTimesOptions{UserID: 3})
|
2017-09-12 14:48:13 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, times, 0)
|
|
|
|
|
|
|
|
// by Repo
|
2022-06-13 17:37:59 +08:00
|
|
|
times, err = issues_model.GetTrackedTimes(db.DefaultContext, &issues_model.FindTrackedTimesOptions{RepositoryID: 2})
|
2017-09-12 14:48:13 +08:00
|
|
|
assert.NoError(t, err)
|
2019-12-28 04:30:58 +08:00
|
|
|
assert.Len(t, times, 3)
|
|
|
|
assert.Equal(t, int64(1), times[0].Time)
|
2022-06-13 17:37:59 +08:00
|
|
|
issue, err := issues_model.GetIssueByID(db.DefaultContext, times[0].IssueID)
|
2017-09-12 14:48:13 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, issue.RepoID, int64(2))
|
|
|
|
|
2022-06-13 17:37:59 +08:00
|
|
|
times, err = issues_model.GetTrackedTimes(db.DefaultContext, &issues_model.FindTrackedTimesOptions{RepositoryID: 1})
|
2017-09-12 14:48:13 +08:00
|
|
|
assert.NoError(t, err)
|
2019-12-28 04:30:58 +08:00
|
|
|
assert.Len(t, times, 5)
|
2017-09-12 14:48:13 +08:00
|
|
|
|
2022-06-13 17:37:59 +08:00
|
|
|
times, err = issues_model.GetTrackedTimes(db.DefaultContext, &issues_model.FindTrackedTimesOptions{RepositoryID: 10})
|
2017-09-12 14:48:13 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, times, 0)
|
|
|
|
}
|
|
|
|
|
2023-09-29 21:04:14 +08:00
|
|
|
func TestTotalTimesForEachUser(t *testing.T) {
|
2021-11-12 22:36:47 +08:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-09-12 14:48:13 +08:00
|
|
|
|
2023-10-03 18:30:41 +08:00
|
|
|
total, err := issues_model.TotalTimesForEachUser(db.DefaultContext, &issues_model.FindTrackedTimesOptions{IssueID: 1})
|
2017-09-12 14:48:13 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, total, 1)
|
|
|
|
for user, time := range total {
|
2023-06-20 00:40:06 +08:00
|
|
|
assert.EqualValues(t, 1, user.ID)
|
|
|
|
assert.EqualValues(t, 400, time)
|
2017-09-12 14:48:13 +08:00
|
|
|
}
|
|
|
|
|
2023-10-03 18:30:41 +08:00
|
|
|
total, err = issues_model.TotalTimesForEachUser(db.DefaultContext, &issues_model.FindTrackedTimesOptions{IssueID: 2})
|
2017-09-12 14:48:13 +08:00
|
|
|
assert.NoError(t, err)
|
2019-12-28 04:30:58 +08:00
|
|
|
assert.Len(t, total, 2)
|
2017-09-12 14:48:13 +08:00
|
|
|
for user, time := range total {
|
2019-12-28 04:30:58 +08:00
|
|
|
if user.ID == 2 {
|
2023-06-20 00:40:06 +08:00
|
|
|
assert.EqualValues(t, 3662, time)
|
2019-12-28 04:30:58 +08:00
|
|
|
} else if user.ID == 1 {
|
2023-06-20 00:40:06 +08:00
|
|
|
assert.EqualValues(t, 20, time)
|
2019-12-28 04:30:58 +08:00
|
|
|
} else {
|
|
|
|
assert.Error(t, assert.AnError)
|
|
|
|
}
|
2017-09-12 14:48:13 +08:00
|
|
|
}
|
|
|
|
|
2023-10-03 18:30:41 +08:00
|
|
|
total, err = issues_model.TotalTimesForEachUser(db.DefaultContext, &issues_model.FindTrackedTimesOptions{IssueID: 5})
|
2017-09-12 14:48:13 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, total, 1)
|
|
|
|
for user, time := range total {
|
2023-06-20 00:40:06 +08:00
|
|
|
assert.EqualValues(t, 2, user.ID)
|
|
|
|
assert.EqualValues(t, 1, time)
|
2017-09-12 14:48:13 +08:00
|
|
|
}
|
|
|
|
|
2023-10-03 18:30:41 +08:00
|
|
|
total, err = issues_model.TotalTimesForEachUser(db.DefaultContext, &issues_model.FindTrackedTimesOptions{IssueID: 4})
|
2017-09-12 14:48:13 +08:00
|
|
|
assert.NoError(t, err)
|
2019-12-28 04:30:58 +08:00
|
|
|
assert.Len(t, total, 2)
|
2017-09-12 14:48:13 +08:00
|
|
|
}
|