diff options
| Commit | 89f56a7e65d69a21635787840d952b2fef2b9955 (patch) | |
|---|---|---|
| Author | 2021-04-04 14:11:08 +0300 | |
| Committer | 2021-04-04 14:11:08 +0300 | |
| Files | a572f02666310bf60b4d60f3dd36046313ba11ed /src/Tests/PCC.Service.Tests | |
| pc-craft-main.tar pc-craft-main.tar.gz pc-craft-main.zip | ||
| Parent | 9f64941ab648a496fe73316c07311562625145af (diff) | |
* Added Unit Test Project for Services
* Added Unit Test file for PartService
* Added small validation functionality for CreateProcessorPart of PartService
* Added Unit Test for PartService CreateProcessorPart method
2 files changed, 130 insertions, 0 deletions
| -rw-r--r-- | src/Tests/PCC.Service.Tests/PCC.Service.Tests.csproj | 23 | ||||
| -rw-r--r-- | src/Tests/PCC.Service.Tests/PartsServiceTests.cs | 107 |
diff --git a/src/Tests/PCC.Service.Tests/PCC.Service.Tests.csproj b/src/Tests/PCC.Service.Tests/PCC.Service.Tests.csproj new file mode 100644 index 0000000..b339b14 --- /dev/null +++ b/src/Tests/PCC.Service.Tests/PCC.Service.Tests.csproj @@ -0,0 +1,23 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <TargetFramework>netcoreapp3.1</TargetFramework> + + <IsPackable>false</IsPackable> + </PropertyGroup> + + <ItemGroup> + <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.4" /> + <PackageReference Include="NUnit" Version="3.12.0" /> + <PackageReference Include="NUnit3TestAdapter" Version="3.16.1" /> + <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" /> + </ItemGroup> + + <ItemGroup> + <ProjectReference Include="..\..\Data\PCC.Data.Models\PCC.Data.Models.csproj" /> + <ProjectReference Include="..\..\Data\PCC.Data\PCC.Data.csproj" /> + <ProjectReference Include="..\..\Service\PCC.Services.Models\PCC.Services.Models.csproj" /> + <ProjectReference Include="..\..\Service\PCC.Services\PCC.Services.csproj" /> + </ItemGroup> + +</Project> diff --git a/src/Tests/PCC.Service.Tests/PartsServiceTests.cs b/src/Tests/PCC.Service.Tests/PartsServiceTests.cs new file mode 100644 index 0000000..46b8624 --- /dev/null +++ b/src/Tests/PCC.Service.Tests/PartsServiceTests.cs @@ -0,0 +1,107 @@ +namespace PCC.Service.Tests +{ + using AutoMapperConfiguration; + using Microsoft.EntityFrameworkCore; + using NUnit.Framework; + using PCC.Data; + using PCC.Data.Models; + using PCC.Services; + using PCC.Services.Models.Parts; + using System; + using System.Threading.Tasks; + + [TestFixture] + public class PartsServiceTests + { + private PCCDbContext pCCDbContext; + + private IPartsService partService; + + [SetUp] + public void Setup() + { + AutoMapperConfig.RegisterMappings( + typeof(PCCUser).Assembly.GetTypes(), + typeof(PartServiceModel).Assembly.GetTypes()); + + DbContextOptions<PCCDbContext> options = new DbContextOptionsBuilder<PCCDbContext>() + .UseInMemoryDatabase($"TESTS-DB-{Guid.NewGuid().ToString()}") + .Options; + + this.pCCDbContext = new PCCDbContext(options); + this.partService = new PartsService(this.pCCDbContext); + } + + [Test] + public async Task CreateProcessorPart_WithValidData_ShouldCorrectlyCreateEntity() + { + ProcessorServiceModel partServiceModel = new ProcessorServiceModel + { + Brand = "Intel", + Model = "Core I9", + Cores = 8, + Clockspeed = 4500, + ElectricalPowerConsumption = 3000, + Price = 2500, + Socket = "QWERTY", + ImageUrl = "https://never-gonna-give-you-up.com/rick.png", + }; + + Processor expectedEntity = new Processor + { + Brand = "Intel", + Model = "Core I9", + Cores = 8, + Clockspeed = 4500, + ElectricalPowerConsumption = 3000, + Price = 2500, + Socket = "QWERTY", + ImageUrl = "https://never-gonna-give-you-up.com/rick.png", + }; + + bool result = await this.partService.CreateProcessorPart(partServiceModel); + Processor actualEntity = await this.pCCDbContext.Processors.FirstOrDefaultAsync(); + + // TODO: Check if result is truthy (should be extracted into separate test) + Assert.True(result, "CreateProcessorPart() method should return truthy value with valid data."); + + // TODO: This can be a constant message with placeholder + Assert.AreEqual(expectedEntity.Brand, actualEntity.Brand, "CreateProcessorPart() method does not map Brand property correctly."); + Assert.AreEqual(expectedEntity.Model, actualEntity.Model, "CreateProcessorPart() method does not map Model property correctly."); + Assert.AreEqual(expectedEntity.Cores, actualEntity.Cores, "CreateProcessorPart() method does not map Cores property correctly."); + Assert.AreEqual(expectedEntity.Clockspeed, actualEntity.Clockspeed, "CreateProcessorPart() method does not map Clockspeed property correctly."); + Assert.AreEqual(expectedEntity.ElectricalPowerConsumption, actualEntity.ElectricalPowerConsumption, "CreateProcessorPart() method does not map ElectricalPowerConsumption property correctly."); + Assert.AreEqual(expectedEntity.Price, actualEntity.Price, "CreateProcessorPart() method does not map Price property correctly."); + Assert.AreEqual(expectedEntity.Socket, actualEntity.Socket, "CreateProcessorPart() method does not map Socket property correctly."); + Assert.AreEqual(expectedEntity.ImageUrl, actualEntity.ImageUrl, "CreateProcessorPart() method does not map ImageUrl property correctly."); + Assert.NotNull(actualEntity.Id, "CreateProcessorPart() method does not set Id property correctly."); + } + + [Test] + public async Task CreateProcessorPart_WithInvalidData_ShouldCorrectlyCreateEntity() + { + ProcessorServiceModel partServiceModel = new ProcessorServiceModel + { + Brand = null, + Model = "Core I9", + Cores = 8, + Clockspeed = 4500, + ElectricalPowerConsumption = 3000, + Price = 2500, + Socket = "QWERTY", + ImageUrl = "https://never-gonna-give-you-up.com/rick.png", + }; + + Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.partService.CreateProcessorPart(partServiceModel)); + + Assert.That(ex.Message == "Processor Brand cannot be null or empty!", "CreateProcessorPart() method with invalid Brand property, should throw ArgumentException with correct message."); + } + + [TearDown] + public void Dispose() + { + this.pCCDbContext.Dispose(); + this.partService = null; + } + } +} |