From 6eb7432507242b8c38873ace047f58c37beabfda Mon Sep 17 00:00:00 2001 From: "Yada.ArunKumar" Date: Wed, 10 Jan 2024 06:30:10 +0000 Subject: [PATCH 01/17] added tests for SimplCommerce.Module.ProductComparison -ComparingProductController --- SimplCommerce.sln | 15 + .../ComparingProductControllerTests.cs | 275 ++++++++++++++++++ ...erce.Module.ProductComparison.Tests.csproj | 32 ++ 3 files changed, 322 insertions(+) create mode 100644 test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs create mode 100644 test/SimplCommerce.Module.ProductComparison.Tests/SimplCommerce.Module.ProductComparison.Tests.csproj diff --git a/SimplCommerce.sln b/SimplCommerce.sln index 2c5362143..4581d34d7 100644 --- a/SimplCommerce.sln +++ b/SimplCommerce.sln @@ -136,6 +136,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimplCommerce.Module.Paymen EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimplCommerce.Module.Checkouts", "src\Modules\SimplCommerce.Module.Checkouts\SimplCommerce.Module.Checkouts.csproj", "{4473538D-2BFA-4C53-B642-0D0DC4F16863}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimplCommerce.Module.ProductComparison.Tests", "test\SimplCommerce.Module.ProductComparison.Tests\SimplCommerce.Module.ProductComparison.Tests.csproj", "{EEC02E89-E89A-4871-8C3E-4BABA1E56CA7}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -710,6 +712,18 @@ Global {4473538D-2BFA-4C53-B642-0D0DC4F16863}.Release|x64.Build.0 = Release|Any CPU {4473538D-2BFA-4C53-B642-0D0DC4F16863}.Release|x86.ActiveCfg = Release|Any CPU {4473538D-2BFA-4C53-B642-0D0DC4F16863}.Release|x86.Build.0 = Release|Any CPU + {EEC02E89-E89A-4871-8C3E-4BABA1E56CA7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EEC02E89-E89A-4871-8C3E-4BABA1E56CA7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EEC02E89-E89A-4871-8C3E-4BABA1E56CA7}.Debug|x64.ActiveCfg = Debug|Any CPU + {EEC02E89-E89A-4871-8C3E-4BABA1E56CA7}.Debug|x64.Build.0 = Debug|Any CPU + {EEC02E89-E89A-4871-8C3E-4BABA1E56CA7}.Debug|x86.ActiveCfg = Debug|Any CPU + {EEC02E89-E89A-4871-8C3E-4BABA1E56CA7}.Debug|x86.Build.0 = Debug|Any CPU + {EEC02E89-E89A-4871-8C3E-4BABA1E56CA7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EEC02E89-E89A-4871-8C3E-4BABA1E56CA7}.Release|Any CPU.Build.0 = Release|Any CPU + {EEC02E89-E89A-4871-8C3E-4BABA1E56CA7}.Release|x64.ActiveCfg = Release|Any CPU + {EEC02E89-E89A-4871-8C3E-4BABA1E56CA7}.Release|x64.Build.0 = Release|Any CPU + {EEC02E89-E89A-4871-8C3E-4BABA1E56CA7}.Release|x86.ActiveCfg = Release|Any CPU + {EEC02E89-E89A-4871-8C3E-4BABA1E56CA7}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -765,6 +779,7 @@ Global {14586564-62CC-4117-AC1B-858ED53C2D6C} = {7EFA2FA7-32DD-4047-B021-50E77A83D714} {E30CF10F-FABF-4917-8BEB-CB81E4CE2C92} = {7EFA2FA7-32DD-4047-B021-50E77A83D714} {4473538D-2BFA-4C53-B642-0D0DC4F16863} = {7EFA2FA7-32DD-4047-B021-50E77A83D714} + {EEC02E89-E89A-4871-8C3E-4BABA1E56CA7} = {D9FD9ABA-AE5E-4427-AA6B-6285BE2E212D} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {B9D0D8F0-1AB9-44DD-839F-ED8CEE7DDB10} diff --git a/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs b/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs new file mode 100644 index 000000000..2bc37de4e --- /dev/null +++ b/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs @@ -0,0 +1,275 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Mvc; +using Moq; +using SimplCommerce.Infrastructure.Data; +using SimplCommerce.Module.Catalog.Models; +using SimplCommerce.Module.Catalog.Services; +using SimplCommerce.Module.Core.Extensions; +using SimplCommerce.Module.Core.Models; +using SimplCommerce.Module.Core.Services; +using SimplCommerce.Module.ProductComparison.Areas.ProductComparison.Controllers; +using SimplCommerce.Module.ProductComparison.Areas.ProductComparison.ViewModels; +using SimplCommerce.Module.ProductComparison.Models; +using SimplCommerce.Module.ProductComparison.Services; +using Xunit; +namespace SimplCommerce.Module.ProductComparison.Tests.Controllers +{ + public class ComparingProductControllerTests + { + #region PositiveTestCases + [Fact] + public async Task AddToComparison_SuccessfullyAddsProduct() + { + // Arrange + var userId = 1; // Use a unique user id + var productId = 1001; + var model = new AddToComparisonModel { ProductId = productId }; + var repositoryMock = new Mock>(); + var comparingProductServiceMock = new Mock(); + var productPricingServiceMock = new Mock(); + var mediaServiceMock = new Mock(); + var contentLocalizationServiceMock = new Mock(); + var workContextMock = new Mock(); + var userManagerMock = new Mock>( + Mock.Of>(), null, null, null, null, null, null, null, null); + var controller = new ComparingProductController( + userManagerMock.Object, + repositoryMock.Object, + comparingProductServiceMock.Object, + productPricingServiceMock.Object, + mediaServiceMock.Object, + contentLocalizationServiceMock.Object, + workContextMock.Object + ); + var user = new User() + { + Id = userId, + UserName = "TestUser" + }; + workContextMock.Setup(repo => repo.GetCurrentUser()).ReturnsAsync(user); + // Act + var result = await controller.AddToComparison(model); + // Assert + comparingProductServiceMock.Verify(s => s.AddToComparison(userId, productId), Times.Once); + Assert.IsType(result); + } + + [Fact] + public async Task Remove_ValidProduct_RemovesProduct() + { + // Arrange + var userId = 1; + var productId = 1001; + var repositoryMock = new Mock>(); + repositoryMock.Setup(r => r.Query()).Returns(new[] + { + new ComparingProduct { UserId = userId, ProductId = productId } + }.AsQueryable()); + var comparingProductServiceMock = new Mock(); + var productPricingServiceMock = new Mock(); + var mediaServiceMock = new Mock(); + var contentLocalizationServiceMock = new Mock(); + var workContextMock = new Mock(); + var userManagerMock = new Mock>( + Mock.Of>(), null, null, null, null, null, null, null, null); + var controller = new ComparingProductController( + userManagerMock.Object, + repositoryMock.Object, + comparingProductServiceMock.Object, + productPricingServiceMock.Object, + mediaServiceMock.Object, + contentLocalizationServiceMock.Object, + workContextMock.Object + ); + var user = new User() + { + Id = userId, + UserName = "TestUser" + }; + workContextMock.Setup(repo => repo.GetCurrentUser()).ReturnsAsync(user); + // Act + var result = await controller.Remove(productId); + // Assert + repositoryMock.Verify(r => r.Remove(It.IsAny()), Times.Once); + repositoryMock.Verify(r => r.SaveChanges(), Times.Once); + Assert.IsType(result); + } + + [Fact] + public async Task Index_ReturnsValidViewModel() + { + // Arrange + var userId = 1; // Use a unique user id + var comparingProducts = new List() + { + new ComparingProduct + { + UserId = userId, + ProductId = 1001, + CreatedOn = DateTime.Now, + Product=new Product + { + AttributeValues={ new ProductAttributeValue() + { + AttributeId=1, + Attribute= new ProductAttribute(){Name="test1"} + }, + new ProductAttributeValue() + { + AttributeId=2, + Attribute= new ProductAttribute(){Name="test2"} + }, + new ProductAttributeValue() + { + AttributeId=3, + Attribute= new ProductAttribute(){Name="test3"} + },} + } + }, + new ComparingProduct + { + UserId = userId, + ProductId = 1002 , + CreatedOn = DateTime.Now, + Product=new Product + { + AttributeValues={ + new ProductAttributeValue() + { + AttributeId=1, + Attribute= new ProductAttribute(){Name="test1"} + }, + new ProductAttributeValue() + { + AttributeId=2, + Attribute= new ProductAttribute(){Name="test2"} + }, + new ProductAttributeValue() + { + AttributeId=3, + Attribute= new ProductAttribute(){Name="test3"} + }, + } + } + }, + }; + var repositoryMock = new Mock>(); + repositoryMock.Setup(r => r.Query()).Returns(comparingProducts.AsQueryable()); + var comparingProductServiceMock = new Mock(); + var productPricingServiceMock = new Mock(); + var mediaServiceMock = new Mock(); + var contentLocalizationServiceMock = new Mock(); + contentLocalizationServiceMock.Setup(s => s.GetLocalizedProperty(It.IsAny(), It.IsAny(), It.IsAny())) + .Returns((obj, propertyName, defaultValue) => defaultValue); + var workContextMock = new Mock(); + var userManagerMock = new Mock>( + Mock.Of>(), null, null, null, null, null, null, null, null); + workContextMock.Setup(w => w.GetCurrentUser()).ReturnsAsync(new User { Id = userId }); + var controller = new ComparingProductController( + userManagerMock.Object, + repositoryMock.Object, + comparingProductServiceMock.Object, + productPricingServiceMock.Object, + mediaServiceMock.Object, + contentLocalizationServiceMock.Object, + workContextMock.Object + ); + // Act + var result = await controller.Index(); + // Assert + var viewResult = Assert.IsType(result); + var viewModel = Assert.IsType(viewResult.Model); + Assert.Equal(comparingProducts.Count, viewModel.Products.Count); + Assert.NotEmpty(viewModel.Attributes); + } + #endregion + + #region NegativeTestCases + [Fact] + public async Task AddToComparison_TooManyComparingProducts_ReturnsErrorMessage() + { + // Arrange + var userId = 1; + var productId = 1001; + var model = new AddToComparisonModel { ProductId = productId }; + var repositoryMock = new Mock>(); + var comparingProductServiceMock = new Mock(); + comparingProductServiceMock.Setup(s => s.AddToComparison(userId, productId)) + .Throws(new TooManyComparingProductException(4)); + var productPricingServiceMock = new Mock(); + var mediaServiceMock = new Mock(); + var contentLocalizationServiceMock = new Mock(); + var workContextMock = new Mock(); + var userManagerMock = new Mock>( + Mock.Of>(), null, null, null, null, null, null, null, null); + var controller = new ComparingProductController( + userManagerMock.Object, + repositoryMock.Object, + comparingProductServiceMock.Object, + productPricingServiceMock.Object, + mediaServiceMock.Object, + contentLocalizationServiceMock.Object, + workContextMock.Object + ); + var user = new User() + { + Id = userId, + UserName = "TestUser" + }; + workContextMock.Setup(repo => repo.GetCurrentUser()).ReturnsAsync(user); + // Act + var result = await controller.AddToComparison(model); + // Assert + comparingProductServiceMock.Verify(s => s.AddToComparison(It.IsAny(), It.IsAny()), Times.Once); + var viewResult = Assert.IsType(result); + var viewModel = Assert.IsType(viewResult.Model); + Assert.Contains("Can not add to comparison items", viewModel.Message); + } + + [Fact] + public async Task Remove_InvalidProduct_ReturnsNotFound() + { + // Arrange + var userId = 1; + var productId = 1001; + var repositoryMock = new Mock>(); + repositoryMock.Setup(r => r.Query()).Returns(new[] + { + new ComparingProduct { UserId = userId, ProductId = 999 } + }.AsQueryable()); + var comparingProductServiceMock = new Mock(); + var productPricingServiceMock = new Mock(); + var mediaServiceMock = new Mock(); + var contentLocalizationServiceMock = new Mock(); + var workContextMock = new Mock(); + var userManagerMock = new Mock>( + Mock.Of>(), null, null, null, null, null, null, null, null); + var controller = new ComparingProductController( + userManagerMock.Object, + repositoryMock.Object, + comparingProductServiceMock.Object, + productPricingServiceMock.Object, + mediaServiceMock.Object, + contentLocalizationServiceMock.Object, + workContextMock.Object + ); + var user = new User() + { + Id = userId, + UserName = "TestUser" + }; + workContextMock.Setup(repo => repo.GetCurrentUser()).ReturnsAsync(user); + // Act + var result = await controller.Remove(productId); + // Assert + repositoryMock.Verify(r => r.Remove(It.IsAny()), Times.Never); + repositoryMock.Verify(r => r.SaveChanges(), Times.Never); + Assert.IsType(result); + } + #endregion + } +} diff --git a/test/SimplCommerce.Module.ProductComparison.Tests/SimplCommerce.Module.ProductComparison.Tests.csproj b/test/SimplCommerce.Module.ProductComparison.Tests/SimplCommerce.Module.ProductComparison.Tests.csproj new file mode 100644 index 000000000..3b9a311ed --- /dev/null +++ b/test/SimplCommerce.Module.ProductComparison.Tests/SimplCommerce.Module.ProductComparison.Tests.csproj @@ -0,0 +1,32 @@ + + + + net8.0 + false + true + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + + + + + From 3b9e6b15017b336362cbbbf830200490a21afc4c Mon Sep 17 00:00:00 2001 From: "Yada.ArunKumar" Date: Wed, 10 Jan 2024 06:33:50 +0000 Subject: [PATCH 02/17] added tests for SimplCommerce.Module.ProductComparison - ComparingProductService --- .../Services/ComparingProductServiceTests.cs | 143 ++++++++++++++++++ ...erce.Module.ProductComparison.Tests.csproj | 1 + 2 files changed, 144 insertions(+) create mode 100644 test/SimplCommerce.Module.ProductComparison.Tests/Services/ComparingProductServiceTests.cs diff --git a/test/SimplCommerce.Module.ProductComparison.Tests/Services/ComparingProductServiceTests.cs b/test/SimplCommerce.Module.ProductComparison.Tests/Services/ComparingProductServiceTests.cs new file mode 100644 index 000000000..c8f63225a --- /dev/null +++ b/test/SimplCommerce.Module.ProductComparison.Tests/Services/ComparingProductServiceTests.cs @@ -0,0 +1,143 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Moq; +using SimplCommerce.Infrastructure.Data; +using SimplCommerce.Module.Catalog.Models; +using SimplCommerce.Module.ProductComparison.Models; +using SimplCommerce.Module.ProductComparison.Services; +using Xunit; +namespace SimplCommerce.Module.ProductComparison.Tests.Services +{ + public class ComparingProductServiceTests + { + #region PositiveTestCases + [Fact] + public void AddToComparison_SuccessfullyAddsProduct() + { + // Arrange + var userId = 1; + var productId = 1001; + var repositoryMock = new Mock>(); + var service = new ComparingProductService(repositoryMock.Object); + + // Act + service.AddToComparison(userId, productId); + + // Assert + repositoryMock.Verify(r => r.Add(It.IsAny()), Times.Once); + repositoryMock.Verify(r => r.SaveChanges(), Times.Once); + } + + [Fact] + public void MigrateComparingProduct_SuccessfullyMigratesProducts() + { + // Arrange + var fromUserId = 1; + var toUserId = 2; + var repositoryMock = new Mock>(); + var comparingProducts = new List() + { + new ComparingProduct { UserId = fromUserId, ProductId = 1001 }, + new ComparingProduct { UserId = fromUserId, ProductId = 1002 }, + }; + repositoryMock.Setup(r => r.Query()).Returns(comparingProducts.AsQueryable()); + var service = new ComparingProductService(repositoryMock.Object); + + // Act + service.MigrateComparingProduct(fromUserId, toUserId); + + // Assert + repositoryMock.Verify(r => r.SaveChanges(), Times.Once); + Assert.All(comparingProducts, cp => Assert.Equal(toUserId, cp.UserId)); + } + #endregion + + #region NegativeTestCases + [Fact] + public void AddToComparison_TooManyComparingProducts_ThrowsException() + { + // Arrange + var userId = 1; + var productId = 1001; + var repositoryMock = new Mock>(); + var listComparingProducts = new List() + { + new ComparingProduct() + { + ProductId = 1001, + Product=new Product() + { + Name="TestProduct" + }, + CreatedOn = DateTime.Now, + UserId=1 + }, + new ComparingProduct() + { + ProductId = 1002, + Product=new Product() + { + Name="TestProduct1" + }, + CreatedOn = DateTime.Now, + UserId=1 + }, + new ComparingProduct() + { + ProductId = 1003, + Product=new Product() + { + Name="TestProduct2" + }, + CreatedOn = DateTime.Now, + UserId=1 + }, + new ComparingProduct() + { + ProductId = 1004, + Product=new Product() + { + Name="TestProduct3" + }, + CreatedOn = DateTime.Now, + UserId=1 + }, + }; + repositoryMock.Setup(r => r.Query()).Returns(listComparingProducts.AsQueryable); + var service = new ComparingProductService(repositoryMock.Object); + + // Act & Assert + Assert.Throws(() => service.AddToComparison(userId, productId)); + } + + [Fact] + public void AddToComparison_ProductAlreadyExists_DoesNotAddAgain() + { + // Arrange + var comparingProduct = new ComparingProduct() + { + ProductId = 1001, + Product = new Product() + { + Name = "TestProduct" + }, + CreatedOn = DateTime.Now, + UserId = 1 + }; + var userId = 1; + var productId = 1001; + var repositoryMock = new Mock>(); + repositoryMock.Setup(r => r.Query()).Returns(new List { comparingProduct }.AsQueryable); + var service = new ComparingProductService(repositoryMock.Object); + + // Act + service.AddToComparison(userId, productId); + + // Assert + repositoryMock.Verify(r => r.Add(comparingProduct), Times.Never); + repositoryMock.Verify(r => r.SaveChanges(), Times.Never); + } + #endregion + } +} diff --git a/test/SimplCommerce.Module.ProductComparison.Tests/SimplCommerce.Module.ProductComparison.Tests.csproj b/test/SimplCommerce.Module.ProductComparison.Tests/SimplCommerce.Module.ProductComparison.Tests.csproj index 3b9a311ed..45aa47759 100644 --- a/test/SimplCommerce.Module.ProductComparison.Tests/SimplCommerce.Module.ProductComparison.Tests.csproj +++ b/test/SimplCommerce.Module.ProductComparison.Tests/SimplCommerce.Module.ProductComparison.Tests.csproj @@ -27,6 +27,7 @@ + From 914060c7c2ad052839e4c81ef2fb6f949846835b Mon Sep 17 00:00:00 2001 From: Arun kumar <49372861+Arunkumar0610@users.noreply.github.com> Date: Thu, 11 Jan 2024 10:14:14 +0530 Subject: [PATCH 03/17] Update ComparingProductControllerTests.cs --- .../Controllers/ComparingProductControllerTests.cs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs b/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs index 2bc37de4e..3ac9d1559 100644 --- a/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs +++ b/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs @@ -20,7 +20,6 @@ namespace SimplCommerce.Module.ProductComparison.Tests.Controllers { public class ComparingProductControllerTests { - #region PositiveTestCases [Fact] public async Task AddToComparison_SuccessfullyAddsProduct() { @@ -51,8 +50,10 @@ public async Task AddToComparison_SuccessfullyAddsProduct() UserName = "TestUser" }; workContextMock.Setup(repo => repo.GetCurrentUser()).ReturnsAsync(user); + // Act var result = await controller.AddToComparison(model); + // Assert comparingProductServiceMock.Verify(s => s.AddToComparison(userId, productId), Times.Once); Assert.IsType(result); @@ -91,8 +92,10 @@ public async Task Remove_ValidProduct_RemovesProduct() UserName = "TestUser" }; workContextMock.Setup(repo => repo.GetCurrentUser()).ReturnsAsync(user); + // Act var result = await controller.Remove(productId); + // Assert repositoryMock.Verify(r => r.Remove(It.IsAny()), Times.Once); repositoryMock.Verify(r => r.SaveChanges(), Times.Once); @@ -178,17 +181,17 @@ public async Task Index_ReturnsValidViewModel() contentLocalizationServiceMock.Object, workContextMock.Object ); + // Act var result = await controller.Index(); + // Assert var viewResult = Assert.IsType(result); var viewModel = Assert.IsType(viewResult.Model); Assert.Equal(comparingProducts.Count, viewModel.Products.Count); Assert.NotEmpty(viewModel.Attributes); } - #endregion - #region NegativeTestCases [Fact] public async Task AddToComparison_TooManyComparingProducts_ReturnsErrorMessage() { @@ -221,8 +224,10 @@ public async Task AddToComparison_TooManyComparingProducts_ReturnsErrorMessage() UserName = "TestUser" }; workContextMock.Setup(repo => repo.GetCurrentUser()).ReturnsAsync(user); + // Act var result = await controller.AddToComparison(model); + // Assert comparingProductServiceMock.Verify(s => s.AddToComparison(It.IsAny(), It.IsAny()), Times.Once); var viewResult = Assert.IsType(result); @@ -263,13 +268,14 @@ public async Task Remove_InvalidProduct_ReturnsNotFound() UserName = "TestUser" }; workContextMock.Setup(repo => repo.GetCurrentUser()).ReturnsAsync(user); + // Act var result = await controller.Remove(productId); + // Assert repositoryMock.Verify(r => r.Remove(It.IsAny()), Times.Never); repositoryMock.Verify(r => r.SaveChanges(), Times.Never); Assert.IsType(result); } - #endregion } } From f40ad4ceefed93dd0d32e921f8f7e925722e2c88 Mon Sep 17 00:00:00 2001 From: Arun kumar <49372861+Arunkumar0610@users.noreply.github.com> Date: Thu, 11 Jan 2024 10:16:16 +0530 Subject: [PATCH 04/17] Update ComparingProductServiceTests.cs --- .../Services/ComparingProductServiceTests.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/SimplCommerce.Module.ProductComparison.Tests/Services/ComparingProductServiceTests.cs b/test/SimplCommerce.Module.ProductComparison.Tests/Services/ComparingProductServiceTests.cs index c8f63225a..df12cda91 100644 --- a/test/SimplCommerce.Module.ProductComparison.Tests/Services/ComparingProductServiceTests.cs +++ b/test/SimplCommerce.Module.ProductComparison.Tests/Services/ComparingProductServiceTests.cs @@ -11,7 +11,6 @@ namespace SimplCommerce.Module.ProductComparison.Tests.Services { public class ComparingProductServiceTests { - #region PositiveTestCases [Fact] public void AddToComparison_SuccessfullyAddsProduct() { @@ -51,9 +50,7 @@ public void MigrateComparingProduct_SuccessfullyMigratesProducts() repositoryMock.Verify(r => r.SaveChanges(), Times.Once); Assert.All(comparingProducts, cp => Assert.Equal(toUserId, cp.UserId)); } - #endregion - #region NegativeTestCases [Fact] public void AddToComparison_TooManyComparingProducts_ThrowsException() { @@ -138,6 +135,5 @@ public void AddToComparison_ProductAlreadyExists_DoesNotAddAgain() repositoryMock.Verify(r => r.Add(comparingProduct), Times.Never); repositoryMock.Verify(r => r.SaveChanges(), Times.Never); } - #endregion } } From ba18f6a218c35a66bc7b05ce794663664a75364d Mon Sep 17 00:00:00 2001 From: Arun kumar <49372861+Arunkumar0610@users.noreply.github.com> Date: Thu, 11 Jan 2024 10:16:52 +0530 Subject: [PATCH 05/17] Update SimplCommerce.Module.ProductComparison.Tests.csproj --- .../SimplCommerce.Module.ProductComparison.Tests.csproj | 5 ----- 1 file changed, 5 deletions(-) diff --git a/test/SimplCommerce.Module.ProductComparison.Tests/SimplCommerce.Module.ProductComparison.Tests.csproj b/test/SimplCommerce.Module.ProductComparison.Tests/SimplCommerce.Module.ProductComparison.Tests.csproj index 45aa47759..91821828c 100644 --- a/test/SimplCommerce.Module.ProductComparison.Tests/SimplCommerce.Module.ProductComparison.Tests.csproj +++ b/test/SimplCommerce.Module.ProductComparison.Tests/SimplCommerce.Module.ProductComparison.Tests.csproj @@ -25,9 +25,4 @@ - - - - - From abe238898db3c77c381b6065005051cf4b146234 Mon Sep 17 00:00:00 2001 From: Hisham Bin Ateya Date: Thu, 11 Jan 2024 08:03:37 +0300 Subject: [PATCH 06/17] Apply suggestions from code review --- .../Controllers/ComparingProductControllerTests.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs b/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs index 3ac9d1559..17a730bfb 100644 --- a/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs +++ b/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs @@ -51,9 +51,11 @@ public async Task AddToComparison_SuccessfullyAddsProduct() }; workContextMock.Setup(repo => repo.GetCurrentUser()).ReturnsAsync(user); + // Act var result = await controller.AddToComparison(model); + // Assert comparingProductServiceMock.Verify(s => s.AddToComparison(userId, productId), Times.Once); Assert.IsType(result); @@ -93,9 +95,11 @@ public async Task Remove_ValidProduct_RemovesProduct() }; workContextMock.Setup(repo => repo.GetCurrentUser()).ReturnsAsync(user); + // Act var result = await controller.Remove(productId); + // Assert repositoryMock.Verify(r => r.Remove(It.IsAny()), Times.Once); repositoryMock.Verify(r => r.SaveChanges(), Times.Once); @@ -182,9 +186,11 @@ public async Task Index_ReturnsValidViewModel() workContextMock.Object ); + // Act var result = await controller.Index(); + // Assert var viewResult = Assert.IsType(result); var viewModel = Assert.IsType(viewResult.Model); @@ -225,9 +231,11 @@ public async Task AddToComparison_TooManyComparingProducts_ReturnsErrorMessage() }; workContextMock.Setup(repo => repo.GetCurrentUser()).ReturnsAsync(user); + // Act var result = await controller.AddToComparison(model); + // Assert comparingProductServiceMock.Verify(s => s.AddToComparison(It.IsAny(), It.IsAny()), Times.Once); var viewResult = Assert.IsType(result); @@ -269,9 +277,11 @@ public async Task Remove_InvalidProduct_ReturnsNotFound() }; workContextMock.Setup(repo => repo.GetCurrentUser()).ReturnsAsync(user); + // Act var result = await controller.Remove(productId); + // Assert repositoryMock.Verify(r => r.Remove(It.IsAny()), Times.Never); repositoryMock.Verify(r => r.SaveChanges(), Times.Never); From 6b489ec39a04d479354c067955b883e1f5c22cc8 Mon Sep 17 00:00:00 2001 From: Hisham Bin Ateya Date: Thu, 11 Jan 2024 08:10:21 +0300 Subject: [PATCH 07/17] Apply suggestions from code review --- .../Controllers/ComparingProductControllerTests.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs b/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs index 17a730bfb..f704344e4 100644 --- a/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs +++ b/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs @@ -51,11 +51,9 @@ public async Task AddToComparison_SuccessfullyAddsProduct() }; workContextMock.Setup(repo => repo.GetCurrentUser()).ReturnsAsync(user); - // Act var result = await controller.AddToComparison(model); - // Assert comparingProductServiceMock.Verify(s => s.AddToComparison(userId, productId), Times.Once); Assert.IsType(result); @@ -277,10 +275,8 @@ public async Task Remove_InvalidProduct_ReturnsNotFound() }; workContextMock.Setup(repo => repo.GetCurrentUser()).ReturnsAsync(user); - // Act var result = await controller.Remove(productId); - // Assert repositoryMock.Verify(r => r.Remove(It.IsAny()), Times.Never); From 14f572dbdeeee213db46b95e9224025d63224ea8 Mon Sep 17 00:00:00 2001 From: Hisham Bin Ateya Date: Thu, 11 Jan 2024 08:13:39 +0300 Subject: [PATCH 08/17] Update test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs --- .../Controllers/ComparingProductControllerTests.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs b/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs index f704344e4..70bcea4ca 100644 --- a/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs +++ b/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs @@ -93,7 +93,6 @@ public async Task Remove_ValidProduct_RemovesProduct() }; workContextMock.Setup(repo => repo.GetCurrentUser()).ReturnsAsync(user); - // Act var result = await controller.Remove(productId); From ebb7af613e1e0525badba66d0cf3fe1f97bc559b Mon Sep 17 00:00:00 2001 From: Hisham Bin Ateya Date: Thu, 11 Jan 2024 08:13:54 +0300 Subject: [PATCH 09/17] Update test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs --- .../Controllers/ComparingProductControllerTests.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs b/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs index 70bcea4ca..2e28ac7c7 100644 --- a/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs +++ b/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs @@ -187,7 +187,6 @@ public async Task Index_ReturnsValidViewModel() // Act var result = await controller.Index(); - // Assert var viewResult = Assert.IsType(result); var viewModel = Assert.IsType(viewResult.Model); From 5abcc55f4b234e30fd3df3f1c0c87c5417e73462 Mon Sep 17 00:00:00 2001 From: Hisham Bin Ateya Date: Thu, 11 Jan 2024 08:14:01 +0300 Subject: [PATCH 10/17] Update test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs --- .../Controllers/ComparingProductControllerTests.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs b/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs index 2e28ac7c7..50316bd41 100644 --- a/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs +++ b/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs @@ -227,7 +227,6 @@ public async Task AddToComparison_TooManyComparingProducts_ReturnsErrorMessage() }; workContextMock.Setup(repo => repo.GetCurrentUser()).ReturnsAsync(user); - // Act var result = await controller.AddToComparison(model); From 991af1896331d49dc7a70c6412f9a5fadc52fac9 Mon Sep 17 00:00:00 2001 From: Hisham Bin Ateya Date: Thu, 11 Jan 2024 08:14:07 +0300 Subject: [PATCH 11/17] Update test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs --- .../Controllers/ComparingProductControllerTests.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs b/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs index 50316bd41..60982bd8b 100644 --- a/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs +++ b/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs @@ -230,7 +230,6 @@ public async Task AddToComparison_TooManyComparingProducts_ReturnsErrorMessage() // Act var result = await controller.AddToComparison(model); - // Assert comparingProductServiceMock.Verify(s => s.AddToComparison(It.IsAny(), It.IsAny()), Times.Once); var viewResult = Assert.IsType(result); From 176d0b3de7c645c5906f9b9f65e7bc757b300bfd Mon Sep 17 00:00:00 2001 From: Hisham Bin Ateya Date: Thu, 11 Jan 2024 08:21:37 +0300 Subject: [PATCH 12/17] Update test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs --- .../Controllers/ComparingProductControllerTests.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs b/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs index 60982bd8b..9d9ade15a 100644 --- a/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs +++ b/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs @@ -96,7 +96,6 @@ public async Task Remove_ValidProduct_RemovesProduct() // Act var result = await controller.Remove(productId); - // Assert repositoryMock.Verify(r => r.Remove(It.IsAny()), Times.Once); repositoryMock.Verify(r => r.SaveChanges(), Times.Once); From d77926d42bb041f388d54b79cdc3597c48b89004 Mon Sep 17 00:00:00 2001 From: Hisham Bin Ateya Date: Thu, 11 Jan 2024 08:21:46 +0300 Subject: [PATCH 13/17] Update test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs --- .../Controllers/ComparingProductControllerTests.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs b/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs index 9d9ade15a..4463c7e3c 100644 --- a/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs +++ b/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs @@ -182,7 +182,6 @@ public async Task Index_ReturnsValidViewModel() workContextMock.Object ); - // Act var result = await controller.Index(); From 6ccfc2882facfb6157a276c2148e726b06bb7fef Mon Sep 17 00:00:00 2001 From: Hisham Bin Ateya Date: Thu, 11 Jan 2024 08:21:58 +0300 Subject: [PATCH 14/17] Update test/SimplCommerce.Module.ProductComparison.Tests/Services/ComparingProductServiceTests.cs --- .../Services/ComparingProductServiceTests.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/SimplCommerce.Module.ProductComparison.Tests/Services/ComparingProductServiceTests.cs b/test/SimplCommerce.Module.ProductComparison.Tests/Services/ComparingProductServiceTests.cs index df12cda91..f4c2f2770 100644 --- a/test/SimplCommerce.Module.ProductComparison.Tests/Services/ComparingProductServiceTests.cs +++ b/test/SimplCommerce.Module.ProductComparison.Tests/Services/ComparingProductServiceTests.cs @@ -36,10 +36,10 @@ public void MigrateComparingProduct_SuccessfullyMigratesProducts() var toUserId = 2; var repositoryMock = new Mock>(); var comparingProducts = new List() - { - new ComparingProduct { UserId = fromUserId, ProductId = 1001 }, - new ComparingProduct { UserId = fromUserId, ProductId = 1002 }, - }; + { + new ComparingProduct { UserId = fromUserId, ProductId = 1001 }, + new ComparingProduct { UserId = fromUserId, ProductId = 1002 }, + }; repositoryMock.Setup(r => r.Query()).Returns(comparingProducts.AsQueryable()); var service = new ComparingProductService(repositoryMock.Object); From 21ed5975d47d18eba40a73bcb085069597ebce3f Mon Sep 17 00:00:00 2001 From: Hisham Bin Ateya Date: Thu, 11 Jan 2024 08:22:29 +0300 Subject: [PATCH 15/17] Update test/SimplCommerce.Module.ProductComparison.Tests/Services/ComparingProductServiceTests.cs --- .../Services/ComparingProductServiceTests.cs | 66 +++++++++---------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/test/SimplCommerce.Module.ProductComparison.Tests/Services/ComparingProductServiceTests.cs b/test/SimplCommerce.Module.ProductComparison.Tests/Services/ComparingProductServiceTests.cs index f4c2f2770..0758a2eed 100644 --- a/test/SimplCommerce.Module.ProductComparison.Tests/Services/ComparingProductServiceTests.cs +++ b/test/SimplCommerce.Module.ProductComparison.Tests/Services/ComparingProductServiceTests.cs @@ -59,48 +59,48 @@ public void AddToComparison_TooManyComparingProducts_ThrowsException() var productId = 1001; var repositoryMock = new Mock>(); var listComparingProducts = new List() - { - new ComparingProduct() { - ProductId = 1001, - Product=new Product() + new ComparingProduct() { - Name="TestProduct" + ProductId = 1001, + Product=new Product() + { + Name="TestProduct" + }, + CreatedOn = DateTime.Now, + UserId=1 }, - CreatedOn = DateTime.Now, - UserId=1 - }, - new ComparingProduct() - { - ProductId = 1002, - Product=new Product() + new ComparingProduct() { - Name="TestProduct1" + ProductId = 1002, + Product=new Product() + { + Name="TestProduct1" + }, + CreatedOn = DateTime.Now, + UserId=1 }, - CreatedOn = DateTime.Now, - UserId=1 - }, - new ComparingProduct() - { - ProductId = 1003, - Product=new Product() + new ComparingProduct() { - Name="TestProduct2" + ProductId = 1003, + Product=new Product() + { + Name="TestProduct2" + }, + CreatedOn = DateTime.Now, + UserId=1 }, - CreatedOn = DateTime.Now, - UserId=1 - }, - new ComparingProduct() - { - ProductId = 1004, - Product=new Product() + new ComparingProduct() { - Name="TestProduct3" + ProductId = 1004, + Product=new Product() + { + Name="TestProduct3" + }, + CreatedOn = DateTime.Now, + UserId=1 }, - CreatedOn = DateTime.Now, - UserId=1 - }, - }; + }; repositoryMock.Setup(r => r.Query()).Returns(listComparingProducts.AsQueryable); var service = new ComparingProductService(repositoryMock.Object); From d9eeb381b12c28883fe8638c909c0364dcd6ca59 Mon Sep 17 00:00:00 2001 From: Hisham Bin Ateya Date: Thu, 11 Jan 2024 09:21:26 +0300 Subject: [PATCH 16/17] Update test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs --- .../Controllers/ComparingProductControllerTests.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs b/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs index 4463c7e3c..83ab908e0 100644 --- a/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs +++ b/test/SimplCommerce.Module.ProductComparison.Tests/Controllers/ComparingProductControllerTests.cs @@ -16,6 +16,7 @@ using SimplCommerce.Module.ProductComparison.Models; using SimplCommerce.Module.ProductComparison.Services; using Xunit; + namespace SimplCommerce.Module.ProductComparison.Tests.Controllers { public class ComparingProductControllerTests From 82a912ebebfe82865733d31dff17ab2058459f25 Mon Sep 17 00:00:00 2001 From: Hisham Bin Ateya Date: Thu, 11 Jan 2024 09:21:31 +0300 Subject: [PATCH 17/17] Update test/SimplCommerce.Module.ProductComparison.Tests/Services/ComparingProductServiceTests.cs --- .../Services/ComparingProductServiceTests.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/test/SimplCommerce.Module.ProductComparison.Tests/Services/ComparingProductServiceTests.cs b/test/SimplCommerce.Module.ProductComparison.Tests/Services/ComparingProductServiceTests.cs index 0758a2eed..2a2adeb60 100644 --- a/test/SimplCommerce.Module.ProductComparison.Tests/Services/ComparingProductServiceTests.cs +++ b/test/SimplCommerce.Module.ProductComparison.Tests/Services/ComparingProductServiceTests.cs @@ -7,6 +7,7 @@ using SimplCommerce.Module.ProductComparison.Models; using SimplCommerce.Module.ProductComparison.Services; using Xunit; + namespace SimplCommerce.Module.ProductComparison.Tests.Services { public class ComparingProductServiceTests