github.com/levb/mattermost-server@v5.3.1+incompatible/store/storetest/post_store.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package storetest
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/mattermost/mattermost-server/model"
    13  	"github.com/mattermost/mattermost-server/store"
    14  	"github.com/mattermost/mattermost-server/utils"
    15  	"github.com/stretchr/testify/assert"
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  func TestPostStore(t *testing.T, ss store.Store) {
    20  	t.Run("Save", func(t *testing.T) { testPostStoreSave(t, ss) })
    21  	t.Run("Get", func(t *testing.T) { testPostStoreGet(t, ss) })
    22  	t.Run("GetSingle", func(t *testing.T) { testPostStoreGetSingle(t, ss) })
    23  	t.Run("GetEtagCache", func(t *testing.T) { testGetEtagCache(t, ss) })
    24  	t.Run("Update", func(t *testing.T) { testPostStoreUpdate(t, ss) })
    25  	t.Run("Delete", func(t *testing.T) { testPostStoreDelete(t, ss) })
    26  	t.Run("Delete1Level", func(t *testing.T) { testPostStoreDelete1Level(t, ss) })
    27  	t.Run("Delete2Level", func(t *testing.T) { testPostStoreDelete2Level(t, ss) })
    28  	t.Run("PermDelete1Level", func(t *testing.T) { testPostStorePermDelete1Level(t, ss) })
    29  	t.Run("PermDelete1Level2", func(t *testing.T) { testPostStorePermDelete1Level2(t, ss) })
    30  	t.Run("GetWithChildren", func(t *testing.T) { testPostStoreGetWithChildren(t, ss) })
    31  	t.Run("GetPostsWithDetails", func(t *testing.T) { testPostStoreGetPostsWithDetails(t, ss) })
    32  	t.Run("GetPostsBeforeAfter", func(t *testing.T) { testPostStoreGetPostsBeforeAfter(t, ss) })
    33  	t.Run("GetPostsSince", func(t *testing.T) { testPostStoreGetPostsSince(t, ss) })
    34  	t.Run("Search", func(t *testing.T) { testPostStoreSearch(t, ss) })
    35  	t.Run("UserCountsWithPostsByDay", func(t *testing.T) { testUserCountsWithPostsByDay(t, ss) })
    36  	t.Run("PostCountsByDay", func(t *testing.T) { testPostCountsByDay(t, ss) })
    37  	t.Run("GetFlaggedPostsForTeam", func(t *testing.T) { testPostStoreGetFlaggedPostsForTeam(t, ss) })
    38  	t.Run("GetFlaggedPosts", func(t *testing.T) { testPostStoreGetFlaggedPosts(t, ss) })
    39  	t.Run("GetFlaggedPostsForChannel", func(t *testing.T) { testPostStoreGetFlaggedPostsForChannel(t, ss) })
    40  	t.Run("GetPostsCreatedAt", func(t *testing.T) { testPostStoreGetPostsCreatedAt(t, ss) })
    41  	t.Run("Overwrite", func(t *testing.T) { testPostStoreOverwrite(t, ss) })
    42  	t.Run("GetPostsByIds", func(t *testing.T) { testPostStoreGetPostsByIds(t, ss) })
    43  	t.Run("GetPostsBatchForIndexing", func(t *testing.T) { testPostStoreGetPostsBatchForIndexing(t, ss) })
    44  	t.Run("PermanentDeleteBatch", func(t *testing.T) { testPostStorePermanentDeleteBatch(t, ss) })
    45  	t.Run("GetOldest", func(t *testing.T) { testPostStoreGetOldest(t, ss) })
    46  	t.Run("TestGetMaxPostSize", func(t *testing.T) { testGetMaxPostSize(t, ss) })
    47  }
    48  
    49  func testPostStoreSave(t *testing.T, ss store.Store) {
    50  	o1 := model.Post{}
    51  	o1.ChannelId = model.NewId()
    52  	o1.UserId = model.NewId()
    53  	o1.Message = "zz" + model.NewId() + "b"
    54  
    55  	if err := (<-ss.Post().Save(&o1)).Err; err != nil {
    56  		t.Fatal("couldn't save item", err)
    57  	}
    58  
    59  	if err := (<-ss.Post().Save(&o1)).Err; err == nil {
    60  		t.Fatal("shouldn't be able to update from save")
    61  	}
    62  }
    63  
    64  func testPostStoreGet(t *testing.T, ss store.Store) {
    65  	o1 := &model.Post{}
    66  	o1.ChannelId = model.NewId()
    67  	o1.UserId = model.NewId()
    68  	o1.Message = "zz" + model.NewId() + "b"
    69  
    70  	etag1 := (<-ss.Post().GetEtag(o1.ChannelId, false)).Data.(string)
    71  	if strings.Index(etag1, model.CurrentVersion+".") != 0 {
    72  		t.Fatal("Invalid Etag")
    73  	}
    74  
    75  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
    76  
    77  	etag2 := (<-ss.Post().GetEtag(o1.ChannelId, false)).Data.(string)
    78  	if strings.Index(etag2, fmt.Sprintf("%v.%v", model.CurrentVersion, o1.UpdateAt)) != 0 {
    79  		t.Fatal("Invalid Etag")
    80  	}
    81  
    82  	if r1 := <-ss.Post().Get(o1.Id); r1.Err != nil {
    83  		t.Fatal(r1.Err)
    84  	} else {
    85  		if r1.Data.(*model.PostList).Posts[o1.Id].CreateAt != o1.CreateAt {
    86  			t.Fatal("invalid returned post")
    87  		}
    88  	}
    89  
    90  	if err := (<-ss.Post().Get("123")).Err; err == nil {
    91  		t.Fatal("Missing id should have failed")
    92  	}
    93  
    94  	if err := (<-ss.Post().Get("")).Err; err == nil {
    95  		t.Fatal("should fail for blank post ids")
    96  	}
    97  }
    98  
    99  func testPostStoreGetSingle(t *testing.T, ss store.Store) {
   100  	o1 := &model.Post{}
   101  	o1.ChannelId = model.NewId()
   102  	o1.UserId = model.NewId()
   103  	o1.Message = "zz" + model.NewId() + "b"
   104  
   105  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
   106  
   107  	if r1 := <-ss.Post().GetSingle(o1.Id); r1.Err != nil {
   108  		t.Fatal(r1.Err)
   109  	} else {
   110  		if r1.Data.(*model.Post).CreateAt != o1.CreateAt {
   111  			t.Fatal("invalid returned post")
   112  		}
   113  	}
   114  
   115  	if err := (<-ss.Post().GetSingle("123")).Err; err == nil {
   116  		t.Fatal("Missing id should have failed")
   117  	}
   118  }
   119  
   120  func testGetEtagCache(t *testing.T, ss store.Store) {
   121  	o1 := &model.Post{}
   122  	o1.ChannelId = model.NewId()
   123  	o1.UserId = model.NewId()
   124  	o1.Message = "zz" + model.NewId() + "b"
   125  
   126  	etag1 := (<-ss.Post().GetEtag(o1.ChannelId, true)).Data.(string)
   127  	if strings.Index(etag1, model.CurrentVersion+".") != 0 {
   128  		t.Fatal("Invalid Etag")
   129  	}
   130  
   131  	// This one should come from the cache
   132  	etag2 := (<-ss.Post().GetEtag(o1.ChannelId, true)).Data.(string)
   133  	if strings.Index(etag2, model.CurrentVersion+".") != 0 {
   134  		t.Fatal("Invalid Etag")
   135  	}
   136  
   137  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
   138  
   139  	// We have not invalidated the cache so this should be the same as above
   140  	etag3 := (<-ss.Post().GetEtag(o1.ChannelId, true)).Data.(string)
   141  	if strings.Index(etag3, etag2) != 0 {
   142  		t.Fatal("Invalid Etag")
   143  	}
   144  
   145  	ss.Post().InvalidateLastPostTimeCache(o1.ChannelId)
   146  
   147  	// Invalidated cache so we should get a good result
   148  	etag4 := (<-ss.Post().GetEtag(o1.ChannelId, true)).Data.(string)
   149  	if strings.Index(etag4, fmt.Sprintf("%v.%v", model.CurrentVersion, o1.UpdateAt)) != 0 {
   150  		t.Fatal("Invalid Etag")
   151  	}
   152  }
   153  
   154  func testPostStoreUpdate(t *testing.T, ss store.Store) {
   155  	o1 := &model.Post{}
   156  	o1.ChannelId = model.NewId()
   157  	o1.UserId = model.NewId()
   158  	o1.Message = "zz" + model.NewId() + "AAAAAAAAAAA"
   159  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
   160  
   161  	o2 := &model.Post{}
   162  	o2.ChannelId = o1.ChannelId
   163  	o2.UserId = model.NewId()
   164  	o2.Message = "zz" + model.NewId() + "CCCCCCCCC"
   165  	o2.ParentId = o1.Id
   166  	o2.RootId = o1.Id
   167  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
   168  
   169  	o3 := &model.Post{}
   170  	o3.ChannelId = o1.ChannelId
   171  	o3.UserId = model.NewId()
   172  	o3.Message = "zz" + model.NewId() + "QQQQQQQQQQ"
   173  	o3 = (<-ss.Post().Save(o3)).Data.(*model.Post)
   174  
   175  	ro1 := (<-ss.Post().Get(o1.Id)).Data.(*model.PostList).Posts[o1.Id]
   176  	ro2 := (<-ss.Post().Get(o1.Id)).Data.(*model.PostList).Posts[o2.Id]
   177  	ro3 := (<-ss.Post().Get(o3.Id)).Data.(*model.PostList).Posts[o3.Id]
   178  
   179  	if ro1.Message != o1.Message {
   180  		t.Fatal("Failed to save/get")
   181  	}
   182  
   183  	o1a := &model.Post{}
   184  	*o1a = *ro1
   185  	o1a.Message = ro1.Message + "BBBBBBBBBB"
   186  	if result := <-ss.Post().Update(o1a, ro1); result.Err != nil {
   187  		t.Fatal(result.Err)
   188  	}
   189  
   190  	ro1a := (<-ss.Post().Get(o1.Id)).Data.(*model.PostList).Posts[o1.Id]
   191  
   192  	if ro1a.Message != o1a.Message {
   193  		t.Fatal("Failed to update/get")
   194  	}
   195  
   196  	o2a := &model.Post{}
   197  	*o2a = *ro2
   198  	o2a.Message = ro2.Message + "DDDDDDD"
   199  	if result := <-ss.Post().Update(o2a, ro2); result.Err != nil {
   200  		t.Fatal(result.Err)
   201  	}
   202  
   203  	ro2a := (<-ss.Post().Get(o1.Id)).Data.(*model.PostList).Posts[o2.Id]
   204  
   205  	if ro2a.Message != o2a.Message {
   206  		t.Fatal("Failed to update/get")
   207  	}
   208  
   209  	o3a := &model.Post{}
   210  	*o3a = *ro3
   211  	o3a.Message = ro3.Message + "WWWWWWW"
   212  	if result := <-ss.Post().Update(o3a, ro3); result.Err != nil {
   213  		t.Fatal(result.Err)
   214  	}
   215  
   216  	ro3a := (<-ss.Post().Get(o3.Id)).Data.(*model.PostList).Posts[o3.Id]
   217  
   218  	if ro3a.Message != o3a.Message && ro3a.Hashtags != o3a.Hashtags {
   219  		t.Fatal("Failed to update/get")
   220  	}
   221  
   222  	o4 := store.Must(ss.Post().Save(&model.Post{
   223  		ChannelId: model.NewId(),
   224  		UserId:    model.NewId(),
   225  		Message:   model.NewId(),
   226  		Filenames: []string{"test"},
   227  	})).(*model.Post)
   228  
   229  	ro4 := (<-ss.Post().Get(o4.Id)).Data.(*model.PostList).Posts[o4.Id]
   230  
   231  	o4a := &model.Post{}
   232  	*o4a = *ro4
   233  	o4a.Filenames = []string{}
   234  	o4a.FileIds = []string{model.NewId()}
   235  	if result := <-ss.Post().Update(o4a, ro4); result.Err != nil {
   236  		t.Fatal(result.Err)
   237  	}
   238  
   239  	if ro4a := store.Must(ss.Post().Get(o4.Id)).(*model.PostList).Posts[o4.Id]; len(ro4a.Filenames) != 0 {
   240  		t.Fatal("Failed to clear Filenames")
   241  	} else if len(ro4a.FileIds) != 1 {
   242  		t.Fatal("Failed to set FileIds")
   243  	}
   244  }
   245  
   246  func testPostStoreDelete(t *testing.T, ss store.Store) {
   247  	o1 := &model.Post{}
   248  	o1.ChannelId = model.NewId()
   249  	o1.UserId = model.NewId()
   250  	o1.Message = "zz" + model.NewId() + "b"
   251  	deleteByID := model.NewId()
   252  
   253  	etag1 := (<-ss.Post().GetEtag(o1.ChannelId, false)).Data.(string)
   254  	if strings.Index(etag1, model.CurrentVersion+".") != 0 {
   255  		t.Fatal("Invalid Etag")
   256  	}
   257  
   258  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
   259  
   260  	if r1 := <-ss.Post().Get(o1.Id); r1.Err != nil {
   261  		t.Fatal(r1.Err)
   262  	} else {
   263  		if r1.Data.(*model.PostList).Posts[o1.Id].CreateAt != o1.CreateAt {
   264  			t.Fatal("invalid returned post")
   265  		}
   266  	}
   267  
   268  	if r2 := <-ss.Post().Delete(o1.Id, model.GetMillis(), deleteByID); r2.Err != nil {
   269  		t.Fatal(r2.Err)
   270  	}
   271  
   272  	r5 := <-ss.Post().GetPostsCreatedAt(o1.ChannelId, o1.CreateAt)
   273  	post := r5.Data.([]*model.Post)[0]
   274  	actual := post.Props[model.POST_PROPS_DELETE_BY]
   275  	if actual != deleteByID {
   276  		t.Errorf("Expected (*Post).Props[model.POST_PROPS_DELETE_BY] to be %v but got %v.", deleteByID, actual)
   277  	}
   278  
   279  	if r3 := (<-ss.Post().Get(o1.Id)); r3.Err == nil {
   280  		t.Log(r3.Data)
   281  		t.Fatal("Missing id should have failed")
   282  	}
   283  
   284  	etag2 := (<-ss.Post().GetEtag(o1.ChannelId, false)).Data.(string)
   285  	if strings.Index(etag2, model.CurrentVersion+".") != 0 {
   286  		t.Fatal("Invalid Etag")
   287  	}
   288  }
   289  
   290  func testPostStoreDelete1Level(t *testing.T, ss store.Store) {
   291  	o1 := &model.Post{}
   292  	o1.ChannelId = model.NewId()
   293  	o1.UserId = model.NewId()
   294  	o1.Message = "zz" + model.NewId() + "b"
   295  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
   296  
   297  	o2 := &model.Post{}
   298  	o2.ChannelId = o1.ChannelId
   299  	o2.UserId = model.NewId()
   300  	o2.Message = "zz" + model.NewId() + "b"
   301  	o2.ParentId = o1.Id
   302  	o2.RootId = o1.Id
   303  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
   304  
   305  	if r2 := <-ss.Post().Delete(o1.Id, model.GetMillis(), ""); r2.Err != nil {
   306  		t.Fatal(r2.Err)
   307  	}
   308  
   309  	if r3 := (<-ss.Post().Get(o1.Id)); r3.Err == nil {
   310  		t.Fatal("Deleted id should have failed")
   311  	}
   312  
   313  	if r4 := (<-ss.Post().Get(o2.Id)); r4.Err == nil {
   314  		t.Fatal("Deleted id should have failed")
   315  	}
   316  }
   317  
   318  func testPostStoreDelete2Level(t *testing.T, ss store.Store) {
   319  	o1 := &model.Post{}
   320  	o1.ChannelId = model.NewId()
   321  	o1.UserId = model.NewId()
   322  	o1.Message = "zz" + model.NewId() + "b"
   323  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
   324  
   325  	o2 := &model.Post{}
   326  	o2.ChannelId = o1.ChannelId
   327  	o2.UserId = model.NewId()
   328  	o2.Message = "zz" + model.NewId() + "b"
   329  	o2.ParentId = o1.Id
   330  	o2.RootId = o1.Id
   331  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
   332  
   333  	o3 := &model.Post{}
   334  	o3.ChannelId = o1.ChannelId
   335  	o3.UserId = model.NewId()
   336  	o3.Message = "zz" + model.NewId() + "b"
   337  	o3.ParentId = o2.Id
   338  	o3.RootId = o1.Id
   339  	o3 = (<-ss.Post().Save(o3)).Data.(*model.Post)
   340  
   341  	o4 := &model.Post{}
   342  	o4.ChannelId = model.NewId()
   343  	o4.UserId = model.NewId()
   344  	o4.Message = "zz" + model.NewId() + "b"
   345  	o4 = (<-ss.Post().Save(o4)).Data.(*model.Post)
   346  
   347  	if r2 := <-ss.Post().Delete(o1.Id, model.GetMillis(), ""); r2.Err != nil {
   348  		t.Fatal(r2.Err)
   349  	}
   350  
   351  	if r3 := (<-ss.Post().Get(o1.Id)); r3.Err == nil {
   352  		t.Fatal("Deleted id should have failed")
   353  	}
   354  
   355  	if r4 := (<-ss.Post().Get(o2.Id)); r4.Err == nil {
   356  		t.Fatal("Deleted id should have failed")
   357  	}
   358  
   359  	if r5 := (<-ss.Post().Get(o3.Id)); r5.Err == nil {
   360  		t.Fatal("Deleted id should have failed")
   361  	}
   362  
   363  	if r6 := <-ss.Post().Get(o4.Id); r6.Err != nil {
   364  		t.Fatal(r6.Err)
   365  	}
   366  }
   367  
   368  func testPostStorePermDelete1Level(t *testing.T, ss store.Store) {
   369  	o1 := &model.Post{}
   370  	o1.ChannelId = model.NewId()
   371  	o1.UserId = model.NewId()
   372  	o1.Message = "zz" + model.NewId() + "b"
   373  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
   374  
   375  	o2 := &model.Post{}
   376  	o2.ChannelId = o1.ChannelId
   377  	o2.UserId = model.NewId()
   378  	o2.Message = "zz" + model.NewId() + "b"
   379  	o2.ParentId = o1.Id
   380  	o2.RootId = o1.Id
   381  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
   382  
   383  	o3 := &model.Post{}
   384  	o3.ChannelId = model.NewId()
   385  	o3.UserId = model.NewId()
   386  	o3.Message = "zz" + model.NewId() + "b"
   387  	o3 = (<-ss.Post().Save(o3)).Data.(*model.Post)
   388  
   389  	if r2 := <-ss.Post().PermanentDeleteByUser(o2.UserId); r2.Err != nil {
   390  		t.Fatal(r2.Err)
   391  	}
   392  
   393  	if r3 := (<-ss.Post().Get(o1.Id)); r3.Err != nil {
   394  		t.Fatal("Deleted id shouldn't have failed")
   395  	}
   396  
   397  	if r4 := (<-ss.Post().Get(o2.Id)); r4.Err == nil {
   398  		t.Fatal("Deleted id should have failed")
   399  	}
   400  
   401  	if r2 := <-ss.Post().PermanentDeleteByChannel(o3.ChannelId); r2.Err != nil {
   402  		t.Fatal(r2.Err)
   403  	}
   404  
   405  	if r3 := (<-ss.Post().Get(o3.Id)); r3.Err == nil {
   406  		t.Fatal("Deleted id should have failed")
   407  	}
   408  }
   409  
   410  func testPostStorePermDelete1Level2(t *testing.T, ss store.Store) {
   411  	o1 := &model.Post{}
   412  	o1.ChannelId = model.NewId()
   413  	o1.UserId = model.NewId()
   414  	o1.Message = "zz" + model.NewId() + "b"
   415  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
   416  
   417  	o2 := &model.Post{}
   418  	o2.ChannelId = o1.ChannelId
   419  	o2.UserId = model.NewId()
   420  	o2.Message = "zz" + model.NewId() + "b"
   421  	o2.ParentId = o1.Id
   422  	o2.RootId = o1.Id
   423  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
   424  
   425  	o3 := &model.Post{}
   426  	o3.ChannelId = model.NewId()
   427  	o3.UserId = model.NewId()
   428  	o3.Message = "zz" + model.NewId() + "b"
   429  	o3 = (<-ss.Post().Save(o3)).Data.(*model.Post)
   430  
   431  	if r2 := <-ss.Post().PermanentDeleteByUser(o1.UserId); r2.Err != nil {
   432  		t.Fatal(r2.Err)
   433  	}
   434  
   435  	if r3 := (<-ss.Post().Get(o1.Id)); r3.Err == nil {
   436  		t.Fatal("Deleted id should have failed")
   437  	}
   438  
   439  	if r4 := (<-ss.Post().Get(o2.Id)); r4.Err == nil {
   440  		t.Fatal("Deleted id should have failed")
   441  	}
   442  
   443  	if r5 := (<-ss.Post().Get(o3.Id)); r5.Err != nil {
   444  		t.Fatal("Deleted id shouldn't have failed")
   445  	}
   446  }
   447  
   448  func testPostStoreGetWithChildren(t *testing.T, ss store.Store) {
   449  	o1 := &model.Post{}
   450  	o1.ChannelId = model.NewId()
   451  	o1.UserId = model.NewId()
   452  	o1.Message = "zz" + model.NewId() + "b"
   453  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
   454  
   455  	o2 := &model.Post{}
   456  	o2.ChannelId = o1.ChannelId
   457  	o2.UserId = model.NewId()
   458  	o2.Message = "zz" + model.NewId() + "b"
   459  	o2.ParentId = o1.Id
   460  	o2.RootId = o1.Id
   461  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
   462  
   463  	o3 := &model.Post{}
   464  	o3.ChannelId = o1.ChannelId
   465  	o3.UserId = model.NewId()
   466  	o3.Message = "zz" + model.NewId() + "b"
   467  	o3.ParentId = o2.Id
   468  	o3.RootId = o1.Id
   469  	o3 = (<-ss.Post().Save(o3)).Data.(*model.Post)
   470  
   471  	if r1 := <-ss.Post().Get(o1.Id); r1.Err != nil {
   472  		t.Fatal(r1.Err)
   473  	} else {
   474  		pl := r1.Data.(*model.PostList)
   475  		if len(pl.Posts) != 3 {
   476  			t.Fatal("invalid returned post")
   477  		}
   478  	}
   479  
   480  	store.Must(ss.Post().Delete(o3.Id, model.GetMillis(), ""))
   481  
   482  	if r2 := <-ss.Post().Get(o1.Id); r2.Err != nil {
   483  		t.Fatal(r2.Err)
   484  	} else {
   485  		pl := r2.Data.(*model.PostList)
   486  		if len(pl.Posts) != 2 {
   487  			t.Fatal("invalid returned post")
   488  		}
   489  	}
   490  
   491  	store.Must(ss.Post().Delete(o2.Id, model.GetMillis(), ""))
   492  
   493  	if r3 := <-ss.Post().Get(o1.Id); r3.Err != nil {
   494  		t.Fatal(r3.Err)
   495  	} else {
   496  		pl := r3.Data.(*model.PostList)
   497  		if len(pl.Posts) != 1 {
   498  			t.Fatal("invalid returned post")
   499  		}
   500  	}
   501  }
   502  
   503  func testPostStoreGetPostsWithDetails(t *testing.T, ss store.Store) {
   504  	o1 := &model.Post{}
   505  	o1.ChannelId = model.NewId()
   506  	o1.UserId = model.NewId()
   507  	o1.Message = "zz" + model.NewId() + "b"
   508  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
   509  	time.Sleep(2 * time.Millisecond)
   510  
   511  	o2 := &model.Post{}
   512  	o2.ChannelId = o1.ChannelId
   513  	o2.UserId = model.NewId()
   514  	o2.Message = "zz" + model.NewId() + "b"
   515  	o2.ParentId = o1.Id
   516  	o2.RootId = o1.Id
   517  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
   518  	time.Sleep(2 * time.Millisecond)
   519  
   520  	o2a := &model.Post{}
   521  	o2a.ChannelId = o1.ChannelId
   522  	o2a.UserId = model.NewId()
   523  	o2a.Message = "zz" + model.NewId() + "b"
   524  	o2a.ParentId = o1.Id
   525  	o2a.RootId = o1.Id
   526  	o2a = (<-ss.Post().Save(o2a)).Data.(*model.Post)
   527  	time.Sleep(2 * time.Millisecond)
   528  
   529  	o3 := &model.Post{}
   530  	o3.ChannelId = o1.ChannelId
   531  	o3.UserId = model.NewId()
   532  	o3.Message = "zz" + model.NewId() + "b"
   533  	o3.ParentId = o1.Id
   534  	o3.RootId = o1.Id
   535  	o3 = (<-ss.Post().Save(o3)).Data.(*model.Post)
   536  	time.Sleep(2 * time.Millisecond)
   537  
   538  	o4 := &model.Post{}
   539  	o4.ChannelId = o1.ChannelId
   540  	o4.UserId = model.NewId()
   541  	o4.Message = "zz" + model.NewId() + "b"
   542  	o4 = (<-ss.Post().Save(o4)).Data.(*model.Post)
   543  	time.Sleep(2 * time.Millisecond)
   544  
   545  	o5 := &model.Post{}
   546  	o5.ChannelId = o1.ChannelId
   547  	o5.UserId = model.NewId()
   548  	o5.Message = "zz" + model.NewId() + "b"
   549  	o5.ParentId = o4.Id
   550  	o5.RootId = o4.Id
   551  	o5 = (<-ss.Post().Save(o5)).Data.(*model.Post)
   552  
   553  	r1 := (<-ss.Post().GetPosts(o1.ChannelId, 0, 4, false)).Data.(*model.PostList)
   554  
   555  	if r1.Order[0] != o5.Id {
   556  		t.Fatal("invalid order")
   557  	}
   558  
   559  	if r1.Order[1] != o4.Id {
   560  		t.Fatal("invalid order")
   561  	}
   562  
   563  	if r1.Order[2] != o3.Id {
   564  		t.Fatal("invalid order")
   565  	}
   566  
   567  	if r1.Order[3] != o2a.Id {
   568  		t.Fatal("invalid order")
   569  	}
   570  
   571  	if len(r1.Posts) != 6 { //the last 4, + o1 (o2a and o3's parent) + o2 (in same thread as o2a and o3)
   572  		t.Fatal("wrong size")
   573  	}
   574  
   575  	if r1.Posts[o1.Id].Message != o1.Message {
   576  		t.Fatal("Missing parent")
   577  	}
   578  
   579  	r2 := (<-ss.Post().GetPosts(o1.ChannelId, 0, 4, true)).Data.(*model.PostList)
   580  
   581  	if r2.Order[0] != o5.Id {
   582  		t.Fatal("invalid order")
   583  	}
   584  
   585  	if r2.Order[1] != o4.Id {
   586  		t.Fatal("invalid order")
   587  	}
   588  
   589  	if r2.Order[2] != o3.Id {
   590  		t.Fatal("invalid order")
   591  	}
   592  
   593  	if r2.Order[3] != o2a.Id {
   594  		t.Fatal("invalid order")
   595  	}
   596  
   597  	if len(r2.Posts) != 6 { //the last 4, + o1 (o2a and o3's parent) + o2 (in same thread as o2a and o3)
   598  		t.Fatal("wrong size")
   599  	}
   600  
   601  	if r2.Posts[o1.Id].Message != o1.Message {
   602  		t.Fatal("Missing parent")
   603  	}
   604  
   605  	// Run once to fill cache
   606  	<-ss.Post().GetPosts(o1.ChannelId, 0, 30, true)
   607  
   608  	o6 := &model.Post{}
   609  	o6.ChannelId = o1.ChannelId
   610  	o6.UserId = model.NewId()
   611  	o6.Message = "zz" + model.NewId() + "b"
   612  	o6 = (<-ss.Post().Save(o6)).Data.(*model.Post)
   613  
   614  	// Should only be 6 since we hit the cache
   615  	r3 := (<-ss.Post().GetPosts(o1.ChannelId, 0, 30, true)).Data.(*model.PostList)
   616  	assert.Equal(t, 6, len(r3.Order))
   617  
   618  	ss.Post().InvalidateLastPostTimeCache(o1.ChannelId)
   619  
   620  	// Cache was invalidated, we should get all the posts
   621  	r4 := (<-ss.Post().GetPosts(o1.ChannelId, 0, 30, true)).Data.(*model.PostList)
   622  	assert.Equal(t, 7, len(r4.Order))
   623  }
   624  
   625  func testPostStoreGetPostsBeforeAfter(t *testing.T, ss store.Store) {
   626  	o0 := &model.Post{}
   627  	o0.ChannelId = model.NewId()
   628  	o0.UserId = model.NewId()
   629  	o0.Message = "zz" + model.NewId() + "b"
   630  	o0 = (<-ss.Post().Save(o0)).Data.(*model.Post)
   631  	time.Sleep(2 * time.Millisecond)
   632  
   633  	o1 := &model.Post{}
   634  	o1.ChannelId = model.NewId()
   635  	o1.UserId = model.NewId()
   636  	o1.Message = "zz" + model.NewId() + "b"
   637  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
   638  	time.Sleep(2 * time.Millisecond)
   639  
   640  	o2 := &model.Post{}
   641  	o2.ChannelId = o1.ChannelId
   642  	o2.UserId = model.NewId()
   643  	o2.Message = "zz" + model.NewId() + "b"
   644  	o2.ParentId = o1.Id
   645  	o2.RootId = o1.Id
   646  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
   647  	time.Sleep(2 * time.Millisecond)
   648  
   649  	o2a := &model.Post{}
   650  	o2a.ChannelId = o1.ChannelId
   651  	o2a.UserId = model.NewId()
   652  	o2a.Message = "zz" + model.NewId() + "b"
   653  	o2a.ParentId = o1.Id
   654  	o2a.RootId = o1.Id
   655  	o2a = (<-ss.Post().Save(o2a)).Data.(*model.Post)
   656  	time.Sleep(2 * time.Millisecond)
   657  
   658  	o3 := &model.Post{}
   659  	o3.ChannelId = o1.ChannelId
   660  	o3.UserId = model.NewId()
   661  	o3.Message = "zz" + model.NewId() + "b"
   662  	o3.ParentId = o1.Id
   663  	o3.RootId = o1.Id
   664  	o3 = (<-ss.Post().Save(o3)).Data.(*model.Post)
   665  	time.Sleep(2 * time.Millisecond)
   666  
   667  	o4 := &model.Post{}
   668  	o4.ChannelId = o1.ChannelId
   669  	o4.UserId = model.NewId()
   670  	o4.Message = "zz" + model.NewId() + "b"
   671  	o4 = (<-ss.Post().Save(o4)).Data.(*model.Post)
   672  	time.Sleep(2 * time.Millisecond)
   673  
   674  	o5 := &model.Post{}
   675  	o5.ChannelId = o1.ChannelId
   676  	o5.UserId = model.NewId()
   677  	o5.Message = "zz" + model.NewId() + "b"
   678  	o5.ParentId = o4.Id
   679  	o5.RootId = o4.Id
   680  	o5 = (<-ss.Post().Save(o5)).Data.(*model.Post)
   681  
   682  	r1 := (<-ss.Post().GetPostsBefore(o1.ChannelId, o1.Id, 4, 0)).Data.(*model.PostList)
   683  
   684  	if len(r1.Posts) != 0 {
   685  		t.Fatal("Wrong size")
   686  	}
   687  
   688  	r2 := (<-ss.Post().GetPostsAfter(o1.ChannelId, o1.Id, 4, 0)).Data.(*model.PostList)
   689  
   690  	if r2.Order[0] != o4.Id {
   691  		t.Fatal("invalid order")
   692  	}
   693  
   694  	if r2.Order[1] != o3.Id {
   695  		t.Fatal("invalid order")
   696  	}
   697  
   698  	if r2.Order[2] != o2a.Id {
   699  		t.Fatal("invalid order")
   700  	}
   701  
   702  	if r2.Order[3] != o2.Id {
   703  		t.Fatal("invalid order")
   704  	}
   705  
   706  	if len(r2.Posts) != 5 {
   707  		t.Fatal("wrong size")
   708  	}
   709  
   710  	r3 := (<-ss.Post().GetPostsBefore(o3.ChannelId, o3.Id, 2, 0)).Data.(*model.PostList)
   711  
   712  	if r3.Order[0] != o2a.Id {
   713  		t.Fatal("invalid order")
   714  	}
   715  
   716  	if r3.Order[1] != o2.Id {
   717  		t.Fatal("invalid order")
   718  	}
   719  
   720  	if len(r3.Posts) != 3 {
   721  		t.Fatal("wrong size")
   722  	}
   723  
   724  	if r3.Posts[o1.Id].Message != o1.Message {
   725  		t.Fatal("Missing parent")
   726  	}
   727  }
   728  
   729  func testPostStoreGetPostsSince(t *testing.T, ss store.Store) {
   730  	o0 := &model.Post{}
   731  	o0.ChannelId = model.NewId()
   732  	o0.UserId = model.NewId()
   733  	o0.Message = "zz" + model.NewId() + "b"
   734  	o0 = (<-ss.Post().Save(o0)).Data.(*model.Post)
   735  	time.Sleep(2 * time.Millisecond)
   736  
   737  	o1 := &model.Post{}
   738  	o1.ChannelId = model.NewId()
   739  	o1.UserId = model.NewId()
   740  	o1.Message = "zz" + model.NewId() + "b"
   741  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
   742  	time.Sleep(2 * time.Millisecond)
   743  
   744  	o2 := &model.Post{}
   745  	o2.ChannelId = o1.ChannelId
   746  	o2.UserId = model.NewId()
   747  	o2.Message = "zz" + model.NewId() + "b"
   748  	o2.ParentId = o1.Id
   749  	o2.RootId = o1.Id
   750  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
   751  	time.Sleep(2 * time.Millisecond)
   752  
   753  	o2a := &model.Post{}
   754  	o2a.ChannelId = o1.ChannelId
   755  	o2a.UserId = model.NewId()
   756  	o2a.Message = "zz" + model.NewId() + "b"
   757  	o2a.ParentId = o1.Id
   758  	o2a.RootId = o1.Id
   759  	o2a = (<-ss.Post().Save(o2a)).Data.(*model.Post)
   760  	time.Sleep(2 * time.Millisecond)
   761  
   762  	o3 := &model.Post{}
   763  	o3.ChannelId = o1.ChannelId
   764  	o3.UserId = model.NewId()
   765  	o3.Message = "zz" + model.NewId() + "b"
   766  	o3.ParentId = o1.Id
   767  	o3.RootId = o1.Id
   768  	o3 = (<-ss.Post().Save(o3)).Data.(*model.Post)
   769  	time.Sleep(2 * time.Millisecond)
   770  
   771  	o4 := &model.Post{}
   772  	o4.ChannelId = o1.ChannelId
   773  	o4.UserId = model.NewId()
   774  	o4.Message = "zz" + model.NewId() + "b"
   775  	o4 = (<-ss.Post().Save(o4)).Data.(*model.Post)
   776  	time.Sleep(2 * time.Millisecond)
   777  
   778  	o5 := &model.Post{}
   779  	o5.ChannelId = o1.ChannelId
   780  	o5.UserId = model.NewId()
   781  	o5.Message = "zz" + model.NewId() + "b"
   782  	o5.ParentId = o4.Id
   783  	o5.RootId = o4.Id
   784  	o5 = (<-ss.Post().Save(o5)).Data.(*model.Post)
   785  
   786  	r1 := (<-ss.Post().GetPostsSince(o1.ChannelId, o1.CreateAt, false)).Data.(*model.PostList)
   787  
   788  	if r1.Order[0] != o5.Id {
   789  		t.Fatal("invalid order")
   790  	}
   791  
   792  	if r1.Order[1] != o4.Id {
   793  		t.Fatal("invalid order")
   794  	}
   795  
   796  	if r1.Order[2] != o3.Id {
   797  		t.Fatal("invalid order")
   798  	}
   799  
   800  	if r1.Order[3] != o2a.Id {
   801  		t.Fatal("invalid order")
   802  	}
   803  
   804  	if len(r1.Posts) != 6 {
   805  		t.Fatal("wrong size")
   806  	}
   807  
   808  	if r1.Posts[o1.Id].Message != o1.Message {
   809  		t.Fatal("Missing parent")
   810  	}
   811  
   812  	r2 := (<-ss.Post().GetPostsSince(o1.ChannelId, o5.UpdateAt, true)).Data.(*model.PostList)
   813  
   814  	if len(r2.Order) != 0 {
   815  		t.Fatal("wrong size ", len(r2.Posts))
   816  	}
   817  }
   818  
   819  func testPostStoreSearch(t *testing.T, ss store.Store) {
   820  	teamId := model.NewId()
   821  	userId := model.NewId()
   822  
   823  	c1 := &model.Channel{}
   824  	c1.TeamId = teamId
   825  	c1.DisplayName = "Channel1"
   826  	c1.Name = "zz" + model.NewId() + "b"
   827  	c1.Type = model.CHANNEL_OPEN
   828  	c1 = (<-ss.Channel().Save(c1, -1)).Data.(*model.Channel)
   829  
   830  	m1 := model.ChannelMember{}
   831  	m1.ChannelId = c1.Id
   832  	m1.UserId = userId
   833  	m1.NotifyProps = model.GetDefaultChannelNotifyProps()
   834  	store.Must(ss.Channel().SaveMember(&m1))
   835  
   836  	c2 := &model.Channel{}
   837  	c2.TeamId = teamId
   838  	c2.DisplayName = "Channel1"
   839  	c2.Name = "zz" + model.NewId() + "b"
   840  	c2.Type = model.CHANNEL_OPEN
   841  	c2 = (<-ss.Channel().Save(c2, -1)).Data.(*model.Channel)
   842  
   843  	c3 := &model.Channel{}
   844  	c3.TeamId = teamId
   845  	c3.DisplayName = "Channel1"
   846  	c3.Name = "zz" + model.NewId() + "b"
   847  	c3.Type = model.CHANNEL_OPEN
   848  	c3 = (<-ss.Channel().Save(c3, -1)).Data.(*model.Channel)
   849  	<-ss.Channel().Delete(c3.Id, model.GetMillis())
   850  
   851  	m3 := model.ChannelMember{}
   852  	m3.ChannelId = c3.Id
   853  	m3.UserId = userId
   854  	m3.NotifyProps = model.GetDefaultChannelNotifyProps()
   855  	store.Must(ss.Channel().SaveMember(&m3))
   856  
   857  	o1 := &model.Post{}
   858  	o1.ChannelId = c1.Id
   859  	o1.UserId = model.NewId()
   860  	o1.Message = "corey mattermost new york"
   861  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
   862  
   863  	o1a := &model.Post{}
   864  	o1a.ChannelId = c1.Id
   865  	o1a.UserId = model.NewId()
   866  	o1a.Message = "corey mattermost new york"
   867  	o1a.Type = model.POST_JOIN_CHANNEL
   868  	o1a = (<-ss.Post().Save(o1a)).Data.(*model.Post)
   869  
   870  	o2 := &model.Post{}
   871  	o2.ChannelId = c1.Id
   872  	o2.UserId = model.NewId()
   873  	o2.Message = "New Jersey is where John is from"
   874  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
   875  
   876  	o3 := &model.Post{}
   877  	o3.ChannelId = c2.Id
   878  	o3.UserId = model.NewId()
   879  	o3.Message = "New Jersey is where John is from corey new york"
   880  	o3 = (<-ss.Post().Save(o3)).Data.(*model.Post)
   881  
   882  	o4 := &model.Post{}
   883  	o4.ChannelId = c1.Id
   884  	o4.UserId = model.NewId()
   885  	o4.Hashtags = "#hashtag"
   886  	o4.Message = "(message)blargh"
   887  	o4 = (<-ss.Post().Save(o4)).Data.(*model.Post)
   888  
   889  	o5 := &model.Post{}
   890  	o5.ChannelId = c1.Id
   891  	o5.UserId = model.NewId()
   892  	o5.Hashtags = "#secret #howdy"
   893  	o5 = (<-ss.Post().Save(o5)).Data.(*model.Post)
   894  
   895  	o6 := &model.Post{}
   896  	o6.ChannelId = c3.Id
   897  	o6.UserId = model.NewId()
   898  	o6.Hashtags = "#hashtag"
   899  	o6 = (<-ss.Post().Save(o6)).Data.(*model.Post)
   900  
   901  	o7 := &model.Post{}
   902  	o7.ChannelId = c3.Id
   903  	o7.UserId = model.NewId()
   904  	o7.Message = "New Jersey is where John is from corey new york"
   905  	o7 = (<-ss.Post().Save(o7)).Data.(*model.Post)
   906  
   907  	o8 := &model.Post{}
   908  	o8.ChannelId = c3.Id
   909  	o8.UserId = model.NewId()
   910  	o8.Message = "Deleted"
   911  	o8 = (<-ss.Post().Save(o8)).Data.(*model.Post)
   912  
   913  	tt := []struct {
   914  		name                     string
   915  		searchParams             *model.SearchParams
   916  		extectedResultsCount     int
   917  		expectedMessageResultIds []string
   918  	}{
   919  		{
   920  			"normal-search-1",
   921  			&model.SearchParams{Terms: "corey"},
   922  			1,
   923  			[]string{o1.Id},
   924  		},
   925  		{
   926  			"normal-search-2",
   927  			&model.SearchParams{Terms: "new"},
   928  			2,
   929  			[]string{o1.Id, o2.Id},
   930  		},
   931  		{
   932  			"normal-search-3",
   933  			&model.SearchParams{Terms: "john"},
   934  			1,
   935  			[]string{o2.Id},
   936  		},
   937  		{
   938  			"wildcard-search",
   939  			&model.SearchParams{Terms: "matter*"},
   940  			1,
   941  			[]string{o1.Id},
   942  		},
   943  		{
   944  			"hashtag-search",
   945  			&model.SearchParams{Terms: "#hashtag", IsHashtag: true},
   946  			1,
   947  			[]string{o4.Id},
   948  		},
   949  		{
   950  			"hashtag-search-2",
   951  			&model.SearchParams{Terms: "#secret", IsHashtag: true},
   952  			1,
   953  			[]string{o5.Id},
   954  		},
   955  		{
   956  			"no-match-mention",
   957  			&model.SearchParams{Terms: "@thisshouldmatchnothing", IsHashtag: true},
   958  			0,
   959  			[]string{},
   960  		},
   961  		{
   962  			"no-results-search",
   963  			&model.SearchParams{Terms: "mattermost jersey"},
   964  			0,
   965  			[]string{},
   966  		},
   967  		{
   968  			"multiple-words-search",
   969  			&model.SearchParams{Terms: "corey new york"},
   970  			1,
   971  			[]string{o1.Id},
   972  		},
   973  		{
   974  			"multiple-wildcard-search",
   975  			&model.SearchParams{Terms: "matter* jer*"},
   976  			0,
   977  			[]string{},
   978  		},
   979  		{
   980  			"search-with-work-next-to-a-symbol",
   981  			&model.SearchParams{Terms: "message blargh"},
   982  			1,
   983  			[]string{o4.Id},
   984  		},
   985  		{
   986  			"search-with-or",
   987  			&model.SearchParams{Terms: "Jersey corey", OrTerms: true},
   988  			2,
   989  			[]string{o1.Id, o2.Id},
   990  		},
   991  		{
   992  			"search-with-or-and-deleted",
   993  			&model.SearchParams{Terms: "Jersey corey", OrTerms: true, IncludeDeletedChannels: true},
   994  			3,
   995  			[]string{o1.Id, o2.Id, o7.Id},
   996  		},
   997  		{
   998  			"search-hashtag-deleted",
   999  			&model.SearchParams{Terms: "#hashtag", IsHashtag: true, IncludeDeletedChannels: true},
  1000  			2,
  1001  			[]string{o4.Id, o6.Id},
  1002  		},
  1003  		{
  1004  			"search-deleted-only",
  1005  			&model.SearchParams{Terms: "Deleted", IncludeDeletedChannels: true},
  1006  			1,
  1007  			[]string{o8.Id},
  1008  		},
  1009  	}
  1010  	for _, tc := range tt {
  1011  		t.Run(tc.name, func(t *testing.T) {
  1012  			result := (<-ss.Post().Search(teamId, userId, tc.searchParams)).Data.(*model.PostList)
  1013  			require.Len(t, result.Order, tc.extectedResultsCount)
  1014  			for _, expectedMessageResultId := range tc.expectedMessageResultIds {
  1015  				assert.Contains(t, result.Order, expectedMessageResultId)
  1016  			}
  1017  		})
  1018  	}
  1019  }
  1020  
  1021  func testUserCountsWithPostsByDay(t *testing.T, ss store.Store) {
  1022  	t1 := &model.Team{}
  1023  	t1.DisplayName = "DisplayName"
  1024  	t1.Name = "zz" + model.NewId() + "b"
  1025  	t1.Email = MakeEmail()
  1026  	t1.Type = model.TEAM_OPEN
  1027  	t1 = store.Must(ss.Team().Save(t1)).(*model.Team)
  1028  
  1029  	c1 := &model.Channel{}
  1030  	c1.TeamId = t1.Id
  1031  	c1.DisplayName = "Channel2"
  1032  	c1.Name = "zz" + model.NewId() + "b"
  1033  	c1.Type = model.CHANNEL_OPEN
  1034  	c1 = store.Must(ss.Channel().Save(c1, -1)).(*model.Channel)
  1035  
  1036  	o1 := &model.Post{}
  1037  	o1.ChannelId = c1.Id
  1038  	o1.UserId = model.NewId()
  1039  	o1.CreateAt = utils.MillisFromTime(utils.Yesterday())
  1040  	o1.Message = "zz" + model.NewId() + "b"
  1041  	o1 = store.Must(ss.Post().Save(o1)).(*model.Post)
  1042  
  1043  	o1a := &model.Post{}
  1044  	o1a.ChannelId = c1.Id
  1045  	o1a.UserId = model.NewId()
  1046  	o1a.CreateAt = o1.CreateAt
  1047  	o1a.Message = "zz" + model.NewId() + "b"
  1048  	o1a = store.Must(ss.Post().Save(o1a)).(*model.Post)
  1049  
  1050  	o2 := &model.Post{}
  1051  	o2.ChannelId = c1.Id
  1052  	o2.UserId = model.NewId()
  1053  	o2.CreateAt = o1.CreateAt - (1000 * 60 * 60 * 24)
  1054  	o2.Message = "zz" + model.NewId() + "b"
  1055  	o2 = store.Must(ss.Post().Save(o2)).(*model.Post)
  1056  
  1057  	o2a := &model.Post{}
  1058  	o2a.ChannelId = c1.Id
  1059  	o2a.UserId = o2.UserId
  1060  	o2a.CreateAt = o1.CreateAt - (1000 * 60 * 60 * 24)
  1061  	o2a.Message = "zz" + model.NewId() + "b"
  1062  	o2a = store.Must(ss.Post().Save(o2a)).(*model.Post)
  1063  
  1064  	if r1 := <-ss.Post().AnalyticsUserCountsWithPostsByDay(t1.Id); r1.Err != nil {
  1065  		t.Fatal(r1.Err)
  1066  	} else {
  1067  		row1 := r1.Data.(model.AnalyticsRows)[0]
  1068  		if row1.Value != 2 {
  1069  			t.Fatal("wrong value")
  1070  		}
  1071  
  1072  		row2 := r1.Data.(model.AnalyticsRows)[1]
  1073  		if row2.Value != 1 {
  1074  			t.Fatal("wrong value")
  1075  		}
  1076  	}
  1077  }
  1078  
  1079  func testPostCountsByDay(t *testing.T, ss store.Store) {
  1080  	t1 := &model.Team{}
  1081  	t1.DisplayName = "DisplayName"
  1082  	t1.Name = "zz" + model.NewId() + "b"
  1083  	t1.Email = MakeEmail()
  1084  	t1.Type = model.TEAM_OPEN
  1085  	t1 = store.Must(ss.Team().Save(t1)).(*model.Team)
  1086  
  1087  	c1 := &model.Channel{}
  1088  	c1.TeamId = t1.Id
  1089  	c1.DisplayName = "Channel2"
  1090  	c1.Name = "zz" + model.NewId() + "b"
  1091  	c1.Type = model.CHANNEL_OPEN
  1092  	c1 = store.Must(ss.Channel().Save(c1, -1)).(*model.Channel)
  1093  
  1094  	o1 := &model.Post{}
  1095  	o1.ChannelId = c1.Id
  1096  	o1.UserId = model.NewId()
  1097  	o1.CreateAt = utils.MillisFromTime(utils.Yesterday())
  1098  	o1.Message = "zz" + model.NewId() + "b"
  1099  	o1 = store.Must(ss.Post().Save(o1)).(*model.Post)
  1100  
  1101  	o1a := &model.Post{}
  1102  	o1a.ChannelId = c1.Id
  1103  	o1a.UserId = model.NewId()
  1104  	o1a.CreateAt = o1.CreateAt
  1105  	o1a.Message = "zz" + model.NewId() + "b"
  1106  	o1a = store.Must(ss.Post().Save(o1a)).(*model.Post)
  1107  
  1108  	o2 := &model.Post{}
  1109  	o2.ChannelId = c1.Id
  1110  	o2.UserId = model.NewId()
  1111  	o2.CreateAt = o1.CreateAt - (1000 * 60 * 60 * 24 * 2)
  1112  	o2.Message = "zz" + model.NewId() + "b"
  1113  	o2 = store.Must(ss.Post().Save(o2)).(*model.Post)
  1114  
  1115  	o2a := &model.Post{}
  1116  	o2a.ChannelId = c1.Id
  1117  	o2a.UserId = o2.UserId
  1118  	o2a.CreateAt = o1.CreateAt - (1000 * 60 * 60 * 24 * 2)
  1119  	o2a.Message = "zz" + model.NewId() + "b"
  1120  	o2a = store.Must(ss.Post().Save(o2a)).(*model.Post)
  1121  
  1122  	time.Sleep(1 * time.Second)
  1123  
  1124  	if r1 := <-ss.Post().AnalyticsPostCountsByDay(t1.Id); r1.Err != nil {
  1125  		t.Fatal(r1.Err)
  1126  	} else {
  1127  		row1 := r1.Data.(model.AnalyticsRows)[0]
  1128  		if row1.Value != 2 {
  1129  			t.Fatal(row1)
  1130  		}
  1131  
  1132  		row2 := r1.Data.(model.AnalyticsRows)[1]
  1133  		if row2.Value != 2 {
  1134  			t.Fatal("wrong value")
  1135  		}
  1136  	}
  1137  
  1138  	if r1 := <-ss.Post().AnalyticsPostCount(t1.Id, false, false); r1.Err != nil {
  1139  		t.Fatal(r1.Err)
  1140  	} else {
  1141  		if r1.Data.(int64) != 4 {
  1142  			t.Fatal("wrong value")
  1143  		}
  1144  	}
  1145  }
  1146  
  1147  func testPostStoreGetFlaggedPostsForTeam(t *testing.T, ss store.Store) {
  1148  	c1 := &model.Channel{}
  1149  	c1.TeamId = model.NewId()
  1150  	c1.DisplayName = "Channel1"
  1151  	c1.Name = "zz" + model.NewId() + "b"
  1152  	c1.Type = model.CHANNEL_OPEN
  1153  	c1 = store.Must(ss.Channel().Save(c1, -1)).(*model.Channel)
  1154  
  1155  	o1 := &model.Post{}
  1156  	o1.ChannelId = c1.Id
  1157  	o1.UserId = model.NewId()
  1158  	o1.Message = "zz" + model.NewId() + "b"
  1159  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
  1160  	time.Sleep(2 * time.Millisecond)
  1161  
  1162  	o2 := &model.Post{}
  1163  	o2.ChannelId = o1.ChannelId
  1164  	o2.UserId = model.NewId()
  1165  	o2.Message = "zz" + model.NewId() + "b"
  1166  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
  1167  	time.Sleep(2 * time.Millisecond)
  1168  
  1169  	o3 := &model.Post{}
  1170  	o3.ChannelId = o1.ChannelId
  1171  	o3.UserId = model.NewId()
  1172  	o3.Message = "zz" + model.NewId() + "b"
  1173  	o3.DeleteAt = 1
  1174  	o3 = (<-ss.Post().Save(o3)).Data.(*model.Post)
  1175  	time.Sleep(2 * time.Millisecond)
  1176  
  1177  	o4 := &model.Post{}
  1178  	o4.ChannelId = model.NewId()
  1179  	o4.UserId = model.NewId()
  1180  	o4.Message = "zz" + model.NewId() + "b"
  1181  	o4 = (<-ss.Post().Save(o4)).Data.(*model.Post)
  1182  	time.Sleep(2 * time.Millisecond)
  1183  
  1184  	c2 := &model.Channel{}
  1185  	c2.DisplayName = "DMChannel1"
  1186  	c2.Name = "zz" + model.NewId() + "b"
  1187  	c2.Type = model.CHANNEL_DIRECT
  1188  
  1189  	m1 := &model.ChannelMember{}
  1190  	m1.ChannelId = c2.Id
  1191  	m1.UserId = o1.UserId
  1192  	m1.NotifyProps = model.GetDefaultChannelNotifyProps()
  1193  
  1194  	m2 := &model.ChannelMember{}
  1195  	m2.ChannelId = c2.Id
  1196  	m2.UserId = model.NewId()
  1197  	m2.NotifyProps = model.GetDefaultChannelNotifyProps()
  1198  
  1199  	c2 = store.Must(ss.Channel().SaveDirectChannel(c2, m1, m2)).(*model.Channel)
  1200  
  1201  	o5 := &model.Post{}
  1202  	o5.ChannelId = c2.Id
  1203  	o5.UserId = m2.UserId
  1204  	o5.Message = "zz" + model.NewId() + "b"
  1205  	o5 = (<-ss.Post().Save(o5)).Data.(*model.Post)
  1206  	time.Sleep(2 * time.Millisecond)
  1207  
  1208  	r1 := (<-ss.Post().GetFlaggedPosts(o1.ChannelId, 0, 2)).Data.(*model.PostList)
  1209  
  1210  	if len(r1.Order) != 0 {
  1211  		t.Fatal("should be empty")
  1212  	}
  1213  
  1214  	preferences := model.Preferences{
  1215  		{
  1216  			UserId:   o1.UserId,
  1217  			Category: model.PREFERENCE_CATEGORY_FLAGGED_POST,
  1218  			Name:     o1.Id,
  1219  			Value:    "true",
  1220  		},
  1221  	}
  1222  
  1223  	store.Must(ss.Preference().Save(&preferences))
  1224  
  1225  	r2 := (<-ss.Post().GetFlaggedPostsForTeam(o1.UserId, c1.TeamId, 0, 2)).Data.(*model.PostList)
  1226  
  1227  	if len(r2.Order) != 1 {
  1228  		t.Fatal("should have 1 post")
  1229  	}
  1230  
  1231  	preferences = model.Preferences{
  1232  		{
  1233  			UserId:   o1.UserId,
  1234  			Category: model.PREFERENCE_CATEGORY_FLAGGED_POST,
  1235  			Name:     o2.Id,
  1236  			Value:    "true",
  1237  		},
  1238  	}
  1239  
  1240  	store.Must(ss.Preference().Save(&preferences))
  1241  
  1242  	r3 := (<-ss.Post().GetFlaggedPostsForTeam(o1.UserId, c1.TeamId, 0, 1)).Data.(*model.PostList)
  1243  
  1244  	if len(r3.Order) != 1 {
  1245  		t.Fatal("should have 1 post")
  1246  	}
  1247  
  1248  	r3 = (<-ss.Post().GetFlaggedPostsForTeam(o1.UserId, c1.TeamId, 1, 1)).Data.(*model.PostList)
  1249  
  1250  	if len(r3.Order) != 1 {
  1251  		t.Fatal("should have 1 post")
  1252  	}
  1253  
  1254  	r3 = (<-ss.Post().GetFlaggedPostsForTeam(o1.UserId, c1.TeamId, 1000, 10)).Data.(*model.PostList)
  1255  
  1256  	if len(r3.Order) != 0 {
  1257  		t.Fatal("should be empty")
  1258  	}
  1259  
  1260  	r4 := (<-ss.Post().GetFlaggedPostsForTeam(o1.UserId, c1.TeamId, 0, 2)).Data.(*model.PostList)
  1261  
  1262  	if len(r4.Order) != 2 {
  1263  		t.Fatal("should have 2 posts")
  1264  	}
  1265  
  1266  	preferences = model.Preferences{
  1267  		{
  1268  			UserId:   o1.UserId,
  1269  			Category: model.PREFERENCE_CATEGORY_FLAGGED_POST,
  1270  			Name:     o3.Id,
  1271  			Value:    "true",
  1272  		},
  1273  	}
  1274  
  1275  	store.Must(ss.Preference().Save(&preferences))
  1276  
  1277  	r4 = (<-ss.Post().GetFlaggedPostsForTeam(o1.UserId, c1.TeamId, 0, 2)).Data.(*model.PostList)
  1278  
  1279  	if len(r4.Order) != 2 {
  1280  		t.Fatal("should have 2 posts")
  1281  	}
  1282  
  1283  	preferences = model.Preferences{
  1284  		{
  1285  			UserId:   o1.UserId,
  1286  			Category: model.PREFERENCE_CATEGORY_FLAGGED_POST,
  1287  			Name:     o4.Id,
  1288  			Value:    "true",
  1289  		},
  1290  	}
  1291  	store.Must(ss.Preference().Save(&preferences))
  1292  
  1293  	r4 = (<-ss.Post().GetFlaggedPostsForTeam(o1.UserId, c1.TeamId, 0, 2)).Data.(*model.PostList)
  1294  
  1295  	if len(r4.Order) != 2 {
  1296  		t.Fatal("should have 2 posts")
  1297  	}
  1298  
  1299  	r4 = (<-ss.Post().GetFlaggedPostsForTeam(o1.UserId, model.NewId(), 0, 2)).Data.(*model.PostList)
  1300  
  1301  	if len(r4.Order) != 0 {
  1302  		t.Fatal("should have 0 posts")
  1303  	}
  1304  
  1305  	preferences = model.Preferences{
  1306  		{
  1307  			UserId:   o1.UserId,
  1308  			Category: model.PREFERENCE_CATEGORY_FLAGGED_POST,
  1309  			Name:     o5.Id,
  1310  			Value:    "true",
  1311  		},
  1312  	}
  1313  	store.Must(ss.Preference().Save(&preferences))
  1314  
  1315  	r4 = (<-ss.Post().GetFlaggedPostsForTeam(o1.UserId, c1.TeamId, 0, 10)).Data.(*model.PostList)
  1316  
  1317  	if len(r4.Order) != 3 {
  1318  		t.Fatal("should have 3 posts")
  1319  	}
  1320  }
  1321  
  1322  func testPostStoreGetFlaggedPosts(t *testing.T, ss store.Store) {
  1323  	o1 := &model.Post{}
  1324  	o1.ChannelId = model.NewId()
  1325  	o1.UserId = model.NewId()
  1326  	o1.Message = "zz" + model.NewId() + "b"
  1327  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
  1328  	time.Sleep(2 * time.Millisecond)
  1329  
  1330  	o2 := &model.Post{}
  1331  	o2.ChannelId = o1.ChannelId
  1332  	o2.UserId = model.NewId()
  1333  	o2.Message = "zz" + model.NewId() + "b"
  1334  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
  1335  	time.Sleep(2 * time.Millisecond)
  1336  
  1337  	o3 := &model.Post{}
  1338  	o3.ChannelId = o1.ChannelId
  1339  	o3.UserId = model.NewId()
  1340  	o3.Message = "zz" + model.NewId() + "b"
  1341  	o3.DeleteAt = 1
  1342  	o3 = (<-ss.Post().Save(o3)).Data.(*model.Post)
  1343  	time.Sleep(2 * time.Millisecond)
  1344  
  1345  	r1 := (<-ss.Post().GetFlaggedPosts(o1.UserId, 0, 2)).Data.(*model.PostList)
  1346  
  1347  	if len(r1.Order) != 0 {
  1348  		t.Fatal("should be empty")
  1349  	}
  1350  
  1351  	preferences := model.Preferences{
  1352  		{
  1353  			UserId:   o1.UserId,
  1354  			Category: model.PREFERENCE_CATEGORY_FLAGGED_POST,
  1355  			Name:     o1.Id,
  1356  			Value:    "true",
  1357  		},
  1358  	}
  1359  
  1360  	store.Must(ss.Preference().Save(&preferences))
  1361  
  1362  	r2 := (<-ss.Post().GetFlaggedPosts(o1.UserId, 0, 2)).Data.(*model.PostList)
  1363  
  1364  	if len(r2.Order) != 1 {
  1365  		t.Fatal("should have 1 post")
  1366  	}
  1367  
  1368  	preferences = model.Preferences{
  1369  		{
  1370  			UserId:   o1.UserId,
  1371  			Category: model.PREFERENCE_CATEGORY_FLAGGED_POST,
  1372  			Name:     o2.Id,
  1373  			Value:    "true",
  1374  		},
  1375  	}
  1376  
  1377  	store.Must(ss.Preference().Save(&preferences))
  1378  
  1379  	r3 := (<-ss.Post().GetFlaggedPosts(o1.UserId, 0, 1)).Data.(*model.PostList)
  1380  
  1381  	if len(r3.Order) != 1 {
  1382  		t.Fatal("should have 1 post")
  1383  	}
  1384  
  1385  	r3 = (<-ss.Post().GetFlaggedPosts(o1.UserId, 1, 1)).Data.(*model.PostList)
  1386  
  1387  	if len(r3.Order) != 1 {
  1388  		t.Fatal("should have 1 post")
  1389  	}
  1390  
  1391  	r3 = (<-ss.Post().GetFlaggedPosts(o1.UserId, 1000, 10)).Data.(*model.PostList)
  1392  
  1393  	if len(r3.Order) != 0 {
  1394  		t.Fatal("should be empty")
  1395  	}
  1396  
  1397  	r4 := (<-ss.Post().GetFlaggedPosts(o1.UserId, 0, 2)).Data.(*model.PostList)
  1398  
  1399  	if len(r4.Order) != 2 {
  1400  		t.Fatal("should have 2 posts")
  1401  	}
  1402  
  1403  	preferences = model.Preferences{
  1404  		{
  1405  			UserId:   o1.UserId,
  1406  			Category: model.PREFERENCE_CATEGORY_FLAGGED_POST,
  1407  			Name:     o3.Id,
  1408  			Value:    "true",
  1409  		},
  1410  	}
  1411  
  1412  	store.Must(ss.Preference().Save(&preferences))
  1413  
  1414  	r4 = (<-ss.Post().GetFlaggedPosts(o1.UserId, 0, 2)).Data.(*model.PostList)
  1415  
  1416  	if len(r4.Order) != 2 {
  1417  		t.Fatal("should have 2 posts")
  1418  	}
  1419  }
  1420  
  1421  func testPostStoreGetFlaggedPostsForChannel(t *testing.T, ss store.Store) {
  1422  	o1 := &model.Post{}
  1423  	o1.ChannelId = model.NewId()
  1424  	o1.UserId = model.NewId()
  1425  	o1.Message = "zz" + model.NewId() + "b"
  1426  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
  1427  	time.Sleep(2 * time.Millisecond)
  1428  
  1429  	o2 := &model.Post{}
  1430  	o2.ChannelId = o1.ChannelId
  1431  	o2.UserId = model.NewId()
  1432  	o2.Message = "zz" + model.NewId() + "b"
  1433  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
  1434  	time.Sleep(2 * time.Millisecond)
  1435  
  1436  	// deleted post
  1437  	o3 := &model.Post{}
  1438  	o3.ChannelId = model.NewId()
  1439  	o3.UserId = o1.ChannelId
  1440  	o3.Message = "zz" + model.NewId() + "b"
  1441  	o3.DeleteAt = 1
  1442  	o3 = (<-ss.Post().Save(o3)).Data.(*model.Post)
  1443  	time.Sleep(2 * time.Millisecond)
  1444  
  1445  	o4 := &model.Post{}
  1446  	o4.ChannelId = model.NewId()
  1447  	o4.UserId = model.NewId()
  1448  	o4.Message = "zz" + model.NewId() + "b"
  1449  	o4 = (<-ss.Post().Save(o4)).Data.(*model.Post)
  1450  	time.Sleep(2 * time.Millisecond)
  1451  
  1452  	r := (<-ss.Post().GetFlaggedPostsForChannel(o1.UserId, o1.ChannelId, 0, 10)).Data.(*model.PostList)
  1453  
  1454  	if len(r.Order) != 0 {
  1455  		t.Fatal("should be empty")
  1456  	}
  1457  
  1458  	preference := model.Preference{
  1459  		UserId:   o1.UserId,
  1460  		Category: model.PREFERENCE_CATEGORY_FLAGGED_POST,
  1461  		Name:     o1.Id,
  1462  		Value:    "true",
  1463  	}
  1464  
  1465  	store.Must(ss.Preference().Save(&model.Preferences{preference}))
  1466  
  1467  	r = (<-ss.Post().GetFlaggedPostsForChannel(o1.UserId, o1.ChannelId, 0, 10)).Data.(*model.PostList)
  1468  
  1469  	if len(r.Order) != 1 {
  1470  		t.Fatal("should have 1 post")
  1471  	}
  1472  
  1473  	preference.Name = o2.Id
  1474  	store.Must(ss.Preference().Save(&model.Preferences{preference}))
  1475  
  1476  	preference.Name = o3.Id
  1477  	store.Must(ss.Preference().Save(&model.Preferences{preference}))
  1478  
  1479  	r = (<-ss.Post().GetFlaggedPostsForChannel(o1.UserId, o1.ChannelId, 0, 1)).Data.(*model.PostList)
  1480  
  1481  	if len(r.Order) != 1 {
  1482  		t.Fatal("should have 1 post")
  1483  	}
  1484  
  1485  	r = (<-ss.Post().GetFlaggedPostsForChannel(o1.UserId, o1.ChannelId, 1, 1)).Data.(*model.PostList)
  1486  
  1487  	if len(r.Order) != 1 {
  1488  		t.Fatal("should have 1 post")
  1489  	}
  1490  
  1491  	r = (<-ss.Post().GetFlaggedPostsForChannel(o1.UserId, o1.ChannelId, 1000, 10)).Data.(*model.PostList)
  1492  
  1493  	if len(r.Order) != 0 {
  1494  		t.Fatal("should be empty")
  1495  	}
  1496  
  1497  	r = (<-ss.Post().GetFlaggedPostsForChannel(o1.UserId, o1.ChannelId, 0, 10)).Data.(*model.PostList)
  1498  
  1499  	if len(r.Order) != 2 {
  1500  		t.Fatal("should have 2 posts")
  1501  	}
  1502  
  1503  	preference.Name = o4.Id
  1504  	store.Must(ss.Preference().Save(&model.Preferences{preference}))
  1505  
  1506  	r = (<-ss.Post().GetFlaggedPostsForChannel(o1.UserId, o4.ChannelId, 0, 10)).Data.(*model.PostList)
  1507  
  1508  	if len(r.Order) != 1 {
  1509  		t.Fatal("should have 1 post")
  1510  	}
  1511  }
  1512  
  1513  func testPostStoreGetPostsCreatedAt(t *testing.T, ss store.Store) {
  1514  	createTime := model.GetMillis() + 1
  1515  
  1516  	o0 := &model.Post{}
  1517  	o0.ChannelId = model.NewId()
  1518  	o0.UserId = model.NewId()
  1519  	o0.Message = "zz" + model.NewId() + "b"
  1520  	o0.CreateAt = createTime
  1521  	o0 = (<-ss.Post().Save(o0)).Data.(*model.Post)
  1522  
  1523  	o1 := &model.Post{}
  1524  	o1.ChannelId = o0.ChannelId
  1525  	o1.UserId = model.NewId()
  1526  	o1.Message = "zz" + model.NewId() + "b"
  1527  	o1.CreateAt = createTime
  1528  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
  1529  
  1530  	o2 := &model.Post{}
  1531  	o2.ChannelId = o1.ChannelId
  1532  	o2.UserId = model.NewId()
  1533  	o2.Message = "zz" + model.NewId() + "b"
  1534  	o2.ParentId = o1.Id
  1535  	o2.RootId = o1.Id
  1536  	o2.CreateAt = createTime + 1
  1537  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
  1538  
  1539  	o3 := &model.Post{}
  1540  	o3.ChannelId = model.NewId()
  1541  	o3.UserId = model.NewId()
  1542  	o3.Message = "zz" + model.NewId() + "b"
  1543  	o3.CreateAt = createTime
  1544  	o3 = (<-ss.Post().Save(o3)).Data.(*model.Post)
  1545  
  1546  	r1 := (<-ss.Post().GetPostsCreatedAt(o1.ChannelId, createTime)).Data.([]*model.Post)
  1547  	assert.Equal(t, 2, len(r1))
  1548  }
  1549  
  1550  func testPostStoreOverwrite(t *testing.T, ss store.Store) {
  1551  	o1 := &model.Post{}
  1552  	o1.ChannelId = model.NewId()
  1553  	o1.UserId = model.NewId()
  1554  	o1.Message = "zz" + model.NewId() + "AAAAAAAAAAA"
  1555  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
  1556  
  1557  	o2 := &model.Post{}
  1558  	o2.ChannelId = o1.ChannelId
  1559  	o2.UserId = model.NewId()
  1560  	o2.Message = "zz" + model.NewId() + "CCCCCCCCC"
  1561  	o2.ParentId = o1.Id
  1562  	o2.RootId = o1.Id
  1563  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
  1564  
  1565  	o3 := &model.Post{}
  1566  	o3.ChannelId = o1.ChannelId
  1567  	o3.UserId = model.NewId()
  1568  	o3.Message = "zz" + model.NewId() + "QQQQQQQQQQ"
  1569  	o3 = (<-ss.Post().Save(o3)).Data.(*model.Post)
  1570  
  1571  	ro1 := (<-ss.Post().Get(o1.Id)).Data.(*model.PostList).Posts[o1.Id]
  1572  	ro2 := (<-ss.Post().Get(o1.Id)).Data.(*model.PostList).Posts[o2.Id]
  1573  	ro3 := (<-ss.Post().Get(o3.Id)).Data.(*model.PostList).Posts[o3.Id]
  1574  
  1575  	if ro1.Message != o1.Message {
  1576  		t.Fatal("Failed to save/get")
  1577  	}
  1578  
  1579  	o1a := &model.Post{}
  1580  	*o1a = *ro1
  1581  	o1a.Message = ro1.Message + "BBBBBBBBBB"
  1582  	if result := <-ss.Post().Overwrite(o1a); result.Err != nil {
  1583  		t.Fatal(result.Err)
  1584  	}
  1585  
  1586  	ro1a := (<-ss.Post().Get(o1.Id)).Data.(*model.PostList).Posts[o1.Id]
  1587  
  1588  	if ro1a.Message != o1a.Message {
  1589  		t.Fatal("Failed to overwrite/get")
  1590  	}
  1591  
  1592  	o2a := &model.Post{}
  1593  	*o2a = *ro2
  1594  	o2a.Message = ro2.Message + "DDDDDDD"
  1595  	if result := <-ss.Post().Overwrite(o2a); result.Err != nil {
  1596  		t.Fatal(result.Err)
  1597  	}
  1598  
  1599  	ro2a := (<-ss.Post().Get(o1.Id)).Data.(*model.PostList).Posts[o2.Id]
  1600  
  1601  	if ro2a.Message != o2a.Message {
  1602  		t.Fatal("Failed to overwrite/get")
  1603  	}
  1604  
  1605  	o3a := &model.Post{}
  1606  	*o3a = *ro3
  1607  	o3a.Message = ro3.Message + "WWWWWWW"
  1608  	if result := <-ss.Post().Overwrite(o3a); result.Err != nil {
  1609  		t.Fatal(result.Err)
  1610  	}
  1611  
  1612  	ro3a := (<-ss.Post().Get(o3.Id)).Data.(*model.PostList).Posts[o3.Id]
  1613  
  1614  	if ro3a.Message != o3a.Message && ro3a.Hashtags != o3a.Hashtags {
  1615  		t.Fatal("Failed to overwrite/get")
  1616  	}
  1617  
  1618  	o4 := store.Must(ss.Post().Save(&model.Post{
  1619  		ChannelId: model.NewId(),
  1620  		UserId:    model.NewId(),
  1621  		Message:   model.NewId(),
  1622  		Filenames: []string{"test"},
  1623  	})).(*model.Post)
  1624  
  1625  	ro4 := (<-ss.Post().Get(o4.Id)).Data.(*model.PostList).Posts[o4.Id]
  1626  
  1627  	o4a := &model.Post{}
  1628  	*o4a = *ro4
  1629  	o4a.Filenames = []string{}
  1630  	o4a.FileIds = []string{model.NewId()}
  1631  	if result := <-ss.Post().Overwrite(o4a); result.Err != nil {
  1632  		t.Fatal(result.Err)
  1633  	}
  1634  
  1635  	if ro4a := store.Must(ss.Post().Get(o4.Id)).(*model.PostList).Posts[o4.Id]; len(ro4a.Filenames) != 0 {
  1636  		t.Fatal("Failed to clear Filenames")
  1637  	} else if len(ro4a.FileIds) != 1 {
  1638  		t.Fatal("Failed to set FileIds")
  1639  	}
  1640  }
  1641  
  1642  func testPostStoreGetPostsByIds(t *testing.T, ss store.Store) {
  1643  	o1 := &model.Post{}
  1644  	o1.ChannelId = model.NewId()
  1645  	o1.UserId = model.NewId()
  1646  	o1.Message = "zz" + model.NewId() + "AAAAAAAAAAA"
  1647  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
  1648  
  1649  	o2 := &model.Post{}
  1650  	o2.ChannelId = o1.ChannelId
  1651  	o2.UserId = model.NewId()
  1652  	o2.Message = "zz" + model.NewId() + "CCCCCCCCC"
  1653  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
  1654  
  1655  	o3 := &model.Post{}
  1656  	o3.ChannelId = o1.ChannelId
  1657  	o3.UserId = model.NewId()
  1658  	o3.Message = "zz" + model.NewId() + "QQQQQQQQQQ"
  1659  	o3 = (<-ss.Post().Save(o3)).Data.(*model.Post)
  1660  
  1661  	ro1 := (<-ss.Post().Get(o1.Id)).Data.(*model.PostList).Posts[o1.Id]
  1662  	ro2 := (<-ss.Post().Get(o2.Id)).Data.(*model.PostList).Posts[o2.Id]
  1663  	ro3 := (<-ss.Post().Get(o3.Id)).Data.(*model.PostList).Posts[o3.Id]
  1664  
  1665  	postIds := []string{
  1666  		ro1.Id,
  1667  		ro2.Id,
  1668  		ro3.Id,
  1669  	}
  1670  
  1671  	if ro4 := store.Must(ss.Post().GetPostsByIds(postIds)).([]*model.Post); len(ro4) != 3 {
  1672  		t.Fatalf("Expected 3 posts in results. Got %v", len(ro4))
  1673  	}
  1674  
  1675  	store.Must(ss.Post().Delete(ro1.Id, model.GetMillis(), ""))
  1676  
  1677  	if ro5 := store.Must(ss.Post().GetPostsByIds(postIds)).([]*model.Post); len(ro5) != 3 {
  1678  		t.Fatalf("Expected 3 posts in results. Got %v", len(ro5))
  1679  	}
  1680  }
  1681  
  1682  func testPostStoreGetPostsBatchForIndexing(t *testing.T, ss store.Store) {
  1683  	c1 := &model.Channel{}
  1684  	c1.TeamId = model.NewId()
  1685  	c1.DisplayName = "Channel1"
  1686  	c1.Name = "zz" + model.NewId() + "b"
  1687  	c1.Type = model.CHANNEL_OPEN
  1688  	c1 = (<-ss.Channel().Save(c1, -1)).Data.(*model.Channel)
  1689  
  1690  	c2 := &model.Channel{}
  1691  	c2.TeamId = model.NewId()
  1692  	c2.DisplayName = "Channel2"
  1693  	c2.Name = "zz" + model.NewId() + "b"
  1694  	c2.Type = model.CHANNEL_OPEN
  1695  	c2 = (<-ss.Channel().Save(c2, -1)).Data.(*model.Channel)
  1696  
  1697  	o1 := &model.Post{}
  1698  	o1.ChannelId = c1.Id
  1699  	o1.UserId = model.NewId()
  1700  	o1.Message = "zz" + model.NewId() + "AAAAAAAAAAA"
  1701  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
  1702  
  1703  	o2 := &model.Post{}
  1704  	o2.ChannelId = c2.Id
  1705  	o2.UserId = model.NewId()
  1706  	o2.Message = "zz" + model.NewId() + "CCCCCCCCC"
  1707  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
  1708  
  1709  	o3 := &model.Post{}
  1710  	o3.ChannelId = c1.Id
  1711  	o3.UserId = model.NewId()
  1712  	o3.ParentId = o1.Id
  1713  	o3.RootId = o1.Id
  1714  	o3.Message = "zz" + model.NewId() + "QQQQQQQQQQ"
  1715  	o3 = (<-ss.Post().Save(o3)).Data.(*model.Post)
  1716  
  1717  	if r := store.Must(ss.Post().GetPostsBatchForIndexing(o1.CreateAt, model.GetMillis()+100000, 100)).([]*model.PostForIndexing); len(r) != 3 {
  1718  		t.Fatalf("Expected 3 posts in results. Got %v", len(r))
  1719  	} else {
  1720  		for _, p := range r {
  1721  			if p.Id == o1.Id {
  1722  				if p.TeamId != c1.TeamId {
  1723  					t.Fatalf("Unexpected team ID")
  1724  				}
  1725  				if p.ParentCreateAt != nil {
  1726  					t.Fatalf("Unexpected parent create at")
  1727  				}
  1728  			} else if p.Id == o2.Id {
  1729  				if p.TeamId != c2.TeamId {
  1730  					t.Fatalf("Unexpected team ID")
  1731  				}
  1732  				if p.ParentCreateAt != nil {
  1733  					t.Fatalf("Unexpected parent create at")
  1734  				}
  1735  			} else if p.Id == o3.Id {
  1736  				if p.TeamId != c1.TeamId {
  1737  					t.Fatalf("Unexpected team ID")
  1738  				}
  1739  				if *p.ParentCreateAt != o1.CreateAt {
  1740  					t.Fatalf("Unexpected parent create at")
  1741  				}
  1742  			} else {
  1743  				t.Fatalf("unexpected post returned")
  1744  			}
  1745  		}
  1746  	}
  1747  }
  1748  
  1749  func testPostStorePermanentDeleteBatch(t *testing.T, ss store.Store) {
  1750  	o1 := &model.Post{}
  1751  	o1.ChannelId = model.NewId()
  1752  	o1.UserId = model.NewId()
  1753  	o1.Message = "zz" + model.NewId() + "AAAAAAAAAAA"
  1754  	o1.CreateAt = 1000
  1755  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
  1756  
  1757  	o2 := &model.Post{}
  1758  	o2.ChannelId = model.NewId()
  1759  	o2.UserId = model.NewId()
  1760  	o2.Message = "zz" + model.NewId() + "AAAAAAAAAAA"
  1761  	o2.CreateAt = 1000
  1762  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
  1763  
  1764  	o3 := &model.Post{}
  1765  	o3.ChannelId = model.NewId()
  1766  	o3.UserId = model.NewId()
  1767  	o3.Message = "zz" + model.NewId() + "AAAAAAAAAAA"
  1768  	o3.CreateAt = 100000
  1769  	o3 = (<-ss.Post().Save(o3)).Data.(*model.Post)
  1770  
  1771  	store.Must(ss.Post().PermanentDeleteBatch(2000, 1000))
  1772  
  1773  	if p := <-ss.Post().Get(o1.Id); p.Err == nil {
  1774  		t.Fatalf("Should have not found post 1 after purge")
  1775  	}
  1776  
  1777  	if p := <-ss.Post().Get(o2.Id); p.Err == nil {
  1778  		t.Fatalf("Should have not found post 2 after purge")
  1779  	}
  1780  
  1781  	if p := <-ss.Post().Get(o3.Id); p.Err != nil {
  1782  		t.Fatalf("Should have found post 3 after purge")
  1783  	}
  1784  }
  1785  
  1786  func testPostStoreGetOldest(t *testing.T, ss store.Store) {
  1787  	o0 := &model.Post{}
  1788  	o0.ChannelId = model.NewId()
  1789  	o0.UserId = model.NewId()
  1790  	o0.Message = "zz" + model.NewId() + "b"
  1791  	o0.CreateAt = 3
  1792  	o0 = (<-ss.Post().Save(o0)).Data.(*model.Post)
  1793  
  1794  	o1 := &model.Post{}
  1795  	o1.ChannelId = o0.Id
  1796  	o1.UserId = model.NewId()
  1797  	o1.Message = "zz" + model.NewId() + "b"
  1798  	o1.CreateAt = 2
  1799  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
  1800  
  1801  	o2 := &model.Post{}
  1802  	o2.ChannelId = o1.ChannelId
  1803  	o2.UserId = model.NewId()
  1804  	o2.Message = "zz" + model.NewId() + "b"
  1805  	o2.CreateAt = 1
  1806  	o2 = (<-ss.Post().Save(o2)).Data.(*model.Post)
  1807  
  1808  	r1 := (<-ss.Post().GetOldest()).Data.(*model.Post)
  1809  
  1810  	assert.EqualValues(t, o2.Id, r1.Id)
  1811  }
  1812  
  1813  func testGetMaxPostSize(t *testing.T, ss store.Store) {
  1814  	assert.Equal(t, model.POST_MESSAGE_MAX_RUNES_V2, (<-ss.Post().GetMaxPostSize()).Data.(int))
  1815  	assert.Equal(t, model.POST_MESSAGE_MAX_RUNES_V2, (<-ss.Post().GetMaxPostSize()).Data.(int))
  1816  }