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