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"
2018-10-24 16:17:21 +03:00
2021-03-02 00:08:10 +03:00
jsoniter "github.com/json-iterator/go"
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 {
userID int64
2020-12-22 05:53:37 +03:00
doerID int64
2018-10-24 16:17:21 +03:00
CountResult int
JSONResult string
} {
2020-12-22 05:53:37 +03:00
{ 2 , 2 , 1 , ` [ { "timestamp":1603152000,"contributions":1}] ` } , // self looks at action in private repo
{ 2 , 1 , 1 , ` [ { "timestamp":1603152000,"contributions":1}] ` } , // admin looks at action in private repo
{ 2 , 3 , 0 , ` [] ` } , // other user looks at action in private repo
{ 2 , 0 , 0 , ` [] ` } , // nobody looks at action in private repo
{ 16 , 15 , 1 , ` [ { "timestamp":1603238400,"contributions":1}] ` } , // collaborator looks at action in private repo
{ 3 , 3 , 0 , ` [] ` } , // no action action not performed by target user
2018-10-24 16:17:21 +03:00
}
2018-10-23 05:57:42 +03:00
// Prepare
assert . NoError ( t , PrepareTestDatabase ( ) )
2020-12-22 05:53:37 +03:00
for i , tc := range testCases {
2018-10-24 16:17:21 +03:00
user := AssertExistsAndLoadBean ( t , & User { ID : tc . userID } ) . ( * User )
2020-12-22 05:53:37 +03:00
doer := & User { ID : tc . doerID }
_ , err := loadBeanIfExists ( doer )
assert . NoError ( t , err )
if tc . doerID == 0 {
doer = nil
}
2018-10-24 16:17:21 +03:00
// get the action for comparison
actions , err := GetFeeds ( 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 )
2018-10-24 16:17:21 +03:00
assert . NoError ( t , err )
2019-10-21 23:19:53 +03:00
assert . Equal ( t , len ( actions ) , len ( heatmap ) , "invalid action count: did the test data became too old?" )
2020-12-22 05:53:37 +03:00
assert . Equal ( t , tc . CountResult , len ( heatmap ) , fmt . Sprintf ( "testcase %d" , i ) )
2018-10-24 16:17:21 +03:00
2021-03-14 21:52:12 +03:00
// Test JSON rendering
2021-03-02 00:08:10 +03:00
json := jsoniter . ConfigCompatibleWithStandardLibrary
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
}