fix tree score sort

This commit is contained in:
Umputun
2018-05-06 13:17:00 -05:00
parent 75ece05a82
commit a5c261caa7
4 changed files with 44 additions and 5 deletions
+6 -1
View File
@@ -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)
}
+2 -2
View File
@@ -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)
+34
View File
@@ -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")
+2 -2
View File
@@ -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