From 975d31178b8da9c68478ff360b1b5c44c582e9cd Mon Sep 17 00:00:00 2001 From: Qstick Date: Wed, 8 Apr 2020 22:13:32 -0400 Subject: [PATCH] Fixed: History Repo GetById not always ordered by Date --- .../HistoryTests/HistoryRepositoryFixture.cs | 55 +++++++++++++++++++ .../History/HistoryRepository.cs | 4 +- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/NzbDrone.Core.Test/HistoryTests/HistoryRepositoryFixture.cs b/src/NzbDrone.Core.Test/HistoryTests/HistoryRepositoryFixture.cs index 916b83a80..8686d342f 100644 --- a/src/NzbDrone.Core.Test/HistoryTests/HistoryRepositoryFixture.cs +++ b/src/NzbDrone.Core.Test/HistoryTests/HistoryRepositoryFixture.cs @@ -1,4 +1,6 @@ +using System; using System.Collections.Generic; +using System.Linq; using FizzWare.NBuilder; using FluentAssertions; using NUnit.Framework; @@ -57,5 +59,58 @@ namespace NzbDrone.Core.Test.HistoryTests downloadHistory.Should().HaveCount(1); } + + [Test] + public void should_get_movie_history() + { + var historyMovie1 = Builder.CreateNew() + .With(c => c.Quality = new QualityModel(Quality.Bluray1080p)) + .With(c => c.Languages = new List { Language.English }) + .With(c => c.MovieId = 12) + .With(c => c.EventType = HistoryEventType.Grabbed) + .BuildNew(); + + var historyMovie2 = Builder.CreateNew() + .With(c => c.Quality = new QualityModel(Quality.Bluray1080p)) + .With(c => c.Languages = new List { Language.English }) + .With(c => c.MovieId = 13) + .With(c => c.EventType = HistoryEventType.Grabbed) + .BuildNew(); + + Subject.Insert(historyMovie1); + Subject.Insert(historyMovie2); + + var movieHistory = Subject.GetByMovieId(12, null); + + movieHistory.Should().HaveCount(1); + } + + [Test] + public void should_sort_movie_history_by_date() + { + var historyFirst = Builder.CreateNew() + .With(c => c.Quality = new QualityModel(Quality.Bluray1080p)) + .With(c => c.Languages = new List { Language.English }) + .With(c => c.MovieId = 12) + .With(c => c.EventType = HistoryEventType.MovieFileRenamed) + .With(c => c.Date = DateTime.UtcNow) + .BuildNew(); + + var historySecond = Builder.CreateNew() + .With(c => c.Quality = new QualityModel(Quality.Bluray1080p)) + .With(c => c.Languages = new List { Language.English }) + .With(c => c.MovieId = 12) + .With(c => c.EventType = HistoryEventType.Grabbed) + .With(c => c.Date = DateTime.UtcNow.AddMinutes(10)) + .BuildNew(); + + Subject.Insert(historyFirst); + Subject.Insert(historySecond); + + var movieHistory = Subject.GetByMovieId(12, null); + + movieHistory.Should().HaveCount(2); + movieHistory.First().EventType.Should().Be(HistoryEventType.Grabbed); + } } } diff --git a/src/NzbDrone.Core/History/HistoryRepository.cs b/src/NzbDrone.Core/History/HistoryRepository.cs index 4bef354ac..d547d4e49 100644 --- a/src/NzbDrone.Core/History/HistoryRepository.cs +++ b/src/NzbDrone.Core/History/HistoryRepository.cs @@ -66,9 +66,7 @@ namespace NzbDrone.Core.History query = query.Where(h => h.EventType == eventType).ToList(); } - query.OrderByDescending(h => h.Date); - - return query; + return query.OrderByDescending(h => h.Date).ToList(); } public void DeleteForMovie(int movieId)