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;
using NzbDrone.Core.Repository.Quality; using NzbDrone.Core.Repository.Quality;
using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Test.Framework;
using PetaPoco;
using SubSonic.Repository; using SubSonic.Repository;
namespace NzbDrone.Core.Test namespace NzbDrone.Core.Test
@ -51,24 +52,20 @@ namespace NzbDrone.Core.Test
mocker.GetMock<DiskProvider>() mocker.GetMock<DiskProvider>()
.Setup(e => e.GetSize(fileName)).Returns(12345).Verifiable(); .Setup(e => e.GetSize(fileName)).Returns(12345).Verifiable();
mocker.GetMock<IRepository>() var database = mocker.GetMock<IDatabase>(MockBehavior.Strict);
.Setup(r => r.Exists(It.IsAny<Expression<Func<EpisodeFile, Boolean>>>())).Returns(false).Verifiable(); 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>() mocker.GetMock<EpisodeProvider>()
.Setup(e => e.GetEpisode(fakeSeries.SeriesId, seasonNumber, episodeNumner)).Returns(fakeEpisode); .Setup(e => e.GetEpisode(fakeSeries.SeriesId, seasonNumber, episodeNumner)).Returns(fakeEpisode);
//Act //Act
var result = mocker.Resolve<MediaFileProvider>().ImportFile(fakeSeries, fileName); var result = mocker.Resolve<MediaFileProvider>().ImportFile(fakeSeries, fileName);
//Assert //Assert
Assert.IsNotNull(result); 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(); mocker.VerifyAllMocks();
result.SeasonNumber.Should().Be(fakeEpisode.SeasonNumber); result.SeasonNumber.Should().Be(fakeEpisode.SeasonNumber);
@ -99,10 +96,9 @@ namespace NzbDrone.Core.Test
//Mocks //Mocks
var mocker = new AutoMoqer(); var mocker = new AutoMoqer();
mocker.GetMock<IRepository>() var database = mocker.GetMock<IDatabase>(MockBehavior.Strict);
.Setup(r => r.Exists(It.IsAny<Expression<Func<EpisodeFile, Boolean>>>())).Returns(false).Verifiable(); database.Setup(r => r.Single<int>(It.IsAny<string>(), It.IsAny<object>())).Returns(0).Verifiable();
mocker.GetMock<IRepository>() database.Setup(r => r.Insert(It.IsAny<EpisodeFile>())).Returns(1).Verifiable();
.Setup(r => r.Add(It.IsAny<EpisodeFile>())).Returns(0).Verifiable();
mocker.GetMock<EpisodeProvider>() mocker.GetMock<EpisodeProvider>()
.Setup(e => e.GetEpisode(fakeSeries.SeriesId, airDate)).Returns(fakeEpisode). .Setup(e => e.GetEpisode(fakeSeries.SeriesId, airDate)).Returns(fakeEpisode).
@ -117,8 +113,8 @@ namespace NzbDrone.Core.Test
//Assert //Assert
Assert.IsNotNull(result); Assert.IsNotNull(result);
mocker.GetMock<IRepository>().VerifyAll(); mocker.GetMock<IDatabase>().VerifyAll();
mocker.GetMock<IRepository>().Verify(r => r.Add(result), Times.Once()); mocker.GetMock<IDatabase>().Verify(r => r.Insert(result), Times.Once());
mocker.GetMock<EpisodeProvider>().VerifyAll(); mocker.GetMock<EpisodeProvider>().VerifyAll();
mocker.GetMock<DiskProvider>().VerifyAll(); mocker.GetMock<DiskProvider>().VerifyAll();
@ -147,12 +143,13 @@ namespace NzbDrone.Core.Test
//Fakes //Fakes
var fakeSeries = Builder<Series>.CreateNew().Build(); var fakeSeries = Builder<Series>.CreateNew().Build();
var fakeEpisodeFile = Builder<EpisodeFile>.CreateNew().With(s => s.SeriesId = fakeSeries.SeriesId).Build();
//Mocks //Mocks
var mocker = new AutoMoqer(); var mocker = new AutoMoqer();
mocker.GetMock<IRepository>(MockBehavior.Strict) mocker.GetMock<IDatabase>(MockBehavior.Strict)
.Setup(r => r.Exists(It.IsAny<Expression<Func<EpisodeFile, Boolean>>>())).Returns(true).Verifiable(); .Setup(r => r.Single<int>(It.IsAny<string>(), It.IsAny<object>())).Returns(1).Verifiable();
mocker.GetMock<DiskProvider>() mocker.GetMock<DiskProvider>()
.Setup(e => e.GetSize(fileName)).Returns(size).Verifiable(); .Setup(e => e.GetSize(fileName)).Returns(size).Verifiable();
@ -183,13 +180,12 @@ namespace NzbDrone.Core.Test
//Mocks //Mocks
var mocker = new AutoMoqer(); var mocker = new AutoMoqer();
mocker.GetMock<IRepository>(MockBehavior.Strict) mocker.GetMock<IDatabase>(MockBehavior.Strict)
.Setup(r => r.Exists(It.IsAny<Expression<Func<EpisodeFile, Boolean>>>())).Returns(false).Verifiable(); .Setup(r => r.Single<int>(It.IsAny<string>(), It.IsAny<object>())).Returns(0).Verifiable();
mocker.GetMock<DiskProvider>() mocker.GetMock<DiskProvider>()
.Setup(e => e.GetSize(fileName)).Returns(size).Verifiable(); .Setup(e => e.GetSize(fileName)).Returns(size).Verifiable();
//Act //Act
var result = mocker.Resolve<MediaFileProvider>().ImportFile(fakeSeries, fileName); var result = mocker.Resolve<MediaFileProvider>().ImportFile(fakeSeries, fileName);
@ -252,8 +248,9 @@ namespace NzbDrone.Core.Test
//Mocks //Mocks
var mocker = new AutoMoqer(); var mocker = new AutoMoqer();
mocker.GetMock<IRepository>(MockBehavior.Strict) mocker.GetMock<IDatabase>(MockBehavior.Strict)
.Setup(r => r.Exists(It.IsAny<Expression<Func<EpisodeFile, Boolean>>>())).Returns(true).Verifiable(); .Setup(r => r.Single<int>(It.IsAny<string>(), It.IsAny<object>())).Returns(1).Verifiable();
mocker.GetMock<EpisodeProvider>(MockBehavior.Strict); mocker.GetMock<EpisodeProvider>(MockBehavior.Strict);
mocker.GetMock<DiskProvider>() mocker.GetMock<DiskProvider>()
.Setup(e => e.GetSize(fileName)).Returns(500000).Verifiable(); .Setup(e => e.GetSize(fileName)).Returns(500000).Verifiable();
@ -264,7 +261,7 @@ namespace NzbDrone.Core.Test
//Assert //Assert
Assert.IsNull(result); 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(); mocker.VerifyAllMocks();
} }
@ -285,8 +282,8 @@ namespace NzbDrone.Core.Test
//Mocks //Mocks
var mocker = new AutoMoqer(); var mocker = new AutoMoqer();
mocker.GetMock<IRepository>(MockBehavior.Strict) mocker.GetMock<IDatabase>(MockBehavior.Strict)
.Setup(r => r.Exists(It.IsAny<Expression<Func<EpisodeFile, Boolean>>>())).Returns(false).Verifiable(); .Setup(r => r.Single<int>(It.IsAny<string>(), It.IsAny<object>())).Returns(0).Verifiable();
mocker.GetMock<EpisodeProvider>(MockBehavior.Strict) mocker.GetMock<EpisodeProvider>(MockBehavior.Strict)
.Setup(e => e.GetEpisode(fakeSeries.SeriesId, seasonNumber, episodeNumner)).Returns<Episode>(null). .Setup(e => e.GetEpisode(fakeSeries.SeriesId, seasonNumber, episodeNumner)).Returns<Episode>(null).
@ -320,7 +317,6 @@ namespace NzbDrone.Core.Test
} }
[Test] [Test]
public void scan_media_job_should_not_scan_new_series() 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(); .WhereAll().Have(s => s.SeriesId = 20).Build();
var mocker = new AutoMoqer(); var mocker = new AutoMoqer();
var repo = MockLib.GetEmptyRepository(true);
repo.AddMany(firstSeriesFiles); var database = MockLib.GetEmptyDatabase(true);
repo.AddMany(secondSeriesFiles);
mocker.SetConstant(repo); 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); 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.NumberStyle).Returns(2);
configProvider.SetupGet(c => c.ReplaceSpaces).Returns(false); configProvider.SetupGet(c => c.ReplaceSpaces).Returns(false);
var repo = mocker.GetMock<IRepository>(); var database = mocker.GetMock<IDatabase>(MockBehavior.Strict);
repo.Setup(r => r.Exists(It.IsAny<Expression<Func<EpisodeFile, Boolean>>>())).Returns(false).Verifiable(); database.Setup(r => r.Single<int>(It.IsAny<string>(), It.IsAny<object>())).Returns(0).Verifiable();
repo.Setup(r => r.Add<EpisodeFile>(It.IsAny<EpisodeFile>())).Returns(1); database.Setup(r => r.Insert(It.IsAny<EpisodeFile>())).Returns(1).Verifiable();
//Act //Act
var result = mocker.Resolve<MediaFileProvider>().ImportNewFiles(@"C:\Test\30 Rock - 1x05 - Episode Title", fakeSeries); 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.NumberStyle).Returns(2);
configProvider.SetupGet(c => c.ReplaceSpaces).Returns(false); configProvider.SetupGet(c => c.ReplaceSpaces).Returns(false);
var repo = mocker.GetMock<IRepository>(); var database = mocker.GetMock<IDatabase>(MockBehavior.Strict);
repo.Setup(r => r.Exists(It.IsAny<Expression<Func<EpisodeFile, Boolean>>>())).Returns(false).Verifiable(); database.Setup(r => r.Single<int>(It.IsAny<string>(), It.IsAny<object>())).Returns(0).Verifiable();
repo.Setup(r => r.Add<EpisodeFile>(It.IsAny<EpisodeFile>())).Returns(1); database.Setup(r => r.Insert(It.IsAny<EpisodeFile>())).Returns(1);
database.Setup(r => r.Delete<EpisodeFile>(It.IsAny<int>())).Returns(1);
//Act //Act
var result = mocker.Resolve<MediaFileProvider>().ImportNewFiles(@"C:\Test\30 Rock - 1x05 - Episode Title", fakeSeries); 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("AirDate", DbType.DateTime)
.WithNullableColumn("GrabDate", 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.Providers.Core;
using NzbDrone.Core.Repository; using NzbDrone.Core.Repository;
using NzbDrone.Core.Repository.Quality; using NzbDrone.Core.Repository.Quality;
using SubSonic.Repository; using PetaPoco;
namespace NzbDrone.Core.Providers namespace NzbDrone.Core.Providers
{ {
@ -21,19 +21,19 @@ namespace NzbDrone.Core.Providers
private readonly DiskProvider _diskProvider; private readonly DiskProvider _diskProvider;
private readonly EpisodeProvider _episodeProvider; private readonly EpisodeProvider _episodeProvider;
private readonly SeriesProvider _seriesProvider; private readonly SeriesProvider _seriesProvider;
private readonly IRepository _repository;
private readonly ConfigProvider _configProvider; private readonly ConfigProvider _configProvider;
private readonly IDatabase _database;
[Inject] [Inject]
public MediaFileProvider(IRepository repository, DiskProvider diskProvider, public MediaFileProvider(DiskProvider diskProvider, EpisodeProvider episodeProvider,
EpisodeProvider episodeProvider, SeriesProvider seriesProvider, SeriesProvider seriesProvider, ConfigProvider configProvider,
ConfigProvider configProvider) IDatabase database)
{ {
_repository = repository;
_diskProvider = diskProvider; _diskProvider = diskProvider;
_episodeProvider = episodeProvider; _episodeProvider = episodeProvider;
_seriesProvider = seriesProvider; _seriesProvider = seriesProvider;
_configProvider = configProvider; _configProvider = configProvider;
_database = database;
} }
public MediaFileProvider() { } public MediaFileProvider() { }
@ -76,7 +76,7 @@ namespace NzbDrone.Core.Providers
} }
//Check to see if file already exists in the database //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); var parseResult = Parser.ParseEpisodeInfo(filePath);
@ -132,7 +132,7 @@ namespace NzbDrone.Core.Providers
episodeFile.Quality = parseResult.Quality.QualityType; episodeFile.Quality = parseResult.Quality.QualityType;
episodeFile.Proper = parseResult.Quality.Proper; episodeFile.Proper = parseResult.Quality.Proper;
episodeFile.SeasonNumber = parseResult.SeasonNumber; 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 //This is for logging + updating the episodes that are linked to this EpisodeFile
string episodeList = String.Empty; string episodeList = String.Empty;
@ -179,29 +179,29 @@ namespace NzbDrone.Core.Providers
} }
//Delete it from the DB //Delete it from the DB
_repository.Delete<EpisodeFile>(episodeFile.EpisodeFileId); _database.Delete<EpisodeFile>(episodeFile.EpisodeFileId);
} }
} }
} }
public virtual void Update(EpisodeFile episodeFile) public virtual void Update(EpisodeFile episodeFile)
{ {
_repository.Update(episodeFile); _database.Update(episodeFile);
} }
public virtual EpisodeFile GetEpisodeFile(int episodeFileId) public virtual EpisodeFile GetEpisodeFile(int episodeFileId)
{ {
return _repository.Single<EpisodeFile>(episodeFileId); return _database.Single<EpisodeFile>(episodeFileId);
} }
public virtual List<EpisodeFile> GetEpisodeFiles() 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) public virtual Tuple<int, int> GetEpisodeFilesCount(int seriesId)

View File

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

View File

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