Sonarr/NzbDrone.Core.Test/MediaFileTests/CleanUpDatabaseFixture.cs

133 lines
4.2 KiB
C#
Raw Normal View History

2013-02-24 03:54:50 +00:00
using System;
using System.Linq;
using FizzWare.NBuilder;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.Configuration;
2013-03-01 07:03:41 +00:00
using NzbDrone.Core.MediaFiles;
2013-02-27 03:19:22 +00:00
using NzbDrone.Core.Qualities;
2013-02-24 03:54:50 +00:00
using NzbDrone.Core.Tv;
using NzbDrone.Core.Model;
using NzbDrone.Core.Test.Framework;
2013-03-01 07:03:41 +00:00
namespace NzbDrone.Core.Test.MediaFileTests
2013-02-24 03:54:50 +00:00
{
[TestFixture]
public class CleanUpDatabaseFixture : SqlCeTest
{
[SetUp]
public void Setup()
{
WithRealDb();
}
private void WithAutoIgnore(bool autoIgnore)
{
2013-02-25 00:14:01 +00:00
Mocker.GetMock<IConfigService>()
2013-02-24 03:54:50 +00:00
.SetupGet(c => c.AutoIgnorePreviouslyDownloadedEpisodes).Returns(autoIgnore);
}
[Test]
public void CleanUpDatabse_should_detach_none_existing_file_from_episodes_with_auto_ignore()
{
WithAutoIgnore(true);
var episodes = Builder<Episode>.CreateListOfSize(3)
.All().With(c => c.GrabDate = DateTime.Now)
.And(c => c.Ignored = false)
.And(c => c.PostDownloadStatus = PostDownloadStatusType.NoError)
.Build();
Db.InsertMany(episodes);
//Act
var result = Db.Fetch<Episode>();
//Assert
result.Should().HaveSameCount(episodes);
result.Should().OnlyContain(e => e.EpisodeFileId == 0);
result.Should().OnlyContain(e => e.PostDownloadStatus == PostDownloadStatusType.Unknown);
result.Should().OnlyContain(e => e.Ignored);
result.Should().OnlyContain(e => e.GrabDate == null);
}
[Test]
public void CleanUpDatabse_should_detach_none_existing_file_from_episodes_with_no_auto_ignore()
{
WithAutoIgnore(false);
var episodes = Builder<Episode>.CreateListOfSize(3)
.All().With(c => c.GrabDate = DateTime.Now)
.And(c => c.PostDownloadStatus = PostDownloadStatusType.NoError)
.TheFirst(2).With(c => c.Ignored = true)
.TheLast(1).With(c => c.Ignored = false)
.Build();
Db.InsertMany(episodes);
//Act
var result = Db.Fetch<Episode>();
//Assert
result.Should().HaveSameCount(episodes);
result.Should().OnlyContain(e => e.EpisodeFileId == 0);
result.Should().OnlyContain(e => e.PostDownloadStatus == PostDownloadStatusType.Unknown);
result.Should().OnlyContain(e => e.GrabDate == null);
result.Should().Contain(c => c.Ignored == true);
result.Should().Contain(c => c.Ignored == false);
}
[Test]
public void CleanUpDatabse_should_not_change_episodes_with_no_file_id()
{
//Setup
var episodes = Builder<Episode>.CreateListOfSize(3)
.All().With(c => c.GrabDate = DateTime.Now)
.And(c => c.Ignored = false)
.And(c => c.PostDownloadStatus = PostDownloadStatusType.NoError)
.Build();
Db.InsertMany(episodes);
//Act
var result = Db.Fetch<Episode>();
//Assert
result.Should().HaveSameCount(episodes);
result.Should().OnlyContain(e => e.EpisodeFileId == 0);
result.Should().NotContain(e => e.PostDownloadStatus == PostDownloadStatusType.Unknown);
result.Should().NotContain(e => e.Ignored);
result.Should().NotContain(e => e.GrabDate == null);
}
[Test]
public void DeleteOrphanedEpisodeFiles()
{
//Setup
var episodeFiles = Builder<EpisodeFile>
.CreateListOfSize(10)
.All()
2013-02-27 03:19:22 +00:00
.With(e => e.Quality = Quality.DVD)
2013-02-24 03:54:50 +00:00
.Build();
var episodes = Builder<Episode>.CreateListOfSize(5).Build();
Db.InsertMany(episodes);
Db.InsertMany(episodeFiles);
//Act
var result = Db.Fetch<EpisodeFile>();
//Assert
result.Should().HaveCount(5);
2013-03-01 07:03:41 +00:00
result.Should().OnlyContain(e => e.Id > 0);
2013-02-24 03:54:50 +00:00
}
}
}