SnapshotControllerTest.cs 4.4 KB

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