2018-10-23 02:57:42 +00:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.package models
|
|
|
|
|
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2020-12-22 02:53:37 +00:00
|
|
|
"fmt"
|
2018-10-23 02:57:42 +00:00
|
|
|
"testing"
|
2021-10-18 21:48:23 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2018-10-24 13:17:21 +00:00
|
|
|
|
2021-03-01 21:08:10 +00:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
2018-10-24 13:17:21 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-10-23 02:57:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetUserHeatmapDataByUser(t *testing.T) {
|
2018-10-24 13:17:21 +00:00
|
|
|
testCases := []struct {
|
|
|
|
userID int64
|
2020-12-22 02:53:37 +00:00
|
|
|
doerID int64
|
2018-10-24 13:17:21 +00:00
|
|
|
CountResult int
|
|
|
|
JSONResult string
|
|
|
|
}{
|
2021-06-25 16:59:25 +00: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 13:17:21 +00:00
|
|
|
}
|
2018-10-23 02:57:42 +00:00
|
|
|
// Prepare
|
|
|
|
assert.NoError(t, PrepareTestDatabase())
|
|
|
|
|
2021-10-18 21:48:23 +00:00
|
|
|
// Mock time
|
|
|
|
timeutil.Set(time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC))
|
|
|
|
defer timeutil.Unset()
|
|
|
|
|
2020-12-22 02:53:37 +00:00
|
|
|
for i, tc := range testCases {
|
2018-10-24 13:17:21 +00:00
|
|
|
user := AssertExistsAndLoadBean(t, &User{ID: tc.userID}).(*User)
|
|
|
|
|
2020-12-22 02:53:37 +00:00
|
|
|
doer := &User{ID: tc.doerID}
|
|
|
|
_, err := loadBeanIfExists(doer)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if tc.doerID == 0 {
|
|
|
|
doer = nil
|
|
|
|
}
|
|
|
|
|
2018-10-24 13:17:21 +00:00
|
|
|
// get the action for comparison
|
|
|
|
actions, err := GetFeeds(GetFeedsOptions{
|
2020-01-13 17:33:46 +00:00
|
|
|
RequestedUser: user,
|
2020-12-22 02:53:37 +00:00
|
|
|
Actor: doer,
|
2020-01-13 17:33:46 +00:00
|
|
|
IncludePrivate: true,
|
2020-12-22 02:53:37 +00:00
|
|
|
OnlyPerformedBy: true,
|
2020-01-13 17:33:46 +00:00
|
|
|
IncludeDeleted: true,
|
2018-10-24 13:17:21 +00:00
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Get the heatmap and compare
|
2020-12-22 02:53:37 +00:00
|
|
|
heatmap, err := GetUserHeatmapDataByUser(user, doer)
|
2021-06-25 16:59:25 +00:00
|
|
|
var contributions int
|
|
|
|
for _, hm := range heatmap {
|
|
|
|
contributions += int(hm.Contributions)
|
|
|
|
}
|
2018-10-24 13:17:21 +00:00
|
|
|
assert.NoError(t, err)
|
2021-06-25 16:59:25 +00:00
|
|
|
assert.Len(t, actions, contributions, "invalid action count: did the test data became too old?")
|
|
|
|
assert.Equal(t, tc.CountResult, contributions, fmt.Sprintf("testcase %d", i))
|
2018-10-24 13:17:21 +00:00
|
|
|
|
2021-03-14 18:52:12 +00:00
|
|
|
// Test JSON rendering
|
2021-03-01 21:08:10 +00:00
|
|
|
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
2018-10-24 13:17:21 +00:00
|
|
|
jsonData, err := json.Marshal(heatmap)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, tc.JSONResult, string(jsonData))
|
|
|
|
}
|
2018-10-23 02:57:42 +00:00
|
|
|
}
|