github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/model/post_list_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package model 5 6 import ( 7 "strings" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestPostListJson(t *testing.T) { 14 15 pl := PostList{} 16 p1 := &Post{Id: NewId(), Message: NewId()} 17 pl.AddPost(p1) 18 p2 := &Post{Id: NewId(), Message: NewId()} 19 pl.AddPost(p2) 20 21 pl.AddOrder(p1.Id) 22 pl.AddOrder(p2.Id) 23 24 json := pl.ToJson() 25 rpl := PostListFromJson(strings.NewReader(json)) 26 27 assert.Equal(t, p1.Message, rpl.Posts[p1.Id].Message, "failed to serialize p1 message") 28 assert.Equal(t, p2.Message, rpl.Posts[p2.Id].Message, "failed to serialize p2 message") 29 assert.Equal(t, p2.Id, rpl.Order[1], "failed to serialize p2 Id") 30 } 31 32 func TestPostListExtend(t *testing.T) { 33 p1 := &Post{Id: NewId(), Message: NewId()} 34 p2 := &Post{Id: NewId(), Message: NewId()} 35 p3 := &Post{Id: NewId(), Message: NewId()} 36 37 l1 := PostList{} 38 l1.AddPost(p1) 39 l1.AddOrder(p1.Id) 40 l1.AddPost(p2) 41 l1.AddOrder(p2.Id) 42 43 l2 := PostList{} 44 l2.AddPost(p3) 45 l2.AddOrder(p3.Id) 46 47 l2.Extend(&l1) 48 49 // should not changed l1 50 assert.Len(t, l1.Posts, 2) 51 assert.Len(t, l1.Order, 2) 52 53 // should extend l2 54 assert.Len(t, l2.Posts, 3) 55 assert.Len(t, l2.Order, 3) 56 57 // should extend the Order of l2 correctly 58 assert.Equal(t, l2.Order[0], p3.Id) 59 assert.Equal(t, l2.Order[1], p1.Id) 60 assert.Equal(t, l2.Order[2], p2.Id) 61 62 // extend l2 again 63 l2.Extend(&l1) 64 // extending l2 again should not changed l1 65 assert.Len(t, l1.Posts, 2) 66 assert.Len(t, l1.Order, 2) 67 68 // extending l2 again should extend l2 69 assert.Len(t, l2.Posts, 3) 70 assert.Len(t, l2.Order, 3) 71 72 // p3 could be last unread 73 p4 := &Post{Id: NewId(), Message: NewId()} 74 p5 := &Post{Id: NewId(), RootId: p1.Id, Message: NewId()} 75 p6 := &Post{Id: NewId(), RootId: p1.Id, Message: NewId()} 76 77 // Create before and after post list where p3 could be last unread 78 79 // Order has 2 but Posts are 4 which includes additional 2 comments (p5 & p6) to parent post (p1) 80 beforePostList := PostList{ 81 Order: []string{p1.Id, p2.Id}, 82 Posts: map[string]*Post{p1.Id: p1, p2.Id: p2, p5.Id: p5, p6.Id: p6}, 83 } 84 85 // Order has 3 but Posts are 4 which includes 1 parent post (p1) of comments (p5 & p6) 86 afterPostList := PostList{ 87 Order: []string{p4.Id, p5.Id, p6.Id}, 88 Posts: map[string]*Post{p1.Id: p1, p4.Id: p4, p5.Id: p5, p6.Id: p6}, 89 } 90 91 beforePostList.Extend(&afterPostList) 92 93 // should not changed afterPostList 94 assert.Len(t, afterPostList.Posts, 4) 95 assert.Len(t, afterPostList.Order, 3) 96 97 // should extend beforePostList 98 assert.Len(t, beforePostList.Posts, 5) 99 assert.Len(t, beforePostList.Order, 5) 100 101 // should extend the Order of beforePostList correctly 102 assert.Equal(t, beforePostList.Order[0], p1.Id) 103 assert.Equal(t, beforePostList.Order[1], p2.Id) 104 assert.Equal(t, beforePostList.Order[2], p4.Id) 105 assert.Equal(t, beforePostList.Order[3], p5.Id) 106 assert.Equal(t, beforePostList.Order[4], p6.Id) 107 } 108 109 func TestPostListSortByCreateAt(t *testing.T) { 110 pl := PostList{} 111 p1 := &Post{Id: NewId(), Message: NewId(), CreateAt: 2} 112 pl.AddPost(p1) 113 p2 := &Post{Id: NewId(), Message: NewId(), CreateAt: 1} 114 pl.AddPost(p2) 115 p3 := &Post{Id: NewId(), Message: NewId(), CreateAt: 3} 116 pl.AddPost(p3) 117 118 pl.AddOrder(p1.Id) 119 pl.AddOrder(p2.Id) 120 pl.AddOrder(p3.Id) 121 122 pl.SortByCreateAt() 123 124 assert.EqualValues(t, pl.Order[0], p3.Id) 125 assert.EqualValues(t, pl.Order[1], p1.Id) 126 assert.EqualValues(t, pl.Order[2], p2.Id) 127 } 128 129 func TestPostListToSlice(t *testing.T) { 130 pl := PostList{} 131 p1 := &Post{Id: NewId(), Message: NewId(), CreateAt: 2} 132 pl.AddPost(p1) 133 p2 := &Post{Id: NewId(), Message: NewId(), CreateAt: 1} 134 pl.AddPost(p2) 135 p3 := &Post{Id: NewId(), Message: NewId(), CreateAt: 3} 136 pl.AddPost(p3) 137 138 pl.AddOrder(p1.Id) 139 pl.AddOrder(p2.Id) 140 pl.AddOrder(p3.Id) 141 142 want := []*Post{p1, p2, p3} 143 144 assert.Equal(t, want, pl.ToSlice()) 145 }