SnapshotControllerTest.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using AutoFixture;
  2. using EasyCaching.Core;
  3. using Hotline.Api.Controllers;
  4. using Hotline.Api.Controllers.Snapshot;
  5. using Hotline.Identity.Accounts;
  6. using Hotline.Identity.Roles;
  7. using Hotline.Orders;
  8. using Hotline.Settings;
  9. using Hotline.Share.Dtos.Snapshot;
  10. using Hotline.Share.Enums.Snapshot;
  11. using Hotline.Share.Tools;
  12. using Hotline.Snapshot.Interfaces;
  13. using Hotline.ThirdAccountDomainServices.Interfaces;
  14. using Hotline.Users;
  15. using Microsoft.AspNetCore.Http;
  16. using Microsoft.AspNetCore.Mvc;
  17. using Microsoft.Extensions.DependencyInjection;
  18. using Shouldly;
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Linq;
  22. using System.Text;
  23. using System.Threading.Tasks;
  24. using XF.Domain.Cache;
  25. using XF.Domain.Repository;
  26. namespace Hotline.Tests.Controller;
  27. public class SnapshotControllerTest : TestBase
  28. {
  29. private readonly SnapshotController _snapshotController;
  30. private readonly IOrderRepository _orderRepository;
  31. private readonly IOrderSnapshotRepository _orderSnapshotRepository;
  32. private readonly IIndustryRepository _industryRepository;
  33. private readonly IEasyCachingProvider _easyCaching;
  34. private readonly IRedisCachingProvider _redisCaching;
  35. public SnapshotControllerTest(IAccountRepository accountRepository, IRepository<Role> roleRepository, UserController userController, IServiceScopeFactory scopeFactory, IRepository<User> userRepository, IHttpContextAccessor httpContextAccessor, SnapshotController snapshotController, IOrderRepository orderRepository, IOrderSnapshotRepository orderSnapshotRepository, IIndustryRepository industryRepository, IThirdIdentiyService thirdService, IThirdAccountRepository thirdAccount, IEasyCachingProvider easyCaching, IRedisCachingProvider redisCaching, ITypedCache<SystemSetting> cacheSettingData) : base(accountRepository, roleRepository, userController, scopeFactory, userRepository, httpContextAccessor, thirdService, thirdAccount, cacheSettingData)
  36. {
  37. _snapshotController = snapshotController;
  38. _snapshotController.ControllerContext = new ControllerContext
  39. {
  40. HttpContext = new DefaultHttpContext()
  41. };
  42. _orderRepository = orderRepository;
  43. _orderSnapshotRepository = orderSnapshotRepository;
  44. _industryRepository = industryRepository;
  45. _easyCaching = easyCaching;
  46. _redisCaching = redisCaching;
  47. }
  48. [Fact]
  49. public async Task GetAreaTree_Test()
  50. {
  51. var result = await _snapshotController.GetAreaTreeAsync();
  52. result.ShouldNotBeNull();
  53. var zgs = result.Where(m => m.AreaName == "自贡市").FirstOrDefault();
  54. zgs.ShouldNotBeNull();
  55. var rx = zgs.Children.Where(m => m.AreaName == "荣县").FirstOrDefault();
  56. rx.ShouldNotBeNull();
  57. var zzz = rx.Children.Where(m => m.AreaName == "正紫镇").FirstOrDefault();
  58. zzz.ShouldNotBeNull();
  59. }
  60. /// <summary>
  61. /// 测试创建随手拍工单
  62. /// </summary>
  63. /// <returns></returns>
  64. [Fact]
  65. public async Task AddOrder_Test()
  66. {
  67. var cacheKey = "Hotline:CollectionJobType";
  68. await _easyCaching.RemoveAsync(cacheKey, CancellationToken.None);
  69. var exist = await _easyCaching.ExistsAsync(cacheKey);
  70. await _redisCaching.KeyDelAsync(cacheKey);
  71. var existRedis = await _redisCaching.KeyExistsAsync(cacheKey);
  72. var homePage = await _snapshotController.GetHomePageAsync();
  73. var industry = homePage.Industrys.Where(m => m.IndustryType == EIndustryType.Declare).FirstOrDefault();
  74. var pageBase = await _snapshotController.GetIndustryBaseAsync(industry.Id);
  75. var inDto = _fixture.Create<AddSnapshotOrderInDto>();
  76. inDto.Street = "单元测试街道" + DateTime.Now.ToLongDateTimeString();
  77. inDto.IndustryId = industry.Id;
  78. inDto.Town = "仙市镇";
  79. inDto.County = "沿滩区";
  80. inDto.Description = "单元测试添加的时间描述";
  81. inDto.IsSecret = false;
  82. inDto.JobType = 1;
  83. foreach (var item in inDto.Files)
  84. {
  85. item.FileName = DateTime.Now.ToShortTimeString() + "文件.doc";
  86. }
  87. var order = await _snapshotController.AddOrderAsync(inDto);
  88. var orderEntity = await _orderRepository.GetAsync(order.Id);
  89. orderEntity.ShouldNotBeNull();
  90. orderEntity.Latitude.ShouldBe(inDto.Latitude);
  91. orderEntity.Longitude.ShouldBe(inDto.Longitude);
  92. orderEntity.Title.ShouldNotBeNullOrEmpty();
  93. orderEntity.Content.ShouldNotBeNullOrEmpty();
  94. var orderSnapshotEntity = await _orderSnapshotRepository.GetAsync(order.Id);
  95. orderSnapshotEntity.ShouldNotBeNull();
  96. orderSnapshotEntity.JobType.ShouldBe(inDto.JobType);
  97. orderSnapshotEntity.JobTypeName.ShouldBe("气割");
  98. }
  99. }