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.4KB

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