You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LuEfReadDataAccessTest.cs 15KB

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