2011-12-03 15:22:49 -08:00
|
|
|
using System.Linq;
|
2011-12-01 20:24:44 -08:00
|
|
|
using System.IO;
|
|
|
|
using System.Net;
|
2011-06-13 19:15:55 -07:00
|
|
|
using FizzWare.NBuilder;
|
2011-12-01 20:24:44 -08:00
|
|
|
using FluentAssertions;
|
|
|
|
using Moq;
|
2011-06-02 18:15:02 -07:00
|
|
|
using NUnit.Framework;
|
2012-02-10 16:48:20 -08:00
|
|
|
using NzbDrone.Common;
|
2013-02-23 22:48:52 -08:00
|
|
|
using NzbDrone.Core.Configuration;
|
2013-03-02 10:25:39 -08:00
|
|
|
using NzbDrone.Core.ReferenceData;
|
2011-06-02 18:15:02 -07:00
|
|
|
using NzbDrone.Core.Test.Framework;
|
|
|
|
|
2011-12-03 15:22:49 -08:00
|
|
|
namespace NzbDrone.Core.Test.ProviderTests
|
2011-06-02 18:15:02 -07:00
|
|
|
{
|
|
|
|
[TestFixture]
|
|
|
|
// ReSharper disable InconsistentNaming
|
2013-03-23 21:16:00 -07:00
|
|
|
public class SceneMappingProviderTest : DbTest
|
2011-06-02 18:15:02 -07:00
|
|
|
{
|
2012-01-12 19:22:28 -08:00
|
|
|
private const string SceneMappingUrl = "http://services.nzbdrone.com/SceneMapping/Active";
|
2011-12-01 20:24:44 -08:00
|
|
|
|
2012-02-03 21:28:50 -08:00
|
|
|
[SetUp]
|
|
|
|
public void Setup()
|
|
|
|
{
|
2013-02-24 11:39:31 -08:00
|
|
|
Mocker.GetMock<IConfigService>().SetupGet(s => s.ServiceRootUrl)
|
2012-02-03 21:28:50 -08:00
|
|
|
.Returns("http://services.nzbdrone.com");
|
2013-01-19 15:55:58 -08:00
|
|
|
|
2012-02-03 21:28:50 -08:00
|
|
|
}
|
|
|
|
|
2012-01-12 19:22:28 -08:00
|
|
|
private void WithValidJson()
|
2011-12-01 20:24:44 -08:00
|
|
|
{
|
|
|
|
Mocker.GetMock<HttpProvider>()
|
|
|
|
.Setup(s => s.DownloadString(SceneMappingUrl))
|
2012-01-12 19:22:28 -08:00
|
|
|
.Returns(File.ReadAllText(@".\Files\SceneMappings.json"));
|
2011-12-01 20:24:44 -08:00
|
|
|
}
|
|
|
|
|
2012-01-12 19:22:28 -08:00
|
|
|
private void WithErrorDownloadingJson()
|
2011-12-01 20:24:44 -08:00
|
|
|
{
|
|
|
|
Mocker.GetMock<HttpProvider>()
|
|
|
|
.Setup(s => s.DownloadString(SceneMappingUrl))
|
|
|
|
.Throws(new WebException());
|
|
|
|
}
|
|
|
|
|
2011-06-13 19:15:55 -07:00
|
|
|
[Test]
|
|
|
|
public void GetSceneName_exists()
|
|
|
|
{
|
2013-01-24 00:03:10 -08:00
|
|
|
|
2011-06-13 19:15:55 -07:00
|
|
|
//Setup
|
2011-06-16 23:04:01 -07:00
|
|
|
var fakeMap = Builder<SceneMapping>.CreateNew()
|
|
|
|
.With(f => f.CleanTitle = "laworder")
|
2013-03-02 10:25:39 -08:00
|
|
|
.With(f => f.TvdbId = 12345)
|
2011-06-13 19:15:55 -07:00
|
|
|
.With(f => f.SceneName = "Law and Order")
|
2013-01-24 00:03:10 -08:00
|
|
|
.With(f => f.SeasonNumber = -1)
|
2011-06-13 19:15:55 -07:00
|
|
|
.Build();
|
|
|
|
|
2013-01-24 00:03:10 -08:00
|
|
|
Db.Insert(fakeMap);
|
2011-06-13 19:15:55 -07:00
|
|
|
|
|
|
|
//Act
|
2013-03-02 10:25:39 -08:00
|
|
|
var sceneName = Mocker.Resolve<SceneMappingService>().GetSceneName(fakeMap.TvdbId);
|
2011-06-13 19:15:55 -07:00
|
|
|
|
|
|
|
//Assert
|
|
|
|
Assert.AreEqual(fakeMap.SceneName, sceneName);
|
|
|
|
}
|
2011-06-02 18:15:02 -07:00
|
|
|
|
|
|
|
[Test]
|
2011-06-13 19:15:55 -07:00
|
|
|
public void GetSeriesId_exists()
|
2011-06-02 18:15:02 -07:00
|
|
|
{
|
2013-01-24 00:03:10 -08:00
|
|
|
|
2011-06-13 19:15:55 -07:00
|
|
|
//Setup
|
2011-06-16 23:04:01 -07:00
|
|
|
var fakeMap = Builder<SceneMapping>.CreateNew()
|
2013-03-02 10:25:39 -08:00
|
|
|
.With(f => f.TvdbId = 12345)
|
2011-06-13 19:15:55 -07:00
|
|
|
.With(f => f.SceneName = "Law and Order")
|
|
|
|
.With(f => f.SceneName = "laworder")
|
|
|
|
.Build();
|
|
|
|
|
2013-01-19 15:55:58 -08:00
|
|
|
|
2013-01-24 00:03:10 -08:00
|
|
|
Db.Insert(fakeMap);
|
2011-06-13 19:15:55 -07:00
|
|
|
|
|
|
|
//Act
|
2013-03-02 10:25:39 -08:00
|
|
|
var seriesId = Mocker.Resolve<SceneMappingService>().GetTvDbId(fakeMap.CleanTitle);
|
2011-06-13 19:15:55 -07:00
|
|
|
|
|
|
|
//Assert
|
2013-03-02 10:25:39 -08:00
|
|
|
Assert.AreEqual(fakeMap.TvdbId, seriesId);
|
2011-06-02 18:15:02 -07:00
|
|
|
}
|
|
|
|
|
2011-06-13 19:15:55 -07:00
|
|
|
[Test]
|
|
|
|
public void GetSceneName_null()
|
|
|
|
{
|
2013-01-24 00:03:10 -08:00
|
|
|
|
2011-06-13 19:15:55 -07:00
|
|
|
//Setup
|
2011-06-16 23:04:01 -07:00
|
|
|
var fakeMap = Builder<SceneMapping>.CreateNew()
|
2013-03-02 10:25:39 -08:00
|
|
|
.With(f => f.TvdbId = 12345)
|
2011-06-13 19:15:55 -07:00
|
|
|
.With(f => f.SceneName = "Law and Order")
|
2011-06-16 23:04:01 -07:00
|
|
|
.With(f => f.SceneName = "laworder")
|
2011-06-13 19:15:55 -07:00
|
|
|
.Build();
|
|
|
|
|
2013-01-19 15:55:58 -08:00
|
|
|
|
2013-01-24 00:03:10 -08:00
|
|
|
Db.Insert(fakeMap);
|
2011-06-13 19:15:55 -07:00
|
|
|
|
|
|
|
//Act
|
2013-03-02 10:25:39 -08:00
|
|
|
var sceneName = Mocker.Resolve<SceneMappingService>().GetSceneName(54321);
|
2011-06-13 19:15:55 -07:00
|
|
|
|
|
|
|
//Assert
|
|
|
|
Assert.AreEqual(null, sceneName);
|
|
|
|
}
|
2011-06-02 18:15:02 -07:00
|
|
|
|
|
|
|
[Test]
|
2011-06-13 19:15:55 -07:00
|
|
|
public void GetSeriesId_null()
|
2011-06-02 18:15:02 -07:00
|
|
|
{
|
2013-01-24 00:03:10 -08:00
|
|
|
|
2011-06-13 19:15:55 -07:00
|
|
|
//Setup
|
2011-06-16 23:04:01 -07:00
|
|
|
var fakeMap = Builder<SceneMapping>.CreateNew()
|
2013-03-02 10:25:39 -08:00
|
|
|
.With(f => f.TvdbId = 12345)
|
2011-06-13 19:15:55 -07:00
|
|
|
.With(f => f.SceneName = "Law and Order")
|
2011-06-16 23:04:01 -07:00
|
|
|
.With(f => f.CleanTitle = "laworder")
|
2011-06-13 19:15:55 -07:00
|
|
|
.Build();
|
|
|
|
|
2013-01-24 00:03:10 -08:00
|
|
|
Db.Insert(fakeMap);
|
2011-06-13 19:15:55 -07:00
|
|
|
|
|
|
|
//Act
|
2013-03-02 10:25:39 -08:00
|
|
|
var seriesId = Mocker.Resolve<SceneMappingService>().GetTvDbId("notlaworder");
|
2011-06-13 19:15:55 -07:00
|
|
|
|
|
|
|
//Assert
|
|
|
|
Assert.AreEqual(null, seriesId);
|
2011-06-02 18:15:02 -07:00
|
|
|
}
|
2011-08-22 23:07:04 -07:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void GetSceneName_multiple_clean_names()
|
|
|
|
{
|
|
|
|
//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")
|
2013-03-02 10:25:39 -08:00
|
|
|
.With(f => f.TvdbId = 12345)
|
2011-08-22 23:07:04 -07:00
|
|
|
.With(f => f.SceneName = "The Office")
|
2013-01-24 00:03:10 -08:00
|
|
|
.With(f => f.SeasonNumber = -1)
|
2011-08-22 23:07:04 -07:00
|
|
|
.Build();
|
|
|
|
|
|
|
|
var fakeMap2 = Builder<SceneMapping>.CreateNew()
|
|
|
|
.With(f => f.CleanTitle = "officeus")
|
2013-03-02 10:25:39 -08:00
|
|
|
.With(f => f.TvdbId = 12345)
|
2011-08-22 23:07:04 -07:00
|
|
|
.With(f => f.SceneName = "The Office")
|
2013-01-24 00:03:10 -08:00
|
|
|
.With(f => f.SeasonNumber = -1)
|
2011-08-22 23:07:04 -07:00
|
|
|
.Build();
|
|
|
|
|
2013-01-19 15:55:58 -08:00
|
|
|
|
|
|
|
|
2013-01-24 00:03:10 -08:00
|
|
|
Db.Insert(fakeMap);
|
|
|
|
Db.Insert(fakeMap2);
|
2011-08-22 23:07:04 -07:00
|
|
|
|
|
|
|
//Act
|
2013-03-02 10:25:39 -08:00
|
|
|
var sceneName = Mocker.Resolve<SceneMappingService>().GetSceneName(fakeMap.TvdbId);
|
2011-08-22 23:07:04 -07:00
|
|
|
|
|
|
|
//Assert
|
|
|
|
Assert.AreEqual(fakeMap.SceneName, sceneName);
|
|
|
|
}
|
2011-12-01 20:24:44 -08:00
|
|
|
|
2013-01-24 00:03:10 -08:00
|
|
|
[Test]
|
|
|
|
public void GetSceneName_should_be_null_when_seasonNumber_does_not_match()
|
|
|
|
{
|
2013-03-02 10:25:39 -08:00
|
|
|
|
2013-01-24 00:03:10 -08:00
|
|
|
|
|
|
|
var fakeMap = Builder<SceneMapping>.CreateNew()
|
2013-03-02 10:25:39 -08:00
|
|
|
.With(f => f.TvdbId = 12345)
|
2013-01-24 00:03:10 -08:00
|
|
|
.With(f => f.SceneName = "Law and Order")
|
|
|
|
.With(f => f.SceneName = "laworder")
|
|
|
|
.With(f => f.SeasonNumber = 10)
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
Db.Insert(fakeMap);
|
|
|
|
|
2013-03-02 10:25:39 -08:00
|
|
|
Mocker.Resolve<SceneMappingService>().GetSceneName(54321, 5).Should().BeNull();
|
2013-01-24 00:03:10 -08:00
|
|
|
}
|
|
|
|
|
2011-12-01 20:24:44 -08:00
|
|
|
[Test]
|
|
|
|
public void UpdateMappings_should_add_all_mappings_to_database()
|
|
|
|
{
|
2012-01-12 19:22:28 -08:00
|
|
|
WithValidJson();
|
2011-12-01 20:24:44 -08:00
|
|
|
|
|
|
|
//Act
|
2013-03-02 10:25:39 -08:00
|
|
|
Mocker.Resolve<SceneMappingService>().UpdateMappings();
|
2011-12-01 20:24:44 -08:00
|
|
|
|
|
|
|
//Assert
|
2012-01-12 19:22:28 -08:00
|
|
|
Mocker.Verify<HttpProvider>(v => v.DownloadString(SceneMappingUrl), Times.Once());
|
2011-12-01 20:24:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void UpdateMappings_should_overwrite_existing_mappings()
|
|
|
|
{
|
|
|
|
//Setup
|
|
|
|
var fakeMap = Builder<SceneMapping>.CreateNew()
|
2013-03-02 10:25:39 -08:00
|
|
|
.With(f => f.TvdbId = 12345)
|
2011-12-01 20:24:44 -08:00
|
|
|
.With(f => f.SceneName = "Law and Order")
|
|
|
|
.With(f => f.SceneName = "laworder")
|
|
|
|
.Build();
|
|
|
|
|
2012-01-12 19:22:28 -08:00
|
|
|
WithValidJson();
|
2011-12-01 20:24:44 -08:00
|
|
|
Db.Insert(fakeMap);
|
|
|
|
|
|
|
|
//Act
|
2013-03-02 10:25:39 -08:00
|
|
|
Mocker.Resolve<SceneMappingService>().UpdateMappings();
|
2011-12-01 20:24:44 -08:00
|
|
|
|
|
|
|
//Assert
|
2012-01-12 19:22:28 -08:00
|
|
|
Mocker.Verify<HttpProvider>(v => v.DownloadString(SceneMappingUrl), Times.Once());
|
2011-12-01 20:24:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void UpdateMappings_should_not_delete_if_csv_download_fails()
|
|
|
|
{
|
|
|
|
//Setup
|
|
|
|
var fakeMap = Builder<SceneMapping>.CreateNew()
|
2013-03-02 10:25:39 -08:00
|
|
|
.With(f => f.TvdbId = 12345)
|
2011-12-01 20:24:44 -08:00
|
|
|
.With(f => f.SceneName = "Law and Order")
|
|
|
|
.With(f => f.SceneName = "laworder")
|
|
|
|
.Build();
|
|
|
|
|
2012-01-12 19:22:28 -08:00
|
|
|
WithErrorDownloadingJson();
|
2011-12-01 20:24:44 -08:00
|
|
|
Db.Insert(fakeMap);
|
|
|
|
|
|
|
|
//Act
|
2013-03-02 10:25:39 -08:00
|
|
|
Mocker.Resolve<SceneMappingService>().UpdateMappings();
|
2011-12-01 20:24:44 -08:00
|
|
|
|
|
|
|
//Assert
|
2012-01-12 19:22:28 -08:00
|
|
|
Mocker.Verify<HttpProvider>(v => v.DownloadString(SceneMappingUrl), Times.Once());
|
2011-12-01 20:24:44 -08:00
|
|
|
}
|
|
|
|
|
2011-06-02 18:15:02 -07:00
|
|
|
}
|
|
|
|
}
|