public void GetQueues_Success(Entity.Models.NotificationQueueFilter filter, int expectedResult, int expectedTotal) { // Arrange var helper = new TestHelper(); var user = PrincipalHelper.CreateForPermission(Permissions.SystemAdmin); var init = helper.CreatePimsContext(user, true); var template = init.CreateNotificationTemplate(1, "test"); var notifications = init.CreateNotificationQueues(1, 20, template); var project = init.CreateProject(1); project.ProjectNumber = "test-pass"; notifications.ForEach(n => n.SendOn = DateTime.MinValue); notifications.Next(0).ToAgencyId = 1; notifications.Next(1).ProjectId = 1; notifications.Next(2).Status = Entity.NotificationStatus.Failed; notifications.Next(3).To = "*****@*****.**"; notifications.Next(4).Key = new Guid("8d1a35b3-6280-4103-93f5-792f8954bef8"); notifications.Next(5).Subject = "-find-"; notifications.Next(6).Body = "-find-"; notifications.Next(6).Tag = "-find-"; notifications.Next(7).SendOn = new DateTime(2020, 1, 1); notifications.Next(8).SendOn = new DateTime(2020, 1, 2); notifications.Next(9).Project = project; notifications.Next(10).UpdatedOn = DateTime.UtcNow.AddDays(-1); notifications.Next(11).UpdatedOn = DateTime.UtcNow.AddDays(1); init.AddAndSaveRange(notifications); var service = helper.CreateService <NotificationQueueService>(user); // Act var result = service.GetPage(filter); // Assert Assert.NotNull(result); Assert.IsAssignableFrom <Entity.Models.Paged <Entity.NotificationQueue> >(result); result.Should().HaveCount(expectedResult); result.Total.Should().Be(expectedTotal); }
/// <summary> /// Generate a query for the specified 'filter'. /// </summary> /// <param name="context"></param> /// <param name="user"></param> /// <param name="filter"></param> /// <returns></returns> public static IQueryable <Entity.NotificationQueue> GenerateQuery(this PimsContext context, ClaimsPrincipal user, Entity.Models.NotificationQueueFilter filter) { filter.ThrowIfNull(nameof(user)); filter.ThrowIfNull(nameof(filter)); var query = context.NotificationQueue.AsNoTracking(); if (filter.Key.HasValue) { query = query.Where(p => p.Key == filter.Key); } if (filter.Status.HasValue) { query = query.Where(p => p.Status == filter.Status); } if (filter.ProjectId.HasValue) { query = query.Where(p => p.ProjectId == filter.ProjectId); } if (filter.AgencyId.HasValue) { query = query.Where(p => p.ToAgencyId == filter.AgencyId); } if (!String.IsNullOrWhiteSpace(filter.ProjectNumber)) { query = query.Where(p => p.Project.ProjectNumber == filter.ProjectNumber); } if (!String.IsNullOrWhiteSpace(filter.To)) { query = query.Where(p => EF.Functions.Like(p.To, $"%{filter.To}%")); } if (!String.IsNullOrWhiteSpace(filter.Subject)) { query = query.Where(p => EF.Functions.Like(p.Subject, $"%{filter.Subject}%")); } if (!String.IsNullOrWhiteSpace(filter.Body)) { query = query.Where(p => EF.Functions.Like(p.Body, $"%{filter.Body}%")); } if (!String.IsNullOrWhiteSpace(filter.Tag)) { query = query.Where(p => EF.Functions.Like(p.Tag, $"%{filter.Tag}%")); } if (filter.MinSendOn.HasValue) { query = query.Where(p => p.SendOn >= filter.MinSendOn); } if (filter.MaxSendOn.HasValue) { query = query.Where(p => p.SendOn <= filter.MaxSendOn); } if (filter.Sort?.Any() == true) { query = query.OrderByProperty(filter.Sort); } else { query = query.OrderBy(p => p.SendOn).ThenBy(p => p.Status); } return(query); }