From a9f08caa186f728685a5d25f2203f1f6cd433206 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Thu, 7 Apr 2011 21:03:46 -0700 Subject: [PATCH] Removed IQuality provider Updated AutoMoq to support SetConstant --- .../AutoMoq/{AutoMoq.cs => AutoMoqer.cs} | 17 +++--- NzbDrone.Core.Test/EpisodeProviderTest.cs | 4 +- .../Ninject.Moq/MockProvider.cs | 2 +- NzbDrone.Core.Test/NzbDrone.Core.Test.csproj | 10 +++- NzbDrone.Core.Test/SeriesProviderTest.cs | 56 +++++++++---------- NzbDrone.Core.Test/packages.config | 1 + NzbDrone.Core/CentralDispatch.cs | 2 +- NzbDrone.Core/NzbDrone.Core.csproj | 1 - NzbDrone.Core/Providers/EpisodeProvider.cs | 4 +- NzbDrone.Core/Providers/IQualityProvider.cs | 17 ------ NzbDrone.Core/Providers/QualityProvider.cs | 15 +++-- NzbDrone.Core/Providers/SeriesProvider.cs | 4 +- 12 files changed, 61 insertions(+), 72 deletions(-) rename NzbDrone.Core.Test/AutoMoq/{AutoMoq.cs => AutoMoqer.cs} (97%) delete mode 100644 NzbDrone.Core/Providers/IQualityProvider.cs diff --git a/NzbDrone.Core.Test/AutoMoq/AutoMoq.cs b/NzbDrone.Core.Test/AutoMoq/AutoMoqer.cs similarity index 97% rename from NzbDrone.Core.Test/AutoMoq/AutoMoq.cs rename to NzbDrone.Core.Test/AutoMoq/AutoMoqer.cs index 7f4c7c9bf..1fe168dcf 100644 --- a/NzbDrone.Core.Test/AutoMoq/AutoMoq.cs +++ b/NzbDrone.Core.Test/AutoMoq/AutoMoqer.cs @@ -1,12 +1,15 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; +using System.Runtime.CompilerServices; using AutoMoq.Unity; using Microsoft.Practices.Unity; using Moq; using Moq.Language.Flow; +[assembly: InternalsVisibleTo("AutoMoq.Tests")] + namespace AutoMoq { public class AutoMoqer @@ -38,18 +41,18 @@ namespace AutoMoq return TheRegisteredMockForThisType(type); } - public virtual void SetConstant(T instance) where T : class - { - container.RegisterInstance(instance); - SetMock(instance.GetType(), null); - } - internal virtual void SetMock(Type type, Mock mock) { if (registeredMocks.ContainsKey(type) == false) registeredMocks.Add(type, mock); } + public virtual void SetConstant(T instance) where T : class + { + container.RegisterInstance(instance); + SetMock(instance.GetType(), null); + } + #region private methods private void SetupAutoMoqer(IUnityContainer container) diff --git a/NzbDrone.Core.Test/EpisodeProviderTest.cs b/NzbDrone.Core.Test/EpisodeProviderTest.cs index 4526bee73..4828141c3 100644 --- a/NzbDrone.Core.Test/EpisodeProviderTest.cs +++ b/NzbDrone.Core.Test/EpisodeProviderTest.cs @@ -67,8 +67,8 @@ namespace NzbDrone.Core.Test //Setup var season = new Mock(); var series = new Mock(); - var history = new Mock(); - var quality = new Mock(); + //var history = new Mock(); + //var quality = new Mock(); var repo = new Mock(); var epInDb = new Episode diff --git a/NzbDrone.Core.Test/Ninject.Moq/MockProvider.cs b/NzbDrone.Core.Test/Ninject.Moq/MockProvider.cs index 830e81c8b..3edab1a3f 100644 --- a/NzbDrone.Core.Test/Ninject.Moq/MockProvider.cs +++ b/NzbDrone.Core.Test/Ninject.Moq/MockProvider.cs @@ -9,7 +9,7 @@ namespace Ninject.Moq /// /// Creates mocked instances via Moq. /// - public class MockProvider : IProvider + public class MockProvider : IProvider { private static readonly Dictionary _injectors = new Dictionary(); diff --git a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index 04fdc762d..2dd08b835 100644 --- a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -85,9 +85,13 @@ - - - + + + Code + + + Code + diff --git a/NzbDrone.Core.Test/SeriesProviderTest.cs b/NzbDrone.Core.Test/SeriesProviderTest.cs index 3c47f6040..99ec64ac6 100644 --- a/NzbDrone.Core.Test/SeriesProviderTest.cs +++ b/NzbDrone.Core.Test/SeriesProviderTest.cs @@ -65,18 +65,16 @@ namespace NzbDrone.Core.Test [Test] public void Add_new_series() { - var repo = MockLib.GetEmptyRepository(); - var kernel = new MockingKernel(); - kernel.Bind().To(); - kernel.Bind().ToConstant(repo); + var mocker = new AutoMoqer(); + mocker.SetConstant(MockLib.GetEmptyRepository()); string path = "C:\\Test\\"; int tvDbId = 1234; int qualityProfileId = 2; //Act - var seriesProvider = kernel.Get(); + var seriesProvider = mocker.Resolve(); seriesProvider.AddSeries(path, tvDbId, qualityProfileId); @@ -107,24 +105,23 @@ namespace NzbDrone.Core.Test [Test] public void Test_is_monitored() { - var kernel = new MockingKernel(); - var repo = MockLib.GetEmptyRepository(); - kernel.Bind().ToConstant(repo); - kernel.Bind().To(); + var mocker = new AutoMoqer(); - repo.Add(Builder.CreateNew() + mocker.SetConstant(MockLib.GetEmptyRepository()); + + mocker.Resolve().Add(Builder.CreateNew() .With(c => c.Monitored = true) .With(c => c.SeriesId = 12) .Build()); - repo.Add(Builder.CreateNew() + mocker.Resolve().Add(Builder.CreateNew() .With(c => c.Monitored = false) .With(c => c.SeriesId = 11) .Build()); //Act, Assert - var provider = kernel.Get(); + var provider = mocker.Resolve(); Assert.IsTrue(provider.IsMonitored(12)); Assert.IsFalse(provider.IsMonitored(11)); Assert.IsFalse(provider.IsMonitored(1)); @@ -141,32 +138,31 @@ namespace NzbDrone.Core.Test [Row(12, QualityTypes.WEBDL, false)] public void QualityWanted(int seriesId, QualityTypes qualityTypes, Boolean result) { - var kernel = new MockingKernel(); - var repo = MockLib.GetEmptyRepository(); - kernel.Bind().ToConstant(repo); - kernel.Bind().To(); - var quality = Builder.CreateNew() - .With(q => q.Allowed = new List() { QualityTypes.BDRip, QualityTypes.DVD, QualityTypes.TV }) - .With(q => q.Cutoff = QualityTypes.DVD) - .Build(); + .With(q => q.Allowed = new List() { QualityTypes.BDRip, QualityTypes.DVD, QualityTypes.TV }) + .With(q => q.Cutoff = QualityTypes.DVD) + .Build(); - var qualityProviderMock = new Mock(); - qualityProviderMock.Setup(c => c.Find(quality.QualityProfileId)).Returns(quality).Verifiable(); - kernel.Bind().ToConstant(qualityProviderMock.Object); + var series = Builder.CreateNew() + .With(c => c.SeriesId = 12) + .With(c => c.QualityProfileId = quality.QualityProfileId) + .Build(); + var mocker = new AutoMoqer(); + var emptyRepository = MockLib.GetEmptyRepository(); + mocker.SetConstant(emptyRepository); - repo.Add(Builder.CreateNew() - .With(c => c.SeriesId = 12) - .With(c => c.QualityProfileId = quality.QualityProfileId) - .Build()); - //Act - var needed = kernel.Get().QualityWanted(seriesId, qualityTypes); + mocker.GetMock() + .Setup(c => c.Find(quality.QualityProfileId)).Returns(quality); - Assert.AreEqual(result, needed); + emptyRepository.Add(series); + //Act + var needed = mocker.Resolve().QualityWanted(seriesId, qualityTypes); + + Assert.AreEqual(result, needed); } } } diff --git a/NzbDrone.Core.Test/packages.config b/NzbDrone.Core.Test/packages.config index 16a0accaf..353e03756 100644 --- a/NzbDrone.Core.Test/packages.config +++ b/NzbDrone.Core.Test/packages.config @@ -4,4 +4,5 @@ + \ No newline at end of file diff --git a/NzbDrone.Core/CentralDispatch.cs b/NzbDrone.Core/CentralDispatch.cs index b205ad696..d094f2503 100644 --- a/NzbDrone.Core/CentralDispatch.cs +++ b/NzbDrone.Core/CentralDispatch.cs @@ -67,7 +67,7 @@ namespace NzbDrone.Core _kernel.Bind().To(); _kernel.Bind().To(); _kernel.Bind().To(); - _kernel.Bind().To(); + _kernel.Bind().To(); _kernel.Bind().To(); _kernel.Bind().To(); _kernel.Bind().To(); diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 43cdcbeae..024448b3b 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -188,7 +188,6 @@ - diff --git a/NzbDrone.Core/Providers/EpisodeProvider.cs b/NzbDrone.Core/Providers/EpisodeProvider.cs index e11eed74c..a5bd0660e 100644 --- a/NzbDrone.Core/Providers/EpisodeProvider.cs +++ b/NzbDrone.Core/Providers/EpisodeProvider.cs @@ -18,12 +18,12 @@ namespace NzbDrone.Core.Providers private readonly ISeasonProvider _seasons; private readonly TvDbProvider _tvDb; private readonly IHistoryProvider _history; - private readonly IQualityProvider _quality; + private readonly QualityProvider _quality; private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); public EpisodeProvider(IRepository sonicRepo, ISeriesProvider seriesProvider, ISeasonProvider seasonProvider, TvDbProvider tvDbProvider, - IHistoryProvider history, IQualityProvider quality) + IHistoryProvider history, QualityProvider quality) { _sonicRepo = sonicRepo; _series = seriesProvider; diff --git a/NzbDrone.Core/Providers/IQualityProvider.cs b/NzbDrone.Core/Providers/IQualityProvider.cs deleted file mode 100644 index 865fd47da..000000000 --- a/NzbDrone.Core/Providers/IQualityProvider.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using NzbDrone.Core.Repository.Quality; - -namespace NzbDrone.Core.Providers -{ - public interface IQualityProvider - { - void Add(QualityProfile profile); - void Update(QualityProfile profile); - void Delete(int profileId); - List GetAllProfiles(); - QualityProfile Find(int profileId); - } -} diff --git a/NzbDrone.Core/Providers/QualityProvider.cs b/NzbDrone.Core/Providers/QualityProvider.cs index 977cf7e54..6c2fe37da 100644 --- a/NzbDrone.Core/Providers/QualityProvider.cs +++ b/NzbDrone.Core/Providers/QualityProvider.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Text; using NLog; @@ -8,11 +9,13 @@ using SubSonic.Repository; namespace NzbDrone.Core.Providers { - public class QualityProvider : IQualityProvider + public class QualityProvider { private IRepository _sonicRepo; private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); + public QualityProvider() { } + public QualityProvider(IRepository sonicRepo) { _sonicRepo = sonicRepo; @@ -20,12 +23,12 @@ namespace NzbDrone.Core.Providers #region IQualityProvider Members - public void Add(QualityProfile profile) + public virtual void Add(QualityProfile profile) { _sonicRepo.Add(profile); } - public void Update(QualityProfile profile) + public virtual void Update(QualityProfile profile) { if (!_sonicRepo.Exists(q => q.QualityProfileId == profile.QualityProfileId)) { @@ -36,19 +39,19 @@ namespace NzbDrone.Core.Providers _sonicRepo.Update(profile); } - public void Delete(int profileId) + public virtual void Delete(int profileId) { _sonicRepo.Delete(profileId); } - public List GetAllProfiles() + public virtual List GetAllProfiles() { var profiles = _sonicRepo.All().ToList(); return profiles; } - public QualityProfile Find(int profileId) + public virtual QualityProfile Find(int profileId) { return _sonicRepo.Single(q => q.QualityProfileId == profileId); } diff --git a/NzbDrone.Core/Providers/SeriesProvider.cs b/NzbDrone.Core/Providers/SeriesProvider.cs index 9e89f690b..4e5c67cb1 100644 --- a/NzbDrone.Core/Providers/SeriesProvider.cs +++ b/NzbDrone.Core/Providers/SeriesProvider.cs @@ -22,11 +22,11 @@ namespace NzbDrone.Core.Providers private readonly IConfigProvider _config; private readonly IRepository _sonioRepo; private readonly TvDbProvider _tvDb; - private readonly IQualityProvider _quality; + private readonly QualityProvider _quality; private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); public SeriesProvider(IConfigProvider configProvider, - IRepository dataRepository, TvDbProvider tvDbProvider, IQualityProvider quality) + IRepository dataRepository, TvDbProvider tvDbProvider, QualityProvider quality) { _config = configProvider; _sonioRepo = dataRepository;