bug(requests): 🐛 Fixed the requests lists, they now will show the status for 4K and regular requests

pull/4511/head
tidusjar 3 years ago
parent 3752304e79
commit ae8783dfc4

@ -1,135 +1,514 @@
//using System.Collections.Generic;
//using System.Linq;
//using System.Threading.Tasks;
//using Moq;
//using NUnit.Framework;
//using Ombi.Core.Engine;
//using Ombi.Core.Models.Requests;
//using Ombi.Store.Entities.Requests;
//using Ombi.Store.Repository;
//using Assert = Xunit.Assert;
//namespace Ombi.Core.Tests.Engine
//{
// [TestFixture]
// public class MovieRequestEngineTests
// {
// public MovieRequestEngineTests()
// {
// RequestService = new Mock<IMovieRequestRepository>();
// var requestService = new RequestService(null, RequestService.Object);
// Engine = new MovieRequestEngine(null, requestService, null, null, null, null, null, null);
// }
// private MovieRequestEngine Engine { get; }
// private Mock<IMovieRequestRepository> RequestService { get; }
// [Test]
// public async Task GetNewRequests_Should_ReturnEmpty_WhenThereAreNoNewRequests()
// {
// var requests = new List<MovieRequests>
// {
// new MovieRequests { Available = true },
// new MovieRequests { Approved = true },
// };
// var r = DbHelper.GetQueryable(requests[0], requests[1]);
// RequestService.Setup(x => x.Get()).Returns(r);
// var result = await Engine.GetNewRequests();
// Assert.False(result.Any());
// }
// [Test]
// public async Task GetNewRequests_Should_ReturnOnlyNewRequests_WhenThereAreMultipleRequests()
// {
// var requests = new List<MovieRequests>
// {
// new MovieRequests { Available = true },
// new MovieRequests { Approved = true },
// new MovieRequests(),
// };
// RequestService.Setup(x => x.Get()).Returns(requests.AsQueryable);
// var result = await Engine.GetNewRequests();
// Assert.Single(result);
// Assert.All(result, x =>
// {
// Assert.False(x.Available);
// Assert.False(x.Approved);
// });
// }
// [Test]
// public async Task GetApprovedRequests_Should_ReturnEmpty_WhenThereAreNoApprovedRequests()
// {
// var requests = new List<MovieRequests>
// {
// new MovieRequests { Available = true },
// };
// RequestService.Setup(x => x.Get()).Returns(requests.AsQueryable);
// var result = await Engine.GetApprovedRequests();
// Assert.False(result.Any());
// }
// [Test]
// public async Task GetApprovedRequests_Should_ReturnOnlyApprovedRequests_WhenThereAreMultipleRequests()
// {
// var requests = new List<MovieRequests>
// {
// new MovieRequests { Available = true },
// new MovieRequests { Approved = true },
// new MovieRequests(),
// };
// RequestService.Setup(x => x.Get()).Returns(requests.AsQueryable);
// var result = await Engine.GetApprovedRequests();
// Assert.Single(result);
// Assert.All(result, x =>
// {
// Assert.False(x.Available);
// Assert.True(x.Approved);
// });
// }
// [Test]
// public async Task GetAvailableRequests_Should_ReturnEmpty_WhenThereAreNoAvailableRequests()
// {
// var requests = new List<MovieRequests>
// {
// new MovieRequests { Approved = true },
// };
// RequestService.Setup(x => x.Get()).Returns(requests.AsQueryable);
// var result = await Engine.GetAvailableRequests();
// Assert.False(result.Any());
// }
// [Test]
// public async Task GetAvailableRequests_Should_ReturnOnlyAvailableRequests_WhenThereAreMultipleRequests()
// {
// var requests = new List<MovieRequests>
// {
// new MovieRequests { Available = true },
// new MovieRequests { Approved = true },
// new MovieRequests(),
// };
// RequestService.Setup(x => x.Get()).Returns(requests.AsQueryable);
// var result = await Engine.GetAvailableRequests();
// Assert.Single(result);
// Assert.All(result, x =>
// {
// Assert.True(x.Available);
// Assert.False(x.Approved);
// });
// }
// }
//}
using MockQueryable.Moq;
using Moq;
using Moq.AutoMock;
using NUnit.Framework;
using Ombi.Core.Authentication;
using Ombi.Core.Engine;
using Ombi.Core.Models.Requests;
using Ombi.Store.Entities;
using Ombi.Store.Entities.Requests;
using Ombi.Store.Repository;
using Ombi.Store.Repository.Requests;
using Ombi.Test.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Threading.Tasks;
namespace Ombi.Core.Tests.Engine
{
[TestFixture]
public class MovieRequestEngineTests
{
private MovieRequestEngine _subject;
private Mock<IMovieRequestRepository> _repoMock;
private AutoMocker _mocker;
[SetUp]
public void Setup()
{
_mocker = new AutoMocker();
var userManager = MockHelper.MockUserManager(new List<OmbiUser> { new OmbiUser { NormalizedUserName = "TEST", Id = "a" } });
userManager.Setup(x => x.IsInRoleAsync(It.IsAny<OmbiUser>(), It.IsAny<string>())).ReturnsAsync(true);
var principle = new Mock<IPrincipal>();
var identity = new Mock<IIdentity>();
identity.Setup(x => x.Name).Returns("Test");
principle.Setup(x => x.Identity).Returns(identity.Object);
_repoMock = new Mock<IMovieRequestRepository>();
var requestServiceMock = new Mock<IRequestServiceMain>();
requestServiceMock.Setup(x => x.MovieRequestService).Returns(_repoMock.Object);
_mocker.Use(principle.Object);
_mocker.Use(userManager.Object);
_mocker.Use(requestServiceMock);
_subject = _mocker.CreateInstance<MovieRequestEngine>();
var list = DbHelper.GetQueryableMockDbSet(new RequestSubscription());
_mocker.Setup<IRepository<RequestSubscription>, IQueryable<RequestSubscription>>(x => x.GetAll()).Returns(new List<RequestSubscription>().AsQueryable().BuildMock().Object);
}
[Test]
public async Task GetRequestByStatus_PendingRequests_Non4K()
{
var movies = RegularRequestData();
_repoMock.Setup(x => x.GetWithUser()).Returns(movies.AsQueryable());
var result = await _subject.GetRequestsByStatus(10, 0, "id", "asc", Models.Requests.RequestStatus.PendingApproval);
Assert.That(result.Total, Is.EqualTo(1));
Assert.That(result.Collection.First().Id, Is.EqualTo(4));
}
[Test]
public async Task GetRequestByStatus_PendingRequests_4K()
{
var movies = new List<MovieRequests>
{
new MovieRequests
{
Id= 1,
Approved4K = true,
Has4KRequest = true,
RequestedDate = DateTime.MinValue
},
new MovieRequests
{
Id = 2,
Approved4K = false,
Available4K = true,
Has4KRequest = true,
RequestedDate = DateTime.MinValue
},
new MovieRequests
{
Id = 3,
Denied4K = true,
Has4KRequest = true,
RequestedDate = DateTime.MinValue
},
new MovieRequests
{
Id = 4,
Has4KRequest = true,
Approved4K = false,
Available4K = false,
Denied4K = false,
RequestedDate = DateTime.MinValue
}
};
_repoMock.Setup(x => x.GetWithUser()).Returns(movies.AsQueryable());
var result = await _subject.GetRequestsByStatus(10, 0, "id", "asc", RequestStatus.PendingApproval);
Assert.That(result.Total, Is.EqualTo(1));
Assert.That(result.Collection.First().Id, Is.EqualTo(4));
}
[Test]
public async Task GetRequestByStatus_PendingRequests_Both4K_And_Regular()
{
var movies = new List<MovieRequests>
{
new MovieRequests
{
Id= 1,
Approved = false,
Approved4K = true,
Has4KRequest = true,
RequestedDate = DateTime.Now
},
new MovieRequests
{
Id = 2,
Approved4K = false,
Available4K = true,
Has4KRequest = true,
RequestedDate = DateTime.MinValue
},
new MovieRequests
{
Id = 3,
Denied4K = true,
Has4KRequest = true,
RequestedDate = DateTime.MinValue
},
new MovieRequests
{
Id = 4,
Has4KRequest = true,
Approved4K = false,
Available4K = false,
Denied4K = false,
RequestedDate = DateTime.MinValue
}
};
_repoMock.Setup(x => x.GetWithUser()).Returns(movies.AsQueryable());
var result = await _subject.GetRequestsByStatus(10, 0, "id", "asc", RequestStatus.PendingApproval);
Assert.That(result.Total, Is.EqualTo(2));
Assert.That(result.Collection.First().Id, Is.EqualTo(1));
Assert.That(result.Collection.ToArray()[1].Id, Is.EqualTo(4));
}
[Test]
public async Task GetRequestByStatus_ProcessingRequests_Non4K()
{
var movies = RegularRequestData();
_repoMock.Setup(x => x.GetWithUser()).Returns(movies.AsQueryable());
var result = await _subject.GetRequestsByStatus(10, 0, "id", "asc", Models.Requests.RequestStatus.ProcessingRequest);
Assert.That(result.Total, Is.EqualTo(1));
Assert.That(result.Collection.First().Id, Is.EqualTo(1));
}
[Test]
public async Task GetRequestByStatus_ProcessingRequests_4K()
{
var movies = new List<MovieRequests>
{
new MovieRequests
{
Id= 1,
Approved4K = true,
Has4KRequest = true,
RequestedDate = DateTime.MinValue
},
new MovieRequests
{
Id = 2,
Approved4K = false,
Available4K = true,
Has4KRequest = true,
RequestedDate = DateTime.MinValue
},
new MovieRequests
{
Id = 3,
Denied4K = true,
Has4KRequest = true,
RequestedDate = DateTime.MinValue
},
new MovieRequests
{
Id = 4,
Has4KRequest = true,
Approved4K = false,
Available4K = false,
Denied4K = false,
RequestedDate = DateTime.MinValue
}
};
_repoMock.Setup(x => x.GetWithUser()).Returns(movies.AsQueryable());
var result = await _subject.GetRequestsByStatus(10, 0, "id", "asc", RequestStatus.ProcessingRequest);
Assert.That(result.Total, Is.EqualTo(1));
Assert.That(result.Collection.First().Id, Is.EqualTo(1));
}
[Test]
public async Task GetRequestByStatus_ProcessingRequests_Both4K_And_Regular()
{
var movies = new List<MovieRequests>
{
new MovieRequests
{
Id= 1,
Approved = false,
Approved4K = true,
Has4KRequest = true,
RequestedDate = DateTime.Now
},
new MovieRequests
{
Id = 2,
Approved4K = false,
Available4K = true,
Has4KRequest = true,
RequestedDate = DateTime.MinValue
},
new MovieRequests
{
Id = 3,
Denied4K = true,
Has4KRequest = true,
RequestedDate = DateTime.MinValue
},
new MovieRequests
{
Id = 4,
Has4KRequest = true,
Approved4K = false,
Approved = true,
Available4K = false,
Denied4K = false,
RequestedDate = DateTime.Now
}
};
_repoMock.Setup(x => x.GetWithUser()).Returns(movies.AsQueryable());
var result = await _subject.GetRequestsByStatus(10, 0, "id", "asc", RequestStatus.ProcessingRequest);
Assert.That(result.Total, Is.EqualTo(2));
Assert.That(result.Collection.First().Id, Is.EqualTo(1));
Assert.That(result.Collection.ToArray()[1].Id, Is.EqualTo(4));
}
[Test]
public async Task GetRequestByStatus_AvailableRequests_Non4K()
{
List<MovieRequests> movies = RegularRequestData();
_repoMock.Setup(x => x.GetWithUser()).Returns(movies.AsQueryable());
var result = await _subject.GetRequestsByStatus(10, 0, "id", "asc", Models.Requests.RequestStatus.Available);
Assert.That(result.Total, Is.EqualTo(1));
Assert.That(result.Collection.First().Id, Is.EqualTo(2));
}
[Test]
public async Task GetRequestByStatus_AvailableRequests_4K()
{
var movies = new List<MovieRequests>
{
new MovieRequests
{
Id= 1,
Approved4K = true,
Has4KRequest = true,
RequestedDate = DateTime.MinValue
},
new MovieRequests
{
Id = 2,
Approved4K = false,
Available4K = true,
Has4KRequest = true,
RequestedDate = DateTime.MinValue
},
new MovieRequests
{
Id = 3,
Denied4K = true,
Has4KRequest = true,
RequestedDate = DateTime.MinValue
},
new MovieRequests
{
Id = 4,
Has4KRequest = true,
Approved4K = false,
Available4K = false,
Denied4K = false,
RequestedDate = DateTime.MinValue
}
};
_repoMock.Setup(x => x.GetWithUser()).Returns(movies.AsQueryable());
var result = await _subject.GetRequestsByStatus(10, 0, "id", "asc", RequestStatus.Available);
Assert.That(result.Total, Is.EqualTo(1));
Assert.That(result.Collection.First().Id, Is.EqualTo(2));
}
[Test]
public async Task GetRequestByStatus_AvailableRequests_Both4K_And_Regular()
{
var movies = new List<MovieRequests>
{
new MovieRequests
{
Id= 1,
Available = true,
Approved = false,
Approved4K = true,
Has4KRequest = true,
RequestedDate = DateTime.Now
},
new MovieRequests
{
Id = 2,
Approved4K = false,
Available4K = true,
Has4KRequest = true,
RequestedDate = DateTime.MinValue
},
new MovieRequests
{
Id = 3,
Denied4K = true,
Has4KRequest = true,
RequestedDate = DateTime.MinValue
},
new MovieRequests
{
Id = 4,
Has4KRequest = true,
Approved4K = false,
Approved = true,
Available4K = false,
Denied4K = false,
RequestedDate = DateTime.Now
}
};
_repoMock.Setup(x => x.GetWithUser()).Returns(movies.AsQueryable());
var result = await _subject.GetRequestsByStatus(10, 0, "id", "asc", RequestStatus.Available);
Assert.That(result.Total, Is.EqualTo(2));
Assert.That(result.Collection.First().Id, Is.EqualTo(1));
Assert.That(result.Collection.ToArray()[1].Id, Is.EqualTo(2));
}
[Test]
public async Task GetRequestByStatus_DeniedRequests_Non4K()
{
List<MovieRequests> movies = RegularRequestData();
_repoMock.Setup(x => x.GetWithUser()).Returns(movies.AsQueryable());
var result = await _subject.GetRequestsByStatus(10, 0, "id", "asc", Models.Requests.RequestStatus.Denied);
Assert.That(result.Total, Is.EqualTo(1));
Assert.That(result.Collection.First().Id, Is.EqualTo(3));
}
[Test]
public async Task GetRequestByStatus_DeniedRequests_4K()
{
var movies = new List<MovieRequests>
{
new MovieRequests
{
Id= 1,
Approved4K = true,
Has4KRequest = true,
RequestedDate = DateTime.MinValue
},
new MovieRequests
{
Id = 2,
Approved4K = false,
Available4K = true,
Has4KRequest = true,
RequestedDate = DateTime.MinValue
},
new MovieRequests
{
Id = 3,
Denied4K = true,
Has4KRequest = true,
RequestedDate = DateTime.MinValue
},
new MovieRequests
{
Id = 4,
Has4KRequest = true,
Approved4K = false,
Available4K = false,
Denied4K = false,
RequestedDate = DateTime.MinValue
}
};
_repoMock.Setup(x => x.GetWithUser()).Returns(movies.AsQueryable());
var result = await _subject.GetRequestsByStatus(10, 0, "id", "asc", RequestStatus.Denied);
Assert.That(result.Total, Is.EqualTo(1));
Assert.That(result.Collection.First().Id, Is.EqualTo(3));
}
[Test]
public async Task GetRequestByStatus_DeniedRequests_Both4K_And_Regular()
{
var movies = new List<MovieRequests>
{
new MovieRequests
{
Id= 1,
Available = true,
Approved = false,
Approved4K = true,
Has4KRequest = true,
RequestedDate = DateTime.Now
},
new MovieRequests
{
Id = 2,
Approved4K = false,
Available4K = true,
Denied = true,
Has4KRequest = true,
RequestedDate = DateTime.MinValue
},
new MovieRequests
{
Id = 3,
Denied4K = true,
Has4KRequest = true,
RequestedDate = DateTime.MinValue
},
new MovieRequests
{
Id = 4,
Has4KRequest = true,
Approved4K = false,
Approved = true,
Available4K = false,
Denied4K = false,
RequestedDate = DateTime.Now
}
};
_repoMock.Setup(x => x.GetWithUser()).Returns(movies.AsQueryable());
var result = await _subject.GetRequestsByStatus(10, 0, "id", "asc", RequestStatus.Denied);
Assert.That(result.Total, Is.EqualTo(2));
Assert.That(result.Collection.First().Id, Is.EqualTo(2));
Assert.That(result.Collection.ToArray()[1].Id, Is.EqualTo(3));
}
private static List<MovieRequests> RegularRequestData()
{
return new List<MovieRequests>
{
new MovieRequests
{
Id= 1,
Approved = true,
RequestedDate = DateTime.Now
},
new MovieRequests
{
Id = 2,
Approved = false,
Available = true,
RequestedDate = DateTime.Now
},
new MovieRequests
{
Id = 3,
Denied = true,
RequestedDate = DateTime.Now
},
new MovieRequests
{
Id = 4,
Approved = false,
RequestedDate = DateTime.Now
}
};
}
}
}

@ -321,21 +321,44 @@ namespace Ombi.Core.Engine
switch (status)
{
case RequestStatus.PendingApproval:
allRequests = allRequests.Where(x => !x.Approved && !x.Available && (!x.Denied.HasValue || !x.Denied.Value));
allRequests = allRequests.Where(x =>
(x.RequestedDate != DateTime.MinValue && !x.Approved && !x.Available && (!x.Denied.HasValue || !x.Denied.Value))
||
(x.Has4KRequest && !x.Approved4K && !x.Available4K && (!x.Denied4K.HasValue || !x.Denied4K.Value))
);
break;
case RequestStatus.ProcessingRequest:
allRequests = allRequests.Where(x => x.Approved && !x.Available && (!x.Denied.HasValue || !x.Denied.Value));
allRequests = allRequests.Where(x =>
(x.RequestedDate != DateTime.MinValue && x.Approved && !x.Available && (!x.Denied.HasValue || !x.Denied.Value))
||
(x.Has4KRequest && x.Approved4K && !x.Available && (!x.Denied.HasValue || !x.Denied.Value))
);
break;
case RequestStatus.Available:
allRequests = allRequests.Where(x => x.Available);
allRequests = allRequests.Where(x => x.Available || x.Available4K);
break;
case RequestStatus.Denied:
allRequests = allRequests.Where(x => x.Denied.HasValue && x.Denied.Value && !x.Available);
allRequests = allRequests.Where(x =>
(x.Denied.HasValue && x.Denied.Value && !x.Available)
||
(x.Has4KRequest && x.Denied4K.HasValue && x.Denied4K.Value && !x.Available4K)
);
break;
default:
break;
}
var requests = allRequests.ToList();
var total = requests.Count;
if (total == 0)
{
return new RequestsViewModel<MovieRequests>
{
Collection = Enumerable.Empty<MovieRequests>(),
Total = total
};
}
var prop = TypeDescriptor.GetProperties(typeof(MovieRequests)).Find(sortProperty, true);
if (sortProperty.Contains('.'))
@ -348,11 +371,11 @@ namespace Ombi.Core.Engine
//var secondProp = TypeDescriptor.GetProperties(propType).Find(properties[1], true);
}
// TODO fix this so we execute this on the server
var requests = sortOrder.Equals("asc", StringComparison.InvariantCultureIgnoreCase)
requests = sortOrder.Equals("asc", StringComparison.InvariantCultureIgnoreCase)
? allRequests.ToList().OrderBy(x => prop.GetValue(x)).ToList()
: allRequests.ToList().OrderByDescending(x => prop.GetValue(x)).ToList();
var total = requests.Count();
// TODO fix this so we execute this on the server
requests = requests.Skip(position).Take(count).ToList();
await CheckForSubscription(shouldHide, requests);

Loading…
Cancel
Save