You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

user_heatmap_test.go 2.6KB

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