2011-11-14 01:22:18 +01:00
|
|
|
|
|
2011-12-02 05:24:44 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Net;
|
2011-06-14 04:15:55 +02:00
|
|
|
|
using FizzWare.NBuilder;
|
2011-12-02 05:24:44 +01:00
|
|
|
|
using FluentAssertions;
|
|
|
|
|
using Moq;
|
2011-06-03 03:15:02 +02:00
|
|
|
|
using NUnit.Framework;
|
2011-06-14 04:15:55 +02:00
|
|
|
|
using NzbDrone.Core.Providers;
|
2011-12-02 05:24:44 +01:00
|
|
|
|
using NzbDrone.Core.Providers.Core;
|
2011-06-14 04:15:55 +02:00
|
|
|
|
using NzbDrone.Core.Repository;
|
2011-06-03 03:15:02 +02:00
|
|
|
|
using NzbDrone.Core.Test.Framework;
|
2011-11-14 01:22:18 +01:00
|
|
|
|
using NzbDrone.Test.Common.AutoMoq;
|
2011-06-03 03:15:02 +02:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Test
|
|
|
|
|
{
|
|
|
|
|
[TestFixture]
|
|
|
|
|
// ReSharper disable InconsistentNaming
|
2011-12-02 05:24:44 +01:00
|
|
|
|
public class SceneMappingProviderTest : CoreTest
|
2011-06-03 03:15:02 +02:00
|
|
|
|
{
|
2011-12-02 05:24:44 +01:00
|
|
|
|
private const string SceneMappingUrl = "http://www.nzbdrone.com/SceneMappings.csv";
|
|
|
|
|
|
|
|
|
|
private void WithValidCsv()
|
|
|
|
|
{
|
|
|
|
|
Mocker.GetMock<HttpProvider>()
|
|
|
|
|
.Setup(s => s.DownloadString(SceneMappingUrl))
|
|
|
|
|
.Returns(File.ReadAllText(@".\Files\SceneMappings.csv"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void WithErrorDownloadingCsv()
|
|
|
|
|
{
|
|
|
|
|
Mocker.GetMock<HttpProvider>()
|
|
|
|
|
.Setup(s => s.DownloadString(SceneMappingUrl))
|
|
|
|
|
.Throws(new WebException());
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-14 04:15:55 +02:00
|
|
|
|
[Test]
|
|
|
|
|
public void GetSceneName_exists()
|
|
|
|
|
{
|
|
|
|
|
//Setup
|
2011-06-17 08:04:01 +02:00
|
|
|
|
var fakeMap = Builder<SceneMapping>.CreateNew()
|
|
|
|
|
.With(f => f.CleanTitle = "laworder")
|
2011-06-14 04:15:55 +02:00
|
|
|
|
.With(f => f.SeriesId = 12345)
|
|
|
|
|
.With(f => f.SceneName = "Law and Order")
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
var mocker = new AutoMoqer();
|
|
|
|
|
|
2011-11-23 06:58:26 +01:00
|
|
|
|
var emptyDatabase = TestDbHelper.GetEmptyDatabase();
|
2011-06-17 08:04:01 +02:00
|
|
|
|
mocker.SetConstant(emptyDatabase);
|
|
|
|
|
emptyDatabase.Insert(fakeMap);
|
2011-06-14 04:15:55 +02:00
|
|
|
|
|
|
|
|
|
//Act
|
2011-06-17 08:04:01 +02:00
|
|
|
|
var sceneName = mocker.Resolve<SceneMappingProvider>().GetSceneName(fakeMap.SeriesId);
|
2011-06-14 04:15:55 +02:00
|
|
|
|
|
|
|
|
|
//Assert
|
|
|
|
|
Assert.AreEqual(fakeMap.SceneName, sceneName);
|
|
|
|
|
}
|
2011-06-03 03:15:02 +02:00
|
|
|
|
|
|
|
|
|
[Test]
|
2011-06-14 04:15:55 +02:00
|
|
|
|
public void GetSeriesId_exists()
|
2011-06-03 03:15:02 +02:00
|
|
|
|
{
|
2011-06-14 04:15:55 +02:00
|
|
|
|
//Setup
|
2011-06-17 08:04:01 +02:00
|
|
|
|
var fakeMap = Builder<SceneMapping>.CreateNew()
|
2011-06-14 04:15:55 +02:00
|
|
|
|
.With(f => f.SeriesId = 12345)
|
|
|
|
|
.With(f => f.SceneName = "Law and Order")
|
|
|
|
|
.With(f => f.SceneName = "laworder")
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
var mocker = new AutoMoqer();
|
|
|
|
|
|
2011-11-23 06:58:26 +01:00
|
|
|
|
var emptyDatabase = TestDbHelper.GetEmptyDatabase();
|
2011-06-17 08:04:01 +02:00
|
|
|
|
mocker.SetConstant(emptyDatabase);
|
|
|
|
|
emptyDatabase.Insert(fakeMap);
|
2011-06-14 04:15:55 +02:00
|
|
|
|
|
|
|
|
|
//Act
|
2011-06-17 08:04:01 +02:00
|
|
|
|
var seriesId = mocker.Resolve<SceneMappingProvider>().GetSeriesId(fakeMap.CleanTitle);
|
2011-06-14 04:15:55 +02:00
|
|
|
|
|
|
|
|
|
//Assert
|
|
|
|
|
Assert.AreEqual(fakeMap.SeriesId, seriesId);
|
2011-06-03 03:15:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-06-14 04:15:55 +02:00
|
|
|
|
[Test]
|
|
|
|
|
public void GetSceneName_null()
|
|
|
|
|
{
|
|
|
|
|
//Setup
|
2011-06-17 08:04:01 +02:00
|
|
|
|
var fakeMap = Builder<SceneMapping>.CreateNew()
|
2011-06-14 04:15:55 +02:00
|
|
|
|
.With(f => f.SeriesId = 12345)
|
|
|
|
|
.With(f => f.SceneName = "Law and Order")
|
2011-06-17 08:04:01 +02:00
|
|
|
|
.With(f => f.SceneName = "laworder")
|
2011-06-14 04:15:55 +02:00
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
var mocker = new AutoMoqer();
|
|
|
|
|
|
2011-11-23 06:58:26 +01:00
|
|
|
|
var emptyDatabase = TestDbHelper.GetEmptyDatabase();
|
2011-06-17 08:04:01 +02:00
|
|
|
|
mocker.SetConstant(emptyDatabase);
|
|
|
|
|
emptyDatabase.Insert(fakeMap);
|
2011-06-14 04:15:55 +02:00
|
|
|
|
|
|
|
|
|
//Act
|
2011-06-17 08:04:01 +02:00
|
|
|
|
var sceneName = mocker.Resolve<SceneMappingProvider>().GetSceneName(54321);
|
2011-06-14 04:15:55 +02:00
|
|
|
|
|
|
|
|
|
//Assert
|
|
|
|
|
Assert.AreEqual(null, sceneName);
|
|
|
|
|
}
|
2011-06-03 03:15:02 +02:00
|
|
|
|
|
|
|
|
|
[Test]
|
2011-06-14 04:15:55 +02:00
|
|
|
|
public void GetSeriesId_null()
|
2011-06-03 03:15:02 +02:00
|
|
|
|
{
|
2011-06-14 04:15:55 +02:00
|
|
|
|
//Setup
|
2011-06-17 08:04:01 +02:00
|
|
|
|
var fakeMap = Builder<SceneMapping>.CreateNew()
|
2011-06-14 04:15:55 +02:00
|
|
|
|
.With(f => f.SeriesId = 12345)
|
|
|
|
|
.With(f => f.SceneName = "Law and Order")
|
2011-06-17 08:04:01 +02:00
|
|
|
|
.With(f => f.CleanTitle = "laworder")
|
2011-06-14 04:15:55 +02:00
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
var mocker = new AutoMoqer();
|
|
|
|
|
|
2011-11-23 06:58:26 +01:00
|
|
|
|
var emptyDatabase = TestDbHelper.GetEmptyDatabase();
|
2011-06-17 08:04:01 +02:00
|
|
|
|
mocker.SetConstant(emptyDatabase);
|
|
|
|
|
emptyDatabase.Insert(fakeMap);
|
2011-06-14 04:15:55 +02:00
|
|
|
|
|
|
|
|
|
//Act
|
2011-06-17 08:04:01 +02:00
|
|
|
|
var seriesId = mocker.Resolve<SceneMappingProvider>().GetSeriesId("notlaworder");
|
2011-06-14 04:15:55 +02:00
|
|
|
|
|
|
|
|
|
//Assert
|
|
|
|
|
Assert.AreEqual(null, seriesId);
|
2011-06-03 03:15:02 +02:00
|
|
|
|
}
|
2011-08-23 08:07:04 +02: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")
|
|
|
|
|
.With(f => f.SeriesId = 12345)
|
|
|
|
|
.With(f => f.SceneName = "The Office")
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
var fakeMap2 = Builder<SceneMapping>.CreateNew()
|
|
|
|
|
.With(f => f.CleanTitle = "officeus")
|
|
|
|
|
.With(f => f.SeriesId = 12345)
|
|
|
|
|
.With(f => f.SceneName = "The Office")
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
var mocker = new AutoMoqer();
|
|
|
|
|
|
2011-11-23 06:58:26 +01:00
|
|
|
|
var db = TestDbHelper.GetEmptyDatabase();
|
2011-08-23 08:07:04 +02:00
|
|
|
|
mocker.SetConstant(db);
|
|
|
|
|
|
|
|
|
|
db.Insert(fakeMap);
|
|
|
|
|
db.Insert(fakeMap2);
|
|
|
|
|
|
|
|
|
|
//Act
|
|
|
|
|
var sceneName = mocker.Resolve<SceneMappingProvider>().GetSceneName(fakeMap.SeriesId);
|
|
|
|
|
|
|
|
|
|
//Assert
|
|
|
|
|
Assert.AreEqual(fakeMap.SceneName, sceneName);
|
|
|
|
|
}
|
2011-12-02 05:24:44 +01:00
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void UpdateMappings_should_add_all_mappings_to_database()
|
|
|
|
|
{
|
|
|
|
|
//Setup
|
|
|
|
|
WithRealDb();
|
|
|
|
|
WithValidCsv();
|
|
|
|
|
|
|
|
|
|
//Act
|
|
|
|
|
Mocker.Resolve<SceneMappingProvider>().UpdateMappings();
|
|
|
|
|
|
|
|
|
|
//Assert
|
|
|
|
|
Mocker.Verify<HttpProvider>(v => v.DownloadString(It.IsAny<string>()), Times.Once());
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
WithRealDb();
|
|
|
|
|
WithValidCsv();
|
|
|
|
|
Db.Insert(fakeMap);
|
|
|
|
|
|
|
|
|
|
//Act
|
|
|
|
|
Mocker.Resolve<SceneMappingProvider>().UpdateMappings();
|
|
|
|
|
|
|
|
|
|
//Assert
|
|
|
|
|
Mocker.Verify<HttpProvider>(v => v.DownloadString(It.IsAny<string>()), Times.Once());
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
WithRealDb();
|
|
|
|
|
WithErrorDownloadingCsv();
|
|
|
|
|
Db.Insert(fakeMap);
|
|
|
|
|
|
|
|
|
|
//Act
|
|
|
|
|
Mocker.Resolve<SceneMappingProvider>().UpdateMappings();
|
|
|
|
|
|
|
|
|
|
//Assert
|
|
|
|
|
Mocker.Verify<HttpProvider>(v => v.DownloadString(It.IsAny<string>()), Times.Once());
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
WithRealDb();
|
|
|
|
|
Db.Insert(fakeMap);
|
|
|
|
|
|
|
|
|
|
//Act
|
|
|
|
|
Mocker.Resolve<SceneMappingProvider>().UpdateIfEmpty();
|
|
|
|
|
|
|
|
|
|
//Assert
|
|
|
|
|
Mocker.Verify<HttpProvider>(v => v.DownloadString(It.IsAny<string>()), Times.Never());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void UpdateIfEmpty_should_update_if_count_is_zero()
|
|
|
|
|
{
|
|
|
|
|
//Setup
|
|
|
|
|
WithRealDb();
|
|
|
|
|
WithValidCsv();
|
|
|
|
|
|
|
|
|
|
//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 03:15:02 +02:00
|
|
|
|
}
|
|
|
|
|
}
|