1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-22 10:18:38 +00:00

Fix JSON result of empty array (#5154)

This commit is contained in:
Antoine GIRARD
2018-10-24 15:17:21 +02:00
committed by Lunny Xiao
parent 317ddb7283
commit 70ad46133f
2 changed files with 39 additions and 19 deletions

View File

@@ -5,29 +5,48 @@
package models
import (
"github.com/stretchr/testify/assert"
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetUserHeatmapDataByUser(t *testing.T) {
testCases := []struct {
userID int64
CountResult int
JSONResult string
}{
{2, 1, `[{"timestamp":1540080000,"contributions":1}]`},
{3, 0, `[]`},
}
// Prepare
assert.NoError(t, PrepareTestDatabase())
// Insert some action
user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
for _, tc := range testCases {
// get the action for comparison
actions, err := GetFeeds(GetFeedsOptions{
RequestedUser: user,
RequestingUserID: user.ID,
IncludePrivate: true,
OnlyPerformedBy: false,
IncludeDeleted: true,
})
assert.NoError(t, err)
// Insert some action
user := AssertExistsAndLoadBean(t, &User{ID: tc.userID}).(*User)
// Get the heatmap and compare
heatmap, err := GetUserHeatmapDataByUser(user)
assert.NoError(t, err)
assert.Equal(t, len(actions), len(heatmap))
// get the action for comparison
actions, err := GetFeeds(GetFeedsOptions{
RequestedUser: user,
RequestingUserID: user.ID,
IncludePrivate: true,
OnlyPerformedBy: false,
IncludeDeleted: true,
})
assert.NoError(t, err)
// Get the heatmap and compare
heatmap, err := GetUserHeatmapDataByUser(user)
assert.NoError(t, err)
assert.Equal(t, len(actions), len(heatmap))
assert.Equal(t, tc.CountResult, len(heatmap))
//Test JSON rendering
jsonData, err := json.Marshal(heatmap)
assert.NoError(t, err)
assert.Equal(t, tc.JSONResult, string(jsonData))
}
}