1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-11-23 19:23:31 +01:00

failed attempt to write a test for Series Controller

This commit is contained in:
Keivan 2010-09-27 20:04:39 -07:00
parent 020a7462c0
commit babe2735ee
10 changed files with 105 additions and 23 deletions

View File

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Moq;
using NzbDrone.Core.Controllers;
namespace NzbDrone.Core.Test
{
/// <summary>
/// Provides the standard Mocks needed for a typical test
/// </summary>
static class MockLib
{
public static string[] StandardSeries
{
get { return new string[] { "C:\\TV\\The Simpsons", "C:\\TV\\Family Guy" }; }
}
public static IConfigController StandardConfig
{
get
{
var mock = new Mock<IConfigController>();
mock.SetupGet(c => c.SeriesRoot).Returns("C:\\");
return mock.Object;
}
}
public static IDiskController StandardDisk
{
get
{
var mock = new Mock<IDiskController>();
mock.Setup(c => c.GetDirectories(It.IsAny<String>())).Returns(StandardSeries);
mock.Setup(c => c.Exists(It.Is<String>(d => StandardSeries.Contains(d)))).Returns(true);
return mock.Object;
}
}
}
}

View File

@ -67,6 +67,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="DbConfigControllerTest.cs" /> <Compile Include="DbConfigControllerTest.cs" />
<Compile Include="MockLib.cs" />
<Compile Include="Ninject.Moq\ExtensionsForBindingSyntax.cs" /> <Compile Include="Ninject.Moq\ExtensionsForBindingSyntax.cs" />
<Compile Include="Ninject.Moq\MockingKernel.cs" /> <Compile Include="Ninject.Moq\MockingKernel.cs" />
<Compile Include="Ninject.Moq\MockProvider.cs" /> <Compile Include="Ninject.Moq\MockProvider.cs" />

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Text; using System.Text;
using FizzWare.NBuilder; using FizzWare.NBuilder;
using Gallio.Framework; using Gallio.Framework;
@ -21,6 +22,7 @@ namespace NzbDrone.Core.Test
public class SeriesTest public class SeriesTest
{ {
[Test] [Test]
[Ignore("Can't get it to work")]
[Description("This test will confirm that a folder will be skipped if it has been resolved to a series already assigned to another folder")] [Description("This test will confirm that a folder will be skipped if it has been resolved to a series already assigned to another folder")]
public void skip_same_series_diffrent_folder() public void skip_same_series_diffrent_folder()
{ {
@ -35,21 +37,34 @@ namespace NzbDrone.Core.Test
.With(f => f.TvdbId = tvDbId.ToString()) .With(f => f.TvdbId = tvDbId.ToString())
.Build(); .Build();
moqData.Setup(f => f.Single<Series>(tvDbId)). moqData.Setup(f => f.Exists<Series>(c => c.TvdbId == tvDbId.ToString())).
Returns(fakeSeries); Returns(true);
//setup tvdb to return the same show, //setup tvdb to return the same show,
IList<TvdbSearchResult> fakeSearchResult = Builder<TvdbSearchResult>.CreateListOfSize(4).WhereTheFirst(1).Has(f => f.Id = tvDbId).Build(); IList<TvdbSearchResult> fakeSearchResult = Builder<TvdbSearchResult>.CreateListOfSize(4).WhereTheFirst(1).Has(f => f.Id = tvDbId).Build();
TvdbSeries fakeTvDbSeries = Builder<TvdbSeries>.CreateNew()
.With(f => f.Id = tvDbId)
.Build();
moqTvdb.Setup(f => f.GetSeries(It.IsAny<int>(), It.IsAny<TvdbLanguage>())).Returns(fakeTvDbSeries);
moqTvdb.Setup(f => f.SearchSeries(It.IsAny<string>())). moqTvdb.Setup(f => f.SearchSeries(It.IsAny<string>())).
Returns(fakeSearchResult); Returns(fakeSearchResult);
var kernel = new MockingKernel(); var kernel = new MockingKernel();
kernel.Bind<IRepository>().ToConstant(moqData.Object); kernel.Bind<IRepository>().ToConstant(moqData.Object);
kernel.Bind<ITvDbController>().ToConstant(moqTvdb.Object); kernel.Bind<ITvDbController>().ToConstant(moqTvdb.Object);
kernel.Bind<IConfigController>().ToConstant(MockLib.StandardConfig);
kernel.Bind<IDiskController>().ToConstant(MockLib.StandardDisk);
kernel.Bind<ISeriesController>().To<SeriesController>();
//Act
var seriesController = kernel.Get<ISeriesController>(); var seriesController = kernel.Get<ISeriesController>();
seriesController.a seriesController.SyncSeriesWithDisk();
//Assert
//Verify that the show was added to the database only once.
moqData.Verify(c => c.Add(It.IsAny<Series>()), Times.Once());
} }
} }
} }

View File

@ -22,14 +22,6 @@ namespace NzbDrone.Core.Controllers
return Directory.CreateDirectory(path).FullName; return Directory.CreateDirectory(path).FullName;
} }
public string CleanPath(string path)
{
if (string.IsNullOrEmpty(path)) throw new ArgumentException("Path can not be null or empty");
return path.ToLower().Trim('/', '\\', ' ');
}
#endregion #endregion
} }
} }

View File

@ -7,6 +7,5 @@ namespace NzbDrone.Core.Controllers
bool Exists(string path); bool Exists(string path);
string[] GetDirectories(string path); string[] GetDirectories(string path);
String CreateDirectory(string path); String CreateDirectory(string path);
string CleanPath(string path);
} }
} }

View File

@ -2,6 +2,7 @@
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using log4net; using log4net;
using NzbDrone.Core.Helpers;
using NzbDrone.Core.Repository; using NzbDrone.Core.Repository;
using SubSonic.Repository; using SubSonic.Repository;
using TvdbLib.Data; using TvdbLib.Data;
@ -34,7 +35,7 @@ namespace NzbDrone.Core.Controllers
public Series GetSeries(int tvdbId) public Series GetSeries(int tvdbId)
{ {
return _sonioRepo.Single<Series>(s=> s.TvdbId == tvdbId.ToString()); return _sonioRepo.Single<Series>(s => s.TvdbId == tvdbId.ToString());
} }
@ -44,7 +45,7 @@ namespace NzbDrone.Core.Controllers
foreach (string seriesFolder in _diskController.GetDirectories(_config.SeriesRoot)) foreach (string seriesFolder in _diskController.GetDirectories(_config.SeriesRoot))
{ {
var cleanPath =_diskController.CleanPath(new DirectoryInfo(seriesFolder).FullName); var cleanPath = Disk.CleanPath(new DirectoryInfo(seriesFolder).FullName);
if (!_sonioRepo.Exists<Series>(s => s.Path == cleanPath)) if (!_sonioRepo.Exists<Series>(s => s.Path == cleanPath))
{ {
_logger.InfoFormat("Folder '{0} isn't mapped to a series in the database. Trying to map it.'", cleanPath); _logger.InfoFormat("Folder '{0} isn't mapped to a series in the database. Trying to map it.'", cleanPath);
@ -59,7 +60,7 @@ namespace NzbDrone.Core.Controllers
private void AddShow(string path) private void AddShow(string path)
{ {
var searchResults = _tvDb.SearchSeries(new DirectoryInfo(path).Name); var searchResults = _tvDb.SearchSeries(new DirectoryInfo(path).Name);
if (searchResults.Count != 0) if (searchResults.Count != 0 && !_sonioRepo.Exists<Series>(s => s.TvdbId == searchResults[0].Id.ToString()))
{ {
AddShow(path, _tvDb.GetSeries(searchResults[0].Id, searchResults[0].Language)); AddShow(path, _tvDb.GetSeries(searchResults[0].Id, searchResults[0].Language));
} }
@ -67,7 +68,16 @@ namespace NzbDrone.Core.Controllers
private void AddShow(string path, TvdbSeries series) private void AddShow(string path, TvdbSeries series)
{ {
_sonioRepo.Add(new Series { TvdbId = series.Id.ToString(), SeriesName = series.SeriesName, AirTimes = series.AirsTime, AirsDayOfWeek = series.AirsDayOfWeek, Overview = series.Overview, Status = series.Status, Language = series.Language.Abbriviation, Path = path }); var repoSeries = new Series();
repoSeries.TvdbId = series.Id.ToString();
repoSeries.SeriesName = series.SeriesName;
repoSeries.AirTimes = series.AirsTime;
repoSeries.AirsDayOfWeek = series.AirsDayOfWeek;
repoSeries.Overview = series.Overview;
repoSeries.Status = series.Status;
repoSeries.Language = series.Language != null ? series.Language.Abbriviation : string.Empty;
repoSeries.Path = path;
_sonioRepo.Add(repoSeries);
} }
} }
} }

View File

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NzbDrone.Core.Helpers
{
static class Disk
{
/// <summary>
/// Cleans the path. making it a uniform path.
/// this will normalize all different presentations of a single folder.
/// </summary>
/// <param name="path">The path.</param>
/// <returns>Cleaned Path</returns>
public static string CleanPath(string path)
{
if (string.IsNullOrEmpty(path)) throw new ArgumentException("Path can not be null or empty");
return path.ToLower().Trim('/', '\\', ' ');
}
}
}

View File

@ -14,7 +14,7 @@ namespace NzbDrone.Core
public static void BindKernel(IKernel kernel) public static void BindKernel(IKernel kernel)
{ {
string connectionString = String.Format("Data Source={0};Version=3;",Path.Combine(AppPath, "nzbdrone.db")) ; string connectionString = String.Format("Data Source={0};Version=3;", Path.Combine(AppPath, "nzbdrone.db"));
var provider = ProviderFactory.GetProvider(connectionString, "System.Data.SQLite"); var provider = ProviderFactory.GetProvider(connectionString, "System.Data.SQLite");
kernel.Bind<ISeriesController>().To<SeriesController>(); kernel.Bind<ISeriesController>().To<SeriesController>();
@ -25,9 +25,6 @@ namespace NzbDrone.Core
kernel.Bind<IRepository>().ToMethod(c => new SimpleRepository(provider, SimpleRepositoryOptions.RunMigrations)); kernel.Bind<IRepository>().ToMethod(c => new SimpleRepository(provider, SimpleRepositoryOptions.RunMigrations));
} }
private static string _appPath;
public static String AppPath public static String AppPath
{ {
get { return new DirectoryInfo(HttpContext.Current.Server.MapPath("\\")).Parent.FullName; } get { return new DirectoryInfo(HttpContext.Current.Server.MapPath("\\")).Parent.FullName; }

View File

@ -138,6 +138,7 @@
<Compile Include="Controllers\ISeriesController.cs" /> <Compile Include="Controllers\ISeriesController.cs" />
<Compile Include="Controllers\ITvDbController.cs" /> <Compile Include="Controllers\ITvDbController.cs" />
<Compile Include="Controllers\SabController.cs" /> <Compile Include="Controllers\SabController.cs" />
<Compile Include="Helpers\Disk.cs" />
<Compile Include="Repository\Config.cs" /> <Compile Include="Repository\Config.cs" />
<Compile Include="Repository\Series.cs" /> <Compile Include="Repository\Series.cs" />
<Compile Include="Main.cs" /> <Compile Include="Main.cs" />

View File

@ -37,7 +37,7 @@ namespace NzbDrone.Core.Repository
set; set;
} }
public string AirTimes public String AirTimes
{ {
get; get;
set; set;