1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-12 11:15:43 +02:00

Removed IQuality provider

Updated AutoMoq to support SetConstant
This commit is contained in:
kay.one 2011-04-07 21:03:46 -07:00
parent 0a783542a6
commit a9f08caa18
12 changed files with 61 additions and 72 deletions

View File

@ -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 @@ public virtual Mock<T> GetMock<T>() where T : class
return TheRegisteredMockForThisType<T>(type);
}
public virtual void SetConstant<T>(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>(T instance) where T : class
{
container.RegisterInstance(instance);
SetMock(instance.GetType(), null);
}
#region private methods
private void SetupAutoMoqer(IUnityContainer container)

View File

@ -67,8 +67,8 @@ public void IsNeededTrue()
//Setup
var season = new Mock<ISeasonProvider>();
var series = new Mock<ISeriesProvider>();
var history = new Mock<IHistoryProvider>();
var quality = new Mock<IQualityProvider>();
//var history = new Mock<IHistoryProvider>();
//var quality = new Mock<IQualityProvider>();
var repo = new Mock<IRepository>();
var epInDb = new Episode

View File

@ -9,7 +9,7 @@ namespace Ninject.Moq
/// <summary>
/// Creates mocked instances via Moq.
/// </summary>
public class MockProvider : IProvider
public class MockProvider : IProvider
{
private static readonly Dictionary<Type, ConstructorInjector> _injectors = new Dictionary<Type, ConstructorInjector>();

View File

@ -85,9 +85,13 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="AutoMoq\AutoMoq.cs" />
<Compile Include="AutoMoq\Unity\AutoMockingBuilderStrategy.cs" />
<Compile Include="AutoMoq\Unity\AutoMockingContainerExtension.cs" />
<Compile Include="AutoMoq\AutoMoqer.cs" />
<Compile Include="AutoMoq\Unity\AutoMockingBuilderStrategy.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="AutoMoq\Unity\AutoMockingContainerExtension.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="RssProviderTest.cs" />
<Compile Include="HistoryProviderTest.cs" />
<Compile Include="MediaFileProviderTests.cs" />

View File

@ -65,18 +65,16 @@ public void Map_path_to_series()
[Test]
public void Add_new_series()
{
var repo = MockLib.GetEmptyRepository();
var kernel = new MockingKernel();
kernel.Bind<ISeriesProvider>().To<SeriesProvider>();
kernel.Bind<IRepository>().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<ISeriesProvider>();
var seriesProvider = mocker.Resolve<SeriesProvider>();
seriesProvider.AddSeries(path, tvDbId, qualityProfileId);
@ -107,24 +105,23 @@ public void Test_Parse_Success(string postTitle, string title)
[Test]
public void Test_is_monitored()
{
var kernel = new MockingKernel();
var repo = MockLib.GetEmptyRepository();
kernel.Bind<IRepository>().ToConstant(repo);
kernel.Bind<ISeriesProvider>().To<SeriesProvider>();
var mocker = new AutoMoqer();
repo.Add(Builder<Series>.CreateNew()
mocker.SetConstant(MockLib.GetEmptyRepository());
mocker.Resolve<IRepository>().Add(Builder<Series>.CreateNew()
.With(c => c.Monitored = true)
.With(c => c.SeriesId = 12)
.Build());
repo.Add(Builder<Series>.CreateNew()
mocker.Resolve<IRepository>().Add(Builder<Series>.CreateNew()
.With(c => c.Monitored = false)
.With(c => c.SeriesId = 11)
.Build());
//Act, Assert
var provider = kernel.Get<ISeriesProvider>();
var provider = mocker.Resolve<SeriesProvider>();
Assert.IsTrue(provider.IsMonitored(12));
Assert.IsFalse(provider.IsMonitored(11));
Assert.IsFalse(provider.IsMonitored(1));
@ -141,32 +138,31 @@ public void Test_is_monitored()
[Row(12, QualityTypes.WEBDL, false)]
public void QualityWanted(int seriesId, QualityTypes qualityTypes, Boolean result)
{
var kernel = new MockingKernel();
var repo = MockLib.GetEmptyRepository();
kernel.Bind<IRepository>().ToConstant(repo);
kernel.Bind<ISeriesProvider>().To<SeriesProvider>();
var quality = Builder<QualityProfile>.CreateNew()
.With(q => q.Allowed = new List<QualityTypes>() { QualityTypes.BDRip, QualityTypes.DVD, QualityTypes.TV })
.With(q => q.Cutoff = QualityTypes.DVD)
.Build();
.With(q => q.Allowed = new List<QualityTypes>() { QualityTypes.BDRip, QualityTypes.DVD, QualityTypes.TV })
.With(q => q.Cutoff = QualityTypes.DVD)
.Build();
var qualityProviderMock = new Mock<IQualityProvider>();
qualityProviderMock.Setup(c => c.Find(quality.QualityProfileId)).Returns(quality).Verifiable();
kernel.Bind<IQualityProvider>().ToConstant(qualityProviderMock.Object);
var series = Builder<Series>.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<Series>.CreateNew()
.With(c => c.SeriesId = 12)
.With(c => c.QualityProfileId = quality.QualityProfileId)
.Build());
mocker.GetMock<QualityProvider>()
.Setup(c => c.Find(quality.QualityProfileId)).Returns(quality);
emptyRepository.Add(series);
//Act
var needed = kernel.Get<ISeriesProvider>().QualityWanted(seriesId, qualityTypes);
var needed = mocker.Resolve<SeriesProvider>().QualityWanted(seriesId, qualityTypes);
Assert.AreEqual(result, needed);
}
}
}

View File

@ -4,4 +4,5 @@
<package id="NBuilder" version="2.3.0.0" />
<package id="Moq" version="4.0.10827" />
<package id="Unity" version="2.0" />
<package id="AutoMoq" version="1.3.1.3" />
</packages>

View File

@ -67,7 +67,7 @@ public static void BindKernel()
_kernel.Bind<IDownloadProvider>().To<SabProvider>();
_kernel.Bind<HttpProvider>().To<HttpProvider>();
_kernel.Bind<IHistoryProvider>().To<HistoryProvider>();
_kernel.Bind<IQualityProvider>().To<QualityProvider>();
_kernel.Bind<QualityProvider>().To<QualityProvider>();
_kernel.Bind<IRootDirProvider>().To<RootDirProvider>();
_kernel.Bind<IExtenalNotificationProvider>().To<ExternalNotificationProvider>();
_kernel.Bind<IXbmcProvider>().To<XbmcProvider>();

View File

@ -188,7 +188,6 @@
<Compile Include="Providers\IIndexerProvider.cs" />
<Compile Include="Providers\IndexerProvider.cs" />
<Compile Include="Providers\IPostProcessingProvider.cs" />
<Compile Include="Providers\IQualityProvider.cs" />
<Compile Include="Providers\IRenameProvider.cs" />
<Compile Include="Providers\IRootDirProvider.cs" />
<Compile Include="Providers\IRssSyncProvider.cs" />

View File

@ -18,12 +18,12 @@ public class EpisodeProvider : IEpisodeProvider
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;

View File

@ -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<QualityProfile> GetAllProfiles();
QualityProfile Find(int profileId);
}
}

View File

@ -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 @@
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 @@ public QualityProvider(IRepository sonicRepo)
#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<QualityProfile>(q => q.QualityProfileId == profile.QualityProfileId))
{
@ -36,19 +39,19 @@ public void Update(QualityProfile profile)
_sonicRepo.Update(profile);
}
public void Delete(int profileId)
public virtual void Delete(int profileId)
{
_sonicRepo.Delete<QualityProfile>(profileId);
}
public List<QualityProfile> GetAllProfiles()
public virtual List<QualityProfile> GetAllProfiles()
{
var profiles = _sonicRepo.All<QualityProfile>().ToList();
return profiles;
}
public QualityProfile Find(int profileId)
public virtual QualityProfile Find(int profileId)
{
return _sonicRepo.Single<QualityProfile>(q => q.QualityProfileId == profileId);
}

View File

@ -22,11 +22,11 @@ public class SeriesProvider : ISeriesProvider
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;