More unit tests

pull/3895/head
Jamie Rees 6 years ago
parent 90b38e09ec
commit b98f5607d6

@ -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<IMovieRequestRepository>();
Rule = new ExistingMovieRequestRule(ContextMock.Object);
}
private ExistingMovieRequestRule Rule { get; set; }
private Mock<IMovieRequestRepository> ContextMock { get; set; }
[Test]
public async Task ExistingRequestRule_Movie_Has_Been_Requested_With_TheMovieDBId()
{
ContextMock.Setup(x => x.GetAll()).Returns(new List<MovieRequests>
{
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<MovieRequests>
{
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<MovieRequests>
{
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);
}
}
}

@ -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<IExternalRepository<LidarrAlbumCache>>();
Rule = new LidarrAlbumCacheRule(ContextMock.Object);
}
private LidarrAlbumCacheRule Rule { get; set; }
private Mock<IExternalRepository<LidarrAlbumCache>> 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<LidarrAlbumCache>
{
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<LidarrAlbumCache>
{
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<LidarrAlbumCache>
{
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<LidarrAlbumCache>
{
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));
}
}
}

@ -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<IExternalRepository<LidarrArtistCache>>();
Rule = new LidarrArtistCacheRule(ContextMock.Object);
}
private LidarrArtistCacheRule Rule { get; set; }
private Mock<IExternalRepository<LidarrArtistCache>> 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<LidarrArtistCache>
{
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<LidarrArtistCache>
{
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);
}
}
}

@ -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");
}

@ -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);
}

@ -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))]

Loading…
Cancel
Save