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.

contributors_graph_test.go 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2024 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repository
  4. import (
  5. "slices"
  6. "testing"
  7. "code.gitea.io/gitea/models/db"
  8. repo_model "code.gitea.io/gitea/models/repo"
  9. "code.gitea.io/gitea/models/unittest"
  10. "code.gitea.io/gitea/modules/cache"
  11. "code.gitea.io/gitea/modules/setting"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func TestRepository_ContributorsGraph(t *testing.T) {
  15. assert.NoError(t, unittest.PrepareTestDatabase())
  16. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
  17. assert.NoError(t, repo.LoadOwner(db.DefaultContext))
  18. mockCache, err := cache.NewStringCache(setting.Cache{})
  19. assert.NoError(t, err)
  20. generateContributorStats(nil, mockCache, "key", repo, "404ref")
  21. var data map[string]*ContributorData
  22. _, getErr := mockCache.GetJSON("key", &data)
  23. assert.NotNil(t, getErr)
  24. assert.ErrorContains(t, getErr.ToError(), "object does not exist")
  25. generateContributorStats(nil, mockCache, "key2", repo, "master")
  26. exist, _ := mockCache.GetJSON("key2", &data)
  27. assert.True(t, exist)
  28. var keys []string
  29. for k := range data {
  30. keys = append(keys, k)
  31. }
  32. slices.Sort(keys)
  33. assert.EqualValues(t, []string{
  34. "ethantkoenig@gmail.com",
  35. "jimmy.praet@telenet.be",
  36. "jon@allspice.io",
  37. "total", // generated summary
  38. }, keys)
  39. assert.EqualValues(t, &ContributorData{
  40. Name: "Ethan Koenig",
  41. AvatarLink: "https://secure.gravatar.com/avatar/b42fb195faa8c61b8d88abfefe30e9e3?d=identicon",
  42. TotalCommits: 1,
  43. Weeks: map[int64]*WeekData{
  44. 1511654400000: {
  45. Week: 1511654400000, // sunday 2017-11-26
  46. Additions: 3,
  47. Deletions: 0,
  48. Commits: 1,
  49. },
  50. },
  51. }, data["ethantkoenig@gmail.com"])
  52. assert.EqualValues(t, &ContributorData{
  53. Name: "Total",
  54. AvatarLink: "",
  55. TotalCommits: 3,
  56. Weeks: map[int64]*WeekData{
  57. 1511654400000: {
  58. Week: 1511654400000, // sunday 2017-11-26 (2017-11-26 20:31:18 -0800)
  59. Additions: 3,
  60. Deletions: 0,
  61. Commits: 1,
  62. },
  63. 1607817600000: {
  64. Week: 1607817600000, // sunday 2020-12-13 (2020-12-15 15:23:11 -0500)
  65. Additions: 10,
  66. Deletions: 0,
  67. Commits: 1,
  68. },
  69. 1624752000000: {
  70. Week: 1624752000000, // sunday 2021-06-27 (2021-06-29 21:54:09 +0200)
  71. Additions: 2,
  72. Deletions: 0,
  73. Commits: 1,
  74. },
  75. },
  76. }, data["total"])
  77. }