192 lines
6.4 KiB
C#
192 lines
6.4 KiB
C#
using LCU.API.Internal.Operations.IncentiveApi.Services;
|
|
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
|
using LCU.Library.Internal.IncentiveApiModels.Enums;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Moq;
|
|
using NUnit.Framework;
|
|
using System;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using LCU.API.Internal.Operations.IncentiveApiLib.Persistence;
|
|
using LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
|
|
|
namespace Application.UnitTests.Repository
|
|
{
|
|
public class CommissionRepositoryTest
|
|
{
|
|
private ApplicationDbContext _context;
|
|
private CommissionRepository _repository;
|
|
private Mock<IDateTime> _mockDateTime;
|
|
|
|
[SetUp]
|
|
public void Setup()
|
|
{
|
|
_mockDateTime = new Mock<IDateTime>();
|
|
var mockCurrentUserService = Mock.Of<ICurrentUserService>();
|
|
Mock.Get(mockCurrentUserService).Setup(s => s.GetCurrentUser()).Returns(new ApplicationUser()
|
|
{
|
|
Email = "[email protected]",
|
|
});
|
|
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
|
|
.UseInMemoryDatabase(Guid.NewGuid().ToString())
|
|
.EnableSensitiveDataLogging(true)
|
|
.Options;
|
|
_context = new ApplicationDbContext(options, mockCurrentUserService, _mockDateTime.Object);
|
|
_repository = new CommissionRepository(_context);
|
|
}
|
|
|
|
[Test]
|
|
public async Task FindExistingCommission_FoundById()
|
|
{
|
|
var commission = new Commission
|
|
{
|
|
Id = 1,
|
|
Product = Product.NewChecking,
|
|
RatePerProduct = 2,
|
|
Role = Role.Teller
|
|
};
|
|
await _repository.SaveCommission(commission);
|
|
await _context.SaveChangesAsync();
|
|
|
|
var found = await _repository.FindExistingCommission(1);
|
|
|
|
Assert.NotNull(found);
|
|
Assert.AreEqual(1, found.Id);
|
|
}
|
|
|
|
[Test]
|
|
public async Task FindExistingCommission_FoundByRole()
|
|
{
|
|
var commission = new Commission
|
|
{
|
|
Id = 1,
|
|
Product = Product.NewChecking,
|
|
RatePerProduct = 2,
|
|
Role = Role.Teller
|
|
};
|
|
await _repository.SaveCommission(commission);
|
|
await _context.SaveChangesAsync();
|
|
|
|
var found = await _repository.FindProductCommissions(Role.Teller);
|
|
|
|
Assert.NotNull(found);
|
|
Assert.IsNotEmpty(found);
|
|
Assert.AreEqual(1, found.First().Id);
|
|
Assert.AreEqual(Role.Teller, found.First().Role);
|
|
}
|
|
|
|
[Test]
|
|
public async Task FindExistingCommission_NotFoundById()
|
|
{
|
|
var commission = new Commission
|
|
{
|
|
Id = 1,
|
|
Product = Product.NewChecking,
|
|
RatePerProduct = 2,
|
|
Role = Role.Teller
|
|
};
|
|
await _repository.SaveCommission(commission);
|
|
await _context.SaveChangesAsync();
|
|
|
|
var found = await _repository.FindExistingCommission(2);
|
|
|
|
Assert.Null(found);
|
|
}
|
|
|
|
[Test]
|
|
public async Task FindExistingCommission_FoundByRoleAndProduct()
|
|
{
|
|
var commission = new Commission
|
|
{
|
|
Id = 1,
|
|
Product = Product.NewChecking,
|
|
RatePerProduct = 2,
|
|
Role = Role.Teller
|
|
};
|
|
await _repository.SaveCommission(commission);
|
|
await _context.SaveChangesAsync();
|
|
|
|
var found = await _repository.FindExistingCommission(Role.Teller, Product.NewChecking);
|
|
|
|
Assert.NotNull(found);
|
|
Assert.AreEqual(Role.Teller, found.Role);
|
|
Assert.AreEqual(Product.NewChecking, found.Product);
|
|
}
|
|
|
|
[Test]
|
|
public async Task FindExistingCommission_NotFoundByRoleAndProduct()
|
|
{
|
|
var commission = new Commission
|
|
{
|
|
Id = 1,
|
|
Product = Product.NewChecking,
|
|
RatePerProduct = 2,
|
|
Role = Role.Teller
|
|
};
|
|
await _repository.SaveCommission(commission);
|
|
await _context.SaveChangesAsync();
|
|
|
|
var found = await _repository.FindExistingCommission(Role.Admin, Product.NewChecking);
|
|
|
|
Assert.Null(found);
|
|
}
|
|
|
|
[Test]
|
|
public async Task SaveCommission_Basic()
|
|
{
|
|
_mockDateTime.SetupGet(mock => mock.Now).Returns(new DateTime(2021, 7, 9, 1, 2, 3, 456));
|
|
var commission = new Commission
|
|
{
|
|
Id = 1,
|
|
Product = Product.ATMCard,
|
|
RatePerProduct = 3,
|
|
Role = Role.BM
|
|
};
|
|
await _repository.SaveCommission(commission);
|
|
await _context.SaveChangesAsync();
|
|
|
|
var found = await _repository.FindExistingCommission(1);
|
|
|
|
Assert.NotNull(found);
|
|
Assert.AreEqual(1, found.Id);
|
|
Assert.AreEqual(Product.ATMCard, found.Product);
|
|
Assert.AreEqual(3, found.RatePerProduct);
|
|
Assert.AreEqual(Role.BM, found.Role);
|
|
Assert.AreEqual(new DateTime(2021, 7, 9, 1, 2, 3, 456), found.Created);
|
|
Assert.AreEqual("[email protected]", found.CreatedBy);
|
|
Assert.AreEqual(UserType.Associate, found.CreatedByType);
|
|
Assert.AreEqual(new DateTime(2021, 7, 9, 1, 2, 3, 456), found.LastUpdated);
|
|
Assert.AreEqual("[email protected]", found.LastUpdatedBy);
|
|
Assert.AreEqual(UserType.Associate, found.LastUpdatedByType);
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetCommissionsAsync_Basic()
|
|
{
|
|
var commission1 = new Commission
|
|
{
|
|
Id = 1,
|
|
Product = Product.NewChecking,
|
|
RatePerProduct = 2,
|
|
Role = Role.Teller
|
|
};
|
|
var commission2 = new Commission
|
|
{
|
|
Id = 2,
|
|
Product = Product.ATMCard,
|
|
RatePerProduct = 3,
|
|
Role = Role.BM
|
|
};
|
|
await _repository.SaveCommission(commission1);
|
|
await _repository.SaveCommission(commission2);
|
|
await _context.SaveChangesAsync();
|
|
|
|
var found = await _repository.GetCommissionsAsync();
|
|
|
|
Assert.NotNull(found);
|
|
Assert.IsNotEmpty(found);
|
|
Assert.AreEqual(2, found.Count);
|
|
}
|
|
}
|
|
}
|