Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

issue_tracked_time_test.go 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package models
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. func TestAddTime(t *testing.T) {
  7. assert.NoError(t, PrepareTestDatabase())
  8. user3, err := GetUserByID(3)
  9. assert.NoError(t, err)
  10. issue1, err := GetIssueByID(1)
  11. assert.NoError(t, err)
  12. //3661 = 1h 1min 1s
  13. trackedTime, err := AddTime(user3, issue1, 3661)
  14. assert.NoError(t, err)
  15. assert.Equal(t, int64(3), trackedTime.UserID)
  16. assert.Equal(t, int64(1), trackedTime.IssueID)
  17. assert.Equal(t, int64(3661), trackedTime.Time)
  18. tt := AssertExistsAndLoadBean(t, &TrackedTime{UserID: 3, IssueID: 1}).(*TrackedTime)
  19. assert.Equal(t, tt.Time, int64(3661))
  20. comment := AssertExistsAndLoadBean(t, &Comment{Type: CommentTypeAddTimeManual, PosterID: 3, IssueID: 1}).(*Comment)
  21. assert.Equal(t, comment.Content, "1h 1min 1s")
  22. }
  23. func TestGetTrackedTimes(t *testing.T) {
  24. assert.NoError(t, PrepareTestDatabase())
  25. // by Issue
  26. times, err := GetTrackedTimes(FindTrackedTimesOptions{IssueID: 1})
  27. assert.NoError(t, err)
  28. assert.Len(t, times, 1)
  29. assert.Equal(t, times[0].Time, int64(400))
  30. times, err = GetTrackedTimes(FindTrackedTimesOptions{IssueID: -1})
  31. assert.NoError(t, err)
  32. assert.Len(t, times, 0)
  33. // by User
  34. times, err = GetTrackedTimes(FindTrackedTimesOptions{UserID: 1})
  35. assert.NoError(t, err)
  36. assert.Len(t, times, 1)
  37. assert.Equal(t, times[0].Time, int64(400))
  38. times, err = GetTrackedTimes(FindTrackedTimesOptions{UserID: 3})
  39. assert.NoError(t, err)
  40. assert.Len(t, times, 0)
  41. // by Repo
  42. times, err = GetTrackedTimes(FindTrackedTimesOptions{RepositoryID: 2})
  43. assert.NoError(t, err)
  44. assert.Len(t, times, 1)
  45. assert.Equal(t, times[0].Time, int64(1))
  46. issue, err := GetIssueByID(times[0].IssueID)
  47. assert.NoError(t, err)
  48. assert.Equal(t, issue.RepoID, int64(2))
  49. times, err = GetTrackedTimes(FindTrackedTimesOptions{RepositoryID: 1})
  50. assert.NoError(t, err)
  51. assert.Len(t, times, 4)
  52. times, err = GetTrackedTimes(FindTrackedTimesOptions{RepositoryID: 10})
  53. assert.NoError(t, err)
  54. assert.Len(t, times, 0)
  55. }
  56. func TestTotalTimes(t *testing.T) {
  57. assert.NoError(t, PrepareTestDatabase())
  58. total, err := TotalTimes(FindTrackedTimesOptions{IssueID: 1})
  59. assert.NoError(t, err)
  60. assert.Len(t, total, 1)
  61. for user, time := range total {
  62. assert.Equal(t, int64(1), user.ID)
  63. assert.Equal(t, "6min 40s", time)
  64. }
  65. total, err = TotalTimes(FindTrackedTimesOptions{IssueID: 2})
  66. assert.NoError(t, err)
  67. assert.Len(t, total, 1)
  68. for user, time := range total {
  69. assert.Equal(t, int64(2), user.ID)
  70. assert.Equal(t, "1h 1min 2s", time)
  71. }
  72. total, err = TotalTimes(FindTrackedTimesOptions{IssueID: 5})
  73. assert.NoError(t, err)
  74. assert.Len(t, total, 1)
  75. for user, time := range total {
  76. assert.Equal(t, int64(2), user.ID)
  77. assert.Equal(t, "1s", time)
  78. }
  79. total, err = TotalTimes(FindTrackedTimesOptions{IssueID: 4})
  80. assert.NoError(t, err)
  81. assert.Len(t, total, 0)
  82. }