using System.Collections.Generic; using System.IO; using Luticate2.Utils.DataAccess; using Luticate2.Utils.Dbo.FsFiles; using Luticate2.Utils.Dbo.OrderBy; using Luticate2.Utils.Dbo.PaginatedRequest; using Luticate2.Utils.Dbo.Result; using Luticate2.Utils.Utils; using Xunit; namespace TestUtils.EfCrudDataAccess { public class LuFsFilesCrudDataAccessTest { public static Stream GenerateStreamFromString(string s) { if (s == null) { return null; } var stream = new MemoryStream(); var writer = new StreamWriter(stream); writer.Write(s); writer.Flush(); stream.Position = 0; return stream; } public static bool CheckFileContent(LuFsFilesCrudDataAccess access, string path, string content) { return File.ReadAllText(access.GetFullPath(path)) == content; } public static LuFsFilesDbo TestAdd(LuFsFilesCrudDataAccess access, string path, string content) { var res = access.AddDbo(new LuFsFilesAddDbo { File = GenerateStreamFromString(content), Path = path }); Assert.Equal(LuStatus.Success, res.Status); Assert.Equal(path, res.Data.Path); Assert.Equal(content.Length, res.Data.Size); Assert.True(CheckFileContent(access, res.Data.Path, content)); return res.Data; } public static LuFsFilesDbo TestEdit(LuFsFilesCrudDataAccess access, string path, string content, string pathEdit, string contentEdit) { TestAdd(access, path, content); TestGetSingleById(access, path, content); var res = access.EditSingleByIdDbo(path, new LuFsFilesAddDbo { File = GenerateStreamFromString(contentEdit), Path = pathEdit }); Assert.Equal(LuStatus.Success, res.Status); Assert.Equal(pathEdit ?? path, res.Data.Path); Assert.Equal(contentEdit?.Length ?? content.Length, res.Data.Size); Assert.True(CheckFileContent(access, res.Data.Path, contentEdit ?? content)); return res.Data; } public static LuFsFilesDbo TestGetSingleById(LuFsFilesCrudDataAccess access, string path, string content) { var res = access.GetSingleById(path); Assert.Equal(LuStatus.Success, res.Status); Assert.Equal(path, res.Data.Path); Assert.Equal(content.Length, res.Data.Size); Assert.True(CheckFileContent(access, res.Data.Path, content)); return res.Data; } public static LuFsFilesDbo TestDeleteSingleById(LuFsFilesCrudDataAccess access, string path) { var del = access.DeleteSingleByIdDbo(path); Assert.Equal(LuStatus.Success, del.Status); Assert.Equal(path, del.Data.Path); var get = access.GetSingleById(path); Assert.Equal(LuStatus.NotFound, get.Status); return del.Data; } public static void TestGetMultiple(LuFsFilesCrudDataAccess access, LuPaginatedRequestDbo request, List paths, List contents, List gets, int count, bool add = true) { if (add) { for (var i = 0; i < paths.Count; ++i) { TestAdd(access, paths[i], contents[i]); TestGetSingleById(access, paths[i], contents[i]); } } var get = access.GetMultiple(request); Assert.Equal(LuStatus.Success, get.Status); Assert.Equal(count, get.Data.Count); Assert.Equal(gets.Count, get.Data.Data.Count); for (var i = 0; i < gets.Count; ++i) { Assert.Equal(paths[gets[i]], get.Data.Data[i].Path); Assert.Equal(contents[gets[i]].Length, get.Data.Data[i].Size); Assert.True(CheckFileContent(access, paths[gets[i]], contents[gets[i]])); } } [Fact] public void TestAdd1() { Tests.TestRealFs(access => { var path = "42.txt"; var content = "42"; TestAdd(access, path, content); TestGetSingleById(access, path, content); }); } [Fact] public void TestAdd2() { Tests.TestRealFs(access => { var path = "42.txt"; var content = "42"; TestAdd(access, path, content); TestGetSingleById(access, path, content); var res = access.AddDbo(new LuFsFilesAddDbo { File = GenerateStreamFromString(content), Path = path }); Assert.Equal(LuStatus.InputError, res.Status); }); } [Fact] public void TestGetMutliple1() { Tests.TestRealFs(access => { var paths = new List {"42.txt", "24.txt", "test"}; var contents = new List {"txt.42", "txt.24", "test.test"}; var gets = new List{1, 0, 2}; TestGetMultiple(access, new LuPaginatedRequestDbo { Filter = null, OrderBy = new LuOrderByDbo { Fields = new List { new LuOrderByFieldDbo { Asc = true, Name = "path" } } }, Page = 0, PerPage = int.MaxValue }, paths, contents, gets, gets.Count); }); } [Fact] public void TestGetMutliple2() { Tests.TestRealFs(access => { var paths = new List {"42.txt", "24.txt", "test"}; var contents = new List {"txt.42", "txt.24", "test.test"}; var gets = new List{1, 0}; TestGetMultiple(access, new LuPaginatedRequestDbo { Filter = null, OrderBy = new LuOrderByDbo { Fields = new List { new LuOrderByFieldDbo { Asc = true, Name = "path" } } }, Page = 0, PerPage = 2 }, paths, contents, gets, paths.Count); }); } [Fact] public void TestGetMutliple3() { Tests.TestRealFs(access => { var paths = new List {"42.txt", "24.txt", "test"}; var contents = new List {"txt.42", "txt.24", "test.test"}; var gets = new List{2, 0, 1}; TestGetMultiple(access, new LuPaginatedRequestDbo { Filter = null, OrderBy = new LuOrderByDbo { Fields = new List { new LuOrderByFieldDbo { Asc = false, Name = "path" } } }, Page = 0, PerPage = int.MaxValue }, paths, contents, gets, gets.Count); }); } [Fact] public void TestDelete1() { Tests.TestRealFs(access => { var path = "42.txt"; var content = "42"; TestAdd(access, path, content); TestGetSingleById(access, path, content); TestDeleteSingleById(access, path); }); } [Fact] public void TestDelete2() { Tests.TestRealFs(access => { var path = "42.txt"; var content = "42"; TestAdd(access, path, content); TestGetSingleById(access, path, content); TestDeleteSingleById(access, path); var del = access.DeleteSingleByIdDbo(path); Assert.Equal(LuStatus.NotFound, del.Status); }); } [Fact] public void TestDelete3() { Tests.TestRealFs(access => { var paths = new List {"42.txt", "24.txt", "test"}; var contents = new List {"txt.42", "txt.24", "test.test"}; var gets = new List{2, 0, 1}; TestGetMultiple(access, new LuPaginatedRequestDbo { Filter = null, OrderBy = new LuOrderByDbo { Fields = new List { new LuOrderByFieldDbo { Asc = false, Name = "path" } } }, Page = 0, PerPage = int.MaxValue }, paths, contents, gets, gets.Count); var path = paths[0]; TestDeleteSingleById(access, path); var del = access.DeleteSingleByIdDbo(path); Assert.Equal(LuStatus.NotFound, del.Status); gets = new List{2, 1}; TestGetMultiple(access, new LuPaginatedRequestDbo { Filter = null, OrderBy = new LuOrderByDbo { Fields = new List { new LuOrderByFieldDbo { Asc = false, Name = "path" } } }, Page = 0, PerPage = int.MaxValue }, paths, contents, gets, gets.Count, false); }); } [Fact] public void TestEdit1() { Tests.TestRealFs(access => { var path = "42.txt"; var content = "42"; var pathEdit = "4224.txt"; var contentEdit = "4224"; TestEdit(access, path, content, pathEdit, contentEdit); TestGetSingleById(access, pathEdit, contentEdit); }); } [Fact] public void TestEdit2() { Tests.TestRealFs(access => { var path = "42.txt"; var content = "42"; var contentEdit = "4224"; TestEdit(access, path, content, null, contentEdit); TestGetSingleById(access, path, contentEdit); }); } [Fact] public void TestEdit3() { Tests.TestRealFs(access => { var path = "42.txt"; var content = "42"; var pathEdit = "4224.txt"; TestEdit(access, path, content, pathEdit, null); TestGetSingleById(access, pathEdit, content); }); } [Fact] public void TestEdit4() { Tests.TestRealFs(access => { var path = "42.txt"; var content = "42"; TestEdit(access, path, content, null, null); TestGetSingleById(access, path, content); }); } } }