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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. jsoniter "github.com/json-iterator/go"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestGetUserHeatmapDataByUser(t *testing.T) {
  12. testCases := []struct {
  13. userID int64
  14. doerID int64
  15. CountResult int
  16. JSONResult string
  17. }{
  18. // self looks at action in private repo
  19. {2, 2, 1, `[{"timestamp":1603227600,"contributions":1}]`},
  20. // admin looks at action in private repo
  21. {2, 1, 1, `[{"timestamp":1603227600,"contributions":1}]`},
  22. // other user looks at action in private repo
  23. {2, 3, 0, `[]`},
  24. // nobody looks at action in private repo
  25. {2, 0, 0, `[]`},
  26. // collaborator looks at action in private repo
  27. {16, 15, 1, `[{"timestamp":1603267200,"contributions":1}]`},
  28. // no action action not performed by target user
  29. {3, 3, 0, `[]`},
  30. // multiple actions performed with two grouped together
  31. {10, 10, 3, `[{"timestamp":1603009800,"contributions":1},{"timestamp":1603010700,"contributions":2}]`},
  32. }
  33. // Prepare
  34. assert.NoError(t, PrepareTestDatabase())
  35. for i, tc := range testCases {
  36. user := AssertExistsAndLoadBean(t, &User{ID: tc.userID}).(*User)
  37. doer := &User{ID: tc.doerID}
  38. _, err := loadBeanIfExists(doer)
  39. assert.NoError(t, err)
  40. if tc.doerID == 0 {
  41. doer = nil
  42. }
  43. // get the action for comparison
  44. actions, err := GetFeeds(GetFeedsOptions{
  45. RequestedUser: user,
  46. Actor: doer,
  47. IncludePrivate: true,
  48. OnlyPerformedBy: true,
  49. IncludeDeleted: true,
  50. })
  51. assert.NoError(t, err)
  52. // Get the heatmap and compare
  53. heatmap, err := GetUserHeatmapDataByUser(user, doer)
  54. var contributions int
  55. for _, hm := range heatmap {
  56. contributions += int(hm.Contributions)
  57. }
  58. assert.NoError(t, err)
  59. assert.Len(t, actions, contributions, "invalid action count: did the test data became too old?")
  60. assert.Equal(t, tc.CountResult, contributions, fmt.Sprintf("testcase %d", i))
  61. // Test JSON rendering
  62. json := jsoniter.ConfigCompatibleWithStandardLibrary
  63. jsonData, err := json.Marshal(heatmap)
  64. assert.NoError(t, err)
  65. assert.Equal(t, tc.JSONResult, string(jsonData))
  66. }
  67. }