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.API.Internal.Operations.IncentiveApi.Extensions; using LCU.Library.Internal.IncentiveApiModels.Entities; using LCU.Library.Internal.IncentiveApiModels.Enums; using LCU.Library.Internal.IncentiveApiModels.Requests; using Microsoft.EntityFrameworkCore; using Moq; using NUnit.Framework; using LCU.Library.Internal.IncentiveApiModels.Common; namespace Application.UnitTests.Repository { public class GoalRepositoryTest { private ApplicationDbContext _context; private GoalRepository _repository; [SetUp] public void Setup() { var mockDateTime = new DateTimeService(); var mockCurrentUserService = Mock.Of(); Mock.Get(mockCurrentUserService).Setup(s => s.GetCurrentUser()).Returns(new ApplicationUser() { Email = "JohnTest@test.com", }); var options = new DbContextOptionsBuilder() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .EnableSensitiveDataLogging(true) .Options; _context = new ApplicationDbContext(options, mockCurrentUserService, mockDateTime); _repository = new GoalRepository(_context); } [Test] public async Task Basic_Flow_Test() { var saved = await _repository.GetGoalsAsync(new GoalSearchRequest()); Assert.NotNull(saved); Assert.IsEmpty(saved); var newId = await _repository.SaveGoalAsync(new Goal() { DepartmentId = 6, StartDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1), EndDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1).Subtract(TimeSpan.FromDays(1)), EmployeeId = null }); await _context.SaveChangesAsync(); saved = await _repository.GetGoalsAsync(new GoalSearchRequest() { IncludePersonnel = false } ); Assert.NotNull(saved); Assert.IsNotEmpty(saved); var first = saved.First(); // Make sure our Auditable is working. Assert.NotNull(first.CreatedBy); Assert.NotNull(first.Created); } [Test] public async Task Basic_Search_Test() { var saved = await _repository.GetGoalsAsync(new GoalSearchRequest()); Assert.NotNull(saved); Assert.IsEmpty(saved); var startDate = new DateTime(2021, 3, 1); var endDate = (new DateTime(2021, 3, 1)).EndOfMonth(); var goalPersonnel = new Goal() { Product = Product.NewChecking, Value = 10, DepartmentId = 1, StartDate = startDate, EndDate = endDate, EmployeeId = "programming@landmarkcu.com" }; var goal = new Goal() { Product = Product.NewChecking, Value = 10, DepartmentId = 1, StartDate = startDate, EndDate = endDate }; var goal2 = new Goal() { Product = Product.NewChecking, Value = 8, DepartmentId = 2, StartDate = startDate, EndDate = endDate }; var goal3 = new Goal() { Product = Product.NewChecking, Value = 11, DepartmentId = 1, StartDate = new DateTime(2021, 4, 1), EndDate = (new DateTime(2021, 4, 1)).EndOfMonth() }; await _repository.SaveGoalAsync(goalPersonnel); await _repository.SaveGoalAsync(goal); await _repository.SaveGoalAsync(goal2); await _repository.SaveGoalAsync(goal3); await _context.SaveChangesAsync(); saved = await _repository.GetGoalsAsync(new GoalSearchRequest() { IncludePersonnel = false } ); Assert.NotNull(saved); Assert.IsNotEmpty(saved); Assert.AreEqual(3, saved.Count); saved = await _repository.GetGoalsAsync(new GoalSearchRequest() { IncludePersonnel = true } ); Assert.NotNull(saved); Assert.IsNotEmpty(saved); Assert.AreEqual(1, saved.Count); saved = await _repository.GetGoalsAsync(new GoalSearchRequest() { IncludePersonnel = false, departmentId = 1 } ); Assert.NotNull(saved); Assert.IsNotEmpty(saved); Assert.AreEqual(2, saved.Count); saved = await _repository.GetGoalsAsync(new GoalSearchRequest() { IncludePersonnel = true, departmentId = 1 } ); Assert.NotNull(saved); Assert.IsNotEmpty(saved); Assert.AreEqual(1, saved.Count); saved = await _repository.GetGoalsAsync(new GoalSearchRequest() { IncludePersonnel = false, DateRange = new MonthRange(2021,3) } ); Assert.NotNull(saved); Assert.IsNotEmpty(saved); Assert.AreEqual(2, saved.Count); } [Test] public async Task Multi_Year_Search_Test() { var saved = await _repository.GetGoalsAsync(new GoalSearchRequest()); Assert.NotNull(saved); Assert.IsEmpty(saved); var startDate = new DateTime(2021, 6, 1); var endDate = (new DateTime(2021, 6, 1)).EndOfMonth(); var goal = new Goal() { Product = Product.NewChecking, Value = 10, DepartmentId = 1, StartDate = startDate, EndDate = endDate }; var goal2 = new Goal() { Product = Product.NewChecking, Value = 11, DepartmentId = 1, StartDate = new DateTime(2022, 4, 1), EndDate = (new DateTime(2022, 4, 1)).EndOfMonth() }; await _repository.SaveGoalAsync(goal); await _repository.SaveGoalAsync(goal2); await _context.SaveChangesAsync(); saved = await _repository.GetGoalsAsync(new GoalSearchRequest() { DateRange = new MonthRange(2021,6, 2022,6), IncludePersonnel = false } ); Assert.NotNull(saved); Assert.IsNotEmpty(saved); Assert.AreEqual(2, saved.Count); } [Test] public async Task Find_Goal_Basic() { var saved = await _repository.GetGoalsAsync(new GoalSearchRequest()); Assert.NotNull(saved); Assert.IsEmpty(saved); var goalChange = new GoalChange() { Product = Product.NewChecking, Value = 10, DepartmentId = 1, StartDate = new DateTime(2021, 3, 1), EndDate = (new DateTime(2021, 3, 1)).EndOfMonth(), PersonId = "programming@landmarkcu.com" }; var goal = new Goal() { Product = Product.NewChecking, Value = 10, DepartmentId = 1, StartDate = new DateTime(2021, 3, 1), EndDate = (new DateTime(2021, 3, 1)).EndOfMonth(), EmployeeId = "programming@landmarkcu.com" }; var newId = await _repository.SaveGoalAsync(goal); await _context.SaveChangesAsync(); var foundGoal = await _repository.FindExistingGoal(goalChange); Assert.NotNull(foundGoal); // Make sure our Auditable is working. Assert.NotNull(foundGoal.CreatedBy); Assert.NotNull(foundGoal.Created); } [Test] public async Task Find_Goal_UsesDate() { var saved = await _repository.GetGoalsAsync(new GoalSearchRequest()); Assert.NotNull(saved); Assert.IsEmpty(saved); var goalChange = new GoalChange() { Product = Product.NewChecking, Value = 10, DepartmentId = 1, StartDate = new DateTime(2021, 3, 1), EndDate = (new DateTime(2021, 3, 1)).EndOfMonth(), PersonId = "programming@landmarkcu.com" }; var goal = new Goal() { Product = Product.NewChecking, Value = 10, DepartmentId = 1, StartDate = new DateTime(2021, 3, 1), EndDate = (new DateTime(2021, 3, 1)).AddMonths(1).AddMilliseconds(-1), EmployeeId = "programming@landmarkcu.com" }; var newId = await _repository.SaveGoalAsync(goal); await _context.SaveChangesAsync(); var foundGoal = await _repository.FindExistingGoal(goalChange); Assert.NotNull(foundGoal); // Make sure our Auditable is working. Assert.NotNull(foundGoal.CreatedBy); Assert.NotNull(foundGoal.Created); } [Test] public async Task Find_Goal_Department_Basic() { var saved = await _repository.GetGoalsAsync(new GoalSearchRequest()); Assert.NotNull(saved); Assert.IsEmpty(saved); var startDate = new DateTime(2021, 3, 1); var endDate = (new DateTime(2021, 3, 1)).EndOfMonth(); // This personnel goal shouldn't be returned. var goalPersonnel = new Goal() { Product = Product.NewChecking, Value = 10, DepartmentId = 1, StartDate = startDate, EndDate = endDate, EmployeeId = "programming@landmarkcu.com" }; var goal = new Goal() { Product = Product.NewChecking, Value = 10, DepartmentId = 1, StartDate = startDate, EndDate = endDate }; var goal2 = new Goal() { Product = Product.NewChecking, Value = 8, DepartmentId = 2, StartDate = startDate, EndDate = endDate }; var goal3 = new Goal() { Product = Product.NewChecking, Value = 11, DepartmentId = 1, StartDate = new DateTime(2021, 4, 1), EndDate = (new DateTime(2021, 4, 1)).EndOfMonth() }; await _repository.SaveGoalAsync(goalPersonnel); await _repository.SaveGoalAsync(goal); await _repository.SaveGoalAsync(goal2); await _repository.SaveGoalAsync(goal3); await _context.SaveChangesAsync(); var foundGoals = await _repository.FindDepartmentGoals(1, startDate, endDate); Assert.NotNull(foundGoals); Assert.IsNotEmpty(foundGoals); Assert.AreEqual(1, foundGoals.Count); Assert.AreEqual(10, foundGoals.First().Value); } [Test] public async Task Find_Goal_Department_Annual() { var saved = await _repository.GetGoalsAsync(new GoalSearchRequest()); Assert.NotNull(saved); Assert.IsEmpty(saved); var startDate = new DateTime(2021, 3, 1); var endDate = (new DateTime(2021, 3, 1)).EndOfMonth(); // This personnel goal shouldn't be returned. var goalPersonnel = new Goal() { Product = Product.NewChecking, Value = 7, DepartmentId = 1, StartDate = startDate, EndDate = endDate, EmployeeId = "programming@landmarkcu.com" }; var goal = new Goal() { Product = Product.NewChecking, Value = 10, DepartmentId = 1, StartDate = startDate, EndDate = endDate }; var goal2 = new Goal() { Product = Product.NewChecking, Value = 8, DepartmentId = 2, StartDate = startDate, EndDate = endDate }; var goal3 = new Goal() { Product = Product.NewChecking, Value = 11, DepartmentId = 1, StartDate = new DateTime(2021, 4, 1), EndDate = (new DateTime(2021, 3, 1)).EndOfMonth() }; await _repository.SaveGoalAsync(goalPersonnel); await _repository.SaveGoalAsync(goal); await _repository.SaveGoalAsync(goal2); await _repository.SaveGoalAsync(goal3); await _context.SaveChangesAsync(); var foundGoals = await _repository.FindAnnualDepartmentGoals(1, startDate.Year); Assert.NotNull(foundGoals); Assert.IsNotEmpty(foundGoals); Assert.AreEqual(2, foundGoals.Count); Assert.AreEqual(10, foundGoals.First().Value); Assert.AreEqual(11, foundGoals.ElementAt(1).Value); } [Test] public async Task Find_Goal_Personnel_Basic() { var saved = await _repository.GetGoalsAsync(new GoalSearchRequest()); Assert.NotNull(saved); Assert.IsEmpty(saved); var startDate = new DateTime(2021, 3, 1); var endDate = (new DateTime(2021, 3, 1)).EndOfMonth(); // This department goal shouldn't be returned var departmentGoal = new Goal() { Product = Product.NewChecking, Value = 10, DepartmentId = 1, StartDate = startDate, EndDate = endDate }; var goal = new Goal() { Product = Product.NewChecking, Value = 10, DepartmentId = 1, StartDate = startDate, EndDate = endDate, EmployeeId = "programming@landmarkcu.com" }; var goalx = new Goal() { Product = Product.NewChecking, Value = 10, DepartmentId = 1, StartDate = startDate, EndDate = endDate, EmployeeId = "xyz@landmarkcu.com" }; var goal2 = new Goal() { Product = Product.NewChecking, Value = 8, DepartmentId = 2, StartDate = startDate, EndDate = endDate, EmployeeId = "programming@landmarkcu.com" }; var goal3 = new Goal() { Product = Product.NewChecking, Value = 11, DepartmentId = 1, StartDate = new DateTime(2021, 4, 1), EndDate = (new DateTime(2021, 4, 1)).EndOfMonth(), EmployeeId = "programming@landmarkcu.com" }; await _repository.SaveGoalAsync(departmentGoal); await _repository.SaveGoalAsync(goal); await _repository.SaveGoalAsync(goalx); await _repository.SaveGoalAsync(goal2); await _repository.SaveGoalAsync(goal3); await _context.SaveChangesAsync(); var users = new List() { new AppUser() { AdEmail = "programming@landmarkcu.com", EmployeeId = "programming@landmarkcu.com", FullName = "John McTester", HomeDepartment = "QA", BaseRole = "Tester", Role = Role.Developer, } }; var foundGoals = await _repository.FindPersonnelGoals(1, users, startDate, endDate); Assert.NotNull(foundGoals); Assert.IsNotEmpty(foundGoals); Assert.AreEqual(1, foundGoals.Count); Assert.AreEqual(10, foundGoals.First().Value); } [Test] public async Task Find_Goal_Personnel_Monthly() { var saved = await _repository.GetGoalsAsync(new GoalSearchRequest()); Assert.NotNull(saved); Assert.IsEmpty(saved); var startDate = new DateTime(2021, 3, 1); var endDate = (new DateTime(2021, 3, 1)).EndOfMonth(); // This department goal shouldn't be returned var departmentGoal = new Goal() { Product = Product.NewChecking, Value = 10, DepartmentId = 1, StartDate = startDate, EndDate = endDate }; var goal = new Goal() { Product = Product.NewChecking, Value = 10, DepartmentId = 1, StartDate = startDate, EndDate = endDate, EmployeeId = "programming@landmarkcu.com" }; // Not the same person. var goalx = new Goal() { Product = Product.NewChecking, Value = 10, DepartmentId = 1, StartDate = startDate, EndDate = endDate, EmployeeId = "xyz@landmarkcu.com" }; // Not the same department var goal2 = new Goal() { Product = Product.NewChecking, Value = 8, DepartmentId = 2, StartDate = startDate, EndDate = endDate, EmployeeId = "programming@landmarkcu.com" }; // not the same month var goal3 = new Goal() { Product = Product.NewChecking, Value = 11, DepartmentId = 1, StartDate = new DateTime(2021, 4, 1), EndDate = (new DateTime(2021, 4, 1)).EndOfMonth(), EmployeeId = "programming@landmarkcu.com" }; await _repository.SaveGoalAsync(departmentGoal); await _repository.SaveGoalAsync(goal); await _repository.SaveGoalAsync(goalx); await _repository.SaveGoalAsync(goal2); await _repository.SaveGoalAsync(goal3); await _context.SaveChangesAsync(); var users = new List() { new AppUser() { AdEmail = "programming@landmarkcu.com", EmployeeId = "programming@landmarkcu.com", FullName = "John McTester", HomeDepartment = "QA", BaseRole = "Tester", Role = Role.Developer, } }; var foundGoals = await _repository.FindMonthlyPersonnelGoals(1, users, startDate.Year, startDate.Month); Assert.NotNull(foundGoals); Assert.IsNotEmpty(foundGoals); Assert.AreEqual(1, foundGoals.Count); Assert.AreEqual(10, foundGoals.First().Value); } [Test] public async Task Find_Goal_Personnel_Month_Range() { var saved = await _repository.GetGoalsAsync(new GoalSearchRequest()); Assert.NotNull(saved); Assert.IsEmpty(saved); var startDate = new DateTime(2021, 3, 1); var endDate = (new DateTime(2021, 3, 1)).EndOfMonth(); // This department goal shouldn't be returned var departmentGoal = new Goal() { Product = Product.NewChecking, Value = 10, DepartmentId = 1, StartDate = startDate, EndDate = endDate }; var goal = new Goal() { Product = Product.NewChecking, Value = 10, DepartmentId = 1, StartDate = startDate, EndDate = endDate, EmployeeId = "programming@landmarkcu.com" }; // Not the same person. var goalx = new Goal() { Product = Product.NewChecking, Value = 10, DepartmentId = 1, StartDate = startDate, EndDate = endDate, EmployeeId = "xyz@landmarkcu.com" }; // Not the same department var goal2 = new Goal() { Product = Product.NewChecking, Value = 8, DepartmentId = 2, StartDate = startDate, EndDate = endDate, EmployeeId = "programming@landmarkcu.com" }; // not the same month var goal3 = new Goal() { Product = Product.NewChecking, Value = 11, DepartmentId = 1, StartDate = new DateTime(2021, 4, 1), EndDate = (new DateTime(2021, 4, 1)).EndOfMonth(), EmployeeId = "programming@landmarkcu.com" }; // not in month range var goal4 = new Goal() { Product = Product.NewChecking, Value = 12, DepartmentId = 1, StartDate = new DateTime(2021, 5, 1), EndDate = (new DateTime(2021, 5, 1)).EndOfMonth(), EmployeeId = "programming@landmarkcu.com" }; await _repository.SaveGoalAsync(departmentGoal); await _repository.SaveGoalAsync(goal); await _repository.SaveGoalAsync(goalx); await _repository.SaveGoalAsync(goal2); await _repository.SaveGoalAsync(goal3); await _repository.SaveGoalAsync(goal4); await _context.SaveChangesAsync(); var users = new List() { new AppUser() { AdEmail = "programming@landmarkcu.com", EmployeeId = "programming@landmarkcu.com", FullName = "John McTester", HomeDepartment = "QA", BaseRole = "Tester", Role = Role.Developer, } }; var range = new MonthRange ( startDate.Year, startDate.Month, endDate.Year, endDate.Month + 1 ); var foundGoals = await _repository.FindPersonnelGoalsByMonthRange(1, users, range); Assert.NotNull(foundGoals); Assert.IsNotEmpty(foundGoals); Assert.AreEqual(2, foundGoals.Count); Assert.AreEqual(10, foundGoals.First().Value); Assert.AreEqual(11, foundGoals.ElementAt(1).Value); } [Test] public async Task FindDepartmentGoalsByMonthRange() { var saved = await _repository.GetGoalsAsync(new GoalSearchRequest()); Assert.NotNull(saved); Assert.IsEmpty(saved); var startDate = new DateTime(2021, 3, 1); var endDate = (new DateTime(2021, 3, 1)).EndOfMonth(); var goalInRange = new Goal() { Product = Product.NewChecking, Value = 10, DepartmentId = 1, StartDate = startDate, EndDate = endDate }; var goalNotInRange = new Goal() { Product = Product.ATMCard, Value = 2, DepartmentId = 1, StartDate = startDate.AddMonths(-2), EndDate = endDate.AddMonths(-2) }; var goalWithPersonId = new Goal() { Product = Product.CheckCard, Value = 2, DepartmentId = 1, StartDate = startDate, EndDate = endDate, EmployeeId = "E1" }; var goalForOtherDepartment = new Goal() { Product = Product.CreditCard, Value = 2, DepartmentId = 2, StartDate = startDate, EndDate = endDate }; var range = new MonthRange(2021, 3, 2021, 4); await _repository.SaveGoalAsync(goalInRange); await _repository.SaveGoalAsync(goalNotInRange); await _repository.SaveGoalAsync(goalWithPersonId); await _repository.SaveGoalAsync(goalForOtherDepartment); await _context.SaveChangesAsync(); var found = await _repository.FindDepartmentGoalsByMonthRange(1, range); Assert.NotNull(found); Assert.IsNotEmpty(found); Assert.AreEqual(1, found.Count); Assert.AreEqual(10, found.First().Value); } } }