github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+incompatible/store/storetest/file_info_store.go (about)

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package storetest
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  
    10  	"github.com/mattermost/mattermost-server/model"
    11  	"github.com/mattermost/mattermost-server/store"
    12  )
    13  
    14  func TestFileInfoStore(t *testing.T, ss store.Store) {
    15  	t.Run("FileInfoSaveGet", func(t *testing.T) { testFileInfoSaveGet(t, ss) })
    16  	t.Run("FileInfoSaveGetByPath", func(t *testing.T) { testFileInfoSaveGetByPath(t, ss) })
    17  	t.Run("FileInfoGetForPost", func(t *testing.T) { testFileInfoGetForPost(t, ss) })
    18  	t.Run("FileInfoAttachToPost", func(t *testing.T) { testFileInfoAttachToPost(t, ss) })
    19  	t.Run("FileInfoDeleteForPost", func(t *testing.T) { testFileInfoDeleteForPost(t, ss) })
    20  	t.Run("FileInfoPermanentDelete", func(t *testing.T) { testFileInfoPermanentDelete(t, ss) })
    21  	t.Run("FileInfoPermanentDeleteBatch", func(t *testing.T) { testFileInfoPermanentDeleteBatch(t, ss) })
    22  }
    23  
    24  func testFileInfoSaveGet(t *testing.T, ss store.Store) {
    25  	info := &model.FileInfo{
    26  		CreatorId: model.NewId(),
    27  		Path:      "file.txt",
    28  	}
    29  
    30  	if result := <-ss.FileInfo().Save(info); result.Err != nil {
    31  		t.Fatal(result.Err)
    32  	} else if returned := result.Data.(*model.FileInfo); len(returned.Id) == 0 {
    33  		t.Fatal("should've assigned an id to FileInfo")
    34  	} else {
    35  		info = returned
    36  	}
    37  	defer func() {
    38  		<-ss.FileInfo().PermanentDelete(info.Id)
    39  	}()
    40  
    41  	if result := <-ss.FileInfo().Get(info.Id); result.Err != nil {
    42  		t.Fatal(result.Err)
    43  	} else if returned := result.Data.(*model.FileInfo); returned.Id != info.Id {
    44  		t.Log(info)
    45  		t.Log(returned)
    46  		t.Fatal("should've returned correct FileInfo")
    47  	}
    48  
    49  	info2 := store.Must(ss.FileInfo().Save(&model.FileInfo{
    50  		CreatorId: model.NewId(),
    51  		Path:      "file.txt",
    52  		DeleteAt:  123,
    53  	})).(*model.FileInfo)
    54  
    55  	if result := <-ss.FileInfo().Get(info2.Id); result.Err == nil {
    56  		t.Fatal("shouldn't have gotten deleted file")
    57  	}
    58  	defer func() {
    59  		<-ss.FileInfo().PermanentDelete(info2.Id)
    60  	}()
    61  }
    62  
    63  func testFileInfoSaveGetByPath(t *testing.T, ss store.Store) {
    64  	info := &model.FileInfo{
    65  		CreatorId: model.NewId(),
    66  		Path:      fmt.Sprintf("%v/file.txt", model.NewId()),
    67  	}
    68  
    69  	if result := <-ss.FileInfo().Save(info); result.Err != nil {
    70  		t.Fatal(result.Err)
    71  	} else if returned := result.Data.(*model.FileInfo); len(returned.Id) == 0 {
    72  		t.Fatal("should've assigned an id to FileInfo")
    73  	} else {
    74  		info = returned
    75  	}
    76  	defer func() {
    77  		<-ss.FileInfo().PermanentDelete(info.Id)
    78  	}()
    79  
    80  	if result := <-ss.FileInfo().GetByPath(info.Path); result.Err != nil {
    81  		t.Fatal(result.Err)
    82  	} else if returned := result.Data.(*model.FileInfo); returned.Id != info.Id {
    83  		t.Log(info)
    84  		t.Log(returned)
    85  		t.Fatal("should've returned correct FileInfo")
    86  	}
    87  
    88  	info2 := store.Must(ss.FileInfo().Save(&model.FileInfo{
    89  		CreatorId: model.NewId(),
    90  		Path:      "file.txt",
    91  		DeleteAt:  123,
    92  	})).(*model.FileInfo)
    93  
    94  	if result := <-ss.FileInfo().GetByPath(info2.Id); result.Err == nil {
    95  		t.Fatal("shouldn't have gotten deleted file")
    96  	}
    97  	defer func() {
    98  		<-ss.FileInfo().PermanentDelete(info2.Id)
    99  	}()
   100  }
   101  
   102  func testFileInfoGetForPost(t *testing.T, ss store.Store) {
   103  	userId := model.NewId()
   104  	postId := model.NewId()
   105  
   106  	infos := []*model.FileInfo{
   107  		{
   108  			PostId:    postId,
   109  			CreatorId: userId,
   110  			Path:      "file.txt",
   111  		},
   112  		{
   113  			PostId:    postId,
   114  			CreatorId: userId,
   115  			Path:      "file.txt",
   116  		},
   117  		{
   118  			PostId:    postId,
   119  			CreatorId: userId,
   120  			Path:      "file.txt",
   121  			DeleteAt:  123,
   122  		},
   123  		{
   124  			PostId:    model.NewId(),
   125  			CreatorId: userId,
   126  			Path:      "file.txt",
   127  		},
   128  	}
   129  
   130  	for i, info := range infos {
   131  		infos[i] = store.Must(ss.FileInfo().Save(info)).(*model.FileInfo)
   132  		defer func(id string) {
   133  			<-ss.FileInfo().PermanentDelete(id)
   134  		}(infos[i].Id)
   135  	}
   136  
   137  	if result := <-ss.FileInfo().GetForPost(postId, true, false); result.Err != nil {
   138  		t.Fatal(result.Err)
   139  	} else if returned := result.Data.([]*model.FileInfo); len(returned) != 2 {
   140  		t.Fatal("should've returned exactly 2 file infos")
   141  	}
   142  
   143  	if result := <-ss.FileInfo().GetForPost(postId, false, false); result.Err != nil {
   144  		t.Fatal(result.Err)
   145  	} else if returned := result.Data.([]*model.FileInfo); len(returned) != 2 {
   146  		t.Fatal("should've returned exactly 2 file infos")
   147  	}
   148  
   149  	if result := <-ss.FileInfo().GetForPost(postId, true, true); result.Err != nil {
   150  		t.Fatal(result.Err)
   151  	} else if returned := result.Data.([]*model.FileInfo); len(returned) != 2 {
   152  		t.Fatal("should've returned exactly 2 file infos")
   153  	}
   154  }
   155  
   156  func testFileInfoAttachToPost(t *testing.T, ss store.Store) {
   157  	userId := model.NewId()
   158  	postId := model.NewId()
   159  
   160  	info1 := store.Must(ss.FileInfo().Save(&model.FileInfo{
   161  		CreatorId: userId,
   162  		Path:      "file.txt",
   163  	})).(*model.FileInfo)
   164  	defer func() {
   165  		<-ss.FileInfo().PermanentDelete(info1.Id)
   166  	}()
   167  
   168  	if len(info1.PostId) != 0 {
   169  		t.Fatal("file shouldn't have a PostId")
   170  	}
   171  
   172  	if result := <-ss.FileInfo().AttachToPost(info1.Id, postId); result.Err != nil {
   173  		t.Fatal(result.Err)
   174  	} else {
   175  		info1 = store.Must(ss.FileInfo().Get(info1.Id)).(*model.FileInfo)
   176  	}
   177  
   178  	if len(info1.PostId) == 0 {
   179  		t.Fatal("file should now have a PostId")
   180  	}
   181  
   182  	info2 := store.Must(ss.FileInfo().Save(&model.FileInfo{
   183  		CreatorId: userId,
   184  		Path:      "file.txt",
   185  	})).(*model.FileInfo)
   186  	defer func() {
   187  		<-ss.FileInfo().PermanentDelete(info2.Id)
   188  	}()
   189  
   190  	if result := <-ss.FileInfo().AttachToPost(info2.Id, postId); result.Err != nil {
   191  		t.Fatal(result.Err)
   192  	} else {
   193  		info2 = store.Must(ss.FileInfo().Get(info2.Id)).(*model.FileInfo)
   194  	}
   195  
   196  	if result := <-ss.FileInfo().GetForPost(postId, true, false); result.Err != nil {
   197  		t.Fatal(result.Err)
   198  	} else if infos := result.Data.([]*model.FileInfo); len(infos) != 2 {
   199  		t.Fatal("should've returned exactly 2 file infos")
   200  	}
   201  }
   202  
   203  func testFileInfoDeleteForPost(t *testing.T, ss store.Store) {
   204  	userId := model.NewId()
   205  	postId := model.NewId()
   206  
   207  	infos := []*model.FileInfo{
   208  		{
   209  			PostId:    postId,
   210  			CreatorId: userId,
   211  			Path:      "file.txt",
   212  		},
   213  		{
   214  			PostId:    postId,
   215  			CreatorId: userId,
   216  			Path:      "file.txt",
   217  		},
   218  		{
   219  			PostId:    postId,
   220  			CreatorId: userId,
   221  			Path:      "file.txt",
   222  			DeleteAt:  123,
   223  		},
   224  		{
   225  			PostId:    model.NewId(),
   226  			CreatorId: userId,
   227  			Path:      "file.txt",
   228  		},
   229  	}
   230  
   231  	for i, info := range infos {
   232  		infos[i] = store.Must(ss.FileInfo().Save(info)).(*model.FileInfo)
   233  		defer func(id string) {
   234  			<-ss.FileInfo().PermanentDelete(id)
   235  		}(infos[i].Id)
   236  	}
   237  
   238  	if result := <-ss.FileInfo().DeleteForPost(postId); result.Err != nil {
   239  		t.Fatal(result.Err)
   240  	}
   241  
   242  	if infos := store.Must(ss.FileInfo().GetForPost(postId, true, false)).([]*model.FileInfo); len(infos) != 0 {
   243  		t.Fatal("shouldn't have returned any file infos")
   244  	}
   245  }
   246  
   247  func testFileInfoPermanentDelete(t *testing.T, ss store.Store) {
   248  	info := store.Must(ss.FileInfo().Save(&model.FileInfo{
   249  		PostId:    model.NewId(),
   250  		CreatorId: model.NewId(),
   251  		Path:      "file.txt",
   252  	})).(*model.FileInfo)
   253  
   254  	if result := <-ss.FileInfo().PermanentDelete(info.Id); result.Err != nil {
   255  		t.Fatal(result.Err)
   256  	}
   257  }
   258  
   259  func testFileInfoPermanentDeleteBatch(t *testing.T, ss store.Store) {
   260  	postId := model.NewId()
   261  
   262  	store.Must(ss.FileInfo().Save(&model.FileInfo{
   263  		PostId:    postId,
   264  		CreatorId: model.NewId(),
   265  		Path:      "file.txt",
   266  		CreateAt:  1000,
   267  	}))
   268  
   269  	store.Must(ss.FileInfo().Save(&model.FileInfo{
   270  		PostId:    postId,
   271  		CreatorId: model.NewId(),
   272  		Path:      "file.txt",
   273  		CreateAt:  1200,
   274  	}))
   275  
   276  	store.Must(ss.FileInfo().Save(&model.FileInfo{
   277  		PostId:    postId,
   278  		CreatorId: model.NewId(),
   279  		Path:      "file.txt",
   280  		CreateAt:  2000,
   281  	}))
   282  
   283  	if result := <-ss.FileInfo().GetForPost(postId, true, false); result.Err != nil {
   284  		t.Fatal(result.Err)
   285  	} else if len(result.Data.([]*model.FileInfo)) != 3 {
   286  		t.Fatal("Expected 3 fileInfos")
   287  	}
   288  
   289  	store.Must(ss.FileInfo().PermanentDeleteBatch(1500, 1000))
   290  
   291  	if result := <-ss.FileInfo().GetForPost(postId, true, false); result.Err != nil {
   292  		t.Fatal(result.Err)
   293  	} else if len(result.Data.([]*model.FileInfo)) != 1 {
   294  		t.Fatal("Expected 3 fileInfos")
   295  	}
   296  }