2018-10-23 05:57:42 +03: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 05:53:37 +03:00
"fmt"
2018-10-23 05:57:42 +03:00
"testing"
2021-10-18 23:12:26 +03:00
"time"
2018-10-24 16:17:21 +03:00
2022-03-13 19:40:47 +03:00
"code.gitea.io/gitea/models/db"
2021-11-12 17:36:47 +03:00
"code.gitea.io/gitea/models/unittest"
2021-11-24 12:49:20 +03:00
user_model "code.gitea.io/gitea/models/user"
2021-07-24 19:03:58 +03:00
"code.gitea.io/gitea/modules/json"
2021-10-18 23:12:26 +03:00
"code.gitea.io/gitea/modules/timeutil"
2021-07-24 19:03:58 +03:00
2018-10-24 16:17:21 +03:00
"github.com/stretchr/testify/assert"
2018-10-23 05:57:42 +03:00
)
func TestGetUserHeatmapDataByUser ( t * testing . T ) {
2018-10-24 16:17:21 +03:00
testCases := [ ] struct {
2022-03-10 17:54:51 +03:00
desc string
2018-10-24 16:17:21 +03:00
userID int64
2020-12-22 05:53:37 +03:00
doerID int64
2018-10-24 16:17:21 +03:00
CountResult int
JSONResult string
} {
2022-03-10 17:54:51 +03: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 16:17:21 +03:00
}
2018-10-23 05:57:42 +03:00
// Prepare
2021-11-12 17:36:47 +03:00
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2018-10-23 05:57:42 +03:00
2021-10-18 23:12:26 +03:00
// Mock time
timeutil . Set ( time . Date ( 2021 , 1 , 1 , 0 , 0 , 0 , 0 , time . UTC ) )
defer timeutil . Unset ( )
2022-03-10 17:54:51 +03:00
for _ , tc := range testCases {
2021-11-24 12:49:20 +03:00
user := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : tc . userID } ) . ( * user_model . User )
2018-10-24 16:17:21 +03:00
2021-11-24 12:49:20 +03:00
doer := & user_model . User { ID : tc . doerID }
2021-11-16 11:53:21 +03:00
_ , err := unittest . LoadBeanIfExists ( doer )
2020-12-22 05:53:37 +03:00
assert . NoError ( t , err )
if tc . doerID == 0 {
doer = nil
}
2018-10-24 16:17:21 +03:00
// get the action for comparison
2022-03-13 19:40:47 +03:00
actions , err := GetFeeds ( db . DefaultContext , GetFeedsOptions {
2020-01-13 20:33:46 +03:00
RequestedUser : user ,
2020-12-22 05:53:37 +03:00
Actor : doer ,
2020-01-13 20:33:46 +03:00
IncludePrivate : true ,
2020-12-22 05:53:37 +03:00
OnlyPerformedBy : true ,
2020-01-13 20:33:46 +03:00
IncludeDeleted : true ,
2018-10-24 16:17:21 +03:00
} )
assert . NoError ( t , err )
// Get the heatmap and compare
2020-12-22 05:53:37 +03:00
heatmap , err := GetUserHeatmapDataByUser ( user , doer )
2021-06-25 19:59:25 +03:00
var contributions int
for _ , hm := range heatmap {
contributions += int ( hm . Contributions )
}
2018-10-24 16:17:21 +03:00
assert . NoError ( t , err )
2021-06-25 19:59:25 +03:00
assert . Len ( t , actions , contributions , "invalid action count: did the test data became too old?" )
2022-03-10 17:54:51 +03:00
assert . Equal ( t , tc . CountResult , contributions , fmt . Sprintf ( "testcase '%s'" , tc . desc ) )
2018-10-24 16:17:21 +03:00
2021-03-14 21:52:12 +03:00
// Test JSON rendering
2018-10-24 16:17:21 +03:00
jsonData , err := json . Marshal ( heatmap )
assert . NoError ( t , err )
assert . Equal ( t , tc . JSONResult , string ( jsonData ) )
}
2018-10-23 05:57:42 +03:00
}