github.com/wgh-/mattermost-server@v4.8.0-rc2+incompatible/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  	if rpl.Posts[p1.Id].Message != p1.Message {
    28  		t.Fatal("failed to serialize")
    29  	}
    30  
    31  	if rpl.Posts[p2.Id].Message != p2.Message {
    32  		t.Fatal("failed to serialize")
    33  	}
    34  
    35  	if rpl.Order[1] != p2.Id {
    36  		t.Fatal("failed to serialize")
    37  	}
    38  }
    39  
    40  func TestPostListExtend(t *testing.T) {
    41  	l1 := PostList{}
    42  
    43  	p1 := &Post{Id: NewId(), Message: NewId()}
    44  	l1.AddPost(p1)
    45  	l1.AddOrder(p1.Id)
    46  
    47  	p2 := &Post{Id: NewId(), Message: NewId()}
    48  	l1.AddPost(p2)
    49  	l1.AddOrder(p2.Id)
    50  
    51  	l2 := PostList{}
    52  
    53  	p3 := &Post{Id: NewId(), Message: NewId()}
    54  	l2.AddPost(p3)
    55  	l2.AddOrder(p3.Id)
    56  
    57  	l2.Extend(&l1)
    58  
    59  	if len(l1.Posts) != 2 || len(l1.Order) != 2 {
    60  		t.Fatal("extending l2 changed l1")
    61  	} else if len(l2.Posts) != 3 {
    62  		t.Fatal("failed to extend posts l2")
    63  	} else if l2.Order[0] != p3.Id || l2.Order[1] != p1.Id || l2.Order[2] != p2.Id {
    64  		t.Fatal("failed to extend order of l2")
    65  	}
    66  
    67  	if len(l1.Posts) != 2 || len(l1.Order) != 2 {
    68  		t.Fatal("extending l2 again changed l1")
    69  	} else if len(l2.Posts) != 3 || len(l2.Order) != 3 {
    70  		t.Fatal("extending l2 again changed l2")
    71  	}
    72  }
    73  
    74  func TestPostListSortByCreateAt(t *testing.T) {
    75  	pl := PostList{}
    76  	p1 := &Post{Id: NewId(), Message: NewId(), CreateAt: 2}
    77  	pl.AddPost(p1)
    78  	p2 := &Post{Id: NewId(), Message: NewId(), CreateAt: 1}
    79  	pl.AddPost(p2)
    80  	p3 := &Post{Id: NewId(), Message: NewId(), CreateAt: 3}
    81  	pl.AddPost(p3)
    82  
    83  	pl.AddOrder(p1.Id)
    84  	pl.AddOrder(p2.Id)
    85  	pl.AddOrder(p3.Id)
    86  
    87  	pl.SortByCreateAt()
    88  
    89  	assert.EqualValues(t, pl.Order[0], p3.Id)
    90  	assert.EqualValues(t, pl.Order[1], p1.Id)
    91  	assert.EqualValues(t, pl.Order[2], p2.Id)
    92  }