code.gitea.io/gitea@v1.22.3/models/activities/user_heatmap_test.go (about)

     1  // Copyright 2018 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package activities_test
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  	"time"
    10  
    11  	activities_model "code.gitea.io/gitea/models/activities"
    12  	"code.gitea.io/gitea/models/db"
    13  	"code.gitea.io/gitea/models/unittest"
    14  	user_model "code.gitea.io/gitea/models/user"
    15  	"code.gitea.io/gitea/modules/json"
    16  	"code.gitea.io/gitea/modules/timeutil"
    17  
    18  	"github.com/stretchr/testify/assert"
    19  )
    20  
    21  func TestGetUserHeatmapDataByUser(t *testing.T) {
    22  	testCases := []struct {
    23  		desc        string
    24  		userID      int64
    25  		doerID      int64
    26  		CountResult int
    27  		JSONResult  string
    28  	}{
    29  		{
    30  			"self looks at action in private repo",
    31  			2, 2, 1, `[{"timestamp":1603227600,"contributions":1}]`,
    32  		},
    33  		{
    34  			"admin looks at action in private repo",
    35  			2, 1, 1, `[{"timestamp":1603227600,"contributions":1}]`,
    36  		},
    37  		{
    38  			"other user looks at action in private repo",
    39  			2, 3, 0, `[]`,
    40  		},
    41  		{
    42  			"nobody looks at action in private repo",
    43  			2, 0, 0, `[]`,
    44  		},
    45  		{
    46  			"collaborator looks at action in private repo",
    47  			16, 15, 1, `[{"timestamp":1603267200,"contributions":1}]`,
    48  		},
    49  		{
    50  			"no action action not performed by target user",
    51  			3, 3, 0, `[]`,
    52  		},
    53  		{
    54  			"multiple actions performed with two grouped together",
    55  			10, 10, 3, `[{"timestamp":1603009800,"contributions":1},{"timestamp":1603010700,"contributions":2}]`,
    56  		},
    57  	}
    58  	// Prepare
    59  	assert.NoError(t, unittest.PrepareTestDatabase())
    60  
    61  	// Mock time
    62  	timeutil.MockSet(time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC))
    63  	defer timeutil.MockUnset()
    64  
    65  	for _, tc := range testCases {
    66  		user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: tc.userID})
    67  
    68  		doer := &user_model.User{ID: tc.doerID}
    69  		_, err := unittest.LoadBeanIfExists(doer)
    70  		assert.NoError(t, err)
    71  		if tc.doerID == 0 {
    72  			doer = nil
    73  		}
    74  
    75  		// get the action for comparison
    76  		actions, count, err := activities_model.GetFeeds(db.DefaultContext, activities_model.GetFeedsOptions{
    77  			RequestedUser:   user,
    78  			Actor:           doer,
    79  			IncludePrivate:  true,
    80  			OnlyPerformedBy: true,
    81  			IncludeDeleted:  true,
    82  		})
    83  		assert.NoError(t, err)
    84  
    85  		// Get the heatmap and compare
    86  		heatmap, err := activities_model.GetUserHeatmapDataByUser(db.DefaultContext, user, doer)
    87  		var contributions int
    88  		for _, hm := range heatmap {
    89  			contributions += int(hm.Contributions)
    90  		}
    91  		assert.NoError(t, err)
    92  		assert.Len(t, actions, contributions, "invalid action count: did the test data became too old?")
    93  		assert.Equal(t, count, int64(contributions))
    94  		assert.Equal(t, tc.CountResult, contributions, fmt.Sprintf("testcase '%s'", tc.desc))
    95  
    96  		// Test JSON rendering
    97  		jsonData, err := json.Marshal(heatmap)
    98  		assert.NoError(t, err)
    99  		assert.Equal(t, tc.JSONResult, string(jsonData))
   100  	}
   101  }