SceneMappingProvider will perform an update if database is empty when looking for a SeriesId or SceneName.

This commit is contained in:
Mark McDowall 2011-12-01 20:24:44 -08:00
parent f9dab843ae
commit 3b4b20cc95
4 changed files with 144 additions and 4 deletions

View File

@ -0,0 +1,5 @@
csinewyork,73696,CSI
csiny,73696,CSI
csi,72546,CSI
csilasvegas,72546,CSI
archer,110381,Archer
1 csinewyork 73696 CSI
2 csiny 73696 CSI
3 csi 72546 CSI
4 csilasvegas 72546 CSI
5 archer 110381 Archer

View File

@ -159,7 +159,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="QualityProfileTest.cs" />
<Compile Include="ProviderTests\SabProviderTest.cs" />
<Compile Include="SceneMappingTest.cs" />
<Compile Include="ProviderTests\SceneMappingProviderTest.cs" />
<Compile Include="ProviderTests\SeriesProviderTest.cs" />
<Compile Include="ProviderTests\TvDbProviderTest.cs" />
</ItemGroup>
@ -235,6 +235,9 @@
<SubType>Designer</SubType>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<None Include="Files\SceneMappings.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Files\TestArchive.zip">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>

View File

@ -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<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());
}
[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<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);
}
}
}

View File

@ -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<SceneMapping>();
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<SceneMapping>("WHERE SeriesId = @0", seriesId);
if (item == null)
@ -77,6 +79,8 @@ namespace NzbDrone.Core.Providers
public virtual Nullable<Int32> GetSeriesId(string cleanName)
{
UpdateIfEmpty();
var item = _database.SingleOrDefault<SceneMapping>("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<int>("SELECT COUNT(*) FROM SceneMappings");
if (count == 0)
UpdateMappings();
}
}
}