SnapshotControllerTest.cs 4.6 KB

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