Advertisement
Iv555

Untitled

Dec 15th, 2022
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.49 KB | None | 0 0
  1. namespace ExpressEaglesCourier.Services.Data.Tests
  2. {
  3. using System;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6.  
  7. using ExpressEaglesCourier.Data;
  8. using ExpressEaglesCourier.Data.Models;
  9. using ExpressEaglesCourier.Data.Repositories;
  10. using ExpressEaglesCourier.Services.Data.Customers;
  11. using ExpressEaglesCourier.Web.ViewModels.Customers;
  12. using Microsoft.EntityFrameworkCore;
  13. using Xunit;
  14.  
  15. public class CustomerServiceTests
  16. {
  17. public ApplicationDbContext GetDbContext()
  18. {
  19. DbContextOptionsBuilder<ApplicationDbContext> optionBuilder = new DbContextOptionsBuilder<ApplicationDbContext>()
  20. .UseInMemoryDatabase("TestCustomer");
  21. ApplicationDbContext dbContext = new ApplicationDbContext(optionBuilder.Options);
  22. return dbContext;
  23. }
  24.  
  25. public CustomerService GetCustomerService()
  26. {
  27. EfDeletableEntityRepository<Customer> customerRepo = new EfDeletableEntityRepository<Customer>(this.GetDbContext());
  28.  
  29. CustomerService customerService = new CustomerService(customerRepo);
  30. return customerService;
  31. }
  32.  
  33. public CustomerFormModel GetCustomerFormModel()
  34. {
  35. CustomerFormModel model = new CustomerFormModel()
  36. {
  37. Id = "74d8ad86-ef7a-49bf-b435-96f263b0ed2d",
  38. FirstName = "Gosho",
  39. MiddleName = "Goshev",
  40. LastName = "Goshev",
  41. Address = "Lazur block 33",
  42. City = "Bourgas",
  43. Country = "Bulgaria",
  44. CompanyName = string.Empty,
  45. PhoneNumber = "00359889124567",
  46. };
  47.  
  48. return model;
  49. }
  50.  
  51. [Fact]
  52. public async Task CheckWhetherNewCustomerIsAdded()
  53. {
  54. await this.GetCustomerService().CreateCustomerAsync(this.GetCustomerFormModel());
  55.  
  56. Assert.True(this.GetDbContext().Customers.Any(x => x.FirstName == "Gosho"));
  57. }
  58.  
  59. [Fact]
  60. public async Task GetCustomerByIdTest()
  61. {
  62. await this.GetCustomerService().CreateCustomerAsync(this.GetCustomerFormModel());
  63.  
  64. Customer customer = await this.GetDbContext().Customers.FirstOrDefaultAsync();
  65. Customer customerService = await this.GetCustomerService().GetCustomerById(customer.Id);
  66.  
  67. Assert.Equal(customer.Id, customerService.Id);
  68. Assert.Equal(customer.FirstName, customerService.FirstName);
  69. }
  70.  
  71. [Fact]
  72.  
  73. public async Task GetCustomerDetailsByIdTest()
  74. {
  75. await this.GetCustomerService().CreateCustomerAsync(this.GetCustomerFormModel());
  76. Customer customer = await this.GetDbContext().Customers.FirstOrDefaultAsync();
  77.  
  78. CustomerDetailsViewModel model = await this.GetCustomerService().GetCustomerDetailsById(customer.Id);
  79.  
  80. Assert.Equal(customer.Address + ", " + customer.City + ", " + customer.Country, model.FullAddress);
  81. Assert.Equal(customer.FirstName + " " + customer.LastName, model.FullName);
  82. }
  83.  
  84. [Fact]
  85.  
  86. public async Task GetCustomerForEditAsyncTest()
  87. {
  88. await this.GetCustomerService().CreateCustomerAsync(this.GetCustomerFormModel());
  89. Customer customer = await this.GetDbContext().Customers.FirstOrDefaultAsync();
  90.  
  91. CustomerFormModel model = await this.GetCustomerService()
  92. .GetCustomerForEditAsync(customer.Id);
  93.  
  94. Assert.Equal(customer.FirstName, model.FirstName);
  95. Assert.Equal(customer.Country, model.Country);
  96. }
  97.  
  98. [Fact]
  99. public async Task GetCustomerForEditExceptionTest()
  100. {
  101. Customer customer = await this.GetDbContext().Customers.Where(x => x.FirstName == "Martin").FirstOrDefaultAsync();
  102.  
  103. await Assert.ThrowsAsync<NullReferenceException>(() =>
  104. this.GetCustomerService().GetCustomerForEditAsync(customer.Id));
  105. }
  106.  
  107. [Fact]
  108. public async Task EditCustomerAsyncTest()
  109. {
  110. CustomerFormModel model1 = new CustomerFormModel()
  111. {
  112. Id = "82c5d8b6-2a6d-4756-aeb1-de696291deaf",
  113. FirstName = "Pesho",
  114. MiddleName = "Peshev",
  115. LastName = "Peshev",
  116. Address = "Zornitsa block 15",
  117. City = "Bourgas",
  118. Country = "Bulgaria",
  119. CompanyName = string.Empty,
  120. PhoneNumber = "00359888111111",
  121. };
  122.  
  123. await this.GetCustomerService().CreateCustomerAsync(model1);
  124.  
  125. Customer customer = await this.GetDbContext().Customers.LastOrDefaultAsync();
  126.  
  127. CustomerFormModel model2 = await this.GetCustomerService().GetCustomerForEditAsync(customer.Id);
  128.  
  129. await this.GetCustomerService().EditCustomerAsync(new CustomerFormModel()
  130. {
  131. Id = model2.Id,
  132. FirstName = model2.FirstName,
  133. MiddleName = model2.MiddleName,
  134. LastName = "Goshev",
  135. Address = model2.Address,
  136. City = model2.City,
  137. Country = model2.Country,
  138. CompanyName = model2.CompanyName,
  139. PhoneNumber = model2.PhoneNumber,
  140. });
  141.  
  142. Customer customerNew = await this.GetDbContext().Customers.LastOrDefaultAsync();
  143.  
  144. Assert.Equal("Goshev", customerNew.LastName);
  145. }
  146.  
  147. [Fact]
  148.  
  149. public async Task DeleteCustomerAsyncTest()
  150. {
  151. CustomerFormModel model = new CustomerFormModel()
  152. {
  153. Id = "82c5d8b6-2a6d-4756-aeb1-de696291deaf",
  154. FirstName = "Pesho",
  155. MiddleName = "Peshev",
  156. LastName = "Peshev",
  157. Address = "Zornitsa block 15",
  158. City = "Bourgas",
  159. Country = "Bulgaria",
  160. CompanyName = string.Empty,
  161. PhoneNumber = "00359888111111",
  162. };
  163.  
  164. await this.GetCustomerService().CreateCustomerAsync(model);
  165.  
  166. Customer customer = await this.GetDbContext().Customers.LastOrDefaultAsync();
  167.  
  168. await this.GetCustomerService().DeleteCustomerAsync(customer.Id);
  169.  
  170. Assert.False(await this.GetDbContext().Customers
  171. .AnyAsync(x => x.Id == "82c5d8b6-2a6d-4756-aeb1-de696291deaf"));
  172. }
  173. }
  174. }
  175.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement