2011-12-04 02:22:49 +03:00
|
|
|
using System.Linq;
|
2011-12-02 07:24:44 +03:00
|
|
|
using System.IO;
|
|
|
|
using System.Net;
|
2011-06-14 05:15:55 +03:00
|
|
|
using FizzWare.NBuilder;
|
2011-12-02 07:24:44 +03:00
|
|
|
using FluentAssertions;
|
|
|
|
using Moq;
|
2011-06-03 04:15:02 +03:00
|
|
|
using NUnit.Framework;
|
2012-02-11 03:48:20 +03:00
|
|
|
using NzbDrone.Common;
|
2013-02-24 09:48:52 +03:00
|
|
|
using NzbDrone.Core.Configuration;
|
2011-06-14 05:15:55 +03:00
|
|
|
using NzbDrone.Core.Providers;
|
2011-12-02 07:24:44 +03:00
|
|
|
using NzbDrone.Core.Providers.Core;
|
2011-06-14 05:15:55 +03:00
|
|
|
using NzbDrone.Core.Repository;
|
2011-06-03 04:15:02 +03:00
|
|
|
using NzbDrone.Core.Test.Framework;
|
2011-11-14 03:22:18 +03:00
|
|
|
using NzbDrone.Test.Common.AutoMoq;
|
2011-06-03 04:15:02 +03:00
|
|
|
|
2011-12-04 02:22:49 +03:00
|
|
|
namespace NzbDrone.Core.Test.ProviderTests
|
2011-06-03 04:15:02 +03:00
|
|
|
{
|
|
|
|
[TestFixture]
|
|
|
|
// ReSharper disable InconsistentNaming
|
2013-02-02 23:54:03 +03:00
|
|
|
public class SceneMappingProviderTest : SqlCeTest
|
2011-06-03 04:15:02 +03:00
|
|
|
{
|
2012-01-13 06:22:28 +03:00
|
|
|
private const string SceneMappingUrl = "http://services.nzbdrone.com/SceneMapping/Active";
|
2011-12-02 07:24:44 +03:00
|
|
|
|
2012-02-04 08:28:50 +03:00
|
|
|
[SetUp]
|
|
|
|
public void Setup()
|
|
|
|
{
|
2013-02-24 09:48:52 +03:00
|
|
|
Mocker.GetMock<ConfigService>().SetupGet(s => s.ServiceRootUrl)
|
2012-02-04 08:28:50 +03:00
|
|
|
.Returns("http://services.nzbdrone.com");
|
2013-01-20 02:55:58 +03:00
|
|
|
|
|
|
|
WithRealDb();
|
2012-02-04 08:28:50 +03:00
|
|
|
}
|
|
|
|
|
2012-01-13 06:22:28 +03:00
|
|
|
private void WithValidJson()
|
2011-12-02 07:24:44 +03:00
|
|
|
{
|
|
|
|
Mocker.GetMock<HttpProvider>()
|
|
|
|
.Setup(s => s.DownloadString(SceneMappingUrl))
|
2012-01-13 06:22:28 +03:00
|
|
|
.Returns(File.ReadAllText(@".\Files\SceneMappings.json"));
|
2011-12-02 07:24:44 +03:00
|
|
|
}
|
|
|
|
|
2012-01-13 06:22:28 +03:00
|
|
|
private void WithErrorDownloadingJson()
|
2011-12-02 07:24:44 +03:00
|
|
|
{
|
|
|
|
Mocker.GetMock<HttpProvider>()
|
|
|
|
.Setup(s => s.DownloadString(SceneMappingUrl))
|
|
|
|
.Throws(new WebException());
|
|
|
|
}
|
|
|
|
|
2011-06-14 05:15:55 +03:00
|
|
|
[Test]
|
|
|
|
public void GetSceneName_exists()
|
|
|
|
{
|
2013-01-24 11:03:10 +03:00
|
|
|
WithRealDb();
|
|
|
|
|
2011-06-14 05:15:55 +03:00
|
|
|
//Setup
|
2011-06-17 09:04:01 +03:00
|
|
|
var fakeMap = Builder<SceneMapping>.CreateNew()
|
|
|
|
.With(f => f.CleanTitle = "laworder")
|
2011-06-14 05:15:55 +03:00
|
|
|
.With(f => f.SeriesId = 12345)
|
|
|
|
.With(f => f.SceneName = "Law and Order")
|
2013-01-24 11:03:10 +03:00
|
|
|
.With(f => f.SeasonNumber = -1)
|
2011-06-14 05:15:55 +03:00
|
|
|
.Build();
|
|
|
|
|
2013-01-24 11:03:10 +03:00
|
|
|
Db.Insert(fakeMap);
|
2011-06-14 05:15:55 +03:00
|
|
|
|
|
|
|
//Act
|
2011-12-15 07:15:53 +03:00
|
|
|
var sceneName = Mocker.Resolve<SceneMappingProvider>().GetSceneName(fakeMap.SeriesId);
|
2011-06-14 05:15:55 +03:00
|
|
|
|
|
|
|
//Assert
|
|
|
|
Assert.AreEqual(fakeMap.SceneName, sceneName);
|
|
|
|
}
|
2011-06-03 04:15:02 +03:00
|
|
|
|
|
|
|
[Test]
|
2011-06-14 05:15:55 +03:00
|
|
|
public void GetSeriesId_exists()
|
2011-06-03 04:15:02 +03:00
|
|
|
{
|
2013-01-24 11:03:10 +03:00
|
|
|
WithRealDb();
|
|
|
|
|
2011-06-14 05:15:55 +03:00
|
|
|
//Setup
|
2011-06-17 09:04:01 +03:00
|
|
|
var fakeMap = Builder<SceneMapping>.CreateNew()
|
2011-06-14 05:15:55 +03:00
|
|
|
.With(f => f.SeriesId = 12345)
|
|
|
|
.With(f => f.SceneName = "Law and Order")
|
|
|
|
.With(f => f.SceneName = "laworder")
|
|
|
|
.Build();
|
|
|
|
|
2013-01-20 02:55:58 +03:00
|
|
|
|
2013-01-24 11:03:10 +03:00
|
|
|
Db.Insert(fakeMap);
|
2011-06-14 05:15:55 +03:00
|
|
|
|
|
|
|
//Act
|
2011-12-15 07:15:53 +03:00
|
|
|
var seriesId = Mocker.Resolve<SceneMappingProvider>().GetSeriesId(fakeMap.CleanTitle);
|
2011-06-14 05:15:55 +03:00
|
|
|
|
|
|
|
//Assert
|
|
|
|
Assert.AreEqual(fakeMap.SeriesId, seriesId);
|
2011-06-03 04:15:02 +03:00
|
|
|
}
|
|
|
|
|
2011-06-14 05:15:55 +03:00
|
|
|
[Test]
|
|
|
|
public void GetSceneName_null()
|
|
|
|
{
|
2013-01-24 11:03:10 +03:00
|
|
|
WithRealDb();
|
|
|
|
|
2011-06-14 05:15:55 +03:00
|
|
|
//Setup
|
2011-06-17 09:04:01 +03:00
|
|
|
var fakeMap = Builder<SceneMapping>.CreateNew()
|
2011-06-14 05:15:55 +03:00
|
|
|
.With(f => f.SeriesId = 12345)
|
|
|
|
.With(f => f.SceneName = "Law and Order")
|
2011-06-17 09:04:01 +03:00
|
|
|
.With(f => f.SceneName = "laworder")
|
2011-06-14 05:15:55 +03:00
|
|
|
.Build();
|
|
|
|
|
2013-01-20 02:55:58 +03:00
|
|
|
|
2013-01-24 11:03:10 +03:00
|
|
|
Db.Insert(fakeMap);
|
2011-06-14 05:15:55 +03:00
|
|
|
|
|
|
|
//Act
|
2011-12-15 07:15:53 +03:00
|
|
|
var sceneName = Mocker.Resolve<SceneMappingProvider>().GetSceneName(54321);
|
2011-06-14 05:15:55 +03:00
|
|
|
|
|
|
|
//Assert
|
|
|
|
Assert.AreEqual(null, sceneName);
|
|
|
|
}
|
2011-06-03 04:15:02 +03:00
|
|
|
|
|
|
|
[Test]
|
2011-06-14 05:15:55 +03:00
|
|
|
public void GetSeriesId_null()
|
2011-06-03 04:15:02 +03:00
|
|
|
{
|
2013-01-24 11:03:10 +03:00
|
|
|
WithRealDb();
|
|
|
|
|
2011-06-14 05:15:55 +03:00
|
|
|
//Setup
|
2011-06-17 09:04:01 +03:00
|
|
|
var fakeMap = Builder<SceneMapping>.CreateNew()
|
2011-06-14 05:15:55 +03:00
|
|
|
.With(f => f.SeriesId = 12345)
|
|
|
|
.With(f => f.SceneName = "Law and Order")
|
2011-06-17 09:04:01 +03:00
|
|
|
.With(f => f.CleanTitle = "laworder")
|
2011-06-14 05:15:55 +03:00
|
|
|
.Build();
|
|
|
|
|
2013-01-24 11:03:10 +03:00
|
|
|
Db.Insert(fakeMap);
|
2011-06-14 05:15:55 +03:00
|
|
|
|
|
|
|
//Act
|
2011-12-15 07:15:53 +03:00
|
|
|
var seriesId = Mocker.Resolve<SceneMappingProvider>().GetSeriesId("notlaworder");
|
2011-06-14 05:15:55 +03:00
|
|
|
|
|
|
|
//Assert
|
|
|
|
Assert.AreEqual(null, seriesId);
|
2011-06-03 04:15:02 +03:00
|
|
|
}
|
2011-08-23 09:07:04 +03:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void GetSceneName_multiple_clean_names()
|
|
|
|
{
|
2013-01-24 11:03:10 +03:00
|
|
|
WithRealDb();
|
2011-08-23 09:07:04 +03:00
|
|
|
//Test that ensures a series with clean names (office, officeus) can be looked up by seriesId
|
|
|
|
|
|
|
|
//Setup
|
|
|
|
var fakeMap = Builder<SceneMapping>.CreateNew()
|
|
|
|
.With(f => f.CleanTitle = "office")
|
|
|
|
.With(f => f.SeriesId = 12345)
|
|
|
|
.With(f => f.SceneName = "The Office")
|
2013-01-24 11:03:10 +03:00
|
|
|
.With(f => f.SeasonNumber = -1)
|
2011-08-23 09:07:04 +03:00
|
|
|
.Build();
|
|
|
|
|
|
|
|
var fakeMap2 = Builder<SceneMapping>.CreateNew()
|
|
|
|
.With(f => f.CleanTitle = "officeus")
|
|
|
|
.With(f => f.SeriesId = 12345)
|
|
|
|
.With(f => f.SceneName = "The Office")
|
2013-01-24 11:03:10 +03:00
|
|
|
.With(f => f.SeasonNumber = -1)
|
2011-08-23 09:07:04 +03:00
|
|
|
.Build();
|
|
|
|
|
2013-01-20 02:55:58 +03:00
|
|
|
|
|
|
|
|
2013-01-24 11:03:10 +03:00
|
|
|
Db.Insert(fakeMap);
|
|
|
|
Db.Insert(fakeMap2);
|
2011-08-23 09:07:04 +03:00
|
|
|
|
|
|
|
//Act
|
2011-12-15 07:15:53 +03:00
|
|
|
var sceneName = Mocker.Resolve<SceneMappingProvider>().GetSceneName(fakeMap.SeriesId);
|
2011-08-23 09:07:04 +03:00
|
|
|
|
|
|
|
//Assert
|
|
|
|
Assert.AreEqual(fakeMap.SceneName, sceneName);
|
|
|
|
}
|
2011-12-02 07:24:44 +03:00
|
|
|
|
2013-01-24 11:03:10 +03:00
|
|
|
[Test]
|
|
|
|
public void GetSceneName_should_be_null_when_seasonNumber_does_not_match()
|
|
|
|
{
|
|
|
|
WithRealDb();
|
|
|
|
|
|
|
|
var fakeMap = Builder<SceneMapping>.CreateNew()
|
|
|
|
.With(f => f.SeriesId = 12345)
|
|
|
|
.With(f => f.SceneName = "Law and Order")
|
|
|
|
.With(f => f.SceneName = "laworder")
|
|
|
|
.With(f => f.SeasonNumber = 10)
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
Db.Insert(fakeMap);
|
|
|
|
|
|
|
|
Mocker.Resolve<SceneMappingProvider>().GetSceneName(54321, 5).Should().BeNull();
|
|
|
|
}
|
|
|
|
|
2011-12-02 07:24:44 +03:00
|
|
|
[Test]
|
|
|
|
public void UpdateMappings_should_add_all_mappings_to_database()
|
|
|
|
{
|
2012-01-13 06:22:28 +03:00
|
|
|
WithValidJson();
|
2011-12-02 07:24:44 +03:00
|
|
|
|
|
|
|
//Act
|
|
|
|
Mocker.Resolve<SceneMappingProvider>().UpdateMappings();
|
|
|
|
|
|
|
|
//Assert
|
2012-01-13 06:22:28 +03:00
|
|
|
Mocker.Verify<HttpProvider>(v => v.DownloadString(SceneMappingUrl), Times.Once());
|
2011-12-02 07:24:44 +03:00
|
|
|
var result = Db.Fetch<SceneMapping>();
|
|
|
|
result.Should().HaveCount(5);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void UpdateMappings_should_overwrite_existing_mappings()
|
|
|
|
{
|
|
|
|
//Setup
|
|
|
|
var fakeMap = Builder<SceneMapping>.CreateNew()
|
|
|
|
.With(f => f.SeriesId = 12345)
|
|
|
|
.With(f => f.SceneName = "Law and Order")
|
|
|
|
.With(f => f.SceneName = "laworder")
|
|
|
|
.Build();
|
|
|
|
|
2012-01-13 06:22:28 +03:00
|
|
|
WithValidJson();
|
2011-12-02 07:24:44 +03:00
|
|
|
Db.Insert(fakeMap);
|
|
|
|
|
|
|
|
//Act
|
|
|
|
Mocker.Resolve<SceneMappingProvider>().UpdateMappings();
|
|
|
|
|
|
|
|
//Assert
|
2012-01-13 06:22:28 +03:00
|
|
|
Mocker.Verify<HttpProvider>(v => v.DownloadString(SceneMappingUrl), Times.Once());
|
2011-12-02 07:24:44 +03:00
|
|
|
var result = Db.Fetch<SceneMapping>();
|
|
|
|
result.Should().HaveCount(5);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void UpdateMappings_should_not_delete_if_csv_download_fails()
|
|
|
|
{
|
|
|
|
//Setup
|
|
|
|
var fakeMap = Builder<SceneMapping>.CreateNew()
|
|
|
|
.With(f => f.SeriesId = 12345)
|
|
|
|
.With(f => f.SceneName = "Law and Order")
|
|
|
|
.With(f => f.SceneName = "laworder")
|
|
|
|
.Build();
|
|
|
|
|
2012-01-13 06:22:28 +03:00
|
|
|
WithErrorDownloadingJson();
|
2011-12-02 07:24:44 +03:00
|
|
|
Db.Insert(fakeMap);
|
|
|
|
|
|
|
|
//Act
|
|
|
|
Mocker.Resolve<SceneMappingProvider>().UpdateMappings();
|
|
|
|
|
|
|
|
//Assert
|
2012-01-13 06:22:28 +03:00
|
|
|
Mocker.Verify<HttpProvider>(v => v.DownloadString(SceneMappingUrl), Times.Once());
|
2011-12-02 07:24:44 +03:00
|
|
|
var result = Db.Fetch<SceneMapping>();
|
|
|
|
result.Should().HaveCount(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void UpdateIfEmpty_should_not_update_if_count_is_not_zero()
|
|
|
|
{
|
|
|
|
//Setup
|
|
|
|
var fakeMap = Builder<SceneMapping>.CreateNew()
|
|
|
|
.With(f => f.SeriesId = 12345)
|
|
|
|
.With(f => f.SceneName = "Law and Order")
|
|
|
|
.With(f => f.SceneName = "laworder")
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
Db.Insert(fakeMap);
|
|
|
|
|
|
|
|
//Act
|
|
|
|
Mocker.Resolve<SceneMappingProvider>().UpdateIfEmpty();
|
|
|
|
|
|
|
|
//Assert
|
2012-01-13 06:22:28 +03:00
|
|
|
Mocker.Verify<HttpProvider>(v => v.DownloadString(SceneMappingUrl), Times.Never());
|
2011-12-02 07:24:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void UpdateIfEmpty_should_update_if_count_is_zero()
|
|
|
|
{
|
|
|
|
//Setup
|
2012-01-13 06:22:28 +03:00
|
|
|
WithValidJson();
|
2011-12-02 07:24:44 +03:00
|
|
|
|
|
|
|
//Act
|
|
|
|
Mocker.Resolve<SceneMappingProvider>().UpdateIfEmpty();
|
|
|
|
|
|
|
|
//Assert
|
|
|
|
Mocker.Verify<HttpProvider>(v => v.DownloadString(SceneMappingUrl), Times.Once());
|
|
|
|
var result = Db.Fetch<SceneMapping>();
|
|
|
|
result.Should().HaveCount(5);
|
|
|
|
}
|
2011-06-03 04:15:02 +03:00
|
|
|
}
|
|
|
|
}
|