]> source.dussan.org Git - gitea.git/commitdiff
Fix JSON result of empty array (#5154)
authorAntoine GIRARD <sapk@users.noreply.github.com>
Wed, 24 Oct 2018 13:17:21 +0000 (15:17 +0200)
committerLunny Xiao <xiaolunwen@gmail.com>
Wed, 24 Oct 2018 13:17:21 +0000 (21:17 +0800)
models/user_heatmap.go
models/user_heatmap_test.go

index 8482cba2d5dd2bbfc0af3ccde4fab1ff38b7eaf6..a86ae39fce1f749b7c4d001cb52eeb8a6583b188 100644 (file)
@@ -16,7 +16,8 @@ type UserHeatmapData struct {
 }
 
 // GetUserHeatmapDataByUser returns an array of UserHeatmapData
-func GetUserHeatmapDataByUser(user *User) (hdata []*UserHeatmapData, err error) {
+func GetUserHeatmapDataByUser(user *User) ([]*UserHeatmapData, error) {
+       hdata := make([]*UserHeatmapData, 0)
        var groupBy string
        switch {
        case setting.UseSQLite3:
@@ -29,12 +30,12 @@ func GetUserHeatmapDataByUser(user *User) (hdata []*UserHeatmapData, err error)
                groupBy = "dateadd(DAY,0, datediff(day,0, dateadd(s, created_unix, '19700101')))"
        }
 
-       err = x.Select(groupBy+" as timestamp, count(user_id) as contributions").
+       err := x.Select(groupBy+" as timestamp, count(user_id) as contributions").
                Table("action").
                Where("user_id = ?", user.ID).
                And("created_unix > ?", (util.TimeStampNow() - 31536000)).
                GroupBy("timestamp").
                OrderBy("timestamp").
                Find(&hdata)
-       return
+       return hdata, err
 }
index aeea427499d9c92fb9855ad66fb3a910db2779c8..a71202d85716ea821b489f15e55098e1b8a2ef6a 100644 (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)
-
-       // 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))
+       for _, tc := range testCases {
+
+               // Insert some action
+               user := AssertExistsAndLoadBean(t, &User{ID: tc.userID}).(*User)
+
+               // 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))
+       }
 }