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

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