@ -4,7 +4,6 @@ using FluentAssertions;
using Moq ;
using NUnit.Framework ;
using NzbDrone.Common.Disk ;
using NzbDrone.Core.Configuration ;
using NzbDrone.Core.DecisionEngine ;
using NzbDrone.Core.Download ;
using NzbDrone.Core.Download.TrackedDownloads ;
@ -18,10 +17,10 @@ using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Tv ;
using NzbDrone.Test.Common ;
namespace NzbDrone.Core.Test.Download
namespace NzbDrone.Core.Test.Download .CompletedDownloadServiceTests
{
[TestFixture]
public class CompletedDownloadService Fixture : CoreTest < CompletedDownloadService >
public class Import Fixture : CoreTest < CompletedDownloadService >
{
private TrackedDownload _trackedDownload ;
private Episode _episode1 ;
@ -44,7 +43,7 @@ namespace NzbDrone.Core.Test.Download
var remoteEpisode = BuildRemoteEpisode ( ) ;
_trackedDownload = Builder < TrackedDownload > . CreateNew ( )
. With ( c = > c . State = TrackedDownloadSta g e. Downloading )
. With ( c = > c . State = TrackedDownloadSta t e. Downloading )
. With ( c = > c . DownloadItem = completed )
. With ( c = > c . RemoteEpisode = remoteEpisode )
. Build ( ) ;
@ -79,25 +78,6 @@ namespace NzbDrone.Core.Test.Download
} ;
}
private void GivenNoGrabbedHistory ( )
{
Mocker . GetMock < IHistoryService > ( )
. Setup ( s = > s . MostRecentForDownloadId ( _trackedDownload . DownloadItem . DownloadId ) )
. Returns ( ( History . History ) null ) ;
}
private void GivenSuccessfulImport ( )
{
Mocker . GetMock < IDownloadedEpisodesImportService > ( )
. Setup ( v = > v . ProcessPath ( It . IsAny < string > ( ) , It . IsAny < ImportMode > ( ) , It . IsAny < Series > ( ) , It . IsAny < DownloadClientItem > ( ) ) )
. Returns ( new List < ImportResult >
{
new ImportResult ( new ImportDecision ( new LocalEpisode ( ) { Path = @"C:\TestPath\Droned.S01E01.mkv" , Episodes = { _episode1 } } ) )
} ) ;
}
private void GivenABadlyNamedDownload ( )
{
_trackedDownload . DownloadItem . DownloadId = "1234" ;
@ -122,94 +102,6 @@ namespace NzbDrone.Core.Test.Download
. Returns ( _trackedDownload . RemoteEpisode . Series ) ;
}
[TestCase(DownloadItemStatus.Downloading)]
[TestCase(DownloadItemStatus.Failed)]
[TestCase(DownloadItemStatus.Queued)]
[TestCase(DownloadItemStatus.Paused)]
[TestCase(DownloadItemStatus.Warning)]
public void should_not_process_if_download_status_isnt_completed ( DownloadItemStatus status )
{
_trackedDownload . DownloadItem . Status = status ;
Subject . Process ( _trackedDownload ) ;
AssertNoAttemptedImport ( ) ;
}
[Test]
public void should_not_process_if_matching_history_is_not_found_and_no_category_specified ( )
{
_trackedDownload . DownloadItem . Category = null ;
GivenNoGrabbedHistory ( ) ;
Subject . Process ( _trackedDownload ) ;
AssertNoAttemptedImport ( ) ;
}
[Test]
public void should_process_if_matching_history_is_not_found_but_category_specified ( )
{
_trackedDownload . DownloadItem . Category = "tv" ;
GivenNoGrabbedHistory ( ) ;
GivenSeriesMatch ( ) ;
GivenSuccessfulImport ( ) ;
Subject . Process ( _trackedDownload ) ;
AssertCompletedDownload ( ) ;
}
[Test]
public void should_not_process_if_output_path_is_empty ( )
{
_trackedDownload . DownloadItem . OutputPath = new OsPath ( ) ;
Subject . Process ( _trackedDownload ) ;
AssertNoAttemptedImport ( ) ;
}
[Test]
public void should_mark_as_imported_if_all_episodes_were_imported ( )
{
Mocker . GetMock < IDownloadedEpisodesImportService > ( )
. Setup ( v = > v . ProcessPath ( It . IsAny < string > ( ) , It . IsAny < ImportMode > ( ) , It . IsAny < Series > ( ) , It . IsAny < DownloadClientItem > ( ) ) )
. Returns ( new List < ImportResult >
{
new ImportResult (
new ImportDecision (
new LocalEpisode { Path = @"C:\TestPath\Droned.S01E01.mkv" , Episodes = { _episode1 } } ) ) ,
new ImportResult (
new ImportDecision (
new LocalEpisode { Path = @"C:\TestPath\Droned.S01E02.mkv" , Episodes = { _episode2 } } ) )
} ) ;
Subject . Process ( _trackedDownload ) ;
AssertCompletedDownload ( ) ;
}
[Test]
public void should_mark_as_imported_if_all_multi_episodes_were_imported ( )
{
_trackedDownload . RemoteEpisode . Episodes . Add ( new Episode { Id = 2 } ) ;
Mocker . GetMock < IDownloadedEpisodesImportService > ( )
. Setup ( v = > v . ProcessPath ( It . IsAny < string > ( ) , It . IsAny < ImportMode > ( ) , It . IsAny < Series > ( ) , It . IsAny < DownloadClientItem > ( ) ) )
. Returns ( new List < ImportResult >
{
new ImportResult (
new ImportDecision (
new LocalEpisode { Path = @"C:\TestPath\Droned.S01E01E02.mkv" , Episodes = { _episode1 , _episode2 } } ) )
} ) ;
Subject . Process ( _trackedDownload ) ;
AssertCompletedDownload ( ) ;
}
[Test]
public void should_not_mark_as_imported_if_all_files_were_rejected ( )
{
@ -226,12 +118,12 @@ namespace NzbDrone.Core.Test.Download
new LocalEpisode { Path = @"C:\TestPath\Droned.S01E02.mkv" , Episodes = { _episode2 } } , new Rejection ( "Rejected!" ) ) , "Test Failure" )
} ) ;
Subject . Process ( _trackedDownload ) ;
Subject . Import ( _trackedDownload ) ;
Mocker . GetMock < IEventAggregator > ( )
. Verify ( v = > v . PublishEvent < DownloadCompletedEvent > ( It . IsAny < DownloadCompletedEvent > ( ) ) , Times . Never ( ) ) ;
AssertNo CompletedDownloa d( ) ;
AssertNo tImporte d( ) ;
}
[Test]
@ -252,9 +144,9 @@ namespace NzbDrone.Core.Test.Download
_trackedDownload . RemoteEpisode . Episodes . Clear ( ) ;
Subject . Process ( _trackedDownload ) ;
Subject . Import ( _trackedDownload ) ;
AssertNo CompletedDownloa d( ) ;
AssertNo tImporte d( ) ;
}
[Test]
@ -269,18 +161,18 @@ namespace NzbDrone.Core.Test.Download
} ) ;
Subject . Process ( _trackedDownload ) ;
Subject . Import ( _trackedDownload ) ;
AssertNo CompletedDownloa d( ) ;
AssertNo tImporte d( ) ;
}
[Test]
public void should_ mark_as_imported_if_all_episodes_were_imported_but_extra_files_were_not ( )
public void should_ not_mark_as_imported_if_some_of_episodes_were_not_imported ( )
{
GivenSeriesMatch ( ) ;
_trackedDownload . RemoteEpisode . Episodes = new List < Episode >
{
new Episode ( ) ,
new Episode ( ) ,
new Episode ( )
} ;
@ -288,154 +180,190 @@ namespace NzbDrone.Core.Test.Download
. Setup ( v = > v . ProcessPath ( It . IsAny < string > ( ) , It . IsAny < ImportMode > ( ) , It . IsAny < Series > ( ) , It . IsAny < DownloadClientItem > ( ) ) )
. Returns ( new List < ImportResult >
{
new ImportResult ( new ImportDecision ( new LocalEpisode { Path = @"C:\TestPath\Droned.S01E01.mkv" , Episodes = { _episode1 } } ) ) ,
new ImportResult ( new ImportDecision ( new LocalEpisode { Path = @"C:\TestPath\Droned.S01E02.mkv" , Episodes = { _episode2 } } ) , "Test Failure" )
new ImportResult ( new ImportDecision ( new LocalEpisode { Path = @"C:\TestPath\Droned.S01E01.mkv" } ) ) ,
new ImportResult ( new ImportDecision ( new LocalEpisode { Path = @"C:\TestPath\Droned.S01E01.mkv" } ) , "Test Failure" ) ,
new ImportResult ( new ImportDecision ( new LocalEpisode { Path = @"C:\TestPath\Droned.S01E01.mkv" } ) , "Test Failure" )
} ) ;
Subject . Process ( _trackedDownload ) ;
Mocker . GetMock < IHistoryService > ( )
. Setup ( s = > s . FindByDownloadId ( It . IsAny < string > ( ) ) )
. Returns ( new List < History . History > ( ) ) ;
AssertCompletedDownload ( ) ;
Subject . Import ( _trackedDownload ) ;
AssertNotImported ( ) ;
}
[Test]
public void should_ mark_as_failed_if_some_of_episodes_were_not_imported ( )
public void should_ not_mark_as_imported_if_some_of_episodes_were_not_imported_including_history ( )
{
_trackedDownload . RemoteEpisode . Episodes = new List < Episode >
{
_episode1 ,
_episode2 ,
_episode3
} ;
{
new Episode ( ) ,
new Episode ( ) ,
new Episode ( )
} ;
Mocker . GetMock < IDownloadedEpisodesImportService > ( )
. Setup ( v = > v . ProcessPath ( It . IsAny < string > ( ) , It . IsAny < ImportMode > ( ) , It . IsAny < Series > ( ) , It . IsAny < DownloadClientItem > ( ) ) )
. Returns ( new List < ImportResult >
{
new ImportResult ( new ImportDecision ( new LocalEpisode { Path = @"C:\TestPath\Droned.S01E01.mkv" , Episodes = { _episode1 } }) ) ,
new ImportResult ( new ImportDecision ( new LocalEpisode { Path = @"C:\TestPath\Droned.S01E0 2.mkv", Episodes = { _episode2 } } ) , "Test Failure" ) ,
new ImportResult ( new ImportDecision ( new LocalEpisode { Path = @"C:\TestPath\Droned.S01E0 3.mkv", Episodes = { _episode3 } } ) , "Test Failure" )
new ImportResult ( new ImportDecision ( new LocalEpisode { Path = @"C:\TestPath\Droned.S01E01.mkv" }) ) ,
new ImportResult ( new ImportDecision ( new LocalEpisode { Path = @"C:\TestPath\Droned.S01E0 1.mkv" } ) , "Test Failure" ) ,
new ImportResult ( new ImportDecision ( new LocalEpisode { Path = @"C:\TestPath\Droned.S01E0 1.mkv" } ) , "Test Failure" )
} ) ;
var history = Builder < History . History > . CreateListOfSize ( 2 )
. BuildList ( ) ;
Subject . Process ( _trackedDownload ) ;
Mocker . GetMock < IHistoryService > ( )
. Setup ( s = > s . FindByDownloadId ( It . IsAny < string > ( ) ) )
. Returns ( history ) ;
AssertNoCompletedDownload ( ) ;
Mocker . GetMock < ITrackedDownloadAlreadyImported > ( )
. Setup ( s = > s . IsImported ( _trackedDownload , history ) )
. Returns ( true ) ;
Subject . Import ( _trackedDownload ) ;
AssertNotImported ( ) ;
}
[Test]
public void should_mark_as_imported_if_the_download_can_be_tracked_using_the_source_seriesid ( )
public void should_mark_as_imported_if_ all_episodes_were_importe d( )
{
GivenABadlyNamedDownload ( ) ;
var episode1 = new Episode { Id = 1 } ;
var episode2 = new Episode { Id = 2 } ;
_trackedDownload . RemoteEpisode . Episodes = new List < Episode > { episode1 , episode2 } ;
Mocker . GetMock < IDownloadedEpisodesImportService > ( )
. Setup ( v = > v . ProcessPath ( It . IsAny < string > ( ) , It . IsAny < ImportMode > ( ) , It . IsAny < Series > ( ) , It . IsAny < DownloadClientItem > ( ) ) )
. Returns ( new List < ImportResult >
{
new ImportResult ( new ImportDecision ( new LocalEpisode { Path = @"C:\TestPath\Droned.S01E01.mkv" , Episodes = { _episode1 } } ) )
} ) ;
new ImportResult (
new ImportDecision (
new LocalEpisode { Path = @"C:\TestPath\Droned.S01E01.mkv" , Episodes = new List < Episode > { episode1 } } ) ) ,
Mocker . GetMock < ISeriesService > ( )
. Setup ( v = > v . GetSeries ( It . IsAny < int > ( ) ) )
. Returns ( BuildRemoteEpisode ( ) . Series ) ;
new ImportResult (
new ImportDecision (
new LocalEpisode { Path = @"C:\TestPath\Droned.S01E02.mkv" , Episodes = new List < Episode > { episode2 } } ) )
} ) ;
Subject . Process ( _trackedDownload ) ;
Subject . Import ( _trackedDownload ) ;
Assert CompletedDownloa d( ) ;
Assert Importe d( ) ;
}
[Test]
public void should_ not_mark_as_imported_if_the_download_cannot_be_tracked_using_the_source_title_as_it_was_initiated_externall y( )
public void should_ mark_as_imported_if_all_episodes_were_imported_including_histor y( )
{
GivenABadlyNamedDownload ( ) ;
var episode1 = new Episode { Id = 1 } ;
var episode2 = new Episode { Id = 2 } ;
_trackedDownload . RemoteEpisode . Episodes = new List < Episode > { episode1 , episode2 } ;
Mocker . GetMock < IDownloadedEpisodesImportService > ( )
. Setup ( v = > v . ProcessPath ( It . IsAny < string > ( ) , It . IsAny < ImportMode > ( ) , It . IsAny < Series > ( ) , It . IsAny < DownloadClientItem > ( ) ) )
. Returns ( new List < ImportResult >
{
new ImportResult ( new ImportDecision ( new LocalEpisode { Path = @"C:\TestPath\Droned.S01E01.mkv" , Episodes = { _episode1 } } ) )
} ) ;
. Setup ( v = > v . ProcessPath ( It . IsAny < string > ( ) , It . IsAny < ImportMode > ( ) , It . IsAny < Series > ( ) , It . IsAny < DownloadClientItem > ( ) ) )
. Returns ( new List < ImportResult >
{
new ImportResult (
new ImportDecision (
new LocalEpisode { Path = @"C:\TestPath\Droned.S01E01.mkv" , Episodes = new List < Episode > { episode1 } } ) ) ,
Mocker . GetMock < IHistoryService > ( )
. Setup ( s = > s . MostRecentForDownloadId ( It . Is < string > ( i = > i = = "1234" ) ) ) ;
new ImportResult (
new ImportDecision (
new LocalEpisode { Path = @"C:\TestPath\Droned.S01E02.mkv" , Episodes = new List < Episode > { episode2 } } ) , "Test Failure" )
} ) ;
Subject . Process ( _trackedDownload ) ;
var history = Builder < History . History > . CreateListOfSize ( 2 )
. BuildList ( ) ;
AssertNoCompletedDownload ( ) ;
}
Mocker . GetMock < IHistoryService > ( )
. Setup ( s = > s . FindByDownloadId ( It . IsAny < string > ( ) ) )
. Returns ( history ) ;
[Test]
public void should_not_import_when_there_is_a_title_mismatch ( )
{
Mocker . GetMock < IParsingService > ( )
. Setup ( s = > s . GetSeries ( "Drone.S01E01.HDTV" ) )
. Returns ( ( Series ) null ) ;
Mocker . GetMock < ITrackedDownloadAlreadyImported > ( )
. Setup ( s = > s . IsImported ( It . IsAny < TrackedDownload > ( ) , It . IsAny < List < History . History > > ( ) ) )
. Returns ( true ) ;
Subject . Process ( _trackedDownload ) ;
Subject . Import ( _trackedDownload ) ;
Assert NoCompletedDownloa d( ) ;
Assert Importe d( ) ;
}
[Test]
public void should_mark_as_import _title_mismatch_if_ignore_warnings_is_true ( )
public void should_mark_as_import ed_if_double_episode_file_is_imported ( )
{
_trackedDownload . RemoteEpisode . Episodes = new List < Episode >
{
new Episode ( )
} ;
var episode1 = new Episode { Id = 1 } ;
var episode2 = new Episode { Id = 2 } ;
_trackedDownload . RemoteEpisode . Episodes = new List < Episode > { episode1 , episode2 } ;
Mocker . GetMock < IDownloadedEpisodesImportService > ( )
. Setup ( v = > v . ProcessPath ( It . IsAny < string > ( ) , It . IsAny < ImportMode > ( ) , It . IsAny < Series > ( ) , It . IsAny < DownloadClientItem > ( ) ) )
. Returns ( new List < ImportResult >
{
new ImportResult ( new ImportDecision ( new LocalEpisode { Path = @"C:\TestPath\Droned.S01E01.mkv" , Episodes = { _episode1 } } ) )
new ImportResult (
new ImportDecision (
new LocalEpisode { Path = @"C:\TestPath\Droned.S01E01-E02.mkv" , Episodes = new List < Episode > { episode1 , episode2 } } ) )
} ) ;
Subject . Process( _trackedDownload , true ) ;
Subject . Import( _trackedDownload ) ;
Assert CompletedDownloa d( ) ;
Assert Importe d( ) ;
}
[Test]
public void should_ warn_if_path_is_not_valid_for_windows ( )
public void should_ mark_as_imported_if_all_episodes_were_imported_but_extra_files_were_not ( )
{
WindowsOnly ( ) ;
GivenSeriesMatch ( ) ;
_trackedDownload . DownloadItem . OutputPath = new OsPath ( @"/invalid/Windows/Path" ) ;
_trackedDownload . RemoteEpisode . Episodes = new List < Episode >
{
new Episode ( )
} ;
Subject . Process ( _trackedDownload ) ;
Mocker . GetMock < IDownloadedEpisodesImportService > ( )
. Setup ( v = > v . ProcessPath ( It . IsAny < string > ( ) , It . IsAny < ImportMode > ( ) , It . IsAny < Series > ( ) , It . IsAny < DownloadClientItem > ( ) ) )
. Returns ( new List < ImportResult >
{
new ImportResult ( new ImportDecision ( new LocalEpisode { Path = @"C:\TestPath\Droned.S01E01.mkv" , Episodes = _trackedDownload . RemoteEpisode . Episodes } ) ) ,
new ImportResult ( new ImportDecision ( new LocalEpisode { Path = @"C:\TestPath\Droned.S01E01.mkv" } ) , "Test Failure" )
} ) ;
Subject . Import ( _trackedDownload ) ;
AssertNoAttemptedImport ( ) ;
Assert Imported ( ) ;
}
[Test]
public void should_warn_if_path_is_not_valid_for_linux ( )
public void should_ mark_as_imported_if_the_download_can_be_tracked_using_the_source_seriesid ( )
{
MonoOnly ( ) ;
_trackedDownload . DownloadItem . OutputPath = new OsPath ( @"C:\Invalid\Mono\Path" ) ;
GivenABadlyNamedDownload ( ) ;
Subject . Process ( _trackedDownload ) ;
Mocker . GetMock < IDownloadedEpisodesImportService > ( )
. Setup ( v = > v . ProcessPath ( It . IsAny < string > ( ) , It . IsAny < ImportMode > ( ) , It . IsAny < Series > ( ) , It . IsAny < DownloadClientItem > ( ) ) )
. Returns ( new List < ImportResult >
{
new ImportResult ( new ImportDecision ( new LocalEpisode { Path = @"C:\TestPath\Droned.S01E01.mkv" , Episodes = _trackedDownload . RemoteEpisode . Episodes } ) )
} ) ;
AssertNoAttemptedImport ( ) ;
}
Mocker . GetMock < ISeriesService > ( )
. Setup ( v = > v . GetSeries ( It . IsAny < int > ( ) ) )
. Returns ( BuildRemoteEpisode ( ) . Series ) ;
private void AssertNoAttemptedImport ( )
{
Mocker . GetMock < IDownloadedEpisodesImportService > ( )
. Verify ( v = > v . ProcessPath ( It . IsAny < string > ( ) , It . IsAny < ImportMode > ( ) , It . IsAny < Series > ( ) , It . IsAny < DownloadClientItem > ( ) ) , Times . Never ( ) ) ;
Subject . Import ( _trackedDownload ) ;
Assert NoCompletedDownloa d( ) ;
AssertImported ( ) ;
}
private void AssertNo CompletedDownloa d( )
private void AssertNo tImporte d( )
{
Mocker . GetMock < IEventAggregator > ( )
. Verify ( v = > v . PublishEvent ( It . IsAny < DownloadCompletedEvent > ( ) ) , Times . Never ( ) ) ;
_trackedDownload . State . Should ( ) . NotBe( TrackedDownloadStage . Imported ) ;
_trackedDownload . State . Should ( ) . Be( TrackedDownloadState . ImportPending ) ;
}
private void Assert CompletedDownloa d( )
private void Assert Importe d( )
{
Mocker . GetMock < IDownloadedEpisodesImportService > ( )
. Verify ( v = > v . ProcessPath ( _trackedDownload . DownloadItem . OutputPath . FullPath , ImportMode . Auto , _trackedDownload . RemoteEpisode . Series , _trackedDownload . DownloadItem ) , Times . Once ( ) ) ;
@ -443,7 +371,7 @@ namespace NzbDrone.Core.Test.Download
Mocker . GetMock < IEventAggregator > ( )
. Verify ( v = > v . PublishEvent ( It . IsAny < DownloadCompletedEvent > ( ) ) , Times . Once ( ) ) ;
_trackedDownload . State . Should ( ) . Be ( TrackedDownloadSta g e. Imported ) ;
_trackedDownload . State . Should ( ) . Be ( TrackedDownloadSta t e. Imported ) ;
}
}
}