From b98f5607d67b95c0ced4478da7e2d671b70c0533 Mon Sep 17 00:00:00 2001 From: Jamie Rees Date: Thu, 18 Apr 2019 13:58:19 +0100 Subject: [PATCH] More unit tests --- .../Request/ExistingMovieRequestRuleTests.cs | 100 ++++++++++++++ .../Rule/Search/LidarrAlbumCacheRuleTests.cs | 126 ++++++++++++++++++ .../Rule/Search/LidarrArtistCacheRuleTests.cs | 74 ++++++++++ .../Rules/Request/ExistingMovieRequestRule.cs | 20 ++- .../Rules/Request/ExistingPlexRequestRule.cs | 4 +- .../Entities/Requests/MovieRequests.cs | 4 + 6 files changed, 325 insertions(+), 3 deletions(-) create mode 100644 src/Ombi.Core.Tests/Rule/Request/ExistingMovieRequestRuleTests.cs create mode 100644 src/Ombi.Core.Tests/Rule/Search/LidarrAlbumCacheRuleTests.cs create mode 100644 src/Ombi.Core.Tests/Rule/Search/LidarrArtistCacheRuleTests.cs diff --git a/src/Ombi.Core.Tests/Rule/Request/ExistingMovieRequestRuleTests.cs b/src/Ombi.Core.Tests/Rule/Request/ExistingMovieRequestRuleTests.cs new file mode 100644 index 000000000..7ff69c9f2 --- /dev/null +++ b/src/Ombi.Core.Tests/Rule/Request/ExistingMovieRequestRuleTests.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Principal; +using System.Threading.Tasks; +using MockQueryable.Moq; +using Moq; +using NUnit.Framework; +using Ombi.Core.Authentication; +using Ombi.Core.Rule.Rules; +using Ombi.Core.Rule.Rules.Request; +using Ombi.Helpers; +using Ombi.Store.Entities; +using Ombi.Store.Entities.Requests; +using Ombi.Store.Repository.Requests; +using Ombi.Test.Common; + +namespace Ombi.Core.Tests.Rule.Request +{ + public class ExistingMovieRequestRuleTests + { + + [SetUp] + public void Setup() + { + ContextMock = new Mock(); + Rule = new ExistingMovieRequestRule(ContextMock.Object); + } + + + private ExistingMovieRequestRule Rule { get; set; } + private Mock ContextMock { get; set; } + + [Test] + public async Task ExistingRequestRule_Movie_Has_Been_Requested_With_TheMovieDBId() + { + ContextMock.Setup(x => x.GetAll()).Returns(new List + { + new MovieRequests + { + TheMovieDbId = 1, + RequestType = RequestType.Movie + } + }.AsQueryable().BuildMock().Object); + var o = new MovieRequests + { + TheMovieDbId = 1, + }; + var result = await Rule.Execute(o); + + Assert.That(result.Success, Is.False); + Assert.That(result.Message, Is.Not.Empty); + } + + [Test] + public async Task ExistingRequestRule_Movie_Has_Been_Requested_With_ImdbId() + { + ContextMock.Setup(x => x.GetAll()).Returns(new List + { + new MovieRequests + { + TheMovieDbId = 11111, + ImdbId = 1.ToString(), + RequestType = RequestType.Movie + } + }.AsQueryable().BuildMock().Object); + var o = new MovieRequests + { + ImdbId = 1.ToString(), + }; + var result = await Rule.Execute(o); + + Assert.That(result.Success, Is.False); + Assert.That(result.Message, Is.Not.Empty); + } + + [Test] + public async Task ExistingRequestRule_Movie_HasNot_Been_Requested() + { + ContextMock.Setup(x => x.GetAll()).Returns(new List + { + new MovieRequests + { + TheMovieDbId = 2, + ImdbId = "2", + RequestType = RequestType.Movie + } + }.AsQueryable().BuildMock().Object); + var o = new MovieRequests + { + TheMovieDbId = 1, + ImdbId = "1" + }; + var result = await Rule.Execute(o); + + Assert.That(result.Success, Is.True); + Assert.That(result.Message, Is.Null.Or.Empty); + } + } +} diff --git a/src/Ombi.Core.Tests/Rule/Search/LidarrAlbumCacheRuleTests.cs b/src/Ombi.Core.Tests/Rule/Search/LidarrAlbumCacheRuleTests.cs new file mode 100644 index 000000000..6b04a57b7 --- /dev/null +++ b/src/Ombi.Core.Tests/Rule/Search/LidarrAlbumCacheRuleTests.cs @@ -0,0 +1,126 @@ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading.Tasks; +using Moq; +using NUnit.Framework; +using Ombi.Core.Models.Search; +using Ombi.Core.Rule.Rules.Search; +using Ombi.Store.Entities; +using Ombi.Store.Repository; + +namespace Ombi.Core.Tests.Rule.Search +{ + public class LidarrAlbumCacheRuleTests + { + [SetUp] + public void Setup() + { + ContextMock = new Mock>(); + Rule = new LidarrAlbumCacheRule(ContextMock.Object); + + } + + private LidarrAlbumCacheRule Rule { get; set; } + private Mock> ContextMock { get; set; } + + [Test] + public async Task Should_Not_Be_Monitored_Or_Available() + { + var request = new SearchAlbumViewModel { ForeignAlbumId = "abc" }; + var result = await Rule.Execute(request); + + Assert.True(result.Success); + Assert.False(request.Approved); + Assert.False(request.Monitored); + } + + [Test] + public async Task Should_Be_Monitored_But_Not_Available() + { + ContextMock.Setup(x => x.GetAll()).Returns(new List + { + new LidarrAlbumCache + { + ForeignAlbumId = "abc", + PercentOfTracks = 0 + } + }.AsQueryable()); + var request = new SearchAlbumViewModel { ForeignAlbumId = "abc" }; + var result = await Rule.Execute(request); + + Assert.True(result.Success); + Assert.False(request.Approved); + Assert.True(request.Monitored); + Assert.That(request.PartiallyAvailable, Is.EqualTo(false)); + Assert.That(request.Available, Is.EqualTo(false)); + Assert.That(request.FullyAvailable, Is.EqualTo(false)); + } + + [Test] + public async Task Should_Be_Monitored_And_Partly_Available() + { + ContextMock.Setup(x => x.GetAll()).Returns(new List + { + new LidarrAlbumCache + { + ForeignAlbumId = "abc", + PercentOfTracks = 1 + } + }.AsQueryable()); + var request = new SearchAlbumViewModel { ForeignAlbumId = "abc" }; + var result = await Rule.Execute(request); + + Assert.True(result.Success); + Assert.False(request.Approved); + Assert.True(request.Monitored); + Assert.That(request.PartiallyAvailable, Is.EqualTo(true)); + Assert.That(request.Available, Is.EqualTo(false)); + Assert.That(request.FullyAvailable, Is.EqualTo(false)); + } + + [Test] + public async Task Should_Be_Monitored_And_Fully_Available() + { + ContextMock.Setup(x => x.GetAll()).Returns(new List + { + new LidarrAlbumCache + { + ForeignAlbumId = "abc", + PercentOfTracks = 100 + } + }.AsQueryable()); + var request = new SearchAlbumViewModel { ForeignAlbumId = "abc" }; + var result = await Rule.Execute(request); + + Assert.True(result.Success); + Assert.False(request.Approved); + Assert.True(request.Monitored); + Assert.That(request.PartiallyAvailable, Is.EqualTo(false)); + Assert.That(request.FullyAvailable, Is.EqualTo(true)); + } + + [Test] + public async Task Should_Be_Monitored_And_Fully_Available_Casing() + { + ContextMock.Setup(x => x.GetAll()).Returns(new List + { + new LidarrAlbumCache + { + ForeignAlbumId = "abc", + PercentOfTracks = 100 + } + }.AsQueryable()); + var request = new SearchAlbumViewModel { ForeignAlbumId = "ABC" }; + var result = await Rule.Execute(request); + + Assert.True(result.Success); + Assert.False(request.Approved); + Assert.True(request.Monitored); + Assert.That(request.PartiallyAvailable, Is.EqualTo(false)); + Assert.That(request.FullyAvailable, Is.EqualTo(true)); + } + } +} diff --git a/src/Ombi.Core.Tests/Rule/Search/LidarrArtistCacheRuleTests.cs b/src/Ombi.Core.Tests/Rule/Search/LidarrArtistCacheRuleTests.cs new file mode 100644 index 000000000..7a2da1e25 --- /dev/null +++ b/src/Ombi.Core.Tests/Rule/Search/LidarrArtistCacheRuleTests.cs @@ -0,0 +1,74 @@ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading.Tasks; +using Moq; +using NUnit.Framework; +using Ombi.Core.Models.Search; +using Ombi.Core.Rule.Rules.Search; +using Ombi.Store.Entities; +using Ombi.Store.Repository; + +namespace Ombi.Core.Tests.Rule.Search +{ + public class LidarrArtistCacheRuleTests + { + [SetUp] + public void Setup() + { + ContextMock = new Mock>(); + Rule = new LidarrArtistCacheRule(ContextMock.Object); + } + + private LidarrArtistCacheRule Rule { get; set; } + private Mock> ContextMock { get; set; } + + [Test] + public async Task Should_Not_Be_Monitored() + { + var request = new SearchArtistViewModel { ForignArtistId = "abc" }; + var result = await Rule.Execute(request); + + Assert.True(result.Success); + Assert.False(request.Monitored); + } + + [Test] + public async Task Should_Be_Monitored() + { + ContextMock.Setup(x => x.GetAll()).Returns(new List + { + new LidarrArtistCache + { + ForeignArtistId = "abc", + } + }.AsQueryable()); + var request = new SearchArtistViewModel { ForignArtistId = "abc" }; + var result = await Rule.Execute(request); + + Assert.True(result.Success); + Assert.True(request.Monitored); + } + + + [Test] + public async Task Should_Be_Monitored_Casing() + { + ContextMock.Setup(x => x.GetAll()).Returns(new List + { + new LidarrArtistCache + { + ForeignArtistId = "abc", + } + }.AsQueryable()); + var request = new SearchArtistViewModel { ForignArtistId = "ABC" }; + var result = await Rule.Execute(request); + + Assert.True(result.Success); + Assert.True(request.Monitored); + } + + } +} diff --git a/src/Ombi.Core/Rule/Rules/Request/ExistingMovieRequestRule.cs b/src/Ombi.Core/Rule/Rules/Request/ExistingMovieRequestRule.cs index d48245e9b..bc580e9ea 100644 --- a/src/Ombi.Core/Rule/Rules/Request/ExistingMovieRequestRule.cs +++ b/src/Ombi.Core/Rule/Rules/Request/ExistingMovieRequestRule.cs @@ -1,6 +1,8 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Ombi.Core.Rule.Interfaces; +using Ombi.Helpers; using Ombi.Store.Entities; using Ombi.Store.Entities.Requests; using Ombi.Store.Repository; @@ -28,8 +30,24 @@ namespace Ombi.Core.Rule.Rules.Request { var movie = (MovieRequests) obj; var movieRequests = Movie.GetAll(); + var found = false; var existing = await movieRequests.FirstOrDefaultAsync(x => x.TheMovieDbId == movie.TheMovieDbId); if (existing != null) // Do we already have a request for this? + { + found = true; + } + + if (!found && movie.ImdbId.HasValue()) + { + // Let's check imdbid + existing = await movieRequests.FirstOrDefaultAsync(x => + x.ImdbId.Equals(movie.ImdbId, StringComparison.CurrentCultureIgnoreCase)); + if (existing != null) + { + found = true; + } + } + if(found) { return Fail($"\"{obj.Title}\" has already been requested"); } diff --git a/src/Ombi.Core/Rule/Rules/Request/ExistingPlexRequestRule.cs b/src/Ombi.Core/Rule/Rules/Request/ExistingPlexRequestRule.cs index 5d7658c83..0d887063e 100644 --- a/src/Ombi.Core/Rule/Rules/Request/ExistingPlexRequestRule.cs +++ b/src/Ombi.Core/Rule/Rules/Request/ExistingPlexRequestRule.cs @@ -32,7 +32,7 @@ namespace Ombi.Core.Rule.Rules.Request var tvContent = _plexContent.GetAll().Where(x => x.Type == PlexMediaTypeEntity.Show); // We need to do a check on the TVDBId - var anyTvDbMatches = await tvContent.Include(x => x.Episodes).FirstOrDefaultAsync(x => x.HasTvDb && x.TvDbId.Equals(tvRequest.Id.ToString())); // the Id on the child is the tvdbid at this point + var anyTvDbMatches = await tvContent.Include(x => x.Episodes).FirstOrDefaultAsync(x => x.HasTvDb && x.TvDbId.Equals(tvRequest.Id.ToString(), StringComparison.InvariantCultureIgnoreCase)); // the Id on the child is the tvdbid at this point if (anyTvDbMatches == null) { // So we do not have a TVDB Id, that really sucks. @@ -42,7 +42,7 @@ namespace Ombi.Core.Rule.Rules.Request && x.ReleaseYear == tvRequest.ReleaseYear.Year.ToString()); if (titleAndYearMatch != null) { - // We have a match! Suprise Motherfucker + // We have a match! Surprise Motherfucker return CheckExistingContent(tvRequest, titleAndYearMatch); } diff --git a/src/Ombi.Store/Entities/Requests/MovieRequests.cs b/src/Ombi.Store/Entities/Requests/MovieRequests.cs index 677a4292c..0ccf55f0b 100644 --- a/src/Ombi.Store/Entities/Requests/MovieRequests.cs +++ b/src/Ombi.Store/Entities/Requests/MovieRequests.cs @@ -8,6 +8,10 @@ namespace Ombi.Store.Entities.Requests [Table("MovieRequests")] public class MovieRequests : FullBaseRequest { + public MovieRequests() + { + RequestType = RequestType.Movie; + } public int TheMovieDbId { get; set; } public int? IssueId { get; set; } [ForeignKey(nameof(IssueId))]