2019-12-27 23:30:58 +03: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.
|
|
|
|
|
2022-06-13 12:37:59 +03:00
|
|
|
package issues_test
|
2017-09-12 09:48:13 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2019-12-27 23:30:58 +03:00
|
|
|
"time"
|
2017-09-12 09:48:13 +03:00
|
|
|
|
2022-05-20 17:08:52 +03:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-13 12:37:59 +03:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-11-12 17:36:47 +03:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 12:49:20 +03:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-11-17 15:34:35 +03:00
|
|
|
|
2017-09-12 09:48:13 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAddTime(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-09-12 09:48:13 +03:00
|
|
|
|
2021-11-24 12:49:20 +03:00
|
|
|
user3, err := user_model.GetUserByID(3)
|
2017-09-12 09:48:13 +03:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-06-13 12:37:59 +03:00
|
|
|
issue1, err := issues_model.GetIssueByID(db.DefaultContext, 1)
|
2017-09-12 09:48:13 +03:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-03-14 21:52:12 +03:00
|
|
|
// 3661 = 1h 1min 1s
|
2022-06-13 12:37:59 +03:00
|
|
|
trackedTime, err := issues_model.AddTime(user3, issue1, 3661, time.Now())
|
2017-09-12 09:48:13 +03: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-06-13 12:37:59 +03:00
|
|
|
tt := unittest.AssertExistsAndLoadBean(t, &issues_model.TrackedTime{UserID: 3, IssueID: 1}).(*issues_model.TrackedTime)
|
2019-12-27 23:30:58 +03:00
|
|
|
assert.Equal(t, int64(3661), tt.Time)
|
2017-09-12 09:48:13 +03:00
|
|
|
|
2022-06-13 12:37:59 +03:00
|
|
|
comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{Type: issues_model.CommentTypeAddTimeManual, PosterID: 3, IssueID: 1}).(*issues_model.Comment)
|
2022-03-01 01:57:20 +03:00
|
|
|
assert.Equal(t, comment.Content, "1 hour 1 minute")
|
2017-09-12 09:48:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetTrackedTimes(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-09-12 09:48:13 +03:00
|
|
|
|
|
|
|
// by Issue
|
2022-06-13 12:37:59 +03:00
|
|
|
times, err := issues_model.GetTrackedTimes(db.DefaultContext, &issues_model.FindTrackedTimesOptions{IssueID: 1})
|
2017-09-12 09:48:13 +03:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, times, 1)
|
2019-12-27 23:30:58 +03:00
|
|
|
assert.Equal(t, int64(400), times[0].Time)
|
2017-09-12 09:48:13 +03:00
|
|
|
|
2022-06-13 12:37:59 +03:00
|
|
|
times, err = issues_model.GetTrackedTimes(db.DefaultContext, &issues_model.FindTrackedTimesOptions{IssueID: -1})
|
2017-09-12 09:48:13 +03:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, times, 0)
|
|
|
|
|
|
|
|
// by User
|
2022-06-13 12:37:59 +03:00
|
|
|
times, err = issues_model.GetTrackedTimes(db.DefaultContext, &issues_model.FindTrackedTimesOptions{UserID: 1})
|
2017-09-12 09:48:13 +03:00
|
|
|
assert.NoError(t, err)
|
2019-12-27 23:30:58 +03:00
|
|
|
assert.Len(t, times, 3)
|
|
|
|
assert.Equal(t, int64(400), times[0].Time)
|
2017-09-12 09:48:13 +03:00
|
|
|
|
2022-06-13 12:37:59 +03:00
|
|
|
times, err = issues_model.GetTrackedTimes(db.DefaultContext, &issues_model.FindTrackedTimesOptions{UserID: 3})
|
2017-09-12 09:48:13 +03:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, times, 0)
|
|
|
|
|
|
|
|
// by Repo
|
2022-06-13 12:37:59 +03:00
|
|
|
times, err = issues_model.GetTrackedTimes(db.DefaultContext, &issues_model.FindTrackedTimesOptions{RepositoryID: 2})
|
2017-09-12 09:48:13 +03:00
|
|
|
assert.NoError(t, err)
|
2019-12-27 23:30:58 +03:00
|
|
|
assert.Len(t, times, 3)
|
|
|
|
assert.Equal(t, int64(1), times[0].Time)
|
2022-06-13 12:37:59 +03:00
|
|
|
issue, err := issues_model.GetIssueByID(db.DefaultContext, times[0].IssueID)
|
2017-09-12 09:48:13 +03:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, issue.RepoID, int64(2))
|
|
|
|
|
2022-06-13 12:37:59 +03:00
|
|
|
times, err = issues_model.GetTrackedTimes(db.DefaultContext, &issues_model.FindTrackedTimesOptions{RepositoryID: 1})
|
2017-09-12 09:48:13 +03:00
|
|
|
assert.NoError(t, err)
|
2019-12-27 23:30:58 +03:00
|
|
|
assert.Len(t, times, 5)
|
2017-09-12 09:48:13 +03:00
|
|
|
|
2022-06-13 12:37:59 +03:00
|
|
|
times, err = issues_model.GetTrackedTimes(db.DefaultContext, &issues_model.FindTrackedTimesOptions{RepositoryID: 10})
|
2017-09-12 09:48:13 +03:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, times, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTotalTimes(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-09-12 09:48:13 +03:00
|
|
|
|
2022-06-13 12:37:59 +03:00
|
|
|
total, err := issues_model.TotalTimes(&issues_model.FindTrackedTimesOptions{IssueID: 1})
|
2017-09-12 09:48:13 +03:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, total, 1)
|
|
|
|
for user, time := range total {
|
|
|
|
assert.Equal(t, int64(1), user.ID)
|
2022-03-01 01:57:20 +03:00
|
|
|
assert.Equal(t, "6 minutes 40 seconds", time)
|
2017-09-12 09:48:13 +03:00
|
|
|
}
|
|
|
|
|
2022-06-13 12:37:59 +03:00
|
|
|
total, err = issues_model.TotalTimes(&issues_model.FindTrackedTimesOptions{IssueID: 2})
|
2017-09-12 09:48:13 +03:00
|
|
|
assert.NoError(t, err)
|
2019-12-27 23:30:58 +03:00
|
|
|
assert.Len(t, total, 2)
|
2017-09-12 09:48:13 +03:00
|
|
|
for user, time := range total {
|
2019-12-27 23:30:58 +03:00
|
|
|
if user.ID == 2 {
|
2022-03-01 01:57:20 +03:00
|
|
|
assert.Equal(t, "1 hour 1 minute", time)
|
2019-12-27 23:30:58 +03:00
|
|
|
} else if user.ID == 1 {
|
2022-03-01 01:57:20 +03:00
|
|
|
assert.Equal(t, "20 seconds", time)
|
2019-12-27 23:30:58 +03:00
|
|
|
} else {
|
|
|
|
assert.Error(t, assert.AnError)
|
|
|
|
}
|
2017-09-12 09:48:13 +03:00
|
|
|
}
|
|
|
|
|
2022-06-13 12:37:59 +03:00
|
|
|
total, err = issues_model.TotalTimes(&issues_model.FindTrackedTimesOptions{IssueID: 5})
|
2017-09-12 09:48:13 +03:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, total, 1)
|
|
|
|
for user, time := range total {
|
|
|
|
assert.Equal(t, int64(2), user.ID)
|
2022-03-01 01:57:20 +03:00
|
|
|
assert.Equal(t, "1 second", time)
|
2017-09-12 09:48:13 +03:00
|
|
|
}
|
|
|
|
|
2022-06-13 12:37:59 +03:00
|
|
|
total, err = issues_model.TotalTimes(&issues_model.FindTrackedTimesOptions{IssueID: 4})
|
2017-09-12 09:48:13 +03:00
|
|
|
assert.NoError(t, err)
|
2019-12-27 23:30:58 +03:00
|
|
|
assert.Len(t, total, 2)
|
2017-09-12 09:48:13 +03:00
|
|
|
}
|