Advertisement
BbJLeB

Untitled

Apr 24th, 2024
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.75 KB | None | 0 0
  1. using CarGalleryConsoleAPI.Business;
  2. using CarGalleryConsoleAPI.Business.Contracts;
  3. using CarGalleryConsoleAPI.Data.Model;
  4. using CarGalleryConsoleAPI.DataAccess;
  5. using System.ComponentModel.DataAnnotations;
  6. using Moq;
  7. using CarGalleryConsoleAPI.DataAccess.Contracts;
  8. using CarGalleryConsoleAPI.Business;
  9. using Microsoft.EntityFrameworkCore;
  10.  
  11. namespace CarGalleryConsoleAPI.IntegrationTests.NUnit
  12. {
  13.     internal class IntegrationTests
  14.     {
  15.         private TestCarDbContext dbContext;
  16.         private ICarsManager carsManager;
  17.  
  18.         [SetUp]
  19.         public void SetUp()
  20.         {
  21.             this.dbContext = new TestCarDbContext();
  22.             this.carsManager = new CarsManager(new CarsRepository(this.dbContext));
  23.         }
  24.  
  25.         [TearDown]
  26.         public void TearDown()
  27.         {
  28.             this.dbContext.Database.EnsureDeleted();
  29.             this.dbContext.Dispose();
  30.         }
  31.  
  32.         //positive test
  33.         [Test]
  34.         public async Task AddCarAsync_ShouldAddNewCar()
  35.         {
  36.             // Arrange
  37.             var newCar = new Car
  38.             {
  39.                 CatalogNumber = "ABCD7894EFGH",
  40.                 Make = "Toyota",
  41.                 Model = "Camry",
  42.                 Color = "Blue",
  43.                 Year = 2022,
  44.                 Mileage = 5000,
  45.                 Price = 25000,
  46.                 Availability = true
  47.             };
  48.  
  49.             // Act
  50.             await this.carsManager.AddAsync(newCar);
  51.  
  52.             var car = await dbContext.Cars.FirstOrDefaultAsync(p => p.CatalogNumber == newCar.CatalogNumber);
  53.  
  54.             // Assert
  55.             Assert.That(car, Is.Not.Null);
  56.             Assert.That(car.CatalogNumber, Is.EqualTo(newCar.CatalogNumber));
  57.             Assert.That(car.Make, Is.EqualTo(newCar.Make));
  58.             Assert.That(car.Model, Is.EqualTo(newCar.Model));
  59.             Assert.That(car.Color, Is.EqualTo(newCar.Color));
  60.             Assert.That(car.Year, Is.EqualTo(newCar.Year));
  61.             Assert.That(car.Mileage, Is.EqualTo(newCar.Mileage));
  62.             Assert.That(car.Price, Is.EqualTo(newCar.Price));
  63.             Assert.That(car.Availability, Is.EqualTo(true));
  64.         }
  65.  
  66.         //Negative test
  67.         [Test]
  68.         public async Task AddCarAsync_TryToAddCarWithInvalidCredentials_ShouldThrowException()
  69.         {
  70.             // Arrange
  71.  
  72.             // Act
  73.             var exception = Assert.ThrowsAsync<ValidationException>(() => this.carsManager.AddAsync(new Car
  74.             {
  75.             }));
  76.  
  77.  
  78.  
  79.             // Assert
  80.             Assert.That(exception.Message, Is.EqualTo("Invalid car!"));
  81.  
  82.         }
  83.  
  84.         [Test]
  85.         public async Task DeleteCarAsync_WithValidCatalogNumber_ShouldRemoveCarFromDb()
  86.         {
  87.             // Arrange
  88.             var newCar = new Car
  89.             {
  90.                 CatalogNumber = "ABCD7894EFGH",
  91.                 Make = "Toyota",
  92.                 Model = "Camry",
  93.                 Color = "Blue",
  94.                 Year = 2022,
  95.                 Mileage = 5000,
  96.                 Price = 25000,
  97.                 Availability = true
  98.             };
  99.  
  100.             // Act
  101.             await this.carsManager.DeleteAsync(newCar.CatalogNumber);
  102.  
  103.             // Assert
  104.             Assert.That(dbContext.Cars.Count(), Is.EqualTo(0));
  105.             Assert.That(await dbContext.Cars.FirstOrDefaultAsync(p => p.CatalogNumber == newCar.CatalogNumber), Is.Null);
  106.         }
  107.  
  108.         [TestCase("")]
  109.         [TestCase("    ")]
  110.         [TestCase(null!)]
  111.         public async Task DeleteCarAsync_TryToDeleteWithNullOrWhiteSpaceCatalogNumber_ShouldThrowException(string nullOrWhitespaceNumber)
  112.         {
  113.             // Arrange
  114.  
  115.             // Act
  116.             var exception = Assert.ThrowsAsync<ArgumentException>(() => carsManager.DeleteAsync(nullOrWhitespaceNumber));
  117.  
  118.             // Assert
  119.             Assert.That(exception.Message, Is.EqualTo("Catalog number cannot be empty."));
  120.         }
  121.  
  122.         [Test]
  123.         public async Task GetAllAsync_WhenCarsExist_ShouldReturnAllCars()
  124.         {
  125.             // Arrange
  126.             var newCar = new Car
  127.             {
  128.                 CatalogNumber = "ABCD7894EFGH",
  129.                 Make = "Toyota",
  130.                 Model = "Camry",
  131.                 Color = "Blue",
  132.                 Year = 2022,
  133.                 Mileage = 5000,
  134.                 Price = 25000,
  135.                 Availability = true
  136.             };
  137.             await this.carsManager.AddAsync(newCar);
  138.  
  139.             // Act
  140.             var result = await this.carsManager.GetAllAsync();
  141.  
  142.             // Assert
  143.             Assert.That(result, Is.Not.Null);
  144.             Assert.That(result.Count(), Is.EqualTo(1));
  145.             Assert.That(result.First().CatalogNumber, Is.EqualTo(newCar.CatalogNumber));
  146.             Assert.That(result.First().Make, Is.EqualTo(newCar.Make));
  147.             Assert.That(result.First().Model, Is.EqualTo(newCar.Model));
  148.             Assert.That(result.First().Color, Is.EqualTo(newCar.Color));
  149.             Assert.That(result.First().Year, Is.EqualTo(newCar.Year));
  150.             Assert.That(result.First().Mileage, Is.EqualTo(newCar.Mileage));
  151.             Assert.That(result.First().Price, Is.EqualTo(newCar.Price));
  152.             Assert.That(result.First().Availability, Is.EqualTo(true));
  153.         }
  154.  
  155.         [Test]
  156.         public async Task GetAllAsync_WhenNoCarsExist_ShouldThrowKeyNotFoundException()
  157.         {
  158.             // Arrange
  159.  
  160.             // Act
  161.             var exception = Assert.ThrowsAsync<KeyNotFoundException>(() => carsManager.GetAllAsync());
  162.  
  163.             // Assert
  164.             Assert.That(exception.Message, Is.EqualTo("No car found."));
  165.         }
  166.  
  167.         [Test]
  168.         public async Task SearchByModelAsync_WithExistingModel_ShouldReturnMatchingCars()
  169.         {
  170.             // Arrange
  171.             var newCar = new Car
  172.             {
  173.                 CatalogNumber = "ABCD7894EFGH",
  174.                 Make = "Toyota",
  175.                 Model = "Camry",
  176.                 Color = "Blue",
  177.                 Year = 2022,
  178.                 Mileage = 5000,
  179.                 Price = 25000,
  180.                 Availability = true
  181.             };
  182.             await this.carsManager.AddAsync(newCar);
  183.  
  184.             // Act
  185.             var result = await this.carsManager.SearchByModelAsync(newCar.Model);
  186.  
  187.             // Assert
  188.             Assert.That(result, Is.Not.Null);
  189.             Assert.That(result.Count(), Is.EqualTo(1));
  190.             Assert.That(result.First().CatalogNumber, Is.EqualTo(newCar.CatalogNumber));
  191.             Assert.That(result.First().Make, Is.EqualTo(newCar.Make));
  192.             Assert.That(result.First().Model, Is.EqualTo(newCar.Model));
  193.             Assert.That(result.First().Color, Is.EqualTo(newCar.Color));
  194.             Assert.That(result.First().Year, Is.EqualTo(newCar.Year));
  195.             Assert.That(result.First().Mileage, Is.EqualTo(newCar.Mileage));
  196.             Assert.That(result.First().Price, Is.EqualTo(newCar.Price));
  197.             Assert.That(result.First().Availability, Is.EqualTo(true));
  198.         }
  199.  
  200.         [Test]
  201.         public async Task SearchByModelAsync_WithNonExistingModel_ShouldThrowKeyNotFoundException()
  202.         {
  203.             // Arrange
  204.  
  205.             // Act
  206.             var exception = Assert.ThrowsAsync<KeyNotFoundException>(() => carsManager.SearchByModelAsync("Moskvich"));
  207.  
  208.             // Assert
  209.             Assert.That(exception.Message, Is.EqualTo("No car found with the given model."));
  210.         }
  211.  
  212.         [Test]
  213.         public async Task GetSpecificAsync_WithValidCatalogNumber_ShouldReturnCar()
  214.         {
  215.             // Arrange
  216.             var newCar = new Car
  217.             {
  218.                 CatalogNumber = "ABCD7894EFGH",
  219.                 Make = "Toyota",
  220.                 Model = "Camry",
  221.                 Color = "Blue",
  222.                 Year = 2022,
  223.                 Mileage = 5000,
  224.                 Price = 25000,
  225.                 Availability = true
  226.             };
  227.             await this.carsManager.AddAsync(newCar);
  228.  
  229.             // Act
  230.             var result = await this.carsManager.GetSpecificAsync(newCar.CatalogNumber);
  231.  
  232.             // Assert
  233.             Assert.That(result, Is.Not.Null);
  234.             Assert.That(result.CatalogNumber, Is.EqualTo(newCar.CatalogNumber));
  235.             Assert.That(result.Make, Is.EqualTo(newCar.Make));
  236.             Assert.That(result.Model, Is.EqualTo(newCar.Model));
  237.             Assert.That(result.Color, Is.EqualTo(newCar.Color));
  238.             Assert.That(result.Year, Is.EqualTo(newCar.Year));
  239.             Assert.That(result.Mileage, Is.EqualTo(newCar.Mileage));
  240.             Assert.That(result.Price, Is.EqualTo(newCar.Price));
  241.             Assert.That(result.Availability, Is.EqualTo(true));
  242.         }
  243.  
  244.         [Test]
  245.         public async Task GetSpecificAsync_WithInvalidCatalogNumber_ShouldThrowKeyNotFoundException()
  246.         {
  247.             // Arrange
  248.  
  249.             // Act
  250.             var exception = Assert.ThrowsAsync<KeyNotFoundException>(() => carsManager.GetSpecificAsync("ABCD7894"));
  251.  
  252.             // Assert
  253.             Assert.That(exception.Message, Is.EqualTo("No car found with catalog number: ABCD7894"));
  254.         }
  255.  
  256.         [Test]
  257.         public async Task UpdateAsync_WithValidCar_ShouldUpdateCar()
  258.         {
  259.             // Arrange
  260.             var newCar = new Car
  261.             {
  262.                 CatalogNumber = "ABCD7894EFGH",
  263.                 Make = "Toyota",
  264.                 Model = "Camry",
  265.                 Color = "Blue",
  266.                 Year = 2022,
  267.                 Mileage = 5000,
  268.                 Price = 25000,
  269.                 Availability = true
  270.             };
  271.             await this.carsManager.AddAsync(newCar);
  272.  
  273.             // Act
  274.             var updatedCar = await dbContext.Cars.FirstOrDefaultAsync(p => p.CatalogNumber == newCar.CatalogNumber);
  275.             updatedCar.Color = "Red";
  276.  
  277.             // Assert
  278.             Assert.That(updatedCar, Is.Not.Null);
  279.             Assert.That(updatedCar.CatalogNumber, Is.EqualTo(newCar.CatalogNumber));
  280.             Assert.That(updatedCar.Make, Is.EqualTo(newCar.Make));
  281.             Assert.That(updatedCar.Model, Is.EqualTo(newCar.Model));
  282.             Assert.That(updatedCar.Color, Is.EqualTo("Red"));
  283.             Assert.That(updatedCar.Year, Is.EqualTo(newCar.Year));
  284.             Assert.That(updatedCar.Mileage, Is.EqualTo(newCar.Mileage));
  285.             Assert.That(updatedCar.Price, Is.EqualTo(newCar.Price));
  286.             Assert.That(updatedCar.Availability, Is.EqualTo(true));
  287.         }
  288.  
  289.         [Test]
  290.         public async Task UpdateAsync_WithInvalidCar_ShouldThrowValidationException()
  291.         {
  292.             // Arrange
  293.  
  294.             // Act
  295.             var exception = Assert.ThrowsAsync<ValidationException>(() => carsManager.UpdateAsync(new Car()));
  296.  
  297.             // Assert
  298.             Assert.That(exception.Message, Is.EqualTo("Invalid car!"));
  299.         }
  300.  
  301.     }
  302. }
  303.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement