gitea源码

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package activities_test
  4. import (
  5. "testing"
  6. "time"
  7. activities_model "code.gitea.io/gitea/models/activities"
  8. "code.gitea.io/gitea/models/unittest"
  9. user_model "code.gitea.io/gitea/models/user"
  10. "code.gitea.io/gitea/modules/json"
  11. "code.gitea.io/gitea/modules/timeutil"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func TestGetUserHeatmapDataByUser(t *testing.T) {
  15. testCases := []struct {
  16. desc string
  17. userID int64
  18. doerID int64
  19. CountResult int
  20. JSONResult string
  21. }{
  22. {
  23. "self looks at action in private repo",
  24. 2, 2, 1, `[{"timestamp":1603227600,"contributions":1}]`,
  25. },
  26. {
  27. "admin looks at action in private repo",
  28. 2, 1, 1, `[{"timestamp":1603227600,"contributions":1}]`,
  29. },
  30. {
  31. "other user looks at action in private repo",
  32. 2, 3, 0, `[]`,
  33. },
  34. {
  35. "nobody looks at action in private repo",
  36. 2, 0, 0, `[]`,
  37. },
  38. {
  39. "collaborator looks at action in private repo",
  40. 16, 15, 1, `[{"timestamp":1603267200,"contributions":1}]`,
  41. },
  42. {
  43. "no action action not performed by target user",
  44. 3, 3, 0, `[]`,
  45. },
  46. {
  47. "multiple actions performed with two grouped together",
  48. 10, 10, 3, `[{"timestamp":1603009800,"contributions":1},{"timestamp":1603010700,"contributions":2}]`,
  49. },
  50. }
  51. // Prepare
  52. assert.NoError(t, unittest.PrepareTestDatabase())
  53. // Mock time
  54. timeutil.MockSet(time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC))
  55. defer timeutil.MockUnset()
  56. for _, tc := range testCases {
  57. user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: tc.userID})
  58. var doer *user_model.User
  59. if tc.doerID != 0 {
  60. doer = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: tc.doerID})
  61. }
  62. // get the action for comparison
  63. actions, count, err := activities_model.GetFeeds(t.Context(), activities_model.GetFeedsOptions{
  64. RequestedUser: user,
  65. Actor: doer,
  66. IncludePrivate: true,
  67. OnlyPerformedBy: true,
  68. IncludeDeleted: true,
  69. })
  70. assert.NoError(t, err)
  71. // Get the heatmap and compare
  72. heatmap, err := activities_model.GetUserHeatmapDataByUser(t.Context(), user, doer)
  73. var contributions int
  74. for _, hm := range heatmap {
  75. contributions += int(hm.Contributions)
  76. }
  77. assert.NoError(t, err)
  78. assert.Len(t, actions, contributions, "invalid action count: did the test data became too old?")
  79. assert.Equal(t, count, int64(contributions))
  80. assert.Equal(t, tc.CountResult, contributions, "testcase '%s'", tc.desc)
  81. // Test JSON rendering
  82. jsonData, err := json.Marshal(heatmap)
  83. assert.NoError(t, err)
  84. assert.JSONEq(t, tc.JSONResult, string(jsonData))
  85. }
  86. }