Added some tests and fixed the issue where the DB would get created in the wrong place depending on how you launched the application

pull/110/head
tidusjar 9 years ago
parent 1f9ed51320
commit e2aa4016a8

@ -79,6 +79,10 @@ namespace PlexRequests.Core
public RequestedModel Get(int id)
{
var blob = Repo.Get(id);
if (blob == null)
{
return new RequestedModel();
}
var model = ByteConverterHelper.ReturnObject<RequestedModel>(blob.Content);
return model;
}

@ -32,12 +32,12 @@ using Moq;
using NUnit.Framework;
using PlexRequests.Api.Interfaces;
using PlexRequests.Api.Models;
using PlexRequests.Api.Models.Plex;
using PlexRequests.Core;
using PlexRequests.Core.SettingModels;
using PlexRequests.Helpers.Exceptions;
using PlexRequests.Services.Interfaces;
using PlexRequests.Store;
namespace PlexRequests.Services.Tests
{
@ -66,7 +66,7 @@ namespace PlexRequests.Services.Tests
var requestMock = new Mock<IRequestService>();
var plexMock = new Mock<IPlexApi>();
var searchResult = new PlexSearch {Video = new List<Video> {new Video {Title = "title", Year = "2011"} } };
var searchResult = new PlexSearch { Video = new List<Video> { new Video { Title = "title", Year = "2011" } } };
settingsMock.Setup(x => x.GetSettings()).Returns(new PlexSettings { Ip = "abc" });
authMock.Setup(x => x.GetSettings()).Returns(new AuthenticationSettings { PlexAuthToken = "abc" });
@ -87,7 +87,7 @@ namespace PlexRequests.Services.Tests
var requestMock = new Mock<IRequestService>();
var plexMock = new Mock<IPlexApi>();
var searchResult = new PlexSearch { Directory = new Directory1 {Title = "title", Year = "2013"} };
var searchResult = new PlexSearch { Directory = new Directory1 { Title = "title", Year = "2013" } };
settingsMock.Setup(x => x.GetSettings()).Returns(new PlexSettings { Ip = "abc" });
authMock.Setup(x => x.GetSettings()).Returns(new AuthenticationSettings { PlexAuthToken = "abc" });
@ -100,6 +100,27 @@ namespace PlexRequests.Services.Tests
Assert.That(result, Is.True);
}
[Test]
public void IsAvailableDirectoryTitleWithoutYearTest()
{
var settingsMock = new Mock<ISettingsService<PlexSettings>>();
var authMock = new Mock<ISettingsService<AuthenticationSettings>>();
var requestMock = new Mock<IRequestService>();
var plexMock = new Mock<IPlexApi>();
var searchResult = new PlexSearch { Directory = new Directory1 { Title = "title", } };
settingsMock.Setup(x => x.GetSettings()).Returns(new PlexSettings { Ip = "abc" });
authMock.Setup(x => x.GetSettings()).Returns(new AuthenticationSettings { PlexAuthToken = "abc" });
plexMock.Setup(x => x.SearchContent(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Uri>())).Returns(searchResult);
Checker = new PlexAvailabilityChecker(settingsMock.Object, authMock.Object, requestMock.Object, plexMock.Object);
var result = Checker.IsAvailable("title", null);
Assert.That(result, Is.True);
}
[Test]
public void IsNotAvailableTest()
{
@ -108,7 +129,7 @@ namespace PlexRequests.Services.Tests
var requestMock = new Mock<IRequestService>();
var plexMock = new Mock<IPlexApi>();
var searchResult = new PlexSearch { Video = new List<Video> { new Video { Title = "wrong tistle", Year = "2011"} } };
var searchResult = new PlexSearch { Video = new List<Video> { new Video { Title = "wrong title", Year = "2011" } } };
settingsMock.Setup(x => x.GetSettings()).Returns(new PlexSettings { Ip = "abc" });
authMock.Setup(x => x.GetSettings()).Returns(new AuthenticationSettings { PlexAuthToken = "abc" });
@ -121,6 +142,27 @@ namespace PlexRequests.Services.Tests
Assert.That(result, Is.False);
}
[Test]
public void IsNotAvailableTestWihtoutYear()
{
var settingsMock = new Mock<ISettingsService<PlexSettings>>();
var authMock = new Mock<ISettingsService<AuthenticationSettings>>();
var requestMock = new Mock<IRequestService>();
var plexMock = new Mock<IPlexApi>();
var searchResult = new PlexSearch { Video = new List<Video> { new Video { Title = "wrong title" } } };
settingsMock.Setup(x => x.GetSettings()).Returns(new PlexSettings { Ip = "abc" });
authMock.Setup(x => x.GetSettings()).Returns(new AuthenticationSettings { PlexAuthToken = "abc" });
plexMock.Setup(x => x.SearchContent(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Uri>())).Returns(searchResult);
Checker = new PlexAvailabilityChecker(settingsMock.Object, authMock.Object, requestMock.Object, plexMock.Object);
var result = Checker.IsAvailable("title", null);
Assert.That(result, Is.False);
}
[Test]
public void IsYearDoesNotMatchTest()
{
@ -141,5 +183,155 @@ namespace PlexRequests.Services.Tests
Assert.That(result, Is.False);
}
[Test]
public void TitleDoesNotMatchTest()
{
var settingsMock = new Mock<ISettingsService<PlexSettings>>();
var authMock = new Mock<ISettingsService<AuthenticationSettings>>();
var requestMock = new Mock<IRequestService>();
var plexMock = new Mock<IPlexApi>();
var searchResult = new PlexSearch { Video = new List<Video> { new Video { Title = "title23", Year = "2019" } } };
settingsMock.Setup(x => x.GetSettings()).Returns(new PlexSettings { Ip = "abc" });
authMock.Setup(x => x.GetSettings()).Returns(new AuthenticationSettings { PlexAuthToken = "abc" });
plexMock.Setup(x => x.SearchContent(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Uri>())).Returns(searchResult);
Checker = new PlexAvailabilityChecker(settingsMock.Object, authMock.Object, requestMock.Object, plexMock.Object);
var result = Checker.IsAvailable("title", "2019");
Assert.That(result, Is.False);
}
[Test]
public void TitleDoesNotMatchWithoutYearTest()
{
var settingsMock = new Mock<ISettingsService<PlexSettings>>();
var authMock = new Mock<ISettingsService<AuthenticationSettings>>();
var requestMock = new Mock<IRequestService>();
var plexMock = new Mock<IPlexApi>();
var searchResult = new PlexSearch { Video = new List<Video> { new Video { Title = "title23" } } };
settingsMock.Setup(x => x.GetSettings()).Returns(new PlexSettings { Ip = "abc" });
authMock.Setup(x => x.GetSettings()).Returns(new AuthenticationSettings { PlexAuthToken = "abc" });
plexMock.Setup(x => x.SearchContent(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Uri>())).Returns(searchResult);
Checker = new PlexAvailabilityChecker(settingsMock.Object, authMock.Object, requestMock.Object, plexMock.Object);
var result = Checker.IsAvailable("title", null);
Assert.That(result, Is.False);
}
[Test]
public void CheckAndUpdateNoPlexSettingsTest()
{
var settingsMock = new Mock<ISettingsService<PlexSettings>>();
var authMock = new Mock<ISettingsService<AuthenticationSettings>>();
var requestMock = new Mock<IRequestService>();
var plexMock = new Mock<IPlexApi>();
authMock.Setup(x => x.GetSettings()).Returns(new AuthenticationSettings { PlexAuthToken = "abc" });
Checker = new PlexAvailabilityChecker(settingsMock.Object, authMock.Object, requestMock.Object, plexMock.Object);
Checker.CheckAndUpdateAll(1);
requestMock.Verify(x => x.BatchUpdate(It.IsAny<List<RequestedModel>>()), Times.Never);
requestMock.Verify(x => x.Get(It.IsAny<int>()), Times.Never);
plexMock.Verify(x => x.SearchContent(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Uri>()), Times.Never);
}
[Test]
public void CheckAndUpdateNoAuthSettingsTest()
{
var settingsMock = new Mock<ISettingsService<PlexSettings>>();
var authMock = new Mock<ISettingsService<AuthenticationSettings>>();
var requestMock = new Mock<IRequestService>();
var plexMock = new Mock<IPlexApi>();
settingsMock.Setup(x => x.GetSettings()).Returns(new PlexSettings { Ip = "123" });
Checker = new PlexAvailabilityChecker(settingsMock.Object, authMock.Object, requestMock.Object, plexMock.Object);
Checker.CheckAndUpdateAll(1);
requestMock.Verify(x => x.BatchUpdate(It.IsAny<List<RequestedModel>>()), Times.Never);
requestMock.Verify(x => x.Get(It.IsAny<int>()), Times.Never);
plexMock.Verify(x => x.SearchContent(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Uri>()), Times.Never);
}
[Test]
public void CheckAndUpdateNoRequestsTest()
{
var settingsMock = new Mock<ISettingsService<PlexSettings>>();
var authMock = new Mock<ISettingsService<AuthenticationSettings>>();
var requestMock = new Mock<IRequestService>();
var plexMock = new Mock<IPlexApi>();
settingsMock.Setup(x => x.GetSettings()).Returns(new PlexSettings { Ip = "192.168.1.1" });
authMock.Setup(x => x.GetSettings()).Returns(new AuthenticationSettings { PlexAuthToken = "abc" });
requestMock.Setup(x => x.GetAll()).Returns(new List<RequestedModel>());
Checker = new PlexAvailabilityChecker(settingsMock.Object, authMock.Object, requestMock.Object, plexMock.Object);
Checker.CheckAndUpdateAll(1);
requestMock.Verify(x => x.BatchUpdate(It.IsAny<List<RequestedModel>>()), Times.Never);
requestMock.Verify(x => x.Get(It.IsAny<int>()), Times.Never);
plexMock.Verify(x => x.SearchContent(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Uri>()), Times.Never);
}
[Test]
[Ignore("Need to work out Plex Directory vs Video objects")]
public void CheckAndUpdateRequestsTest()
{
var requests = new List<RequestedModel> {
new RequestedModel
{
Id = 123,
Title = "title1",
Available = false,
},
new RequestedModel
{
Id=222,
Title = "title3",
Available = false
},
new RequestedModel
{
Id = 333,
Title= "missingTitle",
Available = false
},
new RequestedModel
{
Id= 444,
Title = "already found",
Available = true
}
};
var search = new PlexSearch { };
var settingsMock = new Mock<ISettingsService<PlexSettings>>();
var authMock = new Mock<ISettingsService<AuthenticationSettings>>();
var requestMock = new Mock<IRequestService>();
var plexMock = new Mock<IPlexApi>();
settingsMock.Setup(x => x.GetSettings()).Returns(new PlexSettings { Ip = "192.168.1.1" });
authMock.Setup(x => x.GetSettings()).Returns(new AuthenticationSettings { PlexAuthToken = "abc" });
requestMock.Setup(x => x.GetAll()).Returns(requests);
Checker = new PlexAvailabilityChecker(settingsMock.Object, authMock.Object, requestMock.Object, plexMock.Object);
Checker.CheckAndUpdateAll(1);
requestMock.Verify(x => x.BatchUpdate(It.IsAny<List<RequestedModel>>()), Times.Never);
requestMock.Verify(x => x.Get(It.IsAny<int>()), Times.Never);
plexMock.Verify(x => x.SearchContent(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Uri>()), Times.Never);
}
}
}

@ -84,6 +84,10 @@
<Project>{566EFA49-68F8-4716-9693-A6B3F2624DEA}</Project>
<Name>PlexRequests.Services</Name>
</ProjectReference>
<ProjectReference Include="..\PlexRequests.Store\PlexRequests.Store.csproj">
<Project>{92433867-2B7B-477B-A566-96C382427525}</Project>
<Name>PlexRequests.Store</Name>
</ProjectReference>
</ItemGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">

@ -53,7 +53,7 @@ namespace PlexRequests.Services
private ISettingsService<AuthenticationSettings> Auth { get; }
private IRequestService RequestService { get; }
private static Logger Log = LogManager.GetCurrentClassLogger();
private IPlexApi PlexApi { get; set; }
private IPlexApi PlexApi { get; }
public void CheckAndUpdateAll(long check)
@ -63,7 +63,7 @@ namespace PlexRequests.Services
var requests = RequestService.GetAll();
var requestedModels = requests as RequestedModel[] ?? requests.ToArray();
if (!ValidateSettings(plexSettings, authSettings, requestedModels))
if (!ValidateSettings(plexSettings, authSettings) || !requestedModels.Any())
{
return;
}
@ -114,21 +114,6 @@ namespace PlexRequests.Services
}
private bool ValidateSettings(PlexSettings plex, AuthenticationSettings auth, IEnumerable<RequestedModel> requests)
{
if (plex.Ip == null || auth.PlexAuthToken == null || requests == null)
{
Log.Warn("A setting is null, Ensure Plex is configured correctly, and we have a Plex Auth token.");
return false;
}
if (!requests.Any())
{
Log.Info("We have no requests to check if they are available on Plex.");
return false;
}
return true;
}
private bool ValidateSettings(PlexSettings plex, AuthenticationSettings auth)
{
if (plex?.Ip == null || auth?.PlexAuthToken == null)

@ -27,12 +27,11 @@
using System;
using System.Data;
using System.IO;
using System.Windows.Forms;
using Mono.Data.Sqlite;
using NLog;
using PlexRequests.Helpers;
using PlexRequests.Store.Repository;
namespace PlexRequests.Store
{
@ -44,12 +43,14 @@ namespace PlexRequests.Store
Factory = provider;
}
private SqliteFactory Factory { get; set; }
private SqliteFactory Factory { get; }
private string CurrentPath =>Path.Combine(Path.GetDirectoryName(Application.ExecutablePath) ?? string.Empty, DbFile);
public virtual bool CheckDb()
{
Log.Trace("Checking DB");
if (!File.Exists(DbFile))
Console.WriteLine(CurrentPath);
if (!File.Exists(CurrentPath))
{
Log.Trace("DB doesn't exist, creating a new one");
CreateDatabase();
@ -59,7 +60,7 @@ namespace PlexRequests.Store
}
public string DbFile = "PlexRequests.sqlite";
/// <summary>
/// Gets the database connection.
/// </summary>
@ -72,7 +73,7 @@ namespace PlexRequests.Store
{
throw new SqliteException("Factory returned null");
}
fact.ConnectionString = "Data Source=" + DbFile;
fact.ConnectionString = "Data Source=" + CurrentPath;
return fact;
}
@ -83,7 +84,7 @@ namespace PlexRequests.Store
{
try
{
using (File.Create(DbFile))
using (File.Create(CurrentPath))
{
}
}

@ -52,6 +52,7 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />

@ -79,7 +79,6 @@ namespace PlexRequests.UI
container.Register<ISettingsService<HeadphonesSettings>, SettingsServiceV2<HeadphonesSettings>>();
// Repo's
container.Register<IRepository<RequestedModel>, GenericRepository<RequestedModel>>();
container.Register<IRepository<LogEntity>, GenericRepository<LogEntity>>();
container.Register<IRequestService, JsonRequestService>();
container.Register<ISettingsRepository, SettingsJsonRepository>();
@ -102,13 +101,13 @@ namespace PlexRequests.UI
SubscribeAllObservers(container);
base.ConfigureRequestContainer(container, context);
}
protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
{
TaskManager.TaskFactory = new PlexTaskFactory();
TaskManager.Initialize(new PlexRegistry());
}
protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
{
CookieBasedSessions.Enable(pipelines, CryptographyConfiguration.Default);
StaticConfiguration.DisableErrorTraces = false;
@ -128,7 +127,7 @@ namespace PlexRequests.UI
(sender, certificate, chain, sslPolicyErrors) => true;
}
protected override DiagnosticsConfiguration DiagnosticsConfiguration => new DiagnosticsConfiguration { Password = @"password" };

@ -12,12 +12,12 @@ namespace PlexRequests.UI.Jobs
//typeof(AvailabilityUpdateService);
var container = TinyIoCContainer.Current;
var a= container.ResolveAll(typeof(T));
var a= container.Resolve(typeof(T));
object outT;
container.TryResolve(typeof(T), out outT);
return (T)outT;
return (T)a;
}
}
}
Loading…
Cancel
Save