fix(availability): 🐛 Fixed a issue with the availability checker after the previous update. Added full test coverage around that area

pull/4707/head^2
tidusjar 2 years ago
parent 7ea7d58cb8
commit 28e248046a

@ -23,9 +23,13 @@ namespace Ombi.Hubs
public static List<string> AdminConnectionIds public static List<string> AdminConnectionIds
{ {
get get
{
if (UsersOnline.Any())
{ {
return UsersOnline.Where(x => x.Value.Roles.Contains(OmbiRoles.Admin)).Select(x => x.Key).ToList(); return UsersOnline.Where(x => x.Value.Roles.Contains(OmbiRoles.Admin)).Select(x => x.Key).ToList();
} }
return Enumerable.Empty<string>().ToList();
}
} }
public const string NotificationEvent = "Notification"; public const string NotificationEvent = "Notification";

@ -0,0 +1,199 @@
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Logging;
using MockQueryable.Moq;
using Moq;
using Moq.AutoMock;
using NUnit.Framework;
using Ombi.Core;
using Ombi.Hubs;
using Ombi.Notifications.Models;
using Ombi.Schedule.Jobs;
using Ombi.Store.Entities;
using Ombi.Store.Entities.Requests;
using Ombi.Store.Repository.Requests;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ombi.Schedule.Tests
{
[TestFixture]
public class AvailabilityCheckerTests
{
private AutoMocker _mocker;
private TestAvailabilityChecker _subject;
[SetUp]
public void SetUp()
{
_mocker = new AutoMocker();
Mock<IHubClients> mockClients = new Mock<IHubClients>();
Mock<IClientProxy> mockClientProxy = new Mock<IClientProxy>();
mockClients.Setup(clients => clients.Clients(It.IsAny<IReadOnlyList<string>>())).Returns(mockClientProxy.Object);
var hubContext = new Mock<IHubContext<NotificationHub>>();
hubContext.Setup(x => x.Clients).Returns(() => mockClients.Object);
_mocker.Use(hubContext);
_subject = _mocker.CreateInstance<TestAvailabilityChecker>();
}
[Test]
public async Task All_Episodes_Are_Available_In_Request()
{
var request = new ChildRequests
{
Title = "Test",
Id = 1,
RequestedUser = new OmbiUser { Email = "" },
SeasonRequests = new List<SeasonRequests>
{
new SeasonRequests
{
Episodes = new List<EpisodeRequests>
{
new EpisodeRequests
{
Available = false,
EpisodeNumber = 1,
Season = new SeasonRequests
{
SeasonNumber = 1
}
},
new EpisodeRequests
{
Available = false,
EpisodeNumber = 2,
Season = new SeasonRequests
{
SeasonNumber = 1
}
}
}
}
}
};
var databaseEpisodes = new List<IBaseMediaServerEpisode>
{
new PlexEpisode
{
EpisodeNumber = 1,
SeasonNumber = 1,
},
new PlexEpisode
{
EpisodeNumber = 2,
SeasonNumber = 1,
},
}.AsQueryable().BuildMock().Object;
await _subject.ProcessTvShow(databaseEpisodes, request);
Assert.Multiple(() =>
{
Assert.That(request.Available, Is.True);
Assert.That(request.MarkedAsAvailable, Is.Not.Null);
Assert.That(request.SeasonRequests[0].Episodes[0].Available, Is.True);
Assert.That(request.SeasonRequests[0].Episodes[1].Available, Is.True);
});
Assert.Multiple(() =>
{
_mocker.Verify<ITvRequestRepository>(x => x.Save(), Times.Exactly(2));
_mocker.Verify<INotificationHelper>(x => x.Notify(It.Is<NotificationOptions>(x => x.NotificationType == Helpers.NotificationType.RequestAvailable && x.RequestId == 1)), Times.Once);
});
}
[Test]
public async Task All_One_Episode_Is_Available_In_Request()
{
var request = new ChildRequests
{
Title = "Test",
Id = 1,
RequestedUser = new OmbiUser { Email = "" },
SeasonRequests = new List<SeasonRequests>
{
new SeasonRequests
{
Episodes = new List<EpisodeRequests>
{
new EpisodeRequests
{
Available = false,
EpisodeNumber = 1,
Season = new SeasonRequests
{
SeasonNumber = 1
}
},
new EpisodeRequests
{
Available = false,
EpisodeNumber = 2,
Season = new SeasonRequests
{
SeasonNumber = 1
}
},
new EpisodeRequests
{
Available = true,
EpisodeNumber = 3,
Season = new SeasonRequests
{
SeasonNumber = 1
}
}
}
}
}
};
var databaseEpisodes = new List<IBaseMediaServerEpisode>
{
new PlexEpisode
{
EpisodeNumber = 1,
SeasonNumber = 1,
},
new PlexEpisode
{
EpisodeNumber = 3,
SeasonNumber = 1,
},
}.AsQueryable().BuildMock().Object;
await _subject.ProcessTvShow(databaseEpisodes, request);
Assert.Multiple(() =>
{
Assert.That(request.Available, Is.False);
Assert.That(request.MarkedAsAvailable, Is.Null);
Assert.That(request.SeasonRequests[0].Episodes[0].Available, Is.True);
Assert.That(request.SeasonRequests[0].Episodes[1].Available, Is.False);
});
Assert.Multiple(() =>
{
_mocker.Verify<ITvRequestRepository>(x => x.Save(), Times.Once);
_mocker.Verify<INotificationHelper>(x => x.Notify(It.Is<NotificationOptions>(x => x.NotificationType == Helpers.NotificationType.PartiallyAvailable && x.RequestId == 1)), Times.Once);
});
}
}
public class TestAvailabilityChecker : AvailabilityChecker
{
public TestAvailabilityChecker(ITvRequestRepository tvRequest, INotificationHelper notification, ILogger log, IHubContext<NotificationHub> hub) : base(tvRequest, notification, log, hub)
{
}
public new Task ProcessTvShow(IQueryable<IBaseMediaServerEpisode> seriesEpisodes, ChildRequests child) => base.ProcessTvShow(seriesEpisodes, child);
}
}

@ -132,7 +132,7 @@ namespace Ombi.Schedule.Jobs.Radarr
continue; continue;
} }
ProcessTvShow(seriesEpisodes, child); await ProcessTvShow(seriesEpisodes, child);
} }
await _tvRepo.Save(); await _tvRepo.Save();

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@ -14,7 +15,7 @@ using Ombi.Store.Repository.Requests;
namespace Ombi.Schedule.Jobs namespace Ombi.Schedule.Jobs
{ {
public class AvailabilityChecker public abstract class AvailabilityChecker
{ {
protected readonly ITvRequestRepository _tvRepo; protected readonly ITvRequestRepository _tvRepo;
protected readonly INotificationHelper _notificationService; protected readonly INotificationHelper _notificationService;
@ -30,9 +31,8 @@ namespace Ombi.Schedule.Jobs
_hub = hub; _hub = hub;
} }
protected async void ProcessTvShow(IQueryable<IBaseMediaServerEpisode> seriesEpisodes, ChildRequests child) protected async Task ProcessTvShow(IQueryable<IBaseMediaServerEpisode> seriesEpisodes, ChildRequests child)
{ {
var availableEpisode = new List<AvailabilityModel>(); var availableEpisode = new List<AvailabilityModel>();
foreach (var season in child.SeasonRequests) foreach (var season in child.SeasonRequests)
{ {
@ -71,7 +71,7 @@ namespace Ombi.Schedule.Jobs
// We have ful-fulled this request! // We have ful-fulled this request!
child.Available = true; child.Available = true;
child.MarkedAsAvailable = DateTime.UtcNow; child.MarkedAsAvailable = DateTime.UtcNow;
await _hub.Clients.Clients(NotificationHub.AdminConnectionIds) await _hub?.Clients?.Clients(NotificationHub.AdminConnectionIds)?
.SendAsync(NotificationHub.NotificationEvent, "Availability Checker found some new available Shows!"); .SendAsync(NotificationHub.NotificationEvent, "Availability Checker found some new available Shows!");
_log.LogInformation("Child request {0} is now available, sending notification", $"{child.Title} - {child.Id}"); _log.LogInformation("Child request {0} is now available, sending notification", $"{child.Title} - {child.Id}");

@ -157,7 +157,7 @@ namespace Ombi.Schedule.Jobs.Emby
x.Series.Title == child.Title); x.Series.Title == child.Title);
} }
ProcessTvShow(seriesEpisodes, child); await ProcessTvShow(seriesEpisodes, child);
} }
await _tvRepo.Save(); await _tvRepo.Save();

@ -185,7 +185,7 @@ namespace Ombi.Schedule.Jobs.Jellyfin
x.Series.Title == child.Title); x.Series.Title == child.Title);
} }
ProcessTvShow(seriesEpisodes, child); await ProcessTvShow(seriesEpisodes, child);
} }
await _tvRepo.Save(); await _tvRepo.Save();

@ -105,7 +105,7 @@ namespace Ombi.Schedule.Jobs.Plex
} }
ProcessTvShow(seriesEpisodes, child); await ProcessTvShow(seriesEpisodes, child);
} }
await _tvRepo.Save(); await _tvRepo.Save();

Loading…
Cancel
Save