MediaFileProvider moved to PetaPoco.

Season Grid now shows episodeFile quality.
This commit is contained in:
Mark McDowall 2011-06-16 20:29:39 -07:00
parent a151c9cd0f
commit 3dbb38b54f
5 changed files with 74 additions and 56 deletions

View File

@ -17,6 +17,7 @@ using NzbDrone.Core.Providers.Jobs;
using NzbDrone.Core.Repository;
using NzbDrone.Core.Repository.Quality;
using NzbDrone.Core.Test.Framework;
using PetaPoco;
using SubSonic.Repository;
namespace NzbDrone.Core.Test
@ -51,24 +52,20 @@ namespace NzbDrone.Core.Test
mocker.GetMock<DiskProvider>()
.Setup(e => e.GetSize(fileName)).Returns(12345).Verifiable();
mocker.GetMock<IRepository>()
.Setup(r => r.Exists(It.IsAny<Expression<Func<EpisodeFile, Boolean>>>())).Returns(false).Verifiable();
var database = mocker.GetMock<IDatabase>(MockBehavior.Strict);
database.Setup(r => r.Single<int>(It.IsAny<string>(), It.IsAny<object>())).Returns(0).Verifiable();
database.Setup(r => r.Insert(It.IsAny<EpisodeFile>())).Returns(1).Verifiable();
mocker.GetMock<IRepository>()
.Setup(r => r.Add(It.IsAny<EpisodeFile>())).Returns(0).Verifiable();
mocker.GetMock<EpisodeProvider>()
.Setup(e => e.GetEpisode(fakeSeries.SeriesId, seasonNumber, episodeNumner)).Returns(fakeEpisode);
//Act
var result = mocker.Resolve<MediaFileProvider>().ImportFile(fakeSeries, fileName);
//Assert
Assert.IsNotNull(result);
mocker.GetMock<IRepository>().Verify(r => r.Add(result), Times.Once());
mocker.GetMock<IDatabase>().Verify(r => r.Insert(result), Times.Once());
mocker.VerifyAllMocks();
result.SeasonNumber.Should().Be(fakeEpisode.SeasonNumber);
@ -99,10 +96,9 @@ namespace NzbDrone.Core.Test
//Mocks
var mocker = new AutoMoqer();
mocker.GetMock<IRepository>()
.Setup(r => r.Exists(It.IsAny<Expression<Func<EpisodeFile, Boolean>>>())).Returns(false).Verifiable();
mocker.GetMock<IRepository>()
.Setup(r => r.Add(It.IsAny<EpisodeFile>())).Returns(0).Verifiable();
var database = mocker.GetMock<IDatabase>(MockBehavior.Strict);
database.Setup(r => r.Single<int>(It.IsAny<string>(), It.IsAny<object>())).Returns(0).Verifiable();
database.Setup(r => r.Insert(It.IsAny<EpisodeFile>())).Returns(1).Verifiable();
mocker.GetMock<EpisodeProvider>()
.Setup(e => e.GetEpisode(fakeSeries.SeriesId, airDate)).Returns(fakeEpisode).
@ -117,8 +113,8 @@ namespace NzbDrone.Core.Test
//Assert
Assert.IsNotNull(result);
mocker.GetMock<IRepository>().VerifyAll();
mocker.GetMock<IRepository>().Verify(r => r.Add(result), Times.Once());
mocker.GetMock<IDatabase>().VerifyAll();
mocker.GetMock<IDatabase>().Verify(r => r.Insert(result), Times.Once());
mocker.GetMock<EpisodeProvider>().VerifyAll();
mocker.GetMock<DiskProvider>().VerifyAll();
@ -147,12 +143,13 @@ namespace NzbDrone.Core.Test
//Fakes
var fakeSeries = Builder<Series>.CreateNew().Build();
var fakeEpisodeFile = Builder<EpisodeFile>.CreateNew().With(s => s.SeriesId = fakeSeries.SeriesId).Build();
//Mocks
var mocker = new AutoMoqer();
mocker.GetMock<IRepository>(MockBehavior.Strict)
.Setup(r => r.Exists(It.IsAny<Expression<Func<EpisodeFile, Boolean>>>())).Returns(true).Verifiable();
mocker.GetMock<IDatabase>(MockBehavior.Strict)
.Setup(r => r.Single<int>(It.IsAny<string>(), It.IsAny<object>())).Returns(1).Verifiable();
mocker.GetMock<DiskProvider>()
.Setup(e => e.GetSize(fileName)).Returns(size).Verifiable();
@ -183,13 +180,12 @@ namespace NzbDrone.Core.Test
//Mocks
var mocker = new AutoMoqer();
mocker.GetMock<IRepository>(MockBehavior.Strict)
.Setup(r => r.Exists(It.IsAny<Expression<Func<EpisodeFile, Boolean>>>())).Returns(false).Verifiable();
mocker.GetMock<IDatabase>(MockBehavior.Strict)
.Setup(r => r.Single<int>(It.IsAny<string>(), It.IsAny<object>())).Returns(0).Verifiable();
mocker.GetMock<DiskProvider>()
.Setup(e => e.GetSize(fileName)).Returns(size).Verifiable();
//Act
var result = mocker.Resolve<MediaFileProvider>().ImportFile(fakeSeries, fileName);
@ -252,8 +248,9 @@ namespace NzbDrone.Core.Test
//Mocks
var mocker = new AutoMoqer();
mocker.GetMock<IRepository>(MockBehavior.Strict)
.Setup(r => r.Exists(It.IsAny<Expression<Func<EpisodeFile, Boolean>>>())).Returns(true).Verifiable();
mocker.GetMock<IDatabase>(MockBehavior.Strict)
.Setup(r => r.Single<int>(It.IsAny<string>(), It.IsAny<object>())).Returns(1).Verifiable();
mocker.GetMock<EpisodeProvider>(MockBehavior.Strict);
mocker.GetMock<DiskProvider>()
.Setup(e => e.GetSize(fileName)).Returns(500000).Verifiable();
@ -264,7 +261,7 @@ namespace NzbDrone.Core.Test
//Assert
Assert.IsNull(result);
mocker.GetMock<IRepository>().Verify(r => r.Add(result), Times.Never());
mocker.GetMock<IDatabase>().Verify(r => r.Insert(result), Times.Never());
mocker.VerifyAllMocks();
}
@ -285,8 +282,8 @@ namespace NzbDrone.Core.Test
//Mocks
var mocker = new AutoMoqer();
mocker.GetMock<IRepository>(MockBehavior.Strict)
.Setup(r => r.Exists(It.IsAny<Expression<Func<EpisodeFile, Boolean>>>())).Returns(false).Verifiable();
mocker.GetMock<IDatabase>(MockBehavior.Strict)
.Setup(r => r.Single<int>(It.IsAny<string>(), It.IsAny<object>())).Returns(0).Verifiable();
mocker.GetMock<EpisodeProvider>(MockBehavior.Strict)
.Setup(e => e.GetEpisode(fakeSeries.SeriesId, seasonNumber, episodeNumner)).Returns<Episode>(null).
@ -320,7 +317,6 @@ namespace NzbDrone.Core.Test
}
[Test]
public void scan_media_job_should_not_scan_new_series()
{
@ -350,10 +346,16 @@ namespace NzbDrone.Core.Test
.WhereAll().Have(s => s.SeriesId = 20).Build();
var mocker = new AutoMoqer();
var repo = MockLib.GetEmptyRepository(true);
repo.AddMany(firstSeriesFiles);
repo.AddMany(secondSeriesFiles);
mocker.SetConstant(repo);
var database = MockLib.GetEmptyDatabase(true);
foreach (var file in firstSeriesFiles)
database.Insert(file);
foreach (var file in secondSeriesFiles)
database.Insert(file);
mocker.SetConstant(database);
var result = mocker.Resolve<MediaFileProvider>().GetSeriesFiles(12);
@ -402,9 +404,9 @@ namespace NzbDrone.Core.Test
configProvider.SetupGet(c => c.NumberStyle).Returns(2);
configProvider.SetupGet(c => c.ReplaceSpaces).Returns(false);
var repo = mocker.GetMock<IRepository>();
repo.Setup(r => r.Exists(It.IsAny<Expression<Func<EpisodeFile, Boolean>>>())).Returns(false).Verifiable();
repo.Setup(r => r.Add<EpisodeFile>(It.IsAny<EpisodeFile>())).Returns(1);
var database = mocker.GetMock<IDatabase>(MockBehavior.Strict);
database.Setup(r => r.Single<int>(It.IsAny<string>(), It.IsAny<object>())).Returns(0).Verifiable();
database.Setup(r => r.Insert(It.IsAny<EpisodeFile>())).Returns(1).Verifiable();
//Act
var result = mocker.Resolve<MediaFileProvider>().ImportNewFiles(@"C:\Test\30 Rock - 1x05 - Episode Title", fakeSeries);
@ -464,9 +466,10 @@ namespace NzbDrone.Core.Test
configProvider.SetupGet(c => c.NumberStyle).Returns(2);
configProvider.SetupGet(c => c.ReplaceSpaces).Returns(false);
var repo = mocker.GetMock<IRepository>();
repo.Setup(r => r.Exists(It.IsAny<Expression<Func<EpisodeFile, Boolean>>>())).Returns(false).Verifiable();
repo.Setup(r => r.Add<EpisodeFile>(It.IsAny<EpisodeFile>())).Returns(1);
var database = mocker.GetMock<IDatabase>(MockBehavior.Strict);
database.Setup(r => r.Single<int>(It.IsAny<string>(), It.IsAny<object>())).Returns(0).Verifiable();
database.Setup(r => r.Insert(It.IsAny<EpisodeFile>())).Returns(1);
database.Setup(r => r.Delete<EpisodeFile>(It.IsAny<int>())).Returns(1);
//Act
var result = mocker.Resolve<MediaFileProvider>().ImportNewFiles(@"C:\Test\30 Rock - 1x05 - Episode Title", fakeSeries);

View File

@ -44,8 +44,15 @@ namespace NzbDrone.Core.Datastore.Migrations
.WithNullableColumn("AirDate", DbType.DateTime)
.WithNullableColumn("GrabDate", DbType.DateTime);
db.CreateTable("EpisodeFiles")
.WithPrimaryKeyColumn("EpisodeFileId", DbType.Int32).AsIdentity()
.WithNotNullableColumn("SeriesId", DbType.Int32)
.WithNotNullableColumn("Path", DbType.String)
.WithNotNullableColumn("Quality", DbType.Int16)
.WithNotNullableColumn("Proper", DbType.Int16)
.WithNotNullableColumn("Size", DbType.Int64)
.WithNotNullableColumn("DateAdded", DbType.DateTime)
.WithNotNullableColumn("SeasonNumber", DbType.Int16);
}
}
}

View File

@ -10,7 +10,7 @@ using NzbDrone.Core.Model.Notification;
using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Repository;
using NzbDrone.Core.Repository.Quality;
using SubSonic.Repository;
using PetaPoco;
namespace NzbDrone.Core.Providers
{
@ -21,19 +21,19 @@ namespace NzbDrone.Core.Providers
private readonly DiskProvider _diskProvider;
private readonly EpisodeProvider _episodeProvider;
private readonly SeriesProvider _seriesProvider;
private readonly IRepository _repository;
private readonly ConfigProvider _configProvider;
private readonly IDatabase _database;
[Inject]
public MediaFileProvider(IRepository repository, DiskProvider diskProvider,
EpisodeProvider episodeProvider, SeriesProvider seriesProvider,
ConfigProvider configProvider)
public MediaFileProvider(DiskProvider diskProvider, EpisodeProvider episodeProvider,
SeriesProvider seriesProvider, ConfigProvider configProvider,
IDatabase database)
{
_repository = repository;
_diskProvider = diskProvider;
_episodeProvider = episodeProvider;
_seriesProvider = seriesProvider;
_configProvider = configProvider;
_database = database;
}
public MediaFileProvider() { }
@ -76,7 +76,7 @@ namespace NzbDrone.Core.Providers
}
//Check to see if file already exists in the database
if (!_repository.Exists<EpisodeFile>(e => e.Path == Parser.NormalizePath(filePath)))
if (_database.Single<int>("SELECT COUNT (*) FROM EpisodeFiles WHERE Path =@0", Parser.NormalizePath(filePath)) == 0)
{
var parseResult = Parser.ParseEpisodeInfo(filePath);
@ -132,7 +132,7 @@ namespace NzbDrone.Core.Providers
episodeFile.Quality = parseResult.Quality.QualityType;
episodeFile.Proper = parseResult.Quality.Proper;
episodeFile.SeasonNumber = parseResult.SeasonNumber;
var fileId = (int)_repository.Add(episodeFile);
var fileId = (int)_database.Insert(episodeFile);
//This is for logging + updating the episodes that are linked to this EpisodeFile
string episodeList = String.Empty;
@ -179,29 +179,29 @@ namespace NzbDrone.Core.Providers
}
//Delete it from the DB
_repository.Delete<EpisodeFile>(episodeFile.EpisodeFileId);
_database.Delete<EpisodeFile>(episodeFile.EpisodeFileId);
}
}
}
public virtual void Update(EpisodeFile episodeFile)
{
_repository.Update(episodeFile);
_database.Update(episodeFile);
}
public virtual EpisodeFile GetEpisodeFile(int episodeFileId)
{
return _repository.Single<EpisodeFile>(episodeFileId);
return _database.Single<EpisodeFile>(episodeFileId);
}
public virtual List<EpisodeFile> GetEpisodeFiles()
{
return _repository.All<EpisodeFile>().ToList();
return _database.Fetch<EpisodeFile>();
}
public virtual IEnumerable<EpisodeFile> GetSeriesFiles(int seriesId)
public virtual List<EpisodeFile> GetSeriesFiles(int seriesId)
{
return _repository.All<EpisodeFile>().Where(c => c.SeriesId == seriesId);
return _database.Fetch<EpisodeFile>("WHERE seriesId= @0", seriesId);
}
public virtual Tuple<int, int> GetEpisodeFilesCount(int seriesId)

View File

@ -1,10 +1,13 @@
using System;
using System.Collections.Generic;
using NzbDrone.Core.Repository.Quality;
using PetaPoco;
using SubSonic.SqlGeneration.Schema;
namespace NzbDrone.Core.Repository
{
[TableName("EpisodeFiles")]
[PrimaryKey("EpisodeFileId", autoIncrement = true)]
public class EpisodeFile
{
[SubSonicPrimaryKey]
@ -19,9 +22,11 @@ namespace NzbDrone.Core.Repository
public DateTime DateAdded { get; set; }
[SubSonicToManyRelation]
[Ignore]
public virtual IList<Episode> Episodes { get; set; }
[SubSonicToOneRelation(ThisClassContainsJoinKey = true)]
[Ignore]
public virtual Series Series { get; set; }
}
}

View File

@ -235,14 +235,19 @@ namespace NzbDrone.Web.Controllers
foreach (var e in episodesInDb)
{
var episodeFile = e.EpisodeFile;
var episodePath = String.Empty;
var episodeFileId = 0;
var episodePath = String.Empty;
var episodeQuality = String.Empty;
EpisodeFile episodeFile = null;
if (e.EpisodeFileId > 0)
episodeFile = _mediaFileProvider.GetEpisodeFile(e.EpisodeFileId);
if (episodeFile != null)
{
episodePath = episodeFile.Path;
episodeFileId = episodeFile.EpisodeFileId;
episodeQuality = episodeFile.Quality.ToString();
}
episodes.Add(new EpisodeModel
@ -256,9 +261,7 @@ namespace NzbDrone.Web.Controllers
Path = episodePath,
EpisodeFileId = episodeFileId,
Status = e.Status.ToString(),
Quality = e.EpisodeFile == null
? String.Empty
: e.EpisodeFile.Quality.ToString()
Quality = episodeQuality
});
}