Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added unit tests for productcomparison module #1079

Merged
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
6eb7432
added tests for SimplCommerce.Module.ProductComparison -ComparingProd…
Arunkumar0610 Jan 10, 2024
3b9e6b1
added tests for SimplCommerce.Module.ProductComparison - ComparingPro…
Arunkumar0610 Jan 10, 2024
914060c
Update ComparingProductControllerTests.cs
Arunkumar0610 Jan 11, 2024
f40ad4c
Update ComparingProductServiceTests.cs
Arunkumar0610 Jan 11, 2024
ba18f6a
Update SimplCommerce.Module.ProductComparison.Tests.csproj
Arunkumar0610 Jan 11, 2024
abe2388
Apply suggestions from code review
hishamco Jan 11, 2024
6b489ec
Apply suggestions from code review
hishamco Jan 11, 2024
14f572d
Update test/SimplCommerce.Module.ProductComparison.Tests/Controllers/…
hishamco Jan 11, 2024
ebb7af6
Update test/SimplCommerce.Module.ProductComparison.Tests/Controllers/…
hishamco Jan 11, 2024
5abcc55
Update test/SimplCommerce.Module.ProductComparison.Tests/Controllers/…
hishamco Jan 11, 2024
991af18
Update test/SimplCommerce.Module.ProductComparison.Tests/Controllers/…
hishamco Jan 11, 2024
176d0b3
Update test/SimplCommerce.Module.ProductComparison.Tests/Controllers/…
hishamco Jan 11, 2024
d77926d
Update test/SimplCommerce.Module.ProductComparison.Tests/Controllers/…
hishamco Jan 11, 2024
6ccfc28
Update test/SimplCommerce.Module.ProductComparison.Tests/Services/Com…
hishamco Jan 11, 2024
21ed597
Update test/SimplCommerce.Module.ProductComparison.Tests/Services/Com…
hishamco Jan 11, 2024
d9eeb38
Update test/SimplCommerce.Module.ProductComparison.Tests/Controllers/…
hishamco Jan 11, 2024
82a912e
Update test/SimplCommerce.Module.ProductComparison.Tests/Services/Com…
hishamco Jan 11, 2024
dc6a33c
Merge branch 'master' into unit_test_productcomparison_module
Arunkumar0610 Jan 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions SimplCommerce.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
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
hishamco marked this conversation as resolved.
Show resolved Hide resolved
{
public class ComparingProductControllerTests
{
[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<IRepository<ComparingProduct>>();
var comparingProductServiceMock = new Mock<IComparingProductService>();
var productPricingServiceMock = new Mock<IProductPricingService>();
var mediaServiceMock = new Mock<IMediaService>();
var contentLocalizationServiceMock = new Mock<IContentLocalizationService>();
var workContextMock = new Mock<IWorkContext>();
var userManagerMock = new Mock<UserManager<User>>(
Mock.Of<IUserStore<User>>(), 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
hishamco marked this conversation as resolved.
Show resolved Hide resolved
var result = await controller.AddToComparison(model);

// Assert
hishamco marked this conversation as resolved.
Show resolved Hide resolved
comparingProductServiceMock.Verify(s => s.AddToComparison(userId, productId), Times.Once);
Assert.IsType<PartialViewResult>(result);
}

[Fact]
public async Task Remove_ValidProduct_RemovesProduct()
{
// Arrange
var userId = 1;
var productId = 1001;
var repositoryMock = new Mock<IRepository<ComparingProduct>>();
repositoryMock.Setup(r => r.Query()).Returns(new[]
{
new ComparingProduct { UserId = userId, ProductId = productId }
}.AsQueryable());
var comparingProductServiceMock = new Mock<IComparingProductService>();
var productPricingServiceMock = new Mock<IProductPricingService>();
var mediaServiceMock = new Mock<IMediaService>();
var contentLocalizationServiceMock = new Mock<IContentLocalizationService>();
var workContextMock = new Mock<IWorkContext>();
var userManagerMock = new Mock<UserManager<User>>(
Mock.Of<IUserStore<User>>(), 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
hishamco marked this conversation as resolved.
Show resolved Hide resolved
var result = await controller.Remove(productId);


// Assert
hishamco marked this conversation as resolved.
Show resolved Hide resolved
hishamco marked this conversation as resolved.
Show resolved Hide resolved
repositoryMock.Verify(r => r.Remove(It.IsAny<ComparingProduct>()), Times.Once);
repositoryMock.Verify(r => r.SaveChanges(), Times.Once);
Assert.IsType<OkResult>(result);
}

[Fact]
public async Task Index_ReturnsValidViewModel()
{
// Arrange
var userId = 1; // Use a unique user id
var comparingProducts = new List<ComparingProduct>()
{
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<IRepository<ComparingProduct>>();
repositoryMock.Setup(r => r.Query()).Returns(comparingProducts.AsQueryable());
var comparingProductServiceMock = new Mock<IComparingProductService>();
var productPricingServiceMock = new Mock<IProductPricingService>();
var mediaServiceMock = new Mock<IMediaService>();
var contentLocalizationServiceMock = new Mock<IContentLocalizationService>();
contentLocalizationServiceMock.Setup(s => s.GetLocalizedProperty(It.IsAny<ComparingProduct>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns<string, string, string>((obj, propertyName, defaultValue) => defaultValue);
var workContextMock = new Mock<IWorkContext>();
var userManagerMock = new Mock<UserManager<User>>(
Mock.Of<IUserStore<User>>(), 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
hishamco marked this conversation as resolved.
Show resolved Hide resolved
hishamco marked this conversation as resolved.
Show resolved Hide resolved
var result = await controller.Index();

// Assert
hishamco marked this conversation as resolved.
Show resolved Hide resolved
var viewResult = Assert.IsType<ViewResult>(result);
var viewModel = Assert.IsType<ProductComparisonVm>(viewResult.Model);
Assert.Equal(comparingProducts.Count, viewModel.Products.Count);
Assert.NotEmpty(viewModel.Attributes);
}

[Fact]
public async Task AddToComparison_TooManyComparingProducts_ReturnsErrorMessage()
{
// Arrange
var userId = 1;
var productId = 1001;
var model = new AddToComparisonModel { ProductId = productId };
var repositoryMock = new Mock<IRepository<ComparingProduct>>();
var comparingProductServiceMock = new Mock<IComparingProductService>();
comparingProductServiceMock.Setup(s => s.AddToComparison(userId, productId))
.Throws(new TooManyComparingProductException(4));
var productPricingServiceMock = new Mock<IProductPricingService>();
var mediaServiceMock = new Mock<IMediaService>();
var contentLocalizationServiceMock = new Mock<IContentLocalizationService>();
var workContextMock = new Mock<IWorkContext>();
var userManagerMock = new Mock<UserManager<User>>(
Mock.Of<IUserStore<User>>(), 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
hishamco marked this conversation as resolved.
Show resolved Hide resolved
var result = await controller.AddToComparison(model);

// Assert
hishamco marked this conversation as resolved.
Show resolved Hide resolved
comparingProductServiceMock.Verify(s => s.AddToComparison(It.IsAny<long>(), It.IsAny<long>()), Times.Once);
var viewResult = Assert.IsType<PartialViewResult>(result);
var viewModel = Assert.IsType<AddToComparisonResult>(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<IRepository<ComparingProduct>>();
repositoryMock.Setup(r => r.Query()).Returns(new[]
{
new ComparingProduct { UserId = userId, ProductId = 999 }
}.AsQueryable());
var comparingProductServiceMock = new Mock<IComparingProductService>();
var productPricingServiceMock = new Mock<IProductPricingService>();
var mediaServiceMock = new Mock<IMediaService>();
var contentLocalizationServiceMock = new Mock<IContentLocalizationService>();
var workContextMock = new Mock<IWorkContext>();
var userManagerMock = new Mock<UserManager<User>>(
Mock.Of<IUserStore<User>>(), 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
hishamco marked this conversation as resolved.
Show resolved Hide resolved
var result = await controller.Remove(productId);

// Assert
hishamco marked this conversation as resolved.
Show resolved Hide resolved
repositoryMock.Verify(r => r.Remove(It.IsAny<ComparingProduct>()), Times.Never);
repositoryMock.Verify(r => r.SaveChanges(), Times.Never);
Assert.IsType<NotFoundResult>(result);
}
}
}
Loading
Loading