Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.08 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using FluentAssertions;
  5. using Moq;
  6. using SharpCompress.Common;
  7. using Xrm.Rec.DataLayer.Entities.Account;
  8. using Xrm.Rec.DataLayer.Entities.Catalog;
  9. using Xrm.Rec.DataLayer.Entities.DisclosureControl;
  10. using Xrm.Rec.DataLayer.Entities.RegulatedOrganizationsPrograms;
  11. using Xrm.Rec.DataLayer.Entities.RegulatedOrganizationsPrograms.Catalog;
  12. using Xrm.Rec.DataLayer.Entities.RegulatorMeetings;
  13. using Xrm.Rec.DataLayer.Entities.Tariff;
  14. using Xrm.Rec.DataLayer.Repositories.Interfaces;
  15. using Xrm.Rec.Domain.Common.ConstantMapping;
  16. using Xrm.Rec.Domain.Common.Enums.CatalogModule;
  17. using Xrm.Rec.Domain.Services.Catalogs;
  18. using Xrm.Rec.Domain.Services.Catalogs.Dto;
  19. using Xrm.Rec.Domain.Services.Catalogs.Interfaces;
  20. using Xrm.Rec.Domain.Services.Shared.Dto;
  21. using Xrm.Rec.UnitTests.Common;
  22. using Xrm.Rec.UnitTests.Common.DataMocks;
  23. using Xunit;
  24. using Xrm.Rec.Domain.Services.Exceptions;
  25. using Xrm.Universal.DataLayer.Interfaces;
  26. using Xrm.Universal.EventBus.Interfaces;
  27. using Xrm.Universal.Models;
  28.  
  29. namespace Xrm.Rec.Domain.Services.UnitTests.Catalogs
  30. {
  31. public class DomainWorkKindServiceTests : TestBase
  32. {
  33. private readonly RepositoryManagerStub _repositoryManagerStub;
  34. private readonly IDomainWorkKindService _domainWorkKindService;
  35.  
  36. private ICommonRepository<DbWorkKind> WorkKindRepository => _repositoryManagerStub.GetCommonRepository<DbWorkKind>();
  37. private readonly Mock<IWorkKindMapper> _workKindMapperMock;
  38.  
  39. public DomainWorkKindServiceTests()
  40. {
  41. _workKindMapperMock = new Mock<IWorkKindMapper>(MockBehavior.Strict);
  42. _repositoryManagerStub = new RepositoryManagerStub();
  43. _domainWorkKindService = new DomainWorkKindService(_repositoryManagerStub, Mock.Of<IEventBus>(), _workKindMapperMock.Object);
  44. }
  45.  
  46.  
  47. #region Search
  48.  
  49. public static TheoryData<Action<RepositoryManagerStub>> AddRepositoryForSearchData()
  50. {
  51. var data = new TheoryData<Action<RepositoryManagerStub>>
  52. {
  53. (r) => r.SetDbEntities(new DbEseeProgramRequirement { WorkKindId = 3 }),
  54. (r) => r.SetDbEntities(new DbInvestProgramFinancingSource { WorkKindId = 3 }),
  55. (r) => r.SetDbEntities(new DbMonitoring2WorkKind { WorkKindId = 3 }),
  56. (r) => r.SetDbEntities(new DbImplementedWorkKind { WorkKindId = 3 }),
  57. (r) => r.SetDbEntities(new DbEseeProgramRequirement { WorkKindId = 3 }),
  58. (r) => r.SetDbEntities(new DbInvestProgramFinancingSource { WorkKindId = 3 }),
  59. (r) => r.SetDbEntities(new DbPublicationDocument2WorkKind { WorkKindId = 3 }),
  60. (r) => r.SetDbEntities(new DbReport { Report2WorkKinds = new List<DbReport2WorkKind> { new DbReport2WorkKind { WorkKindId = 3 } } }),
  61. (r) => r.SetDbEntities(new DbEseeProgram { WorkKindId = 3 }),
  62. (r) => r.SetDbEntities(new DbInvestProgram { WorkKindId = 3 }),
  63. (r) => r.SetDbEntities(new DbProgram2WorkKind { WorkKindId = 3 }),
  64. (r) => r.SetDbEntities(new DbQuestionVersion { WorkKindId = 3 }),
  65. (r) => r.SetDbEntities(new DbDifferentiationCriterium { WorkKindId = 3 }),
  66. (r) => r.SetDbEntities(new DbSci { WorkKindId = 3 }),
  67. (r) => r.SetDbEntities(new DbTariff { WorkKindId = 3 }),
  68. (r) => r.SetDbEntities(new DbTariffKind { WorkKindId = 3 })
  69. };
  70. return data;
  71. }
  72.  
  73. [Theory]
  74. [MemberData(nameof(AddRepositoryForSearchData))]
  75. public void SearchCanDelete(Action<RepositoryManagerStub> repoAction)
  76. {
  77. _repositoryManagerStub.SetDbEntities(new DbWorkKind { Id = 3, Group = new DbWorkKindGroup() { Id = 3, Description = "grp" } });
  78. repoAction(_repositoryManagerStub);
  79.  
  80. var result = _domainWorkKindService.Search();
  81. result.Single().CanDelete.Should().BeFalse();
  82. }
  83.  
  84. [Fact]
  85. public void Search()
  86. {
  87. _repositoryManagerStub.SetDbEntities(
  88. new DbWorkKind { Id = 1, Code = 1, Name = "WorkKind_1", Group = new DbWorkKindGroup { Id = 1, Description = "WorkKindGroup_1" }, RegulationStartDate = new DateTime(2001, 01, 01), RegulationEndDate = new DateTime(2001, 12, 31) },
  89. new DbWorkKind { Id = 2, Code = 2, Name = "WorkKind_2", Group = new DbWorkKindGroup { Id = 2, Description = "WorkKindGroup_2" }, RegulationStartDate = new DateTime(2002, 01, 01), RegulationEndDate = new DateTime(2002, 12, 31) },
  90. new DbWorkKind { Id = 3, Code = 3, Name = "WorkKind_3", Group = new DbWorkKindGroup { Id = 3, Description = "WorkKindGroup_3" }, RegulationStartDate = new DateTime(2003, 01, 01) },
  91. new DbWorkKind { Id = 4, Code = 4, Name = "WorkKind_4", Group = new DbWorkKindGroup { Id = 4, Description = "WorkKindGroup_4" }, RegulationStartDate = new DateTime(2004, 01, 01) }
  92. );
  93.  
  94. var result = _domainWorkKindService.Search();
  95. result.ShouldBeEquivalentTo(new[]
  96. {
  97. new WorkKindRegistryItem { Id = 1, Code = 1, Name = "WorkKind_1", WorkKindGroup = new IdNamePair { Name = "WorkKindGroup_1" }, RegulationInterval = new DateTimeIntervalDto() { Start = new DateTime(2001, 01, 01), Finish = new DateTime(2001, 12, 31) }, CanDelete = true },
  98. new WorkKindRegistryItem { Id = 2, Code = 2, Name = "WorkKind_2", WorkKindGroup = new IdNamePair { Name = "WorkKindGroup_2" }, RegulationInterval = new DateTimeIntervalDto() { Start = new DateTime(2002, 01, 01), Finish = new DateTime(2002, 12, 31) }, CanDelete = true },
  99. new WorkKindRegistryItem { Id = 3, Code = 3, Name = "WorkKind_3", WorkKindGroup = new IdNamePair { Name = "WorkKindGroup_3" }, RegulationInterval = new DateTimeIntervalDto() { Start = new DateTime(2003, 01, 01) }, CanDelete = true },
  100. new WorkKindRegistryItem { Id = 4, Code = 4, Name = "WorkKind_4", WorkKindGroup = new IdNamePair { Name = "WorkKindGroup_4" }, RegulationInterval = new DateTimeIntervalDto() { Start = new DateTime(2004, 01, 01) }, CanDelete = true }
  101. });
  102. }
  103.  
  104. #endregion
  105.  
  106. #region Get
  107.  
  108.  
  109. [Fact]
  110. public void GetThrow()
  111. {
  112. _repositoryManagerStub.SetDbEntities(
  113. new DbWorkKind { Id = 1, Code = 1, Name = "WorkKind_1", Group = new DbWorkKindGroup { Id = 1, Description = "WorkKindGroup_1" }, RegulationStartDate = new DateTime(2001, 01, 01), RegulationEndDate = new DateTime(2001, 12, 31) }
  114. );
  115.  
  116. Action action = () => _domainWorkKindService.Get(555);
  117. action.ShouldThrow<ServiceException>()
  118. .Which
  119. .ServiceExceptionType.ShouldBeEquivalentTo(ServiceExceptionTypes.EntityNotFound);
  120. }
  121.  
  122. [Fact]
  123. public void Get()
  124. {
  125. _repositoryManagerStub.SetDbEntities(
  126. new DbWorkKind { Id = 1, Code = 1, Name = "WorkKind_1", Group = new DbWorkKindGroup { Id = 1, Description = "WorkKindGroup_1" }, RegulationStartDate = new DateTime(2001, 01, 01), RegulationEndDate = new DateTime(2001, 12, 31) }
  127. );
  128.  
  129. _workKindMapperMock.Setup(x => x.Map(It.IsAny<DbWorkKind>())).Returns(new WorkKindDto());
  130. _domainWorkKindService.Get(1);
  131.  
  132. _workKindMapperMock.VerifyAll();
  133. }
  134.  
  135. [Fact]
  136. public void GetWorkKindGroups()
  137. {
  138. _repositoryManagerStub.SetDbEntities(
  139. new DbWorkKindGroup { Id = 1, Description = "grp1" },
  140. new DbWorkKindGroup { Id = 2, Description = "grp2" },
  141. new DbWorkKindGroup { Id = 3, Description = "grp3" }
  142. );
  143.  
  144. var rezult = _domainWorkKindService.GetWorkKindGroups();
  145. rezult.ShouldBeEquivalentTo(new[]
  146. {
  147. new IdNamePair { Id = 1, Name = "grp1" },
  148. new IdNamePair { Id = 2, Name = "grp2" },
  149. new IdNamePair { Id = 3, Name = "grp3" },
  150. });
  151. }
  152.  
  153. [Fact]
  154. public void GetWorkKindItems()
  155. {
  156. var group1 = new DbWorkKindGroup { Id = WorkKindGroup.Transportation.GetId(), Description = WorkKindGroup.Transportation.GetDescription() };
  157. var group2 = new DbWorkKindGroup { Id = WorkKindGroup.Other.GetId(), Description = WorkKindGroup.Other.GetDescription() };
  158.  
  159. _repositoryManagerStub.SetDbEntities(
  160. new DbWorkKind { Id = 3, Name = "Name_3", Group = group2 },
  161. new DbWorkKind { Id = 2, Name = "Name_2", Group = group1 },
  162. new DbWorkKind { Id = 1, Name = "Name_1", Group = group1 }
  163. );
  164.  
  165. var result = _domainWorkKindService.GetWorkKindItems();
  166.  
  167. result.ShouldBeEquivalentTo(new[]
  168. {
  169. new WorkKindGroupItemDto
  170. {
  171. Id = WorkKindGroup.Other.GetId(),
  172. Name = WorkKindGroup.Other.GetDescription(),
  173. Items = new List<IdNamePair>
  174. {
  175. new IdNamePair { Id = 3, Name = "Name_3" }
  176. }
  177. },
  178. new WorkKindGroupItemDto
  179. {
  180. Id = WorkKindGroup.Transportation.GetId(),
  181. Name = WorkKindGroup.Transportation.GetDescription(),
  182. Items = new List<IdNamePair>
  183. {
  184. new IdNamePair { Id = 1, Name = "Name_1" },
  185. new IdNamePair { Id = 2, Name = "Name_2" }
  186. }
  187. }
  188. });
  189. }
  190.  
  191. #endregion
  192.  
  193. #region Edit
  194.  
  195. [Fact]
  196. public void AddThrows()
  197. {
  198. _repositoryManagerStub.SetDbEntities(new DbWorkKind { Id = 1, Code = 1, Name = "Name_1" });
  199.  
  200. var dto = new WorkKindDto { Name = "Name_1" };
  201.  
  202. Action action = () => _domainWorkKindService.Edit(dto);
  203. action.ShouldThrow<ServiceException>()
  204. .WithMessage("Вид деятельности с таким наименованием уже существует в реестре")
  205. .Which
  206. .ServiceExceptionType.ShouldBeEquivalentTo(ServiceExceptionTypes.InvalidInputData);
  207. }
  208.  
  209. [Fact]
  210. public void Add()
  211. {
  212. _repositoryManagerStub.SetDbEntities(new DbWorkKind { Id = 1, Code = 1, Name = "Name_1" });
  213.  
  214. var dto = new WorkKindDto
  215. {
  216. Name = "Name_2",
  217. WorkKindGroup = new IdNamePair { Id = WorkKindGroup.Other.GetId() },
  218. RegulationInterval = new DateTimeIntervalDto
  219. {
  220. Start = new DateTime(2004, 04, 04),
  221. Finish = new DateTime(2005, 05, 05),
  222. }
  223. };
  224. _workKindMapperMock.Setup(x => x.Update(It.IsAny<DbWorkKind>(), It.IsAny<WorkKindDto>()));
  225. _workKindMapperMock.Setup(x => x.Map(It.IsAny<DbWorkKind>())).Returns(new WorkKindDto());
  226.  
  227. _domainWorkKindService.Edit(dto);
  228.  
  229. _workKindMapperMock.VerifyAll();
  230. }
  231.  
  232. [Fact]
  233. public void EditNameException()
  234. {
  235. _repositoryManagerStub.SetDbEntities(
  236. new DbWorkKind { Id = 1, Code = 1, Name = "Name_1" },
  237. new DbWorkKind { Id = 2, Code = 2, Name = "Name_2" }
  238. );
  239.  
  240. var dto = new WorkKindDto
  241. {
  242. Name = "Name_2",
  243. WorkKindGroup = new IdNamePair { Id = WorkKindGroup.Other.GetId() },
  244. RegulationInterval = new DateTimeIntervalDto
  245. {
  246. Start = new DateTime(2004, 04, 04),
  247. Finish = new DateTime(2005, 05, 05),
  248. }
  249. };
  250.  
  251. Action action = () => _domainWorkKindService.Edit(dto);
  252. action.ShouldThrow<ServiceException>()
  253. .WithMessage("Вид деятельности с таким наименованием уже существует в реестре")
  254. .Which
  255. .ServiceExceptionType.ShouldBeEquivalentTo(ServiceExceptionTypes.InvalidInputData);
  256.  
  257. }
  258.  
  259. [Fact]
  260. public void EditMyName()
  261. {
  262. _repositoryManagerStub.SetDbEntities(
  263. new DbWorkKind { Id = 1, Code = 1, Name = "Name_1" },
  264. new DbWorkKind { Id = 2, Code = 2, Name = "Name_2" }
  265. );
  266.  
  267. var dto = new WorkKindDto
  268. {
  269. Id = 2,
  270. Name = "Name_2",
  271. WorkKindGroup = new IdNamePair { Id = WorkKindGroup.Other.GetId() },
  272. RegulationInterval = new DateTimeIntervalDto
  273. {
  274. Start = new DateTime(2004, 04, 04),
  275. Finish = new DateTime(2005, 05, 05),
  276. }
  277. };
  278. _workKindMapperMock.Setup(x => x.Map(It.IsAny<DbWorkKind>())).Returns(new WorkKindDto());
  279. _workKindMapperMock.Setup(x => x.Update(It.IsAny<DbWorkKind>(), It.IsAny<WorkKindDto>()));
  280.  
  281. _domainWorkKindService.Edit(dto);
  282.  
  283. _workKindMapperMock.VerifyAll();
  284. }
  285.  
  286. [Fact]
  287. public void Edit()
  288. {
  289. _repositoryManagerStub.SetDbEntities(
  290. new DbWorkKind { Id = 1, Code = 1, Name = "Name_1" },
  291. new DbWorkKind { Id = 2, Code = 2, Name = "Name_2" }
  292. );
  293.  
  294. var dto = new WorkKindDto
  295. {
  296. Id = 2,
  297. Name = "Name_2_2",
  298. WorkKindGroup = new IdNamePair { Id = WorkKindGroup.Other.GetId() },
  299. RegulationInterval = new DateTimeIntervalDto
  300. {
  301. Start = new DateTime(2004, 04, 04),
  302. Finish = new DateTime(2005, 05, 05),
  303. }
  304. };
  305. _workKindMapperMock.Setup(x => x.Map(It.IsAny<DbWorkKind>())).Returns(new WorkKindDto());
  306. _workKindMapperMock.Setup(x => x.Update(It.IsAny<DbWorkKind>(), It.IsAny<WorkKindDto>()));
  307.  
  308. _domainWorkKindService.Edit(dto);
  309.  
  310. _workKindMapperMock.VerifyAll();
  311. }
  312.  
  313. #endregion
  314.  
  315. #region Actions
  316.  
  317. [Fact]
  318. public void Delete()
  319. {
  320. _repositoryManagerStub.SetDbEntities(new DbWorkKind { Id = 1 });
  321. var workKindRepository = _repositoryManagerStub.GetCommonRepository<DbWorkKind>();
  322.  
  323. _workKindMapperMock.Setup(x => x.Map(It.IsAny<DbWorkKind>())).Returns(new WorkKindDto());
  324. workKindRepository.Find().Should().HaveCount(1);
  325.  
  326. _domainWorkKindService.Delete(1);
  327. workKindRepository.Find().Should().BeEmpty();
  328. _workKindMapperMock.VerifyAll();
  329. }
  330.  
  331. public static TheoryData<Action<RepositoryManagerStub>> AddRepositoryData()
  332. {
  333. var data = new TheoryData<Action<RepositoryManagerStub>>
  334. {
  335. (r) => r.SetDbEntities(new DbEseeProgramRequirement { WorkKindId = 3 }),
  336. (r) => r.SetDbEntities(new DbInvestProgramFinancingSource { WorkKindId = 3 }),
  337. (r) => r.SetDbEntities(new DbMonitoring2WorkKind { WorkKindId = 3 }),
  338. (r) => r.SetDbEntities(new DbImplementedWorkKind { WorkKindId = 3 }),
  339. (r) => r.SetDbEntities(new DbEseeProgramRequirement { WorkKindId = 3 }),
  340. (r) => r.SetDbEntities(new DbInvestProgramFinancingSource { WorkKindId = 3 }),
  341. (r) => r.SetDbEntities(new DbPublicationDocument2WorkKind { WorkKindId = 3 }),
  342. (r) => r.SetDbEntities(new DbReport { Report2WorkKinds = new List<DbReport2WorkKind> { new DbReport2WorkKind { WorkKindId = 3 } } }),
  343. (r) => r.SetDbEntities(new DbEseeProgram { WorkKindId = 3 }),
  344. (r) => r.SetDbEntities(new DbInvestProgram { WorkKindId = 3 }),
  345. (r) => r.SetDbEntities(new DbProgram2WorkKind { WorkKindId = 3 }),
  346. (r) => r.SetDbEntities(new DbQuestionVersion { WorkKindId = 3 }),
  347. (r) => r.SetDbEntities(new DbDifferentiationCriterium { WorkKindId = 3 }),
  348. (r) => r.SetDbEntities(new DbSci { WorkKindId = 3 }),
  349. (r) => r.SetDbEntities(new DbTariff { WorkKindId = 3 }),
  350. (r) => r.SetDbEntities(new DbTariffKind { WorkKindId = 3 })
  351. };
  352. return data;
  353. }
  354.  
  355. [Theory]
  356. [MemberData(nameof(AddRepositoryData))]
  357. public void DeleteThrow(Action<RepositoryManagerStub> repoAction)
  358. {
  359. _repositoryManagerStub.SetDbEntities(new DbWorkKind { Id = 3 });
  360. repoAction(_repositoryManagerStub);
  361.  
  362. Action action = () => _domainWorkKindService.Delete(3);
  363.  
  364. action.ShouldThrow<ServiceException>()
  365. .Which
  366. .ServiceExceptionType.ShouldBeEquivalentTo(ServiceExceptionTypes.ValidationError);
  367. }
  368.  
  369. #endregion
  370. }
  371. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement