From a5c261caa7e57a8d20d113c4862cbceb7147822a Mon Sep 17 00:00:00 2001 From: Umputun Date: Sun, 6 May 2018 13:17:00 -0500 Subject: [PATCH] fix tree score sort --- app/rest/cache_test.go | 7 ++++++- app/rest/tree.go | 4 ++-- app/rest/tree_test.go | 34 ++++++++++++++++++++++++++++++++++ remark.rest | 4 ++-- 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/app/rest/cache_test.go b/app/rest/cache_test.go index 0851ebc8..47fa3bef 100644 --- a/app/rest/cache_test.go +++ b/app/rest/cache_test.go @@ -44,8 +44,13 @@ func TestLoadingCache_URLKey(t *testing.T) { key := URLKey(r) assert.Equal(t, "http://blah/123", key) + r, err = http.NewRequest("GET", "http://blah/123?key=v&k2=v2", nil) + assert.Nil(t, err) + key = URLKey(r) + assert.Equal(t, "http://blah/123?key=v&k2=v2", key) + user := store.User{Admin: true} r = SetUserInfo(r, user) key = URLKey(r) - assert.Equal(t, "admin!!http://blah/123", key) + assert.Equal(t, "admin!!http://blah/123?key=v&k2=v2", key) } diff --git a/app/rest/tree.go b/app/rest/tree.go index 6f87f306..22b374f1 100644 --- a/app/rest/tree.go +++ b/app/rest/tree.go @@ -77,9 +77,9 @@ func (t *Tree) sortNodes(sortType string) { case "+score", "-score", "score": if strings.HasPrefix(sortType, "-") { - return t.Nodes[i].Comment.Score > t.Nodes[j].Comment.Score + return t.Nodes[i].Comment.Score < t.Nodes[j].Comment.Score } - return t.Nodes[i].Comment.Score < t.Nodes[j].Comment.Score + return t.Nodes[i].Comment.Score > t.Nodes[j].Comment.Score default: return t.Nodes[i].Comment.Timestamp.Before(t.Nodes[j].Comment.Timestamp) diff --git a/app/rest/tree_test.go b/app/rest/tree_test.go index f5afc369..dea92185 100644 --- a/app/rest/tree_test.go +++ b/app/rest/tree_test.go @@ -42,6 +42,40 @@ func TestMakeTree(t *testing.T) { // t.Log(string(buf.Bytes())) } +func TestMakeTreeSorts(t *testing.T) { + // unsorted by purpose + comments := []store.Comment{ + {ID: "14", ParentID: "1", Timestamp: time.Date(2017, 12, 25, 19, 46, 14, 0, time.UTC)}, + {ID: "1", Timestamp: time.Date(2017, 12, 25, 19, 46, 1, 0, time.UTC), Score: 2}, + {ID: "2", Timestamp: time.Date(2017, 12, 25, 19, 47, 2, 0, time.UTC), Score: 3}, + {ID: "11", ParentID: "1", Timestamp: time.Date(2017, 12, 25, 19, 46, 11, 0, time.UTC)}, + {ID: "13", ParentID: "1", Timestamp: time.Date(2017, 12, 25, 19, 46, 13, 0, time.UTC)}, + {ID: "12", ParentID: "1", Timestamp: time.Date(2017, 12, 25, 19, 46, 12, 0, time.UTC)}, + {ID: "131", ParentID: "13", Timestamp: time.Date(2017, 12, 25, 19, 46, 31, 0, time.UTC)}, + {ID: "132", ParentID: "13", Timestamp: time.Date(2017, 12, 25, 19, 46, 32, 0, time.UTC)}, + {ID: "21", ParentID: "2", Timestamp: time.Date(2017, 12, 25, 19, 47, 21, 0, time.UTC)}, + {ID: "22", ParentID: "2", Timestamp: time.Date(2017, 12, 25, 19, 47, 22, 0, time.UTC)}, + {ID: "4", Timestamp: time.Date(2017, 12, 25, 19, 47, 22, 0, time.UTC), Score: -2}, + {ID: "3", Timestamp: time.Date(2017, 12, 25, 19, 47, 22, 100, time.UTC)}, + {ID: "5", Deleted: true}, + } + + res := MakeTree(comments, "time") + assert.Equal(t, "1", res.Nodes[0].Comment.ID) + + res = MakeTree(comments, "+time") + assert.Equal(t, "1", res.Nodes[0].Comment.ID) + + res = MakeTree(comments, "-time") + assert.Equal(t, "3", res.Nodes[0].Comment.ID) + + res = MakeTree(comments, "score") + assert.Equal(t, "2", res.Nodes[0].Comment.ID) + + res = MakeTree(comments, "-score") + assert.Equal(t, "4", res.Nodes[0].Comment.ID) +} + func BenchmarkTree(b *testing.B) { comments := []store.Comment{} data, err := ioutil.ReadFile("testfile.json") diff --git a/remark.rest b/remark.rest index c125e6fb..b49a1df0 100644 --- a/remark.rest +++ b/remark.rest @@ -1,9 +1,9 @@ ### find request with tree -GET {{host}}/api/v1/find?site=remark&sort=time&format=tree&url=https://radio-t.com/p/2017/12/16/podcast-576/ +GET {{host}}/api/v1/find?site=remark&sort=-score&format=tree&url=https://radio-t.com/p/2017/12/16/podcast-576/ ### find request with plain -GET {{host}}/api/v1/find?site=remark&sort=time&format=plain&url=https://radio-t.com/p/2017/12/16/podcast-576/ +GET {{host}}/api/v1/find?site=remark&sort=-score&format=plain&url=https://radio-t.com/p/2017/12/16/podcast-576/ ### last 50 comments GET {{host}}/api/v1/last/50?site=remark