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