SnapshotApplicationTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. using AutoFixture;
  2. using DocumentFormat.OpenXml.Wordprocessing;
  3. using Hotline.Api.Controllers;
  4. using Hotline.Application.Identity;
  5. using Hotline.Application.Snapshot;
  6. using Hotline.Application.Tests.Mock;
  7. using Hotline.File;
  8. using Hotline.Identity.Accounts;
  9. using Hotline.Identity.Roles;
  10. using Hotline.Orders;
  11. using Hotline.Share.Dtos.Article;
  12. using Hotline.Share.Dtos.Snapshot;
  13. using Hotline.Share.Enums;
  14. using Hotline.Share.Enums.Order;
  15. using Hotline.Share.Enums.Snapshot;
  16. using Hotline.Share.Tools;
  17. using Hotline.Snapshot;
  18. using Hotline.Snapshot.Interfaces;
  19. using Hotline.Users;
  20. using Microsoft.AspNetCore.Http;
  21. using Microsoft.Extensions.DependencyInjection;
  22. using Shouldly;
  23. using System;
  24. using XF.Domain.Authentications;
  25. using XF.Domain.Repository;
  26. using XF.Utility.EnumExtensions;
  27. namespace Hotline.Application.Tests.Application;
  28. public class SnapshotApplicationTest : TestBase
  29. {
  30. private readonly ISnapshotApplication _snapshotApplication;
  31. private readonly IIdentityAppService _identityAppService;
  32. private readonly IRepository<RedPackRecord> _redPackRecordRepository;
  33. private readonly IIndustryApplication _industryApplication;
  34. private readonly IIndustryRepository _industryRepository;
  35. private readonly IFileRepository _fileRepository;
  36. private readonly OrderServiceMock _orderServiceMock;
  37. private readonly IOrderRepository _orderRepository;
  38. private readonly IOrderSnapshotRepository _orderSnapshotRepository;
  39. private readonly ISessionContext _sessionContext;
  40. public SnapshotApplicationTest(IAccountRepository accountRepository, IRepository<Role> roleRepository, UserController userController, IServiceScopeFactory scopeFactory, IRepository<User> userRepository, IHttpContextAccessor httpContextAccessor, ISnapshotApplication snapshotApplication, IIdentityAppService identityAppService, IRepository<RedPackRecord> redPackRecordRepository, IIndustryApplication industryApplication, IIndustryRepository industryRepository, IFileRepository fileRepository, OrderServiceMock orderServiceMock, IOrderRepository orderRepository, IOrderSnapshotRepository orderSnapshotRepository, IThirdIdentiyService thirdService, IThirdAccountRepository thirdAccount, ISessionContext sessionContext) : base(accountRepository, roleRepository, userController, scopeFactory, userRepository, httpContextAccessor, thirdService, thirdAccount)
  41. {
  42. _snapshotApplication = snapshotApplication;
  43. _identityAppService = identityAppService;
  44. _redPackRecordRepository = redPackRecordRepository;
  45. _industryApplication = industryApplication;
  46. _industryRepository = industryRepository;
  47. _fileRepository = fileRepository;
  48. _orderServiceMock = orderServiceMock;
  49. _orderRepository = orderRepository;
  50. _orderSnapshotRepository = orderSnapshotRepository;
  51. SetWeiXin();
  52. _sessionContext = sessionContext;
  53. }
  54. [Fact]
  55. public async Task GetHomePage_Test()
  56. {
  57. var result = await _snapshotApplication.GetHomePageAsync();
  58. result.Industrys.Any().ShouldBe(true, "首页数据为空");
  59. result.Industrys.First().DisplayOrder.ShouldBe(1, "排序异常");
  60. }
  61. /// <summary>
  62. /// 添加随手拍公告
  63. /// </summary>
  64. /// <returns></returns>
  65. [Fact]
  66. public async Task AddBulletin_Test()
  67. {
  68. var industry = await _industryRepository.Queryable()
  69. .OrderBy(m => m.DisplayOrder)
  70. .FirstAsync();
  71. if (industry == null)
  72. {
  73. return;
  74. }
  75. var inDto = new AddSnapshotBulletinInDto
  76. {
  77. Title = "单元测试" + DateTime.Now.ToLongDateTimeString(),
  78. Content = "测试内容" + DateTime.Now.ToLongDateTimeString(),
  79. SnapshotBulletinTypeId = industry.BulletinTypeGuideId!,
  80. SnapshotBulletinTypeName = industry.BulletinTypeGuideName!
  81. };
  82. var bulletinId = await _snapshotApplication.AddBulletinAsync(inDto);
  83. inDto = new AddSnapshotBulletinInDto
  84. {
  85. Title = "单元测试" + DateTime.Now.ToLongDateTimeString(),
  86. Content = "测试内容" + DateTime.Now.ToLongDateTimeString(),
  87. SnapshotBulletinTypeId = industry.BulletinTypePublicityId!,
  88. SnapshotBulletinTypeName = industry.BulletinTypePublicityName!
  89. };
  90. bulletinId = await _snapshotApplication.AddBulletinAsync(inDto);
  91. await _snapshotApplication.AuditBulletinAsync(new ExamineBulletinDto { Id = bulletinId, IsPass = true, Reason = "测试审核通过"});
  92. var items = await _snapshotApplication.GetBulletinsAsync(new BulletinInDto { IndustryId = industry.Id }, CancellationToken.None);
  93. items.Count.ShouldNotBe(0, "公告数量为0");
  94. }
  95. /// <summary>
  96. /// 获取公开工单集合
  97. /// </summary>
  98. /// <returns></returns>
  99. [Fact]
  100. public async Task GetPublishOrder_Test()
  101. {
  102. var industry = await _industryRepository.Queryable()
  103. .OrderBy(m => m.DisplayOrder)
  104. .FirstAsync();
  105. SetWeiXin();
  106. var order = _orderServiceMock.CreateSnapshotOrder()
  107. .办理到派单员(SetZuoXi)
  108. .办理到归档(SetPaiDanYuan)
  109. .GetCreateResult();
  110. SetWeiXin();
  111. var orderEntity = await _orderRepository.GetAsync(order.Id);
  112. var orderSnapshot = await _orderSnapshotRepository.GetAsync(order.Id);
  113. var inDto = new AddSnapshotOrderPublishInDto
  114. {
  115. ArrangeContent = orderEntity.Content,
  116. ArrangeOpinion = orderEntity.ActualOpinion,
  117. ArrangeTitle = orderEntity.Title,
  118. OrderId = orderEntity.Id,
  119. Address = orderEntity.FullAddress,
  120. HandleTime = DateTime.Now
  121. };
  122. await _snapshotApplication.AddOrderPublishAsync(inDto, CancellationToken.None);
  123. var items = await _snapshotApplication.GetOrderPublishAsync(new OrderPublishInDto { IndustryId = industry.Id }, CancellationToken.None);
  124. items.Count.ShouldNotBe(0);
  125. var orderPublishDetail = await _snapshotApplication.GetOrderPublishDetailAsync(items.OrderByDescending(m => m.HandleTime).First().Id, CancellationToken.None);
  126. orderPublishDetail.ShouldNotBeNull();
  127. orderPublishDetail.Workflow.Any(m => m.Name.IsNullOrEmpty()).ShouldBeFalse();
  128. }
  129. [Fact]
  130. public async Task GetOrderPublishDetail_Test()
  131. {
  132. }
  133. [Fact]
  134. public async Task GetBulletins_Test()
  135. {
  136. var homePage = await _snapshotApplication.GetHomePageAsync();
  137. var inDto = new BulletinInDto
  138. {
  139. IndustryId = homePage.Industrys.First(m => m.Name == "文化旅游").Id,
  140. };
  141. var items = await _snapshotApplication.GetBulletinsAsync(inDto, CancellationToken.None);
  142. items.ShouldNotBeNull();
  143. items.Any().ShouldBe(true, "公告数据为空");
  144. items.Any(m => m.Title.IsNullOrEmpty()).ShouldBe(false, "标题错误");
  145. items.Any(m => m.Content.IsNullOrEmpty()).ShouldBe(false, "内容错误");
  146. items.Any(m => m.Id.IsNullOrEmpty()).ShouldBe(false, "Id错误");
  147. }
  148. [Fact]
  149. public async Task GetSnapshotUserInfo_Test()
  150. {
  151. await _identityAppService.GetThredTokenAsync(new ThirdTokenInDto());
  152. var result = await _snapshotApplication.GetSnapshotUserInfoAsync();
  153. result.ShouldNotBeNull();
  154. result.PhoneNumber.ShouldNotBeNullOrEmpty();
  155. result.PhoneNumberMask.Contains("***").ShouldBeTrue();
  156. }
  157. [Fact]
  158. public async Task RefreshTokenAsync()
  159. {
  160. var token = await _identityAppService.GetThredTokenAsync(new ThirdTokenInDto());
  161. var newToken = await _identityAppService.RefreshTokenAsync(token.OpenId);
  162. newToken.ShouldNotBeNull();
  163. newToken.OpenId.ShouldBe(token.OpenId);
  164. newToken.PhoneNumber.ShouldNotBeNullOrEmpty();
  165. }
  166. [Fact]
  167. public async Task GetThirdToken_Test()
  168. {
  169. var result = await _identityAppService.GetThredTokenAsync(new ThirdTokenInDto { LoginCode = "0c3Adhll2zDMBe413rnl2KvEym2AdhlH" });
  170. result.PhoneNumber.ShouldNotBeNullOrEmpty();
  171. }
  172. [Theory]
  173. [InlineData("")]
  174. [InlineData("测")]
  175. public async Task SnapshotOrder_Test(string key)
  176. {
  177. var dto = new OrderInDto();
  178. dto.KeyWords = key;
  179. var page = await _snapshotApplication.GetSnapshotOrdersAsync(dto, CancellationToken.None);
  180. page.Count.ShouldNotBe(0);
  181. page.FirstOrDefault()?.IndustryName.ShouldNotBeNullOrEmpty();
  182. page.FirstOrDefault()?.OrderNo.ShouldNotBeNullOrEmpty();
  183. page.FirstOrDefault()?.StatusText.ShouldNotBeNullOrEmpty();
  184. page.FirstOrDefault()?.Area.ShouldNotBeNullOrEmpty();
  185. }
  186. [Theory]
  187. [InlineData(EOrderQueryStatus.All, 3)]
  188. [InlineData(EOrderQueryStatus.Reply, 2)]
  189. [InlineData(EOrderQueryStatus.NoReply, 1)]
  190. [InlineData(EOrderQueryStatus.Appraise, 1)]
  191. public async Task SnapshotOrderStatus_Test(EOrderQueryStatus status, int count)
  192. {
  193. var dto = new OrderInDto { Status = status };
  194. var page = await _snapshotApplication.GetSnapshotOrdersAsync(dto, CancellationToken.None);
  195. page.Count.ShouldNotBe(0, $"状态:{status.GetDescription()} 数据为空");
  196. }
  197. [Fact]
  198. public async Task GetSnapshotOrderDetail_Test()
  199. {
  200. var page = await _snapshotApplication.GetSnapshotOrdersAsync(new OrderInDto(), CancellationToken.None);
  201. var id = page.First().Id;
  202. var detail = await _snapshotApplication.GetSnapshotOrderDetailAsync(id, CancellationToken.None);
  203. detail.Id.ShouldBe(id);
  204. detail.Title.ShouldNotBeNullOrEmpty();
  205. detail.Opinion.ShouldNotBeNullOrEmpty();
  206. detail.Content.ShouldNotBeNullOrEmpty();
  207. }
  208. [Theory]
  209. [InlineData(2, 2)]
  210. [InlineData(12, 12)]
  211. public async Task GetRedPackDateAsync(int count, int exp)
  212. {
  213. var items = await _snapshotApplication.GetRedPackDateAsync(new RedPackDateInDto { QueryCount = count }, CancellationToken.None);
  214. items.Count.ShouldNotBe(0, "0数据");
  215. items.Count.ShouldBe(exp, $"应该:{exp}, 实际 {items.Count}");
  216. }
  217. [Fact]
  218. public async Task GetRedPackReceivedTotal_Test()
  219. {
  220. var amount = await _snapshotApplication.GetRedPackReceivedTotalAsync(CancellationToken.None);
  221. amount.ShouldNotBe("0.00");
  222. amount.ShouldNotBe("0");
  223. }
  224. [Fact]
  225. public async Task AddVolunteerReport_Test()
  226. {
  227. await _snapshotApplication.AddVolunteerAsync(new AddVolunteerInDto { Name = _sessionContext.UserName, PhoneNumber = _sessionContext.Phone }, CancellationToken.None);
  228. var inDto = _fixture.Create<AddVolunteerReportInDto>();
  229. foreach (var item in inDto.Files)
  230. {
  231. item.FileName = DateTime.Now.ToShortTimeString() + "文件.doc";
  232. }
  233. var result = await _snapshotApplication.AddVolunteerReportAsync(inDto, CancellationToken.None);
  234. result.Id.ShouldNotBeNull();
  235. }
  236. [Fact]
  237. public async Task SaveInvitationCode_Test()
  238. {
  239. var code = DateTime.Now.ToShortTimeString();
  240. await _snapshotApplication.SaveInvitationCodeAsync(new SaveInvitationCodeInDto { InvitationCode = code });
  241. var third = await _thirdAccountRepository.GetByOpenIdAsync(_sessionContext.OpenId);
  242. third.InvitationCode.ShouldBe(code);
  243. }
  244. [Fact]
  245. public async Task GetPractitionerItems_Test()
  246. {
  247. var items = await _snapshotApplication.GetPractitionerItemsAsync(new PractitionerItemInDto { AreaId = "510399" }, CancellationToken.None);
  248. items.Count.ShouldNotBe(0);
  249. var item = await _snapshotApplication.GetPractitionerDetailAsync(items.First().Id, CancellationToken.None);
  250. item.Street.ShouldNotBeNullOrEmpty();
  251. item.Name.ShouldNotBeNullOrEmpty();
  252. item.SystemAreaName.ShouldNotBeNullOrEmpty();
  253. item.SystemAreaName.ShouldNotBeNullOrEmpty();
  254. item.Gender.ShouldNotBe(EGender.Unknown);
  255. item.GenderTxt.ShouldNotBeNullOrEmpty();
  256. item.PhoneNumber.ShouldNotBeNullOrEmpty();
  257. }
  258. [Theory]
  259. [InlineData(ERedPackPickupStatus.Unreceived)]
  260. [InlineData(ERedPackPickupStatus.Received)]
  261. public async Task GetRedPacksAsync(ERedPackPickupStatus status)
  262. {
  263. for (int i = 0;i < 12;i++)
  264. {
  265. var now = DateTime.Now;
  266. var entity = new RedPackRecord
  267. {
  268. OrderId = "111111111",
  269. Amount = 10 * 10,
  270. CreationTime = new DateTime(2024, i + 1, 02, now.Hour, now.Minute, now.Second),
  271. WXOpenId = "测试生成的OpenId",
  272. PickupStatus = status,
  273. };
  274. await _redPackRecordRepository.AddAsync(entity);
  275. }
  276. var page = await _snapshotApplication.GetRedPacksAsync(new RedPacksInDto { Status = status }, CancellationToken.None);
  277. page.Count.ShouldNotBe(0, "数据不应该为空");
  278. }
  279. [Fact]
  280. public async Task GetBulletinsDetail_Test()
  281. {
  282. var detail = await _snapshotApplication.GetBulletinsDetailAsync("08dc788f-20f4-4bf1-83d3-b5a8a4f395b0");
  283. detail.Id.ShouldNotBeNullOrEmpty();
  284. detail.Title.ShouldNotBeNullOrEmpty();
  285. detail.Content.ShouldNotBeNullOrEmpty();
  286. }
  287. [Fact]
  288. public async Task InitRedPackDataAsync()
  289. {
  290. }
  291. /// <summary>
  292. /// 测试行业
  293. /// 测试添加数据是否和获取的数据一致
  294. /// </summary>
  295. /// <returns></returns>
  296. [Fact]
  297. public async Task Industry_Test()
  298. {
  299. var industry = new AddIndustryDto
  300. {
  301. Name = "测试行业",
  302. TitleSuffix = "测试标题",
  303. ApproveOrgId = "测试审批部门Id",
  304. ApproveOrgName = "测试审批部门名字",
  305. AcceptType = "测试受理类型",
  306. AcceptTypeCode = "测试受理类型代码",
  307. CitizenReadPackAmount = 100,
  308. GuiderReadPackAmount = 200,
  309. IsEnable = true,
  310. Files = new List<IndustryFileDto>
  311. {
  312. new IndustryFileDto
  313. {
  314. Name = "测试文件" + DateTime.Now.ToShortDateString(),
  315. Path = "测试文件地址" + DateTime.Now.ToShortTimeString(),
  316. FileName ="测试文件" + DateTime.Now.ToShortDateString() + ".doc",
  317. AdditionId = DateTime.Now.ToLongDateString()
  318. }
  319. }
  320. };
  321. var industryId = await _industryApplication.AddIndustryAsync(industry, CancellationToken.None);
  322. var pageDto = await _snapshotApplication.GetIndustryBaseAsync(industryId, CancellationToken.None);
  323. try
  324. {
  325. pageDto.ShouldNotBeNull();
  326. pageDto.Files.ShouldNotBeNull();
  327. foreach (var file in pageDto.Files)
  328. {
  329. file.Id.ShouldNotBeNullOrEmpty();
  330. file.Name.ShouldNotBeNullOrEmpty();
  331. file.Path.ShouldNotBeNullOrEmpty();
  332. file.FileName.ShouldNotBeNullOrEmpty();
  333. file.AdditionId.ShouldNotBeNullOrEmpty();
  334. file.Key.ShouldBe(industryId);
  335. }
  336. pageDto.Workplace.ShouldNotBeNull();
  337. pageDto.WorkplaceName.ShouldNotBeNull();
  338. }
  339. catch (Exception e)
  340. {
  341. // ignore
  342. }
  343. finally
  344. {
  345. await _industryRepository.Removeable().Where(m => m.Id == industryId).ExecuteCommandAsync();
  346. await _fileRepository.Removeable().Where(m => m.Id == pageDto.Files.First().Id).ExecuteCommandAsync();
  347. }
  348. }
  349. }