Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

LuEfCreateDataAccessTest.cs 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Luticate2.Utils.Dbo.Result;
  5. using Luticate2.Utils.Utils;
  6. using TestUtils.DataAccess;
  7. using TestUtils.Dbo.FkPkGuid;
  8. using TestUtils.Dbo.PkBigSerial;
  9. using TestUtils.Dbo.PkGuid;
  10. using Xunit;
  11. namespace TestUtils.EfCrudDataAccess
  12. {
  13. public class LuEfCreateDataAccessTest
  14. {
  15. [Fact]
  16. public void TestAddMultiple1()
  17. {
  18. Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
  19. {
  20. var dbos = new List<PkBigSerialAddDbo>
  21. {
  22. new PkBigSerialAddDbo
  23. {
  24. SomeInt = 42,
  25. SomeText = "42"
  26. },
  27. new PkBigSerialAddDbo
  28. {
  29. SomeInt = 21,
  30. SomeText = "24"
  31. }
  32. };
  33. var res = service.Add(dbos, enumerable => enumerable);
  34. Assert.Equal(LuStatus.Success, res.Status);
  35. foreach (var dbo in dbos.Zip(res.Data, (dbo, serialDbo) => new KeyValuePair<PkBigSerialAddDbo, PkBigSerialDbo>(dbo, serialDbo)))
  36. {
  37. Assert.NotNull(dbo.Key);
  38. Assert.NotNull(dbo.Value);
  39. Assert.NotEqual(0, dbo.Value.Id);
  40. Assert.Equal(dbo.Key.SomeText, dbo.Value.SomeText);
  41. Assert.Equal(dbo.Key.SomeInt, dbo.Value.SomeInt);
  42. var get = service.GetSingleById(dbo.Value.Id);
  43. Assert.Equal(LuStatus.Success, get.Status);
  44. Assert.Equal(dbo.Key.SomeText, get.Data.SomeText);
  45. Assert.Equal(dbo.Key.SomeInt, get.Data.SomeInt);
  46. Assert.Equal(dbo.Value.Id, get.Data.Id);
  47. }
  48. });
  49. }
  50. [Fact]
  51. public void TestAddMultiple2()
  52. {
  53. Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
  54. {
  55. var dbos = new List<PkBigSerialAddDbo>
  56. {
  57. new PkBigSerialAddDbo
  58. {
  59. SomeInt = 42,
  60. SomeText = "42"
  61. },
  62. new PkBigSerialAddDbo
  63. {
  64. SomeInt = 21,
  65. SomeText = "24"
  66. }
  67. };
  68. var res = service.Add(dbos, enumerable => enumerable.Select(dbo => dbo.Id));
  69. Assert.Equal(LuStatus.Success, res.Status);
  70. foreach (var dbo in dbos.Zip(res.Data, (dbo, serialDbo) => new KeyValuePair<PkBigSerialAddDbo, long>(dbo, serialDbo)))
  71. {
  72. Assert.NotNull(dbo.Key);
  73. Assert.NotNull(dbo.Value);
  74. Assert.NotEqual(0, dbo.Value);
  75. var get = service.GetSingleById(dbo.Value);
  76. Assert.Equal(LuStatus.Success, get.Status);
  77. Assert.Equal(dbo.Key.SomeText, get.Data.SomeText);
  78. Assert.Equal(dbo.Key.SomeInt, get.Data.SomeInt);
  79. Assert.Equal(dbo.Value, get.Data.Id);
  80. }
  81. });
  82. }
  83. [Fact]
  84. public void TestAddMultiple3()
  85. {
  86. Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
  87. {
  88. var dbos = new List<PkBigSerialAddDbo>
  89. {
  90. new PkBigSerialAddDbo
  91. {
  92. SomeInt = 42,
  93. SomeText = "42"
  94. },
  95. new PkBigSerialAddDbo
  96. {
  97. SomeInt = 21,
  98. SomeText = "24"
  99. }
  100. };
  101. var res = service.Add(dbos, enumerable => enumerable.Select(dbo => dbo.CreatedAt));
  102. Assert.Equal(LuStatus.Success, res.Status);
  103. foreach (var dbo in res.Data)
  104. {
  105. Assert.NotEqual(default(DateTime), dbo);
  106. }
  107. });
  108. }
  109. [Fact]
  110. public void TestAddMultiple4()
  111. {
  112. Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
  113. {
  114. var dbos = new List<PkBigSerialAddDbo>
  115. {
  116. new PkBigSerialAddDbo
  117. {
  118. SomeInt = 42,
  119. SomeText = "42"
  120. },
  121. new PkBigSerialAddDbo
  122. {
  123. SomeInt = 21,
  124. SomeText = "42"
  125. }
  126. };
  127. var res = service.Add(dbos, enumerable => enumerable.Select(dbo => dbo.CreatedAt));
  128. Assert.Equal(LuStatus.DbError, res.Status);
  129. });
  130. }
  131. [Fact]
  132. public void TestAddSingle1()
  133. {
  134. Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
  135. {
  136. var res = service.Add(new PkBigSerialAddDbo
  137. {
  138. SomeInt = 42,
  139. SomeText = "42"
  140. }, dbo => dbo);
  141. Assert.Equal(LuStatus.Success, res.Status);
  142. Assert.NotEqual(0, res.Data.Id);
  143. Assert.Equal("42", res.Data.SomeText);
  144. Assert.Equal(42, res.Data.SomeInt);
  145. var get = service.GetSingleById(res.Data.Id);
  146. Assert.Equal(LuStatus.Success, get.Status);
  147. Assert.Equal("42", get.Data.SomeText);
  148. Assert.Equal(42, get.Data.SomeInt);
  149. Assert.Equal(res.Data.Id, get.Data.Id);
  150. });
  151. }
  152. [Fact]
  153. public void TestAddSingle2()
  154. {
  155. Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
  156. {
  157. var res = service.Add(new PkBigSerialAddDbo
  158. {
  159. SomeInt = 42,
  160. SomeText = "42"
  161. }, dbo => dbo.Id);
  162. Assert.Equal(LuStatus.Success, res.Status);
  163. Assert.NotEqual(0, res.Data);
  164. var get = service.GetSingleById(res.Data);
  165. Assert.Equal(LuStatus.Success, get.Status);
  166. Assert.Equal("42", get.Data.SomeText);
  167. Assert.Equal(42, get.Data.SomeInt);
  168. Assert.Equal(res.Data, get.Data.Id);
  169. });
  170. }
  171. [Fact]
  172. public void TestAddSingle3()
  173. {
  174. Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
  175. {
  176. var res = service.Add(new PkBigSerialAddDbo
  177. {
  178. SomeInt = 42,
  179. SomeText = "42"
  180. }, dbo => dbo.CreatedAt);
  181. Assert.Equal(LuStatus.Success, res.Status);
  182. Assert.NotEqual(default(DateTime), res.Data);
  183. });
  184. }
  185. [Fact]
  186. public void TestAddGuidMultiple1()
  187. {
  188. Tests.TestRealDb<LuUtilsPkGuidDataAccess>(service =>
  189. {
  190. var dbos = new List<PkGuidAddDbo>
  191. {
  192. new PkGuidAddDbo
  193. {
  194. SomeInt = 42,
  195. SomeText = "42"
  196. },
  197. new PkGuidAddDbo
  198. {
  199. SomeInt = 21,
  200. SomeText = "24"
  201. }
  202. };
  203. var res = service.AddId(dbos);
  204. Assert.Equal(LuStatus.Success, res.Status);
  205. foreach (var dbo in dbos.Zip(res.Data, (dbo, serialDbo) => new KeyValuePair<PkGuidAddDbo, string>(dbo, serialDbo)))
  206. {
  207. Assert.NotNull(dbo.Key);
  208. Assert.NotNull(dbo.Value);
  209. Assert.NotEqual(new Guid().ToString(), dbo.Value);
  210. var get = service.GetSingleById(dbo.Value);
  211. Assert.Equal(LuStatus.Success, get.Status);
  212. Assert.Equal(dbo.Key.SomeText, get.Data.SomeText);
  213. Assert.Equal(dbo.Key.SomeInt, get.Data.SomeInt);
  214. Assert.Equal(dbo.Value, get.Data.Id);
  215. }
  216. });
  217. }
  218. [Fact]
  219. public void TestAddGuidSingle1()
  220. {
  221. Tests.TestRealDb<LuUtilsPkGuidDataAccess>(service =>
  222. {
  223. var res = service.AddId(new PkGuidAddDbo
  224. {
  225. SomeInt = 42,
  226. SomeText = "42"
  227. });
  228. Assert.Equal(LuStatus.Success, res.Status);
  229. Assert.NotEqual(new Guid().ToString(), res.Data);
  230. var get = service.GetSingleById(res.Data);
  231. Assert.Equal(LuStatus.Success, get.Status);
  232. Assert.Equal("42", get.Data.SomeText);
  233. Assert.Equal(42, get.Data.SomeInt);
  234. Assert.Equal(res.Data, get.Data.Id);
  235. });
  236. }
  237. [Fact]
  238. public void TestAddIdMultiple1()
  239. {
  240. Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
  241. {
  242. var dbos = new List<PkBigSerialAddDbo>
  243. {
  244. new PkBigSerialAddDbo
  245. {
  246. SomeInt = 42,
  247. SomeText = "42"
  248. },
  249. new PkBigSerialAddDbo
  250. {
  251. SomeInt = 21,
  252. SomeText = "24"
  253. }
  254. };
  255. var res = service.AddId(dbos);
  256. Assert.Equal(LuStatus.Success, res.Status);
  257. foreach (var dbo in dbos.Zip(res.Data, (dbo, serialDbo) => new KeyValuePair<PkBigSerialAddDbo, long>(dbo, serialDbo)))
  258. {
  259. Assert.NotNull(dbo.Key);
  260. Assert.NotNull(dbo.Value);
  261. Assert.NotEqual(0, dbo.Value);
  262. var get = service.GetSingleById(dbo.Value);
  263. Assert.Equal(LuStatus.Success, get.Status);
  264. Assert.Equal(dbo.Key.SomeText, get.Data.SomeText);
  265. Assert.Equal(dbo.Key.SomeInt, get.Data.SomeInt);
  266. Assert.Equal(dbo.Value, get.Data.Id);
  267. }
  268. });
  269. }
  270. [Fact]
  271. public void TestAddIdSingle1()
  272. {
  273. Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
  274. {
  275. var res = service.AddId(new PkBigSerialAddDbo
  276. {
  277. SomeInt = 42,
  278. SomeText = "42"
  279. });
  280. Assert.Equal(LuStatus.Success, res.Status);
  281. Assert.NotEqual(0, res.Data);
  282. var get = service.GetSingleById(res.Data);
  283. Assert.Equal(LuStatus.Success, get.Status);
  284. Assert.Equal("42", get.Data.SomeText);
  285. Assert.Equal(42, get.Data.SomeInt);
  286. Assert.Equal(res.Data, get.Data.Id);
  287. });
  288. }
  289. [Fact]
  290. public void TestAddDboMultiple1()
  291. {
  292. Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
  293. {
  294. var dbos = new List<PkBigSerialAddDbo>
  295. {
  296. new PkBigSerialAddDbo
  297. {
  298. SomeInt = 42,
  299. SomeText = "42"
  300. },
  301. new PkBigSerialAddDbo
  302. {
  303. SomeInt = 21,
  304. SomeText = "24"
  305. }
  306. };
  307. var res = service.AddDbo(dbos);
  308. Assert.Equal(LuStatus.Success, res.Status);
  309. foreach (var dbo in dbos.Zip(res.Data, (dbo, serialDbo) => new KeyValuePair<PkBigSerialAddDbo, PkBigSerialDbo>(dbo, serialDbo)))
  310. {
  311. Assert.NotNull(dbo.Key);
  312. Assert.NotNull(dbo.Value);
  313. Assert.NotEqual(0, dbo.Value.Id);
  314. var get = service.GetSingleById(dbo.Value.Id);
  315. Assert.Equal(LuStatus.Success, get.Status);
  316. Assert.Equal(dbo.Key.SomeText, get.Data.SomeText);
  317. Assert.Equal(dbo.Key.SomeInt, get.Data.SomeInt);
  318. Assert.Equal(dbo.Value.Id, get.Data.Id);
  319. }
  320. });
  321. }
  322. [Fact]
  323. public void TestAddDboSingle1()
  324. {
  325. Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
  326. {
  327. var res = service.AddDbo(new PkBigSerialAddDbo
  328. {
  329. SomeInt = 42,
  330. SomeText = "42"
  331. });
  332. Assert.Equal(LuStatus.Success, res.Status);
  333. Assert.NotEqual(0, res.Data.Id);
  334. var get = service.GetSingleById(res.Data.Id);
  335. Assert.Equal(LuStatus.Success, get.Status);
  336. Assert.Equal("42", get.Data.SomeText);
  337. Assert.Equal(42, get.Data.SomeInt);
  338. Assert.Equal(res.Data.Id, get.Data.Id);
  339. });
  340. }
  341. [Fact]
  342. public void TestAddDboSingleFk1()
  343. {
  344. Tests.TestRealDb<LuUtilsPkGuidDataAccess, LuUtilsFkPkGuidDataAccess>((pkGuidDataAccess, fkPkGuidDataAccess) =>
  345. {
  346. var pkGuidDbo = pkGuidDataAccess.AddDbo(new PkGuidAddDbo
  347. {
  348. SomeInt = 42,
  349. SomeText = "Test."
  350. });
  351. Assert.Equal(LuStatus.Success, pkGuidDbo.Status);
  352. var fkPkGuidDbo = fkPkGuidDataAccess.AddDbo(new FkPkGuidAddDbo
  353. {
  354. Name = "Test.",
  355. PkGuidId = pkGuidDbo.Data.Id
  356. });
  357. Assert.Equal(LuStatus.Success, fkPkGuidDbo.Status);
  358. Assert.NotNull(fkPkGuidDbo.Data.PkGuid);
  359. Assert.Equal(pkGuidDbo.Data.Id, fkPkGuidDbo.Data.PkGuid.Id);
  360. });
  361. }
  362. }
  363. }