Lidarr/NzbDrone.Core.Test/ProviderTests/SceneMappingProviderTest.cs

229 lines
6.6 KiB
C#
Raw Normal View History

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