From 3ef17273fd9875e82ec8571a35f9766c9d4c1406 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Thu, 16 Jun 2011 23:59:13 -0700 Subject: [PATCH] HistoryProvider now uses PetaPoco --- NzbDrone.Core.Test/HistoryProviderTest.cs | 116 +++++++++++------- NzbDrone.Core/Providers/HistoryProvider.cs | 31 ++--- NzbDrone.Core/Repository/History.cs | 12 +- NzbDrone.Web/Controllers/HistoryController.cs | 13 +- 4 files changed, 96 insertions(+), 76 deletions(-) diff --git a/NzbDrone.Core.Test/HistoryProviderTest.cs b/NzbDrone.Core.Test/HistoryProviderTest.cs index 18c6df726..2ea187452 100644 --- a/NzbDrone.Core.Test/HistoryProviderTest.cs +++ b/NzbDrone.Core.Test/HistoryProviderTest.cs @@ -23,38 +23,65 @@ namespace NzbDrone.Core.Test public void AllItems() { //Setup - var episode = new Episode - { - AirDate = DateTime.Today.AddDays(-1), - EpisodeId = 1234, - EpisodeNumber = 5, - Overview = "This is an Overview", - SeasonNumber = 1, - SeriesId = 5656 - }; + var historyItem = Builder.CreateListOfSize(10).Build(); - var list = new List - { - new History - { - HistoryId = new int(), - Date = DateTime.Now, - IsProper = false, - Quality = QualityTypes.SDTV, - EpisodeId = episode.EpisodeId - } - }; + var mocker = new AutoMoqer(); + var db = MockLib.GetEmptyDatabase(); + mocker.SetConstant(db); - var repo = new Mock(); - repo.Setup(r => r.All()).Returns(list.AsQueryable()); + db.InsertMany(historyItem); - var target = new HistoryProvider(repo.Object); //Act - var result = target.AllItems(); + var result = mocker.Resolve().AllItems(); //Assert - Assert.AreEqual(result.Count(), 1); + result.Should().HaveSameCount(historyItem); + } + + [Test] + public void PurgeItem() + { + //Setup + var historyItem = Builder.CreateListOfSize(10).Build(); + + var mocker = new AutoMoqer(); + var db = MockLib.GetEmptyDatabase(); + mocker.SetConstant(db); + + db.InsertMany(historyItem); + + + //Act + db.Fetch().Should().HaveCount(10); + mocker.Resolve().Purge(); + + //Assert + db.Fetch().Should().HaveCount(0); + } + + [Test] + public void Trim_Items() + { + //Setup + var historyItem = Builder.CreateListOfSize(20) + .WhereTheFirst(10).Have(c => c.Date = DateTime.Now) + .AndTheNext(10).Have(c => c.Date = DateTime.Now.AddDays(-31)) + .Build(); + + var mocker = new AutoMoqer(); + var db = MockLib.GetEmptyDatabase(); + mocker.SetConstant(db); + + db.InsertMany(historyItem); + + + //Act + db.Fetch().Should().HaveCount(20); + mocker.Resolve().Trim(); + + //Assert + db.Fetch().Should().HaveCount(10); } @@ -63,7 +90,7 @@ namespace NzbDrone.Core.Test { var mocker = new AutoMoqer(MockBehavior.Strict); - mocker.SetConstant(MockLib.GetEmptyRepository()); + mocker.SetConstant(MockLib.GetEmptyDatabase()); //Act var result = mocker.Resolve().GetBestQualityInHistory(12); @@ -77,12 +104,12 @@ namespace NzbDrone.Core.Test { var mocker = new AutoMoqer(MockBehavior.Strict); - - var repo = MockLib.GetEmptyRepository(); + var db = MockLib.GetEmptyDatabase(); var history = Builder.CreateNew() .With(h => h.Quality = QualityTypes.Bluray720p).Build(); - repo.Add(history); - mocker.SetConstant(repo); + + db.Insert(history); + mocker.SetConstant(db); //Act var result = mocker.Resolve().GetBestQualityInHistory(history.EpisodeId); @@ -97,35 +124,40 @@ namespace NzbDrone.Core.Test { //Arange var mocker = new AutoMoqer(); - var repo = MockLib.GetEmptyRepository(); + var db = MockLib.GetEmptyDatabase(); - mocker.SetConstant(repo); + mocker.SetConstant(db); - var episodes = MockLib.GetFakeEpisodes(1); - repo.AddMany(episodes); + var episode = Builder.CreateNew().Build(); - var episode = episodes[5]; + const QualityTypes quality = QualityTypes.HDTV; + const bool proper = true; var history = new History { Date = DateTime.Now, EpisodeId = episode.EpisodeId, + SeriesId = episode.SeriesId, NzbTitle = "my title", - Indexer = "Fake Indexer" + Indexer = "Fake Indexer", + Quality = quality, + IsProper = proper }; //Act mocker.Resolve().Add(history); //Assert - var storedHistory = repo.All(); - var newHistiory = repo.All().First(); + var storedHistory = db.Fetch(); storedHistory.Should().HaveCount(1); - Assert.AreEqual(history.Date, newHistiory.Date); - Assert.AreEqual(history.EpisodeId, newHistiory.EpisodeId); - Assert.AreEqual(history.NzbTitle, newHistiory.NzbTitle); - Assert.AreEqual(history.Indexer, newHistiory.Indexer); + Assert.AreEqual(history.Date, storedHistory.First().Date); + Assert.AreEqual(history.EpisodeId, storedHistory.First().EpisodeId); + Assert.AreEqual(history.SeriesId, storedHistory.First().SeriesId); + Assert.AreEqual(history.NzbTitle, storedHistory.First().NzbTitle); + Assert.AreEqual(history.Indexer, storedHistory.First().Indexer); + Assert.AreEqual(history.Quality, storedHistory.First().Quality); + Assert.AreEqual(history.IsProper, storedHistory.First().IsProper); } diff --git a/NzbDrone.Core/Providers/HistoryProvider.cs b/NzbDrone.Core/Providers/HistoryProvider.cs index 393736d51..e389ded79 100644 --- a/NzbDrone.Core/Providers/HistoryProvider.cs +++ b/NzbDrone.Core/Providers/HistoryProvider.cs @@ -5,47 +5,46 @@ using Ninject; using NLog; using NzbDrone.Core.Model; using NzbDrone.Core.Repository; -using NzbDrone.Core.Repository.Quality; -using SubSonic.Repository; +using PetaPoco; namespace NzbDrone.Core.Providers { public class HistoryProvider { + private readonly IDatabase _database; private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); - private readonly IRepository _repository; + [Inject] - public HistoryProvider(IRepository repository) + public HistoryProvider(IDatabase database) { - _repository = repository; + _database = database; } public HistoryProvider() { } - public virtual IQueryable AllItems() + public virtual IEnumerable AllItems() { - return _repository.All(); + return _database.Query(""); } public virtual void Purge() { - _repository.DeleteMany(AllItems()); + _database.Delete(""); Logger.Info("History has been Purged"); } public virtual void Trim() { - var old = AllItems().Where(h => h.Date < DateTime.Now.AddDays(-30)); - _repository.DeleteMany(old); + _database.Delete("WHERE Date < @0", DateTime.Now.AddDays(-30).Date); Logger.Info("History has been trimmed, items older than 30 days have been removed"); } public virtual void Add(History item) { - _repository.Add(item); + _database.Insert(item); Logger.Debug("Item added to history: {0}", item.NzbTitle); } @@ -55,15 +54,5 @@ namespace NzbDrone.Core.Providers return history.FirstOrDefault(); } - - public virtual void Delete(int historyId) - { - _repository.Delete(historyId); - } - - public virtual void DeleteForEpisode(int episodeId) - { - _repository.DeleteMany(h => h.EpisodeId == episodeId); - } } } \ No newline at end of file diff --git a/NzbDrone.Core/Repository/History.cs b/NzbDrone.Core/Repository/History.cs index 56651c224..9cd65e364 100644 --- a/NzbDrone.Core/Repository/History.cs +++ b/NzbDrone.Core/Repository/History.cs @@ -1,24 +1,22 @@ using System; using NzbDrone.Core.Model; using NzbDrone.Core.Repository.Quality; +using PetaPoco; using SubSonic.SqlGeneration.Schema; namespace NzbDrone.Core.Repository { + [PrimaryKey("HistoryId")] public class History { - [SubSonicPrimaryKey] - public virtual int HistoryId { get; set; } + public int HistoryId { get; set; } - public virtual int EpisodeId { get; set; } + public int EpisodeId { get; set; } + public int SeriesId { get; set; } public string NzbTitle { get; set; } public QualityTypes Quality { get; set; } public DateTime Date { get; set; } public bool IsProper { get; set; } public string Indexer { get; set; } - - [SubSonicToOneRelation(ThisClassContainsJoinKey = true)] - public virtual Episode Episode { get; protected set; } - } } \ No newline at end of file diff --git a/NzbDrone.Web/Controllers/HistoryController.cs b/NzbDrone.Web/Controllers/HistoryController.cs index bfebae68b..31aa45275 100644 --- a/NzbDrone.Web/Controllers/HistoryController.cs +++ b/NzbDrone.Web/Controllers/HistoryController.cs @@ -12,10 +12,12 @@ namespace NzbDrone.Web.Controllers public class HistoryController : Controller { private readonly HistoryProvider _historyProvider; + private readonly EpisodeProvider _episodeProvider; - public HistoryController(HistoryProvider historyProvider) + public HistoryController(HistoryProvider historyProvider, EpisodeProvider episodeProvider) { _historyProvider = historyProvider; + _episodeProvider = episodeProvider; } // @@ -48,12 +50,11 @@ namespace NzbDrone.Web.Controllers var historyDb = _historyProvider.AllItems().ToList(); var history = new List(); - + foreach (var item in historyDb) { - var episode = item.Episode; - var series = episode.Series; - + var episode = _episodeProvider.GetEpisode(item.EpisodeId); + history.Add(new HistoryModel { HistoryId = item.HistoryId, @@ -61,7 +62,7 @@ namespace NzbDrone.Web.Controllers EpisodeNumber = episode.EpisodeNumber, EpisodeTitle = episode.Title, EpisodeOverview = episode.Overview, - SeriesTitle = series.Title, + SeriesTitle = episode.Series.Title, NzbTitle = item.NzbTitle, Quality = item.Quality.ToString(), IsProper = item.IsProper,