diff --git a/NzbDrone.Core.Test/Files/SceneMappings.csv b/NzbDrone.Core.Test/Files/SceneMappings.csv
new file mode 100644
index 000000000..4238b2ebb
--- /dev/null
+++ b/NzbDrone.Core.Test/Files/SceneMappings.csv
@@ -0,0 +1,5 @@
+csinewyork,73696,CSI
+csiny,73696,CSI
+csi,72546,CSI
+csilasvegas,72546,CSI
+archer,110381,Archer
\ No newline at end of file
diff --git a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj
index 96228512e..5d3ccc7e0 100644
--- a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj
+++ b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj
@@ -159,7 +159,7 @@
-
+
@@ -235,6 +235,9 @@
Designer
Always
+
+ Always
+
Always
diff --git a/NzbDrone.Core.Test/SceneMappingTest.cs b/NzbDrone.Core.Test/ProviderTests/SceneMappingProviderTest.cs
similarity index 52%
rename from NzbDrone.Core.Test/SceneMappingTest.cs
rename to NzbDrone.Core.Test/ProviderTests/SceneMappingProviderTest.cs
index 5c801f892..bf520f615 100644
--- a/NzbDrone.Core.Test/SceneMappingTest.cs
+++ b/NzbDrone.Core.Test/ProviderTests/SceneMappingProviderTest.cs
@@ -1,7 +1,13 @@
+using System;
+using System.IO;
+using System.Net;
using FizzWare.NBuilder;
+using FluentAssertions;
+using Moq;
using NUnit.Framework;
using NzbDrone.Core.Providers;
+using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Repository;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Test.Common.AutoMoq;
@@ -10,8 +16,24 @@ namespace NzbDrone.Core.Test
{
[TestFixture]
// ReSharper disable InconsistentNaming
- public class SceneMappingTest : CoreTest
+ public class SceneMappingProviderTest : CoreTest
{
+ private const string SceneMappingUrl = "http://www.nzbdrone.com/SceneMappings.csv";
+
+ private void WithValidCsv()
+ {
+ Mocker.GetMock()
+ .Setup(s => s.DownloadString(SceneMappingUrl))
+ .Returns(File.ReadAllText(@".\Files\SceneMappings.csv"));
+ }
+
+ private void WithErrorDownloadingCsv()
+ {
+ Mocker.GetMock()
+ .Setup(s => s.DownloadString(SceneMappingUrl))
+ .Throws(new WebException());
+ }
+
[Test]
public void GetSceneName_exists()
{
@@ -136,5 +158,103 @@ namespace NzbDrone.Core.Test
//Assert
Assert.AreEqual(fakeMap.SceneName, sceneName);
}
+
+ [Test]
+ public void UpdateMappings_should_add_all_mappings_to_database()
+ {
+ //Setup
+ WithRealDb();
+ WithValidCsv();
+
+ //Act
+ Mocker.Resolve().UpdateMappings();
+
+ //Assert
+ Mocker.Verify(v => v.DownloadString(It.IsAny()), Times.Once());
+ var result = Db.Fetch();
+ result.Should().HaveCount(5);
+ }
+
+ [Test]
+ public void UpdateMappings_should_overwrite_existing_mappings()
+ {
+ //Setup
+ var fakeMap = Builder.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().UpdateMappings();
+
+ //Assert
+ Mocker.Verify(v => v.DownloadString(It.IsAny()), Times.Once());
+ var result = Db.Fetch();
+ result.Should().HaveCount(5);
+ }
+
+ [Test]
+ public void UpdateMappings_should_not_delete_if_csv_download_fails()
+ {
+ //Setup
+ var fakeMap = Builder.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().UpdateMappings();
+
+ //Assert
+ Mocker.Verify(v => v.DownloadString(It.IsAny()), Times.Once());
+ var result = Db.Fetch();
+ result.Should().HaveCount(1);
+ }
+
+ [Test]
+ public void UpdateIfEmpty_should_not_update_if_count_is_not_zero()
+ {
+ //Setup
+ var fakeMap = Builder.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().UpdateIfEmpty();
+
+ //Assert
+ Mocker.Verify(v => v.DownloadString(It.IsAny()), Times.Never());
+ }
+
+ [Test]
+ public void UpdateIfEmpty_should_update_if_count_is_zero()
+ {
+ //Setup
+ WithRealDb();
+ WithValidCsv();
+
+ //Act
+ Mocker.Resolve().UpdateIfEmpty();
+
+ //Assert
+ Mocker.Verify(v => v.DownloadString(SceneMappingUrl), Times.Once());
+ var result = Db.Fetch();
+ result.Should().HaveCount(5);
+ }
}
}
diff --git a/NzbDrone.Core/Providers/SceneMappingProvider.cs b/NzbDrone.Core/Providers/SceneMappingProvider.cs
index 9ef2b0f53..313d0c6b5 100644
--- a/NzbDrone.Core/Providers/SceneMappingProvider.cs
+++ b/NzbDrone.Core/Providers/SceneMappingProvider.cs
@@ -29,7 +29,7 @@ namespace NzbDrone.Core.Providers
{
try
{
- var mapping = _httpProvider.DownloadString("http://vps.nzbdrone.com/SceneMappings.csv");
+ var mapping = _httpProvider.DownloadString("http://www.nzbdrone.com/SceneMappings.csv");
var newMaps = new List();
using (var reader = new StringReader(mapping))
@@ -59,7 +59,7 @@ namespace NzbDrone.Core.Providers
catch (Exception ex)
{
- Logger.InfoException("Failed to Update Scene Mappings", ex);
+ Logger.InfoException("Failed to Update Scene Mappings:", ex);
return false;
}
return true;
@@ -67,6 +67,8 @@ namespace NzbDrone.Core.Providers
public virtual string GetSceneName(int seriesId)
{
+ UpdateIfEmpty();
+
var item = _database.FirstOrDefault("WHERE SeriesId = @0", seriesId);
if (item == null)
@@ -77,6 +79,8 @@ namespace NzbDrone.Core.Providers
public virtual Nullable GetSeriesId(string cleanName)
{
+ UpdateIfEmpty();
+
var item = _database.SingleOrDefault("WHERE CleanTitle = @0", cleanName);
if (item == null)
@@ -84,5 +88,13 @@ namespace NzbDrone.Core.Providers
return item.SeriesId;
}
+
+ public void UpdateIfEmpty()
+ {
+ var count = _database.ExecuteScalar("SELECT COUNT(*) FROM SceneMappings");
+
+ if (count == 0)
+ UpdateMappings();
+ }
}
}