gitea源码

contributors_graph_test.go 2.2KB

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