From 2da79128cf73c87f2516974b9111f3893c91e87f Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Fri, 27 Jan 2012 23:55:09 -0800 Subject: [PATCH] Fixes for blacklisting. --- .../ProviderTests/HistoryProviderTest.cs | 34 +++++++++++++++++++ .../QualityNeededFixture.cs | 20 +++++------ .../Datastore/Migrations/Migration20120127.cs | 1 + NzbDrone.Core/Providers/HistoryProvider.cs | 6 ++++ 4 files changed, 51 insertions(+), 10 deletions(-) diff --git a/NzbDrone.Core.Test/ProviderTests/HistoryProviderTest.cs b/NzbDrone.Core.Test/ProviderTests/HistoryProviderTest.cs index 54633acc4..7c6a9782d 100644 --- a/NzbDrone.Core.Test/ProviderTests/HistoryProviderTest.cs +++ b/NzbDrone.Core.Test/ProviderTests/HistoryProviderTest.cs @@ -246,6 +246,24 @@ namespace NzbDrone.Core.Test.ProviderTests result.Should().BeTrue(); } + [Test] + public void IsBlacklisted_should_return_true_if_nzbTitle_is_blacklisted_ignoring_case() + { + WithRealDb(); + + var history = Builder.CreateNew() + .With(h => h.Blacklisted = true) + .Build(); + + Db.Insert(history); + + //Act + var result = Mocker.Resolve().IsBlacklisted(history.NzbTitle.ToLower()); + + //Assert + result.Should().BeTrue(); + } + [Test] public void IsBlacklisted_should_return_false_if_newzbinId_doesnt_exist() { @@ -300,6 +318,14 @@ namespace NzbDrone.Core.Test.ProviderTests result.Should().BeTrue(); } + [Test] + public void IsBlacklisted_should_throw_if_newzbinId_is_less_than_1() + { + Assert.Throws(() => + Mocker.Resolve().IsBlacklisted(0) + ); + } + [Test] public void SetBlacklist_should_set_to_true_when_true_is_passed_in() { @@ -337,5 +363,13 @@ namespace NzbDrone.Core.Test.ProviderTests var result = Db.Single(history.HistoryId); result.Blacklisted.Should().BeFalse(); } + + [Test] + public void SetBlacklist_should_throw_if_newzbinId_is_less_than_1() + { + Assert.Throws(() => + Mocker.Resolve().SetBlacklist(0, true) + ); + } } } \ No newline at end of file diff --git a/NzbDrone.Core.Test/ProviderTests/InventoryProviderTests/QualityNeededFixture.cs b/NzbDrone.Core.Test/ProviderTests/InventoryProviderTests/QualityNeededFixture.cs index f4df614f7..7c1526732 100644 --- a/NzbDrone.Core.Test/ProviderTests/InventoryProviderTests/QualityNeededFixture.cs +++ b/NzbDrone.Core.Test/ProviderTests/InventoryProviderTests/QualityNeededFixture.cs @@ -29,6 +29,13 @@ namespace NzbDrone.Core.Test.ProviderTests.InventoryProviderTests private QualityProfile sdProfile; private Series series; + private void WithNoBlacklist() + { + Mocker.GetMock() + .Setup(s => s.IsBlacklisted(It.IsAny())) + .Returns(false); + } + [SetUp] public void Setup() { @@ -186,6 +193,7 @@ namespace NzbDrone.Core.Test.ProviderTests.InventoryProviderTests public void IsQualityNeeded_lesser_file_in_history_should_be_downloaded() { WithStrictMocker(); + WithNoBlacklist(); parseResultSingle.Series.QualityProfile = sdProfile; parseResultSingle.Quality.QualityType = QualityTypes.DVD; @@ -206,10 +214,6 @@ namespace NzbDrone.Core.Test.ProviderTests.InventoryProviderTests .Setup(s => s.Get(It.IsAny())) .Returns(new QualityType { MaxSize = 100, MinSize = 0 }); - Mocker.GetMock() - .Setup(s => s.IsBlacklisted(It.IsAny())) - .Returns(false); - episode.EpisodeFile.Quality = QualityTypes.SDTV; //Act @@ -244,9 +248,7 @@ namespace NzbDrone.Core.Test.ProviderTests.InventoryProviderTests .Setup(s => s.Get(It.IsAny())) .Returns(new QualityType { MaxSize = 100, MinSize = 0 }); - Mocker.GetMock() - .Setup(s => s.IsBlacklisted(It.IsAny())) - .Returns(false); + WithNoBlacklist(); episode.EpisodeFile.Quality = QualityTypes.SDTV; //Act @@ -307,9 +309,7 @@ namespace NzbDrone.Core.Test.ProviderTests.InventoryProviderTests .Setup(s => s.Get(It.IsAny())) .Returns(new QualityType { MaxSize = 100, MinSize = 0 }); - Mocker.GetMock() - .Setup(s => s.IsBlacklisted(It.IsAny())) - .Returns(false); + WithNoBlacklist(); episode.EpisodeFile.Quality = QualityTypes.SDTV; //Act diff --git a/NzbDrone.Core/Datastore/Migrations/Migration20120127.cs b/NzbDrone.Core/Datastore/Migrations/Migration20120127.cs index a608e1cff..fa393c4e0 100644 --- a/NzbDrone.Core/Datastore/Migrations/Migration20120127.cs +++ b/NzbDrone.Core/Datastore/Migrations/Migration20120127.cs @@ -10,6 +10,7 @@ namespace NzbDrone.Core.Datastore.Migrations { Database.AddColumn("History", "NewzbinId", DbType.Int32, ColumnProperty.Null); Database.AddColumn("History", "Blacklisted", DbType.Boolean, ColumnProperty.Null); + Database.ExecuteNonQuery("UPDATE History SET Blacklisted = 0"); } } } \ No newline at end of file diff --git a/NzbDrone.Core/Providers/HistoryProvider.cs b/NzbDrone.Core/Providers/HistoryProvider.cs index b8663b70e..9768072cd 100644 --- a/NzbDrone.Core/Providers/HistoryProvider.cs +++ b/NzbDrone.Core/Providers/HistoryProvider.cs @@ -76,11 +76,17 @@ namespace NzbDrone.Core.Providers public virtual bool IsBlacklisted(int newzbinId) { + if (newzbinId <= 0) + throw new ArgumentException("Newzbin ID must be greater than 0"); + return _database.Exists("WHERE Blacklisted = 1 AND NewzbinId = @0", newzbinId); } public virtual void SetBlacklist(int historyId, bool toggle) { + if (historyId <= 0) + throw new ArgumentException("HistoryId must be greater than 0"); + _database.Execute("UPDATE History SET Blacklisted = @0 WHERE HistoryId = @1", toggle, historyId); } }