more tests !build

pull/3895/head
Jamie Rees 5 years ago
parent 87b2b83cde
commit aedc94686b

@ -3,16 +3,19 @@ using System.Linq;
using System.Security.Principal;
using System.Threading.Tasks;
using AutoFixture;
using MockQueryable.Moq;
using Moq;
using NUnit.Framework;
using Ombi.Core.Authentication;
using Ombi.Core.Engine;
using Ombi.Core.Engine.Interfaces;
using Ombi.Core.Models;
using Ombi.Core.Rule.Interfaces;
using Ombi.Core.Settings;
using Ombi.Settings.Settings.Models;
using Ombi.Store.Entities;
using Ombi.Store.Repository;
using Ombi.Test.Common;
namespace Ombi.Core.Tests.Engine
{
@ -30,12 +33,17 @@ namespace Ombi.Core.Tests.Engine
MovieRequestEngine = new Mock<IMovieRequestEngine>();
MovieRequestEngine = new Mock<IMovieRequestEngine>();
User = new Mock<IPrincipal>();
UserManager = new Mock<OmbiUserManager>();
UserManager.Setup(x => x.Users)
.Returns(new EnumerableQuery<OmbiUser>(new List<OmbiUser> {new OmbiUser {Id = "abc"}}));
User.Setup(x => x.Identity.Name).Returns("abc");
UserManager = MockHelper.MockUserManager(new List<OmbiUser> { new OmbiUser { Id = "abc", UserName = "abc" } });
Rule = new Mock<IRuleEvaluator>();
Engine = new VoteEngine(VoteRepository.Object, User.Object, UserManager.Object, Rule.Object, VoteSettings.Object, MusicRequestEngine.Object,
TvRequestEngine.Object, MovieRequestEngine.Object);
F.Behaviors.OfType<ThrowingRecursionBehavior>().ToList()
.ForEach(b => F.Behaviors.Remove(b));
F.Behaviors.Add(new OmitOnRecursionBehavior());
}
public Fixture F { get; set; }
@ -49,25 +57,160 @@ namespace Ombi.Core.Tests.Engine
public Mock<ITvRequestEngine> TvRequestEngine { get; set; }
public Mock<IMovieRequestEngine> MovieRequestEngine { get; set; }
[Test]
[Ignore("Need to mock the user manager")]
public async Task New_Upvote()
[TestCaseSource(nameof(VoteData))]
public async Task Vote(VoteType type, RequestType request)
{
VoteSettings.Setup(x => x.GetSettingsAsync()).ReturnsAsync(new VoteSettings());
VoteSettings.Setup(x => x.GetSettingsAsync()).ReturnsAsync(new VoteSettings
{
Enabled = true,
MovieVoteMax = 10
});
var votes = F.CreateMany<Votes>().ToList();
VoteRepository.Setup(x => x.GetAll()).Returns(new EnumerableQuery<Votes>(votes)
.AsQueryable()
.BuildMock().Object);
var result = new VoteEngineResult();
if (type == VoteType.Downvote)
{
result = await Engine.DownVote(1, request);
}
else
{
result = await Engine.UpVote(1, request);
}
Assert.That(result.Result, Is.True);
VoteRepository.Verify(x => x.Add(It.Is<Votes>(c => c.UserId == "abc" && c.VoteType == type)), Times.Once);
VoteRepository.Verify(x => x.Delete(It.IsAny<Votes>()), Times.Never);
MovieRequestEngine.Verify(x => x.ApproveMovieById(1), Times.Never);
}
public static IEnumerable<TestCaseData> VoteData
{
get
{
yield return new TestCaseData(VoteType.Upvote, RequestType.Movie).SetName("Movie_Upvote");
yield return new TestCaseData(VoteType.Downvote, RequestType.Movie).SetName("Movie_Downvote");
yield return new TestCaseData(VoteType.Upvote, RequestType.TvShow).SetName("Tv_Upvote");
yield return new TestCaseData(VoteType.Downvote, RequestType.TvShow).SetName("Tv_Downvote");
}
}
[TestCaseSource(nameof(AttemptedTwiceData))]
public async Task Attempted_Twice(VoteType type, RequestType request)
{
VoteSettings.Setup(x => x.GetSettingsAsync()).ReturnsAsync(new VoteSettings
{
Enabled = true,
MovieVoteMax = 10
});
var votes = F.CreateMany<Votes>().ToList();
votes.Add(new Votes
{
RequestId = 1,
RequestType = RequestType.Movie,
UserId = "abc"
UserId = "abc",
VoteType = type
});
VoteRepository.Setup(x => x.GetAll()).Returns(new EnumerableQuery<Votes>(votes)
.AsQueryable()
.BuildMock().Object);
var result = new VoteEngineResult();
if (type == VoteType.Downvote)
{
result = await Engine.DownVote(1, request);
}
else
{
result = await Engine.UpVote(1, request);
}
Assert.That(result.Result, Is.False);
VoteRepository.Verify(x => x.Delete(It.IsAny<Votes>()), Times.Never);
MovieRequestEngine.Verify(x => x.ApproveMovieById(1), Times.Never);
}
public static IEnumerable<TestCaseData> AttemptedTwiceData
{
get
{
yield return new TestCaseData(VoteType.Upvote, RequestType.Movie).SetName("Upvote_Attemped_Twice_Movie");
yield return new TestCaseData(VoteType.Downvote, RequestType.Movie).SetName("Downvote_Attempted_Twice_Movie");
yield return new TestCaseData(VoteType.Upvote, RequestType.TvShow).SetName("Upvote_Attemped_Twice_Tv");
yield return new TestCaseData(VoteType.Downvote, RequestType.TvShow).SetName("Downvote_Attempted_Twice_Tv");
}
}
[TestCaseSource(nameof(VoteConvertData))]
public async Task Downvote_Converted_To_Upvote(VoteType type, RequestType request)
{
VoteSettings.Setup(x => x.GetSettingsAsync()).ReturnsAsync(new VoteSettings
{
Enabled = true,
MovieVoteMax = 10
});
VoteRepository.Setup(x => x.GetAll()).Returns(new EnumerableQuery<Votes>(votes));
var result = await Engine.UpVote(1, RequestType.Movie);
var votes = F.CreateMany<Votes>().ToList();
votes.Add(new Votes
{
RequestId = 1,
RequestType = request,
UserId = "abc",
VoteType = type == VoteType.Upvote ? VoteType.Downvote : VoteType.Upvote
});
VoteRepository.Setup(x => x.GetAll()).Returns(new EnumerableQuery<Votes>(votes)
.AsQueryable()
.BuildMock().Object);
var result = new VoteEngineResult();
if (type == VoteType.Downvote)
{
result = await Engine.DownVote(1, request);
}
else
{
result = await Engine.UpVote(1, request);
}
Assert.That(result.Result, Is.True);
VoteRepository.Verify(x => x.Add(It.Is<Votes>(c => c.UserId == "abc" && c.VoteType == VoteType.Upvote)), Times.Once);
VoteRepository.Verify(x => x.Delete(It.IsAny<Votes>()), Times.Once);
VoteRepository.Verify(x => x.Add(It.Is<Votes>(v => v.VoteType == type)), Times.Once);
MovieRequestEngine.Verify(x => x.ApproveMovieById(1), Times.Never);
}
public static IEnumerable<TestCaseData> VoteConvertData
{
get
{
yield return new TestCaseData(VoteType.Upvote, RequestType.Movie).SetName("Downvote_Converted_To_UpVote_Movie");
yield return new TestCaseData(VoteType.Downvote, RequestType.Movie).SetName("UpVote_Converted_To_DownVote_Movie");
yield return new TestCaseData(VoteType.Upvote, RequestType.TvShow).SetName("Downvote_Converted_To_UpVote_TvShow");
yield return new TestCaseData(VoteType.Downvote, RequestType.TvShow).SetName("UpVote_Converted_To_DownVote_TvShow");
}
}
[TestCaseSource(nameof(VotingDisabledData))]
public async Task Voting_Disabled(RequestType type)
{
VoteSettings.Setup(x => x.GetSettingsAsync()).ReturnsAsync(new VoteSettings
{
Enabled = false,
MovieVoteMax = 10
});
var result = await Engine.UpVote(1, type);
Assert.That(result.Result, Is.True);
VoteRepository.Verify(x => x.Add(It.IsAny<Votes>()), Times.Never);
}
public static IEnumerable<TestCaseData> VotingDisabledData
{
get
{
yield return new TestCaseData(RequestType.Movie).SetName("Voting_Disabled_Movie");
yield return new TestCaseData(RequestType.TvShow).SetName("Voting_Disabled_TV");
}
}
}
}

@ -18,7 +18,7 @@ namespace Ombi.Core.Tests
{
get
{
yield return new TestCaseData("this!is^a*string",new []{'!','^','*'}).Returns("thisisastring").SetName("Basic Strip Multipe Chars");
yield return new TestCaseData("this!is^a*string",new []{'!','^','*'}).Returns("thisisastring").SetName("Basic Strip Multiple Chars");
yield return new TestCaseData("What is this madness'",new []{'\'','^','*'}).Returns("What is this madness").SetName("Basic Strip Multipe Chars");
}
}

Loading…
Cancel
Save