Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using Luticate2.Utils.DataAccess;
  4. using Luticate2.Utils.Dbo.FsFiles;
  5. using Luticate2.Utils.Dbo.OrderBy;
  6. using Luticate2.Utils.Dbo.PaginatedRequest;
  7. using Luticate2.Utils.Dbo.Result;
  8. using Luticate2.Utils.Utils;
  9. using Xunit;
  10. namespace TestUtils.EfCrudDataAccess
  11. {
  12. public class LuFsFilesCrudDataAccessTest
  13. {
  14. public static Stream GenerateStreamFromString(string s)
  15. {
  16. if (s == null)
  17. {
  18. return null;
  19. }
  20. var stream = new MemoryStream();
  21. var writer = new StreamWriter(stream);
  22. writer.Write(s);
  23. writer.Flush();
  24. stream.Position = 0;
  25. return stream;
  26. }
  27. public static bool CheckFileContent(LuFsFilesCrudDataAccess access, string path, string content)
  28. {
  29. return File.ReadAllText(access.GetFullPath(path)) == content;
  30. }
  31. public static LuFsFilesDbo TestAdd(LuFsFilesCrudDataAccess access, string path, string content)
  32. {
  33. var res = access.AddDbo(new LuFsFilesAddDbo
  34. {
  35. File = GenerateStreamFromString(content),
  36. Path = path
  37. });
  38. Assert.Equal(LuStatus.Success, res.Status);
  39. Assert.Equal(path, res.Data.Path);
  40. Assert.Equal(content.Length, res.Data.Size);
  41. Assert.True(CheckFileContent(access, res.Data.Path, content));
  42. return res.Data;
  43. }
  44. public static LuFsFilesDbo TestEdit(LuFsFilesCrudDataAccess access, string path, string content, string pathEdit, string contentEdit)
  45. {
  46. TestAdd(access, path, content);
  47. TestGetSingleById(access, path, content);
  48. var res = access.EditSingleByIdDbo(path, new LuFsFilesAddDbo
  49. {
  50. File = GenerateStreamFromString(contentEdit),
  51. Path = pathEdit
  52. });
  53. Assert.Equal(LuStatus.Success, res.Status);
  54. Assert.Equal(pathEdit ?? path, res.Data.Path);
  55. Assert.Equal(contentEdit?.Length ?? content.Length, res.Data.Size);
  56. Assert.True(CheckFileContent(access, res.Data.Path, contentEdit ?? content));
  57. return res.Data;
  58. }
  59. public static LuFsFilesDbo TestGetSingleById(LuFsFilesCrudDataAccess access, string path, string content)
  60. {
  61. var res = access.GetSingleById(path);
  62. Assert.Equal(LuStatus.Success, res.Status);
  63. Assert.Equal(path, res.Data.Path);
  64. Assert.Equal(content.Length, res.Data.Size);
  65. Assert.True(CheckFileContent(access, res.Data.Path, content));
  66. return res.Data;
  67. }
  68. public static LuFsFilesDbo TestDeleteSingleById(LuFsFilesCrudDataAccess access, string path)
  69. {
  70. var del = access.DeleteSingleByIdDbo(path);
  71. Assert.Equal(LuStatus.Success, del.Status);
  72. Assert.Equal(path, del.Data.Path);
  73. var get = access.GetSingleById(path);
  74. Assert.Equal(LuStatus.NotFound, get.Status);
  75. return del.Data;
  76. }
  77. public static void TestGetMultiple(LuFsFilesCrudDataAccess access, LuPaginatedRequestDbo request,
  78. List<string> paths, List<string> contents,
  79. List<int> gets, int count, bool add = true)
  80. {
  81. if (add)
  82. {
  83. for (var i = 0; i < paths.Count; ++i)
  84. {
  85. TestAdd(access, paths[i], contents[i]);
  86. TestGetSingleById(access, paths[i], contents[i]);
  87. }
  88. }
  89. var get = access.GetMultiple(request);
  90. Assert.Equal(LuStatus.Success, get.Status);
  91. Assert.Equal(count, get.Data.Count);
  92. Assert.Equal(gets.Count, get.Data.Data.Count);
  93. for (var i = 0; i < gets.Count; ++i)
  94. {
  95. Assert.Equal(paths[gets[i]], get.Data.Data[i].Path);
  96. Assert.Equal(contents[gets[i]].Length, get.Data.Data[i].Size);
  97. Assert.True(CheckFileContent(access, paths[gets[i]], contents[gets[i]]));
  98. }
  99. }
  100. [Fact]
  101. public void TestAdd1()
  102. {
  103. Tests.TestRealFs(access =>
  104. {
  105. var path = "42.txt";
  106. var content = "42";
  107. TestAdd(access, path, content);
  108. TestGetSingleById(access, path, content);
  109. });
  110. }
  111. [Fact]
  112. public void TestAdd2()
  113. {
  114. Tests.TestRealFs(access =>
  115. {
  116. var path = "42.txt";
  117. var content = "42";
  118. TestAdd(access, path, content);
  119. TestGetSingleById(access, path, content);
  120. var res = access.AddDbo(new LuFsFilesAddDbo
  121. {
  122. File = GenerateStreamFromString(content),
  123. Path = path
  124. });
  125. Assert.Equal(LuStatus.InputError, res.Status);
  126. });
  127. }
  128. [Fact]
  129. public void TestGetMutliple1()
  130. {
  131. Tests.TestRealFs(access =>
  132. {
  133. var paths = new List<string> {"42.txt", "24.txt", "test"};
  134. var contents = new List<string> {"txt.42", "txt.24", "test.test"};
  135. var gets = new List<int>{1, 0, 2};
  136. TestGetMultiple(access, new LuPaginatedRequestDbo
  137. {
  138. Filter = null,
  139. OrderBy = new LuOrderByDbo
  140. {
  141. Fields = new List<LuOrderByFieldDbo>
  142. {
  143. new LuOrderByFieldDbo
  144. {
  145. Asc = true,
  146. Name = "path"
  147. }
  148. }
  149. },
  150. Page = 0,
  151. PerPage = int.MaxValue
  152. }, paths, contents, gets, gets.Count);
  153. });
  154. }
  155. [Fact]
  156. public void TestGetMutliple2()
  157. {
  158. Tests.TestRealFs(access =>
  159. {
  160. var paths = new List<string> {"42.txt", "24.txt", "test"};
  161. var contents = new List<string> {"txt.42", "txt.24", "test.test"};
  162. var gets = new List<int>{1, 0};
  163. TestGetMultiple(access, new LuPaginatedRequestDbo
  164. {
  165. Filter = null,
  166. OrderBy = new LuOrderByDbo
  167. {
  168. Fields = new List<LuOrderByFieldDbo>
  169. {
  170. new LuOrderByFieldDbo
  171. {
  172. Asc = true,
  173. Name = "path"
  174. }
  175. }
  176. },
  177. Page = 0,
  178. PerPage = 2
  179. }, paths, contents, gets, paths.Count);
  180. });
  181. }
  182. [Fact]
  183. public void TestGetMutliple3()
  184. {
  185. Tests.TestRealFs(access =>
  186. {
  187. var paths = new List<string> {"42.txt", "24.txt", "test"};
  188. var contents = new List<string> {"txt.42", "txt.24", "test.test"};
  189. var gets = new List<int>{2, 0, 1};
  190. TestGetMultiple(access, new LuPaginatedRequestDbo
  191. {
  192. Filter = null,
  193. OrderBy = new LuOrderByDbo
  194. {
  195. Fields = new List<LuOrderByFieldDbo>
  196. {
  197. new LuOrderByFieldDbo
  198. {
  199. Asc = false,
  200. Name = "path"
  201. }
  202. }
  203. },
  204. Page = 0,
  205. PerPage = int.MaxValue
  206. }, paths, contents, gets, gets.Count);
  207. });
  208. }
  209. [Fact]
  210. public void TestDelete1()
  211. {
  212. Tests.TestRealFs(access =>
  213. {
  214. var path = "42.txt";
  215. var content = "42";
  216. TestAdd(access, path, content);
  217. TestGetSingleById(access, path, content);
  218. TestDeleteSingleById(access, path);
  219. });
  220. }
  221. [Fact]
  222. public void TestDelete2()
  223. {
  224. Tests.TestRealFs(access =>
  225. {
  226. var path = "42.txt";
  227. var content = "42";
  228. TestAdd(access, path, content);
  229. TestGetSingleById(access, path, content);
  230. TestDeleteSingleById(access, path);
  231. var del = access.DeleteSingleByIdDbo(path);
  232. Assert.Equal(LuStatus.NotFound, del.Status);
  233. });
  234. }
  235. [Fact]
  236. public void TestDelete3()
  237. {
  238. Tests.TestRealFs(access =>
  239. {
  240. var paths = new List<string> {"42.txt", "24.txt", "test"};
  241. var contents = new List<string> {"txt.42", "txt.24", "test.test"};
  242. var gets = new List<int>{2, 0, 1};
  243. TestGetMultiple(access, new LuPaginatedRequestDbo
  244. {
  245. Filter = null,
  246. OrderBy = new LuOrderByDbo
  247. {
  248. Fields = new List<LuOrderByFieldDbo>
  249. {
  250. new LuOrderByFieldDbo
  251. {
  252. Asc = false,
  253. Name = "path"
  254. }
  255. }
  256. },
  257. Page = 0,
  258. PerPage = int.MaxValue
  259. }, paths, contents, gets, gets.Count);
  260. var path = paths[0];
  261. TestDeleteSingleById(access, path);
  262. var del = access.DeleteSingleByIdDbo(path);
  263. Assert.Equal(LuStatus.NotFound, del.Status);
  264. gets = new List<int>{2, 1};
  265. TestGetMultiple(access, new LuPaginatedRequestDbo
  266. {
  267. Filter = null,
  268. OrderBy = new LuOrderByDbo
  269. {
  270. Fields = new List<LuOrderByFieldDbo>
  271. {
  272. new LuOrderByFieldDbo
  273. {
  274. Asc = false,
  275. Name = "path"
  276. }
  277. }
  278. },
  279. Page = 0,
  280. PerPage = int.MaxValue
  281. }, paths, contents, gets, gets.Count, false);
  282. });
  283. }
  284. [Fact]
  285. public void TestEdit1()
  286. {
  287. Tests.TestRealFs(access =>
  288. {
  289. var path = "42.txt";
  290. var content = "42";
  291. var pathEdit = "4224.txt";
  292. var contentEdit = "4224";
  293. TestEdit(access, path, content, pathEdit, contentEdit);
  294. TestGetSingleById(access, pathEdit, contentEdit);
  295. });
  296. }
  297. [Fact]
  298. public void TestEdit2()
  299. {
  300. Tests.TestRealFs(access =>
  301. {
  302. var path = "42.txt";
  303. var content = "42";
  304. var contentEdit = "4224";
  305. TestEdit(access, path, content, null, contentEdit);
  306. TestGetSingleById(access, path, contentEdit);
  307. });
  308. }
  309. [Fact]
  310. public void TestEdit3()
  311. {
  312. Tests.TestRealFs(access =>
  313. {
  314. var path = "42.txt";
  315. var content = "42";
  316. var pathEdit = "4224.txt";
  317. TestEdit(access, path, content, pathEdit, null);
  318. TestGetSingleById(access, pathEdit, content);
  319. });
  320. }
  321. [Fact]
  322. public void TestEdit4()
  323. {
  324. Tests.TestRealFs(access =>
  325. {
  326. var path = "42.txt";
  327. var content = "42";
  328. TestEdit(access, path, content, null, null);
  329. TestGetSingleById(access, path, content);
  330. });
  331. }
  332. }
  333. }