Merge branch 'markus101'

pull/4/head
kay.one 14 years ago
commit 852b1e9bb5

@ -41,7 +41,7 @@ namespace NzbDrone.Core.Test
var episode = mocker.Resolve<EpisodeProvider>().GetEpisode(1); var episode = mocker.Resolve<EpisodeProvider>().GetEpisode(1);
//Assert //Assert
episode.ShouldHave().AllPropertiesBut(e => e.Series).EqualTo(fakeEpisodes.First()); episode.ShouldHave().AllPropertiesBut(e => e.Series, e => e.EpisodeFile).EqualTo(fakeEpisodes.First());
episode.Series.ShouldHave().AllProperties().EqualTo(fakeSeries); episode.Series.ShouldHave().AllProperties().EqualTo(fakeSeries);
} }
@ -90,6 +90,35 @@ namespace NzbDrone.Core.Test
episode.Should().BeNull(); episode.Should().BeNull();
} }
[Test]
public void GetEpisode_with_EpisodeFile()
{
var mocker = new AutoMoqer();
var db = MockLib.GetEmptyDatabase();
mocker.SetConstant(db);
var fakeSeries = Builder<Series>.CreateNew().Build();
var fakeFile = Builder<EpisodeFile>.CreateNew().With(f => f.EpisodeFileId).Build();
var fakeEpisodes = Builder<Episode>.CreateListOfSize(5)
.WhereAll().Have(e => e.SeriesId = 1).WhereTheFirst(1).Have(e => e.EpisodeFileId = 1).Have(e => e.EpisodeFile = fakeFile).Build();
db.InsertMany(fakeEpisodes);
db.Insert(fakeFile);
mocker.GetMock<SeriesProvider>()
.Setup(p => p.GetSeries(1))
.Returns(fakeSeries);
//Act
var episode = mocker.Resolve<EpisodeProvider>().GetEpisode(1);
//Assert
episode.ShouldHave().AllPropertiesBut(e => e.Series, e => e.EpisodeFile).EqualTo(fakeEpisodes.First());
episode.Series.ShouldHave().AllProperties().EqualTo(fakeSeries);
episode.EpisodeFile.Should().NotBeNull();
}
[Test] [Test]
[ExpectedException(typeof(InvalidOperationException), ExpectedMessage = "Sequence contains no elements")] [ExpectedException(typeof(InvalidOperationException), ExpectedMessage = "Sequence contains no elements")]
public void GetEpisodes_invalid_series() public void GetEpisodes_invalid_series()
@ -124,7 +153,6 @@ namespace NzbDrone.Core.Test
result.Should().HaveCount(0); result.Should().HaveCount(0);
} }
[Test] [Test]
public void AttachSeries_list_success() public void AttachSeries_list_success()
{ {
@ -195,7 +223,6 @@ namespace NzbDrone.Core.Test
var returnedEpisode = mocker.Resolve<EpisodeProvider>().AttachSeries(fakeEpisodes); var returnedEpisode = mocker.Resolve<EpisodeProvider>().AttachSeries(fakeEpisodes);
} }
[Test] [Test]
public void GetEpisodesBySeason_success() public void GetEpisodesBySeason_success()
{ {
@ -255,7 +282,6 @@ namespace NzbDrone.Core.Test
mocker.VerifyAllMocks(); mocker.VerifyAllMocks();
} }
[Test] [Test]
public void new_episodes_only_calls_Insert() public void new_episodes_only_calls_Insert()
{ {
@ -276,7 +302,7 @@ namespace NzbDrone.Core.Test
.Returns(tvdbSeries); .Returns(tvdbSeries);
mocker.GetMock<IDatabase>() mocker.GetMock<IDatabase>()
.Setup(d => d.Fetch<Episode>(It.IsAny<String>(), It.IsAny<Object[]>())) .Setup(d => d.Fetch<Episode, EpisodeFile>(It.IsAny<String>(), It.IsAny<Object[]>()))
.Returns(currentEpisodes); .Returns(currentEpisodes);
@ -290,7 +316,6 @@ namespace NzbDrone.Core.Test
mocker.VerifyAllMocks(); mocker.VerifyAllMocks();
} }
[Test] [Test]
public void existing_episodes_only_calls_Update() public void existing_episodes_only_calls_Update()
{ {
@ -314,7 +339,7 @@ namespace NzbDrone.Core.Test
.Returns(tvdbSeries); .Returns(tvdbSeries);
mocker.GetMock<IDatabase>() mocker.GetMock<IDatabase>()
.Setup(d => d.Fetch<Episode>(It.IsAny<String>(), It.IsAny<Object[]>())) .Setup(d => d.Fetch<Episode, EpisodeFile>(It.IsAny<String>(), It.IsAny<Object[]>()))
.Returns(currentEpisodes); .Returns(currentEpisodes);
//Act //Act
@ -326,7 +351,6 @@ namespace NzbDrone.Core.Test
mocker.VerifyAllMocks(); mocker.VerifyAllMocks();
} }
[Test] [Test]
public void should_try_to_get_existing_episode_using_tvdbid_first() public void should_try_to_get_existing_episode_using_tvdbid_first()
{ {
@ -345,7 +369,7 @@ namespace NzbDrone.Core.Test
var mocker = new AutoMoqer(); var mocker = new AutoMoqer();
mocker.GetMock<IDatabase>() mocker.GetMock<IDatabase>()
.Setup(d => d.Fetch<Episode>(It.IsAny<String>(), It.IsAny<Object[]>())) .Setup(d => d.Fetch<Episode, EpisodeFile>(It.IsAny<String>(), It.IsAny<Object[]>()))
.Returns(fakeEpisodeList); .Returns(fakeEpisodeList);
mocker.GetMock<TvDbProvider>() mocker.GetMock<TvDbProvider>()
@ -393,7 +417,7 @@ namespace NzbDrone.Core.Test
.Returns(tvdbSeries); .Returns(tvdbSeries);
mocker.GetMock<IDatabase>() mocker.GetMock<IDatabase>()
.Setup(d => d.Fetch<Episode>(It.IsAny<String>(), It.IsAny<Object[]>())) .Setup(d => d.Fetch<Episode, EpisodeFile>(It.IsAny<String>(), It.IsAny<Object[]>()))
.Returns(new List<Episode> { localEpisode }); .Returns(new List<Episode> { localEpisode });
//Act //Act
@ -404,7 +428,6 @@ namespace NzbDrone.Core.Test
mocker.GetMock<IDatabase>().Verify(c => c.Update(localEpisode), Times.Once()); mocker.GetMock<IDatabase>().Verify(c => c.Update(localEpisode), Times.Once());
} }
[Test] [Test]
public void existing_episodes_keep_their_episodeId_file_id() public void existing_episodes_keep_their_episodeId_file_id()
{ {
@ -430,7 +453,7 @@ namespace NzbDrone.Core.Test
var updatedEpisodes = new List<Episode>(); var updatedEpisodes = new List<Episode>();
mocker.GetMock<IDatabase>() mocker.GetMock<IDatabase>()
.Setup(d => d.Fetch<Episode>(It.IsAny<String>(), It.IsAny<Object[]>())) .Setup(d => d.Fetch<Episode, EpisodeFile>(It.IsAny<String>(), It.IsAny<Object[]>()))
.Returns(currentEpisodes); .Returns(currentEpisodes);
mocker.GetMock<IDatabase>() mocker.GetMock<IDatabase>()
@ -448,7 +471,6 @@ namespace NzbDrone.Core.Test
updatedEpisodes.Should().OnlyContain(c => c.Ignored == true); updatedEpisodes.Should().OnlyContain(c => c.Ignored == true);
} }
[Test] [Test]
public void IsSeasonIgnored_should_return_true_if_all_episodes_ignored() public void IsSeasonIgnored_should_return_true_if_all_episodes_ignored()
{ {
@ -556,6 +578,112 @@ namespace NzbDrone.Core.Test
episodes.Should().NotBeEmpty(); episodes.Should().NotBeEmpty();
} }
[Test]
public void GetEpisode_by_Season_Episode_with_EpisodeFile()
{
var mocker = new AutoMoqer();
var db = MockLib.GetEmptyDatabase();
mocker.SetConstant(db);
var fakeSeries = Builder<Series>.CreateNew().Build();
var fakeFile = Builder<EpisodeFile>.CreateNew().With(f => f.EpisodeFileId).Build();
var fakeEpisodes = Builder<Episode>.CreateListOfSize(5)
.WhereAll().Have(e => e.SeriesId = 1).WhereTheFirst(1).Have(e => e.EpisodeFileId = 1).Have(e => e.EpisodeFile = fakeFile).Build();
db.InsertMany(fakeEpisodes);
db.Insert(fakeFile);
mocker.GetMock<SeriesProvider>()
.Setup(p => p.GetSeries(1))
.Returns(fakeSeries);
//Act
var episode = mocker.Resolve<EpisodeProvider>().GetEpisode(1, 1, 1);
//Assert
episode.ShouldHave().AllPropertiesBut(e => e.Series, e => e.EpisodeFile).EqualTo(fakeEpisodes.First());
episode.Series.ShouldHave().AllProperties().EqualTo(fakeSeries);
episode.EpisodeFile.Should().NotBeNull();
}
[Test]
public void GetEpisode_by_Season_Episode_without_EpisodeFile()
{
var mocker = new AutoMoqer();
var db = MockLib.GetEmptyDatabase();
mocker.SetConstant(db);
var fakeSeries = Builder<Series>.CreateNew().Build();
var fakeEpisodes = Builder<Episode>.CreateListOfSize(5)
.WhereAll().Have(e => e.SeriesId = 1).WhereTheFirst(1).Have(e => e.EpisodeFileId = 0).Build();
db.InsertMany(fakeEpisodes);
mocker.GetMock<SeriesProvider>()
.Setup(p => p.GetSeries(1))
.Returns(fakeSeries);
//Act
var episode = mocker.Resolve<EpisodeProvider>().GetEpisode(1, 1, 1);
//Assert
episode.ShouldHave().AllPropertiesBut(e => e.Series).EqualTo(fakeEpisodes.First());
episode.Series.ShouldHave().AllProperties().EqualTo(fakeSeries);
episode.EpisodeFile.Should().BeNull();
}
[Test]
public void GetEpisode_by_AirDate_with_EpisodeFile()
{
var mocker = new AutoMoqer();
var db = MockLib.GetEmptyDatabase();
mocker.SetConstant(db);
var fakeSeries = Builder<Series>.CreateNew().Build();
var fakeFile = Builder<EpisodeFile>.CreateNew().With(f => f.EpisodeFileId).Build();
var fakeEpisodes = Builder<Episode>.CreateListOfSize(5)
.WhereAll().Have(e => e.SeriesId = 1).WhereTheFirst(1).Have(e => e.EpisodeFileId = 1).Have(e => e.EpisodeFile = fakeFile).Build();
db.InsertMany(fakeEpisodes);
db.Insert(fakeFile);
mocker.GetMock<SeriesProvider>()
.Setup(p => p.GetSeries(1))
.Returns(fakeSeries);
//Act
var episode = mocker.Resolve<EpisodeProvider>().GetEpisode(1, fakeEpisodes[0].AirDate);
//Assert
episode.ShouldHave().AllPropertiesBut(e => e.Series, e => e.EpisodeFile).EqualTo(fakeEpisodes.First());
episode.Series.ShouldHave().AllProperties().EqualTo(fakeSeries);
episode.EpisodeFile.Should().NotBeNull();
}
[Test]
public void GetEpisode_by_AirDate_without_EpisodeFile()
{
var mocker = new AutoMoqer();
var db = MockLib.GetEmptyDatabase();
mocker.SetConstant(db);
var fakeSeries = Builder<Series>.CreateNew().Build();
var fakeEpisodes = Builder<Episode>.CreateListOfSize(5)
.WhereAll().Have(e => e.SeriesId = 1).WhereTheFirst(1).Have(e => e.EpisodeFileId = 0).Build();
db.InsertMany(fakeEpisodes);
mocker.GetMock<SeriesProvider>()
.Setup(p => p.GetSeries(1))
.Returns(fakeSeries);
//Act
var episode = mocker.Resolve<EpisodeProvider>().GetEpisode(1, fakeEpisodes[0].AirDate);
//Assert
episode.ShouldHave().AllPropertiesBut(e => e.Series).EqualTo(fakeEpisodes.First());
episode.Series.ShouldHave().AllProperties().EqualTo(fakeSeries);
episode.EpisodeFile.Should().BeNull();
}
} }
} }

@ -29,7 +29,6 @@ namespace NzbDrone.Core.Test
db.InsertMany(historyItem); db.InsertMany(historyItem);
//Act //Act
var result = mocker.Resolve<HistoryProvider>().AllItems(); var result = mocker.Resolve<HistoryProvider>().AllItems();
@ -37,6 +36,39 @@ namespace NzbDrone.Core.Test
result.Should().HaveSameCount(historyItem); result.Should().HaveSameCount(historyItem);
} }
[Test]
public void AllItemsWithRelationships()
{
//Setup
var seriesOne = Builder<Series>.CreateNew().With(s => s.SeriesId = 12345).Build();
var seriesTwo = Builder<Series>.CreateNew().With(s => s.SeriesId = 54321).Build();
var episodes = Builder<Episode>.CreateListOfSize(10).Build();
var historyItems = Builder<History>.CreateListOfSize(10).WhereTheFirst(5).Have(h => h.SeriesId = seriesOne.SeriesId).WhereTheLast(5).Have(h => h.SeriesId = seriesTwo.SeriesId).Build();
var mocker = new AutoMoqer();
var db = MockLib.GetEmptyDatabase();
mocker.SetConstant(db);
db.InsertMany(historyItems);
db.InsertMany(episodes);
db.Insert(seriesOne);
db.Insert(seriesTwo);
//Act
var result = mocker.Resolve<HistoryProvider>().AllItemsWithRelationships();
//Assert
result.Should().HaveSameCount(historyItems);
foreach (var history in result)
{
Assert.NotNull(history.Episode);
Assert.That(!String.IsNullOrEmpty(history.SeriesTitle));
}
}
[Test] [Test]
public void PurgeItem() public void PurgeItem()
{ {
@ -157,7 +189,5 @@ namespace NzbDrone.Core.Test
Assert.AreEqual(history.Quality, storedHistory.First().Quality); Assert.AreEqual(history.Quality, storedHistory.First().Quality);
Assert.AreEqual(history.IsProper, storedHistory.First().IsProper); Assert.AreEqual(history.IsProper, storedHistory.First().IsProper);
} }
} }
} }

@ -35,27 +35,73 @@ namespace NzbDrone.Core.Providers
public virtual Episode GetEpisode(long id) public virtual Episode GetEpisode(long id)
{ {
return AttachSeries(_database.Single<Episode>(id)); var episode = AttachSeries(_database.Fetch<Episode, EpisodeFile>(@"SELECT * FROM Episodes
LEFT JOIN EpisodeFiles ON Episodes.EpisodeFileId = EpisodeFiles.EpisodeFileId
WHERE EpisodeId = @0", id).Single());
if (episode.EpisodeFileId == 0)
episode.EpisodeFile = null;
return episode;
} }
public virtual Episode GetEpisode(int seriesId, int seasonNumber, int episodeNumber) public virtual Episode GetEpisode(int seriesId, int seasonNumber, int episodeNumber)
{ {
return AttachSeries(_database.SingleOrDefault<Episode>("WHERE SeriesId = @0 AND SeasonNumber = @1 AND EpisodeNumber = @2", seriesId, seasonNumber, episodeNumber)); var episode = AttachSeries(_database.Fetch<Episode, EpisodeFile>(@"SELECT * FROM Episodes
LEFT JOIN EpisodeFiles ON Episodes.EpisodeFileId = EpisodeFiles.EpisodeFileId
WHERE Episodes.SeriesId = @0 AND Episodes.SeasonNumber = @1 AND Episodes.EpisodeNumber = @2", seriesId, seasonNumber, episodeNumber).SingleOrDefault());
if (episode == null)
return null;
if (episode.EpisodeFileId == 0)
episode.EpisodeFile = null;
return episode;
} }
public virtual Episode GetEpisode(int seriesId, DateTime date) public virtual Episode GetEpisode(int seriesId, DateTime date)
{ {
return AttachSeries(_database.SingleOrDefault<Episode>("WHERE SeriesId = @0 AND AirDate = @1", seriesId, date.Date)); var episode = AttachSeries(_database.Fetch<Episode, EpisodeFile>(@"SELECT * FROM Episodes
LEFT JOIN EpisodeFiles ON Episodes.EpisodeFileId = EpisodeFiles.EpisodeFileId
WHERE Episodes.SeriesId = @0 AND AirDate = @1", seriesId, date.Date)).SingleOrDefault();
if (episode == null)
return null;
if (episode.EpisodeFileId == 0)
episode.EpisodeFile = null;
return episode;
} }
public virtual IList<Episode> GetEpisodeBySeries(long seriesId) public virtual IList<Episode> GetEpisodeBySeries(long seriesId)
{ {
return AttachSeries(_database.Fetch<Episode>("WHERE SeriesId = @0", seriesId)); var episodes = AttachSeries(_database.Fetch<Episode, EpisodeFile>(@"SELECT * FROM Episodes
LEFT JOIN EpisodeFiles ON Episodes.EpisodeFileId = EpisodeFiles.EpisodeFileId
WHERE Episodes.SeriesId = @0", seriesId));
foreach (var episode in episodes)
{
if (episode.EpisodeFileId == 0)
episode.EpisodeFile = null;
}
return episodes;
} }
public virtual IList<Episode> GetEpisodesBySeason(long seriesId, int seasonNumber) public virtual IList<Episode> GetEpisodesBySeason(long seriesId, int seasonNumber)
{ {
return AttachSeries(_database.Fetch<Episode>("WHERE SeriesId = @0 AND SeasonNumber = @1", seriesId, seasonNumber)); var episodes = AttachSeries(_database.Fetch<Episode, EpisodeFile>(@"SELECT * FROM Episodes
LEFT JOIN EpisodeFiles ON Episodes.EpisodeFileId = EpisodeFiles.EpisodeFileId
WHERE Episodes.SeriesId = @0 AND Episodes.SeasonNumber = @1", seriesId, seasonNumber));
foreach (var episode in episodes)
{
if (episode.EpisodeFileId == 0)
episode.EpisodeFile = null;
}
return episodes;
} }
public virtual List<Episode> GetEpisodes(EpisodeParseResult parseResult) public virtual List<Episode> GetEpisodes(EpisodeParseResult parseResult)
@ -78,6 +124,12 @@ namespace NzbDrone.Core.Providers
episodes.Add(episode); episodes.Add(episode);
} }
foreach (var episode in episodes)
{
if (episode.EpisodeFileId == 0)
episode.EpisodeFile = null;
}
return episodes; return episodes;
} }

@ -25,9 +25,18 @@ namespace NzbDrone.Core.Providers
{ {
} }
public virtual IEnumerable<History> AllItems() public virtual List<History> AllItems()
{ {
return _database.Query<History>(""); return _database.Fetch<History>("");
}
public virtual List<History> AllItemsWithRelationships()
{
return _database.Fetch<History, Episode>(@"
SELECT History.*, Series.Title as SeriesTitle, Episodes.* FROM History
INNER JOIN Series ON History.SeriesId = Series.SeriesId
INNER JOIN Episodes ON History.EpisodeId = Episodes.EpisodeId
");
} }
public virtual void Purge() public virtual void Purge()

@ -19,7 +19,6 @@ namespace NzbDrone.Core.Repository
public string Title { get; set; } public string Title { get; set; }
public DateTime AirDate { get; set; } public DateTime AirDate { get; set; }
public string Overview { get; set; } public string Overview { get; set; }
public Boolean Ignored { get; set; } public Boolean Ignored { get; set; }
@ -33,7 +32,6 @@ namespace NzbDrone.Core.Repository
/// </remarks> /// </remarks>
public DateTime? GrabDate { get; set; } public DateTime? GrabDate { get; set; }
[Ignore] [Ignore]
public EpisodeStatusType Status public EpisodeStatusType Status
{ {
@ -58,15 +56,12 @@ namespace NzbDrone.Core.Repository
} }
} }
[Ignore] [Ignore]
public Series Series { get; set; } public Series Series { get; set; }
[ResultColumn]
[Ignore]
public EpisodeFile EpisodeFile { get; set; } public EpisodeFile EpisodeFile { get; set; }
public override string ToString() public override string ToString()
{ {
string seriesTitle = Series == null ? "[NULL]" : Series.Title; string seriesTitle = Series == null ? "[NULL]" : Series.Title;

@ -16,5 +16,11 @@ namespace NzbDrone.Core.Repository
public DateTime Date { get; set; } public DateTime Date { get; set; }
public bool IsProper { get; set; } public bool IsProper { get; set; }
public string Indexer { get; set; } public string Indexer { get; set; }
[ResultColumn]
public Episode Episode { get; set; }
[ResultColumn]
public string SeriesTitle { get; set; }
} }
} }

@ -46,30 +46,20 @@ namespace NzbDrone.Web.Controllers
[GridAction] [GridAction]
public ActionResult _AjaxBinding() public ActionResult _AjaxBinding()
{ {
var historyDb = _historyProvider.AllItems(); var history = _historyProvider.AllItemsWithRelationships().Select(h => new HistoryModel
var history = new List<HistoryModel>();
foreach (var item in historyDb)
{ {
var episode = _episodeProvider.GetEpisode(item.EpisodeId); HistoryId = h.HistoryId,
var series = _seriesProvider.GetSeries(item.SeriesId); SeasonNumber = h.Episode.SeasonNumber,
EpisodeNumber = h.Episode.EpisodeNumber,
history.Add(new HistoryModel EpisodeTitle = h.Episode.Title,
{ EpisodeOverview = h.Episode.Overview,
HistoryId = item.HistoryId, SeriesTitle = h.SeriesTitle,
SeasonNumber = episode.SeasonNumber, NzbTitle = h.NzbTitle,
EpisodeNumber = episode.EpisodeNumber, Quality = h.Quality.ToString(),
EpisodeTitle = episode.Title, IsProper = h.IsProper,
EpisodeOverview = episode.Overview, Date = h.Date,
SeriesTitle = series.Title, Indexer = h.Indexer
NzbTitle = item.NzbTitle,
Quality = item.Quality.ToString(),
IsProper = item.IsProper,
Date = item.Date,
Indexer = item.Indexer
}); });
}
return View(new GridModel(history)); return View(new GridModel(history));
} }

Loading…
Cancel
Save