234 lines
8.4 KiB
C#
234 lines
8.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using LCU.API.Internal.Operations.IncentiveApiLib.Persistence;
|
|
using LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
|
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
|
using LCU.Library.Internal.IncentiveApiModels.Enums;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Moq;
|
|
using NUnit.Framework;
|
|
|
|
namespace Application.UnitTests.Repository
|
|
{
|
|
public class PayoutRepositoryTest
|
|
{
|
|
private ApplicationDbContext _context;
|
|
private PayoutRepository _repository;
|
|
|
|
[SetUp]
|
|
public void Setup()
|
|
{
|
|
var mockDateTime = new DateTimeService();
|
|
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);
|
|
_repository = new PayoutRepository(_context);
|
|
}
|
|
|
|
[Test]
|
|
public async Task Basic_Flow_Test()
|
|
{
|
|
var period = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
|
|
var saved = await _repository.GetPayoutLineItemsForPeriodAsync(period);
|
|
Assert.NotNull(saved);
|
|
Assert.IsEmpty(saved);
|
|
|
|
var newId = await _repository.SavePayoutLineItemAsync(new PayoutLineItem()
|
|
{
|
|
Product = Product.CheckCard,
|
|
Role = Role.Teller,
|
|
EmployeeId = "11771",
|
|
DepartmentId = 999,
|
|
ActivityTotal = 1,
|
|
PayoutTotal = 10,
|
|
CommissionId = 99,
|
|
CommissionValue = 10,
|
|
ActivityPeriod = period
|
|
});
|
|
await _context.SaveChangesAsync();
|
|
|
|
saved = await _repository.GetPayoutLineItemsForPeriodAsync(period);
|
|
Assert.NotNull(saved);
|
|
Assert.IsNotEmpty(saved);
|
|
var first = saved.First();
|
|
// Make sure our Auditable is working.
|
|
Assert.NotNull(first.CreatedBy);
|
|
Assert.NotNull(first.Created);
|
|
|
|
var savedPayout = await _repository.GetPayoutLineItemById(saved.First().Id);
|
|
Assert.AreSame(saved.First(), savedPayout);
|
|
}
|
|
|
|
[Test]
|
|
public async Task Basic_RangeFlow_Test()
|
|
{
|
|
var period = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
|
|
var saved = await _repository.GetPayoutLineItemsForPeriodAsync(period);
|
|
Assert.NotNull(saved);
|
|
Assert.IsEmpty(saved);
|
|
|
|
var lineItems = new List<PayoutLineItem>()
|
|
{
|
|
new PayoutLineItem()
|
|
{
|
|
Product = Product.CheckCard,
|
|
Role = Role.Teller,
|
|
EmployeeId = "11771",
|
|
DepartmentId = 999,
|
|
ActivityTotal = 1,
|
|
PayoutTotal = 10,
|
|
CommissionId = 99,
|
|
CommissionValue = 10,
|
|
ActivityPeriod = period
|
|
},
|
|
new PayoutLineItem()
|
|
{
|
|
Product = Product.CheckCard,
|
|
Role = Role.Teller,
|
|
EmployeeId = "11772",
|
|
DepartmentId = 999,
|
|
ActivityTotal = 1,
|
|
PayoutTotal = 10,
|
|
CommissionId = 99,
|
|
CommissionValue = 10,
|
|
ActivityPeriod = period
|
|
}
|
|
};
|
|
await _repository.SavePayoutLineItemRangeAsync(lineItems);
|
|
await _context.SaveChangesAsync();
|
|
|
|
saved = await _repository.GetPayoutLineItemsForPeriodAsync(period);
|
|
Assert.NotNull(saved);
|
|
Assert.IsNotEmpty(saved);
|
|
Assert.AreEqual(2, saved.Count());
|
|
var first = saved.First();
|
|
// Make sure our Auditable is working.
|
|
Assert.NotNull(first.CreatedBy);
|
|
Assert.NotNull(first.Created);
|
|
|
|
var savedPayout = await _repository.GetPayoutLineItemById(saved.First().Id);
|
|
Assert.AreSame(saved.First(), savedPayout);
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetEmployeePayoutsForPeriod_None()
|
|
{
|
|
var user = new AppUser()
|
|
{
|
|
Role = Role.Admin,
|
|
AdEmail = "[email protected]",
|
|
BaseRole = "Tester",
|
|
EmployeeId = "11771",
|
|
FullName = "Test McTester",
|
|
HomeDepartment = "HOME",
|
|
PersonId = 565
|
|
};
|
|
|
|
var period = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
|
|
var found = await _repository.GetEmployeePayoutsForPeriod(user, period);
|
|
Assert.NotNull(found);
|
|
Assert.IsEmpty(found);
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetEmployeePayoutsForPeriod_Basic()
|
|
{
|
|
var period = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
|
|
var period2 = new DateTime(DateTime.Today.Year + 1, DateTime.Today.Month, 1);
|
|
var payout1 = new PayoutLineItem()
|
|
{
|
|
Product = Product.CheckCard,
|
|
Role = Role.Teller,
|
|
EmployeeId = "11771",
|
|
DepartmentId = 999,
|
|
ActivityTotal = 1,
|
|
PayoutTotal = 10,
|
|
CommissionId = 99,
|
|
CommissionValue = 10,
|
|
ActivityPeriod = period
|
|
};
|
|
await _repository.SavePayoutLineItemAsync(payout1);
|
|
var payout2 = new PayoutLineItem()
|
|
{
|
|
Product = Product.CheckCard,
|
|
Role = Role.Teller,
|
|
EmployeeId = "11772",
|
|
DepartmentId = 999,
|
|
ActivityTotal = 1,
|
|
PayoutTotal = 10,
|
|
CommissionId = 99,
|
|
CommissionValue = 10,
|
|
ActivityPeriod = period
|
|
};
|
|
await _repository.SavePayoutLineItemAsync(payout2);
|
|
var payout3 = new PayoutLineItem()
|
|
{
|
|
Product = Product.CheckCard,
|
|
Role = Role.Teller,
|
|
EmployeeId = "11771",
|
|
DepartmentId = 999,
|
|
ActivityTotal = 1,
|
|
PayoutTotal = 10,
|
|
CommissionId = 99,
|
|
CommissionValue = 10,
|
|
ActivityPeriod = period
|
|
};
|
|
await _repository.SavePayoutLineItemAsync(payout3);
|
|
var payout4 = new PayoutLineItem()
|
|
{
|
|
Product = Product.CheckCard,
|
|
Role = Role.Teller,
|
|
EmployeeId = "11771",
|
|
DepartmentId = 999,
|
|
ActivityTotal = 1,
|
|
PayoutTotal = 10,
|
|
CommissionId = 99,
|
|
CommissionValue = 10,
|
|
ActivityPeriod = period2
|
|
};
|
|
await _repository.SavePayoutLineItemAsync(payout4);
|
|
await _context.SaveChangesAsync();
|
|
|
|
var user = new AppUser()
|
|
{
|
|
Role = Role.Admin,
|
|
AdEmail = "[email protected]",
|
|
BaseRole = "Tester",
|
|
EmployeeId = "11771",
|
|
FullName = "Test McTester",
|
|
HomeDepartment = "HOME",
|
|
PersonId = 11771,
|
|
Departments = new List<UserDepartment>()
|
|
{
|
|
new UserDepartment()
|
|
{
|
|
DepartmentId = 55,
|
|
Department = new Department()
|
|
{
|
|
Id = 55,
|
|
State = DepartmentState.Active,
|
|
DepartmentName = "Test",
|
|
BusinessLineCode = BusinessLine.Operations.Code
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
var found = await _repository.GetEmployeePayoutsForPeriod(user, period);
|
|
Assert.NotNull(found);
|
|
Assert.IsNotEmpty(found);
|
|
Assert.AreEqual(2, found.Count);
|
|
}
|
|
|
|
}
|
|
}
|