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

LuEfReadDataAccessTest.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Luticate2.Utils.Dbo;
  5. using Luticate2.Utils.Dbo.OrderBy;
  6. using Test.Utils.DataAccess;
  7. using Test.Utils.Dbo.PkBigSerial;
  8. using Test.Utils.Dbo.PkGuid;
  9. using Xunit;
  10. namespace Test.Utils.EfCrudDataAccess
  11. {
  12. public class LuEfReadDataAccessTest
  13. {
  14. [Fact]
  15. public void TestGetSingle1()
  16. {
  17. Tests.TestRealDb(context =>
  18. {
  19. var service = new LuUtilsPkBigSerialDataAccess(context);
  20. var res = service.AddDbo(new PkBigSerialAddDbo
  21. {
  22. SomeInt = 42,
  23. SomeText = "42"
  24. });
  25. Assert.Equal(LuStatus.Success, res.Status);
  26. Assert.NotEqual(0, res.Data.Id);
  27. var get = service.GetSingle(bigserial => bigserial.some_int == 42);
  28. Assert.Equal(LuStatus.Success, get.Status);
  29. Assert.Equal("42", get.Data.SomeText);
  30. Assert.Equal(42, get.Data.SomeInt);
  31. Assert.Equal(res.Data.Id, get.Data.Id);
  32. });
  33. }
  34. [Fact]
  35. public void TestGetSingle2()
  36. {
  37. Tests.TestRealDb(context =>
  38. {
  39. var service = new LuUtilsPkBigSerialDataAccess(context);
  40. var res = service.AddDbo(new PkBigSerialAddDbo
  41. {
  42. SomeInt = 42,
  43. SomeText = "42"
  44. });
  45. Assert.Equal(LuStatus.Success, res.Status);
  46. Assert.NotEqual(0, res.Data.Id);
  47. var get = service.GetSingle(bigserial => bigserial.some_int == 24);
  48. Assert.Equal(LuStatus.NotFound, get.Status);
  49. });
  50. }
  51. [Fact]
  52. public void TestGetSingleByKeys1()
  53. {
  54. Tests.TestRealDb(context =>
  55. {
  56. var service = new LuUtilsPkBigSerialDataAccess(context);
  57. var res = service.AddDbo(new PkBigSerialAddDbo
  58. {
  59. SomeInt = 42,
  60. SomeText = "42"
  61. });
  62. Assert.Equal(LuStatus.Success, res.Status);
  63. Assert.NotEqual(0, res.Data.Id);
  64. var get = service.GetSingleByKeys(new KeyValuePair<string, object>("some_text", "42"));
  65. Assert.Equal(LuStatus.Success, get.Status);
  66. Assert.Equal("42", get.Data.SomeText);
  67. Assert.Equal(42, get.Data.SomeInt);
  68. Assert.Equal(res.Data.Id, get.Data.Id);
  69. });
  70. }
  71. [Fact]
  72. public void TestGetSingleByKeys2()
  73. {
  74. Tests.TestRealDb(context =>
  75. {
  76. var service = new LuUtilsPkBigSerialDataAccess(context);
  77. var res = service.AddDbo(new PkBigSerialAddDbo
  78. {
  79. SomeInt = 42,
  80. SomeText = "42"
  81. });
  82. Assert.Equal(LuStatus.Success, res.Status);
  83. Assert.NotEqual(0, res.Data.Id);
  84. var get = service.GetSingleByKeys(new KeyValuePair<string, object>("some_text", "24"));
  85. Assert.Equal(LuStatus.NotFound, get.Status);
  86. });
  87. }
  88. [Fact]
  89. public void TestGetSingleById1()
  90. {
  91. Tests.TestRealDb(context =>
  92. {
  93. var service = new LuUtilsPkBigSerialDataAccess(context);
  94. var res = service.AddId(new PkBigSerialAddDbo
  95. {
  96. SomeInt = 42,
  97. SomeText = "42"
  98. });
  99. Assert.Equal(LuStatus.Success, res.Status);
  100. Assert.NotEqual(0, res.Data);
  101. var get = service.GetSingleById(res.Data);
  102. Assert.Equal(LuStatus.Success, get.Status);
  103. Assert.Equal("42", get.Data.SomeText);
  104. Assert.Equal(42, get.Data.SomeInt);
  105. Assert.Equal(res.Data, get.Data.Id);
  106. });
  107. }
  108. [Fact]
  109. public void TestGetSingleById2()
  110. {
  111. Tests.TestRealDb(context =>
  112. {
  113. var service = new LuUtilsPkBigSerialDataAccess(context);
  114. var res = service.AddId(new PkBigSerialAddDbo
  115. {
  116. SomeInt = 42,
  117. SomeText = "42"
  118. });
  119. Assert.Equal(LuStatus.Success, res.Status);
  120. Assert.NotEqual(0, res.Data);
  121. var get = service.GetSingle(bigserial => bigserial.some_int == 24);
  122. Assert.Equal(LuStatus.NotFound, get.Status);
  123. });
  124. }
  125. [Fact]
  126. public void TestGetSingleById3()
  127. {
  128. Tests.TestRealDb(context =>
  129. {
  130. var service = new LuUtilsPkGuidDataAccess(context);
  131. var res = service.AddGuid(new PkGuidAddDbo
  132. {
  133. SomeInt = 42,
  134. SomeText = "42"
  135. });
  136. Assert.Equal(LuStatus.Success, res.Status);
  137. Assert.NotEqual(new Guid().ToString(), res.Data);
  138. var get = service.GetSingleById(res.Data);
  139. Assert.Equal(LuStatus.Success, get.Status);
  140. Assert.Equal("42", get.Data.SomeText);
  141. Assert.Equal(42, get.Data.SomeInt);
  142. Assert.Equal(res.Data, get.Data.Id);
  143. });
  144. }
  145. [Fact]
  146. public void TestGetSingleById4()
  147. {
  148. Tests.TestRealDb(context =>
  149. {
  150. var service = new LuUtilsPkGuidDataAccess(context);
  151. var res = service.AddGuid(new PkGuidAddDbo
  152. {
  153. SomeInt = 42,
  154. SomeText = "42"
  155. });
  156. Assert.Equal(LuStatus.Success, res.Status);
  157. Assert.NotEqual(new Guid().ToString(), res.Data);
  158. var get = service.GetSingle(bigserial => bigserial.some_int == 24);
  159. Assert.Equal(LuStatus.NotFound, get.Status);
  160. });
  161. }
  162. [Fact]
  163. public void TestGetMultiple1()
  164. {
  165. Tests.TestRealDb(context =>
  166. {
  167. var dbos = new List<PkGuidAddDbo>
  168. {
  169. new PkGuidAddDbo
  170. {
  171. SomeInt = 42,
  172. SomeText = "442"
  173. },
  174. new PkGuidAddDbo
  175. {
  176. SomeInt = 42,
  177. SomeText = "42"
  178. },
  179. new PkGuidAddDbo
  180. {
  181. SomeInt = 142,
  182. SomeText = "24"
  183. }
  184. };
  185. var service = new LuUtilsPkGuidDataAccess(context);
  186. var res = service.AddGuid(dbos);
  187. Assert.Equal(LuStatus.Success, res.Status);
  188. var get = service.GetMultiple(table => table.OrderBy(guid => guid.some_int),
  189. guid => guid.some_int == 42, 0, int.MaxValue, set => set.ThenBy(guid => guid.some_text));
  190. Assert.Equal(LuStatus.Success, get.Status);
  191. Assert.Equal(2, get.Data.Count);
  192. Assert.Equal(2, get.Data.Data.Count);
  193. var dbo = get.Data.Data[0];
  194. Assert.Equal(42, dbo.SomeInt);
  195. Assert.Equal("42", dbo.SomeText);
  196. dbo = get.Data.Data[1];
  197. Assert.Equal(42, dbo.SomeInt);
  198. Assert.Equal("442", dbo.SomeText);
  199. });
  200. }
  201. [Fact]
  202. public void TestGetMultiple2()
  203. {
  204. Tests.TestRealDb(context =>
  205. {
  206. var dbos = new List<PkGuidAddDbo>
  207. {
  208. new PkGuidAddDbo
  209. {
  210. SomeInt = 42,
  211. SomeText = "442"
  212. },
  213. new PkGuidAddDbo
  214. {
  215. SomeInt = 42,
  216. SomeText = "42"
  217. },
  218. new PkGuidAddDbo
  219. {
  220. SomeInt = 142,
  221. SomeText = "24"
  222. }
  223. };
  224. var service = new LuUtilsPkGuidDataAccess(context);
  225. var res = service.AddGuid(dbos);
  226. Assert.Equal(LuStatus.Success, res.Status);
  227. var get = service.GetMultiple(guid => guid.some_int, guid => guid.some_int == 42, 0, int.MaxValue,
  228. guid => guid.some_text);
  229. Assert.Equal(LuStatus.Success, get.Status);
  230. Assert.Equal(2, get.Data.Count);
  231. Assert.Equal(2, get.Data.Data.Count);
  232. var dbo = get.Data.Data[0];
  233. Assert.Equal(42, dbo.SomeInt);
  234. Assert.Equal("42", dbo.SomeText);
  235. dbo = get.Data.Data[1];
  236. Assert.Equal(42, dbo.SomeInt);
  237. Assert.Equal("442", dbo.SomeText);
  238. });
  239. }
  240. [Fact]
  241. public void TestGetMultiple3()
  242. {
  243. Tests.TestRealDb(context =>
  244. {
  245. var dbos = new List<PkGuidAddDbo>
  246. {
  247. new PkGuidAddDbo
  248. {
  249. SomeInt = 42,
  250. SomeText = "442"
  251. },
  252. new PkGuidAddDbo
  253. {
  254. SomeInt = 42,
  255. SomeText = "42"
  256. },
  257. new PkGuidAddDbo
  258. {
  259. SomeInt = 142,
  260. SomeText = "24"
  261. }
  262. };
  263. var service = new LuUtilsPkGuidDataAccess(context);
  264. var res = service.AddGuid(dbos);
  265. Assert.Equal(LuStatus.Success, res.Status);
  266. var get = service.GetMultiple(table => table.OrderBy(guid => guid.some_int), 0, int.MaxValue,
  267. set => set.ThenBy(guid => guid.some_text));
  268. Assert.Equal(LuStatus.Success, get.Status);
  269. Assert.Equal(3, get.Data.Count);
  270. Assert.Equal(3, get.Data.Data.Count);
  271. var dbo = get.Data.Data[0];
  272. Assert.Equal(42, dbo.SomeInt);
  273. Assert.Equal("42", dbo.SomeText);
  274. dbo = get.Data.Data[1];
  275. Assert.Equal(42, dbo.SomeInt);
  276. Assert.Equal("442", dbo.SomeText);
  277. dbo = get.Data.Data[2];
  278. Assert.Equal(142, dbo.SomeInt);
  279. Assert.Equal("24", dbo.SomeText);
  280. });
  281. }
  282. [Fact]
  283. public void TestGetMultiple4()
  284. {
  285. Tests.TestRealDb(context =>
  286. {
  287. var dbos = new List<PkGuidAddDbo>
  288. {
  289. new PkGuidAddDbo
  290. {
  291. SomeInt = 42,
  292. SomeText = "442"
  293. },
  294. new PkGuidAddDbo
  295. {
  296. SomeInt = 42,
  297. SomeText = "42"
  298. },
  299. new PkGuidAddDbo
  300. {
  301. SomeInt = 142,
  302. SomeText = "24"
  303. }
  304. };
  305. var service = new LuUtilsPkGuidDataAccess(context);
  306. var res = service.AddGuid(dbos);
  307. Assert.Equal(LuStatus.Success, res.Status);
  308. var get = service.GetMultiple(guid => guid.some_int, 0, int.MaxValue, guid => guid.some_text);
  309. Assert.Equal(LuStatus.Success, get.Status);
  310. Assert.Equal(3, get.Data.Count);
  311. Assert.Equal(3, get.Data.Data.Count);
  312. var dbo = get.Data.Data[0];
  313. Assert.Equal(42, dbo.SomeInt);
  314. Assert.Equal("42", dbo.SomeText);
  315. dbo = get.Data.Data[1];
  316. Assert.Equal(42, dbo.SomeInt);
  317. Assert.Equal("442", dbo.SomeText);
  318. dbo = get.Data.Data[2];
  319. Assert.Equal(142, dbo.SomeInt);
  320. Assert.Equal("24", dbo.SomeText);
  321. });
  322. }
  323. [Fact]
  324. public void TestGetMultiple5()
  325. {
  326. Tests.TestRealDb(context =>
  327. {
  328. var dbos = new List<PkGuidAddDbo>
  329. {
  330. new PkGuidAddDbo
  331. {
  332. SomeInt = 42,
  333. SomeText = "442"
  334. },
  335. new PkGuidAddDbo
  336. {
  337. SomeInt = 42,
  338. SomeText = "42"
  339. },
  340. new PkGuidAddDbo
  341. {
  342. SomeInt = 142,
  343. SomeText = "24"
  344. }
  345. };
  346. var service = new LuUtilsPkGuidDataAccess(context);
  347. var res = service.AddGuid(dbos);
  348. Assert.Equal(LuStatus.Success, res.Status);
  349. var orderBy = LuOrderByBinder.FromString("someInt,someText");
  350. Assert.Equal(LuStatus.Success, orderBy.Status);
  351. var get = service.GetMultiple(orderBy.Data);
  352. Assert.Equal(LuStatus.Success, get.Status);
  353. Assert.Equal(3, get.Data.Count);
  354. Assert.Equal(3, get.Data.Data.Count);
  355. var dbo = get.Data.Data[0];
  356. Assert.Equal(42, dbo.SomeInt);
  357. Assert.Equal("42", dbo.SomeText);
  358. dbo = get.Data.Data[1];
  359. Assert.Equal(42, dbo.SomeInt);
  360. Assert.Equal("442", dbo.SomeText);
  361. dbo = get.Data.Data[2];
  362. Assert.Equal(142, dbo.SomeInt);
  363. Assert.Equal("24", dbo.SomeText);
  364. });
  365. }
  366. [Fact]
  367. public void TestGetMultiple6()
  368. {
  369. Tests.TestRealDb(context =>
  370. {
  371. var dbos = new List<PkGuidAddDbo>
  372. {
  373. new PkGuidAddDbo
  374. {
  375. SomeInt = 42,
  376. SomeText = "442"
  377. },
  378. new PkGuidAddDbo
  379. {
  380. SomeInt = 42,
  381. SomeText = "42"
  382. },
  383. new PkGuidAddDbo
  384. {
  385. SomeInt = 142,
  386. SomeText = "24"
  387. }
  388. };
  389. var service = new LuUtilsPkGuidDataAccess(context);
  390. var res = service.AddGuid(dbos);
  391. Assert.Equal(LuStatus.Success, res.Status);
  392. var orderBy = LuOrderByBinder.FromString("someText:desc,someInt");
  393. Assert.Equal(LuStatus.Success, orderBy.Status);
  394. var get = service.GetMultiple(orderBy.Data);
  395. Assert.Equal(LuStatus.Success, get.Status);
  396. Assert.Equal(3, get.Data.Count);
  397. Assert.Equal(3, get.Data.Data.Count);
  398. var dbo = get.Data.Data[0];
  399. Assert.Equal(42, dbo.SomeInt);
  400. Assert.Equal("442", dbo.SomeText);
  401. dbo = get.Data.Data[1];
  402. Assert.Equal(42, dbo.SomeInt);
  403. Assert.Equal("42", dbo.SomeText);
  404. dbo = get.Data.Data[2];
  405. Assert.Equal(142, dbo.SomeInt);
  406. Assert.Equal("24", dbo.SomeText);
  407. });
  408. }
  409. }
  410. }