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