123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- 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<string> paths, List<string> contents,
- List<int> 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<string> {"42.txt", "24.txt", "test"};
- var contents = new List<string> {"txt.42", "txt.24", "test.test"};
- var gets = new List<int>{1, 0, 2};
- TestGetMultiple(access, new LuPaginatedRequestDbo
- {
- Filter = null,
- OrderBy = new LuOrderByDbo
- {
- Fields = new List<LuOrderByFieldDbo>
- {
- 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<string> {"42.txt", "24.txt", "test"};
- var contents = new List<string> {"txt.42", "txt.24", "test.test"};
- var gets = new List<int>{1, 0};
- TestGetMultiple(access, new LuPaginatedRequestDbo
- {
- Filter = null,
- OrderBy = new LuOrderByDbo
- {
- Fields = new List<LuOrderByFieldDbo>
- {
- 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<string> {"42.txt", "24.txt", "test"};
- var contents = new List<string> {"txt.42", "txt.24", "test.test"};
- var gets = new List<int>{2, 0, 1};
- TestGetMultiple(access, new LuPaginatedRequestDbo
- {
- Filter = null,
- OrderBy = new LuOrderByDbo
- {
- Fields = new List<LuOrderByFieldDbo>
- {
- 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<string> {"42.txt", "24.txt", "test"};
- var contents = new List<string> {"txt.42", "txt.24", "test.test"};
- var gets = new List<int>{2, 0, 1};
- TestGetMultiple(access, new LuPaginatedRequestDbo
- {
- Filter = null,
- OrderBy = new LuOrderByDbo
- {
- Fields = new List<LuOrderByFieldDbo>
- {
- 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<int>{2, 1};
- TestGetMultiple(access, new LuPaginatedRequestDbo
- {
- Filter = null,
- OrderBy = new LuOrderByDbo
- {
- Fields = new List<LuOrderByFieldDbo>
- {
- 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);
- });
- }
- }
- }
|