Files

98 lines
2.6 KiB
Go
Raw Permalink Normal View History

2018-10-23 04:57:42 +02:00
// Copyright 2018 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
2018-10-23 04:57:42 +02:00
package activities_test
2018-10-23 04:57:42 +02:00
import (
"testing"
2021-10-18 15:12:26 -05:00
"time"
2018-10-24 15:17:21 +02:00
activities_model "code.gitea.io/gitea/models/activities"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/json"
2021-10-18 15:12:26 -05:00
"code.gitea.io/gitea/modules/timeutil"
2018-10-24 15:17:21 +02:00
"github.com/stretchr/testify/assert"
2018-10-23 04:57:42 +02:00
)
func TestGetUserHeatmapDataByUser(t *testing.T) {
2018-10-24 15:17:21 +02:00
testCases := []struct {
2022-03-10 15:54:51 +01:00
desc string
2018-10-24 15:17:21 +02:00
userID int64
2020-12-22 02:53:37 +00:00
doerID int64
2018-10-24 15:17:21 +02:00
CountResult int
JSONResult string
}{
2022-03-10 15:54:51 +01:00
{
"self looks at action in private repo",
2, 2, 1, `[{"timestamp":1603227600,"contributions":1}]`,
},
{
"admin looks at action in private repo",
2, 1, 1, `[{"timestamp":1603227600,"contributions":1}]`,
},
{
"other user looks at action in private repo",
2, 3, 0, `[]`,
},
{
"nobody looks at action in private repo",
2, 0, 0, `[]`,
},
{
"collaborator looks at action in private repo",
16, 15, 1, `[{"timestamp":1603267200,"contributions":1}]`,
},
{
"no action action not performed by target user",
3, 3, 0, `[]`,
},
{
"multiple actions performed with two grouped together",
10, 10, 3, `[{"timestamp":1603009800,"contributions":1},{"timestamp":1603010700,"contributions":2}]`,
},
2018-10-24 15:17:21 +02:00
}
2018-10-23 04:57:42 +02:00
// Prepare
assert.NoError(t, unittest.PrepareTestDatabase())
2018-10-23 04:57:42 +02:00
2021-10-18 15:12:26 -05:00
// Mock time
2023-12-28 18:09:57 +08:00
timeutil.MockSet(time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC))
defer timeutil.MockUnset()
2021-10-18 15:12:26 -05:00
2022-03-10 15:54:51 +01:00
for _, tc := range testCases {
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: tc.userID})
2018-10-24 15:17:21 +02:00
2024-12-29 09:05:56 +08:00
var doer *user_model.User
if tc.doerID != 0 {
doer = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: tc.doerID})
2020-12-22 02:53:37 +00:00
}
2018-10-24 15:17:21 +02:00
// get the action for comparison
actions, count, err := activities_model.GetFeeds(t.Context(), activities_model.GetFeedsOptions{
2020-01-13 19:33:46 +02:00
RequestedUser: user,
2020-12-22 02:53:37 +00:00
Actor: doer,
2020-01-13 19:33:46 +02:00
IncludePrivate: true,
2020-12-22 02:53:37 +00:00
OnlyPerformedBy: true,
2020-01-13 19:33:46 +02:00
IncludeDeleted: true,
2018-10-24 15:17:21 +02:00
})
assert.NoError(t, err)
// Get the heatmap and compare
heatmap, err := activities_model.GetUserHeatmapDataByUser(t.Context(), user, doer)
2021-06-25 12:59:25 -04:00
var contributions int
for _, hm := range heatmap {
contributions += int(hm.Contributions)
}
2018-10-24 15:17:21 +02:00
assert.NoError(t, err)
2021-06-25 12:59:25 -04:00
assert.Len(t, actions, contributions, "invalid action count: did the test data became too old?")
assert.Equal(t, count, int64(contributions))
2024-12-15 11:41:29 +01:00
assert.Equal(t, tc.CountResult, contributions, "testcase '%s'", tc.desc)
2018-10-24 15:17:21 +02:00
// Test JSON rendering
2018-10-24 15:17:21 +02:00
jsonData, err := json.Marshal(heatmap)
assert.NoError(t, err)
2024-12-15 11:41:29 +01:00
assert.JSONEq(t, tc.JSONResult, string(jsonData))
2018-10-24 15:17:21 +02:00
}
2018-10-23 04:57:42 +02:00
}