2017-09-04 02:20:56 +00:00
|
|
|
using System.Collections.Generic;
|
2014-07-23 23:43:54 +00:00
|
|
|
using System.IO;
|
2013-07-12 05:57:24 +00:00
|
|
|
using System.Linq;
|
|
|
|
using FizzWare.NBuilder;
|
|
|
|
using FluentAssertions;
|
|
|
|
using Moq;
|
|
|
|
using NUnit.Framework;
|
2015-01-27 05:57:07 +00:00
|
|
|
using NzbDrone.Core.DecisionEngine;
|
2014-09-01 23:37:59 +00:00
|
|
|
using NzbDrone.Core.Download;
|
2013-07-12 05:57:24 +00:00
|
|
|
using NzbDrone.Core.MediaFiles;
|
|
|
|
using NzbDrone.Core.MediaFiles.Events;
|
2020-01-03 12:49:24 +00:00
|
|
|
using NzbDrone.Core.MediaFiles.TrackImport;
|
2013-09-14 06:36:07 +00:00
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2020-01-03 12:49:24 +00:00
|
|
|
using NzbDrone.Core.Music;
|
2013-07-12 05:57:24 +00:00
|
|
|
using NzbDrone.Core.Parser.Model;
|
2017-09-04 02:20:56 +00:00
|
|
|
using NzbDrone.Core.Profiles.Qualities;
|
2013-07-12 05:57:24 +00:00
|
|
|
using NzbDrone.Core.Qualities;
|
|
|
|
using NzbDrone.Core.Test.Framework;
|
2013-07-26 06:37:18 +00:00
|
|
|
using NzbDrone.Test.Common;
|
2013-07-12 05:57:24 +00:00
|
|
|
|
2013-09-10 17:54:59 +00:00
|
|
|
namespace NzbDrone.Core.Test.MediaFiles
|
2013-07-12 05:57:24 +00:00
|
|
|
{
|
|
|
|
[TestFixture]
|
2017-12-24 05:03:38 +00:00
|
|
|
public class ImportApprovedTracksFixture : CoreTest<ImportApprovedTracks>
|
2013-07-12 05:57:24 +00:00
|
|
|
{
|
2019-02-16 14:49:24 +00:00
|
|
|
private List<ImportDecision<LocalTrack>> _rejectedDecisions;
|
|
|
|
private List<ImportDecision<LocalTrack>> _approvedDecisions;
|
2013-07-12 05:57:24 +00:00
|
|
|
|
2014-09-01 23:37:59 +00:00
|
|
|
private DownloadClientItem _downloadClientItem;
|
2020-11-16 21:34:49 +00:00
|
|
|
private DownloadClientItemClientInfo _clientInfo;
|
2014-09-01 23:37:59 +00:00
|
|
|
|
2013-07-12 05:57:24 +00:00
|
|
|
[SetUp]
|
|
|
|
public void Setup()
|
|
|
|
{
|
2019-02-16 14:49:24 +00:00
|
|
|
_rejectedDecisions = new List<ImportDecision<LocalTrack>>();
|
|
|
|
_approvedDecisions = new List<ImportDecision<LocalTrack>>();
|
2013-07-12 05:57:24 +00:00
|
|
|
|
2017-07-02 00:21:39 +00:00
|
|
|
var artist = Builder<Artist>.CreateNew()
|
2019-02-23 22:39:11 +00:00
|
|
|
.With(e => e.QualityProfile = new QualityProfile { Items = Qualities.QualityFixture.GetDefaultQualities() })
|
2017-07-02 00:21:39 +00:00
|
|
|
.With(s => s.Path = @"C:\Test\Music\Alien Ant Farm".AsOsAgnostic())
|
2013-07-12 05:57:24 +00:00
|
|
|
.Build();
|
|
|
|
|
2017-12-24 05:03:38 +00:00
|
|
|
var album = Builder<Album>.CreateNew()
|
|
|
|
.With(e => e.Artist = artist)
|
|
|
|
.Build();
|
2018-12-15 00:02:43 +00:00
|
|
|
|
|
|
|
var release = Builder<AlbumRelease>.CreateNew()
|
|
|
|
.With(e => e.AlbumId = album.Id)
|
2019-02-16 14:49:24 +00:00
|
|
|
.With(e => e.Monitored = true)
|
2018-12-15 00:02:43 +00:00
|
|
|
.Build();
|
2017-12-24 05:03:38 +00:00
|
|
|
|
2019-02-16 14:49:24 +00:00
|
|
|
album.AlbumReleases = new List<AlbumRelease> { release };
|
|
|
|
|
2017-07-02 00:21:39 +00:00
|
|
|
var tracks = Builder<Track>.CreateListOfSize(5)
|
2013-07-12 05:57:24 +00:00
|
|
|
.Build();
|
|
|
|
|
2019-02-16 14:49:24 +00:00
|
|
|
_rejectedDecisions.Add(new ImportDecision<LocalTrack>(new LocalTrack(), new Rejection("Rejected!")));
|
|
|
|
_rejectedDecisions.Add(new ImportDecision<LocalTrack>(new LocalTrack(), new Rejection("Rejected!")));
|
|
|
|
_rejectedDecisions.Add(new ImportDecision<LocalTrack>(new LocalTrack(), new Rejection("Rejected!")));
|
2013-07-12 05:57:24 +00:00
|
|
|
|
2017-07-02 00:21:39 +00:00
|
|
|
foreach (var track in tracks)
|
2013-07-12 05:57:24 +00:00
|
|
|
{
|
2020-01-03 12:49:24 +00:00
|
|
|
_approvedDecisions.Add(new ImportDecision<LocalTrack>(
|
2017-07-02 00:21:39 +00:00
|
|
|
new LocalTrack
|
2017-09-04 02:20:56 +00:00
|
|
|
{
|
|
|
|
Artist = artist,
|
2017-12-24 05:03:38 +00:00
|
|
|
Album = album,
|
2018-12-15 00:02:43 +00:00
|
|
|
Release = release,
|
2017-09-04 02:20:56 +00:00
|
|
|
Tracks = new List<Track> { track },
|
2017-12-24 05:03:38 +00:00
|
|
|
Path = Path.Combine(artist.Path, "Alien Ant Farm - 01 - Pilot.mp3"),
|
2017-09-04 02:20:56 +00:00
|
|
|
Quality = new QualityModel(Quality.MP3_256),
|
2019-02-16 14:49:24 +00:00
|
|
|
FileTrackInfo = new ParsedTrackInfo
|
2013-07-12 05:57:24 +00:00
|
|
|
{
|
2017-09-04 02:20:56 +00:00
|
|
|
ReleaseGroup = "DRONE"
|
|
|
|
}
|
|
|
|
}));
|
2013-07-12 05:57:24 +00:00
|
|
|
}
|
2013-08-12 07:20:36 +00:00
|
|
|
|
|
|
|
Mocker.GetMock<IUpgradeMediaFiles>()
|
2017-07-02 00:21:39 +00:00
|
|
|
.Setup(s => s.UpgradeTrackFile(It.IsAny<TrackFile>(), It.IsAny<LocalTrack>(), It.IsAny<bool>()))
|
|
|
|
.Returns(new TrackFileMoveResult());
|
2014-09-01 23:37:59 +00:00
|
|
|
|
2020-11-16 21:34:49 +00:00
|
|
|
_clientInfo = Builder<DownloadClientItemClientInfo>.CreateNew().Build();
|
|
|
|
_downloadClientItem = Builder<DownloadClientItem>.CreateNew().With(x => x.DownloadClientInfo = _clientInfo).Build();
|
2018-01-26 03:01:53 +00:00
|
|
|
|
2019-02-16 14:49:24 +00:00
|
|
|
Mocker.GetMock<IMediaFileService>()
|
|
|
|
.Setup(s => s.GetFilesByAlbum(It.IsAny<int>()))
|
|
|
|
.Returns(new List<TrackFile>());
|
2013-07-12 05:57:24 +00:00
|
|
|
}
|
2014-09-26 21:28:24 +00:00
|
|
|
|
2013-07-12 05:57:24 +00:00
|
|
|
[Test]
|
2014-08-22 00:57:23 +00:00
|
|
|
public void should_not_import_any_if_there_are_no_approved_decisions()
|
2013-07-12 05:57:24 +00:00
|
|
|
{
|
2014-08-22 00:57:23 +00:00
|
|
|
Subject.Import(_rejectedDecisions, false).Where(i => i.Result == ImportResultType.Imported).Should().BeEmpty();
|
|
|
|
|
2017-07-02 00:21:39 +00:00
|
|
|
Mocker.GetMock<IMediaFileService>().Verify(v => v.Add(It.IsAny<TrackFile>()), Times.Never());
|
2013-07-12 05:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void should_import_each_approved()
|
|
|
|
{
|
2014-04-19 15:09:22 +00:00
|
|
|
Subject.Import(_approvedDecisions, false).Should().HaveCount(5);
|
2013-07-12 05:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void should_only_import_approved()
|
|
|
|
{
|
2019-02-16 14:49:24 +00:00
|
|
|
var all = new List<ImportDecision<LocalTrack>>();
|
2013-07-12 05:57:24 +00:00
|
|
|
all.AddRange(_rejectedDecisions);
|
|
|
|
all.AddRange(_approvedDecisions);
|
|
|
|
|
2014-08-22 00:57:23 +00:00
|
|
|
var result = Subject.Import(all, false);
|
2016-10-18 19:17:50 +00:00
|
|
|
|
2014-08-22 00:57:23 +00:00
|
|
|
result.Should().HaveCount(all.Count);
|
|
|
|
result.Where(i => i.Result == ImportResultType.Imported).Should().HaveCount(_approvedDecisions.Count);
|
2013-07-12 05:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2017-12-24 05:03:38 +00:00
|
|
|
public void should_only_import_each_track_once()
|
2013-07-12 05:57:24 +00:00
|
|
|
{
|
2019-02-16 14:49:24 +00:00
|
|
|
var all = new List<ImportDecision<LocalTrack>>();
|
2013-07-12 05:57:24 +00:00
|
|
|
all.AddRange(_approvedDecisions);
|
2019-02-16 14:49:24 +00:00
|
|
|
all.Add(new ImportDecision<LocalTrack>(_approvedDecisions.First().Item));
|
2013-07-12 05:57:24 +00:00
|
|
|
|
2014-08-22 00:57:23 +00:00
|
|
|
var result = Subject.Import(all, false);
|
|
|
|
|
|
|
|
result.Where(i => i.Result == ImportResultType.Imported).Should().HaveCount(_approvedDecisions.Count);
|
2013-07-12 05:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void should_move_new_downloads()
|
|
|
|
{
|
2019-02-16 14:49:24 +00:00
|
|
|
Subject.Import(new List<ImportDecision<LocalTrack>> { _approvedDecisions.First() }, true);
|
2013-07-12 05:57:24 +00:00
|
|
|
|
2013-07-16 02:56:46 +00:00
|
|
|
Mocker.GetMock<IUpgradeMediaFiles>()
|
2019-02-16 14:49:24 +00:00
|
|
|
.Verify(v => v.UpgradeTrackFile(It.IsAny<TrackFile>(), _approvedDecisions.First().Item, false),
|
2013-07-12 05:57:24 +00:00
|
|
|
Times.Once());
|
|
|
|
}
|
|
|
|
|
2017-12-24 05:03:38 +00:00
|
|
|
[Test]
|
|
|
|
public void should_publish_TrackImportedEvent_for_new_downloads()
|
|
|
|
{
|
2019-02-16 14:49:24 +00:00
|
|
|
Subject.Import(new List<ImportDecision<LocalTrack>> { _approvedDecisions.First() }, true);
|
2017-10-18 02:55:02 +00:00
|
|
|
|
2017-12-24 05:03:38 +00:00
|
|
|
Mocker.GetMock<IEventAggregator>()
|
|
|
|
.Verify(v => v.PublishEvent(It.IsAny<TrackImportedEvent>()), Times.Once());
|
|
|
|
}
|
2013-07-12 05:57:24 +00:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void should_not_move_existing_files()
|
|
|
|
{
|
2019-04-04 08:20:47 +00:00
|
|
|
var track = _approvedDecisions.First();
|
|
|
|
track.Item.ExistingFile = true;
|
|
|
|
Subject.Import(new List<ImportDecision<LocalTrack>> { track }, false);
|
2013-07-12 05:57:24 +00:00
|
|
|
|
2013-07-16 02:56:46 +00:00
|
|
|
Mocker.GetMock<IUpgradeMediaFiles>()
|
2019-02-16 14:49:24 +00:00
|
|
|
.Verify(v => v.UpgradeTrackFile(It.IsAny<TrackFile>(), _approvedDecisions.First().Item, false),
|
2013-07-12 05:57:24 +00:00
|
|
|
Times.Never());
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2013-10-14 16:22:16 +00:00
|
|
|
public void should_import_larger_files_first()
|
|
|
|
{
|
|
|
|
var fileDecision = _approvedDecisions.First();
|
2019-02-16 14:49:24 +00:00
|
|
|
fileDecision.Item.Size = 1.Gigabytes();
|
2013-10-14 16:22:16 +00:00
|
|
|
|
2020-01-03 12:49:24 +00:00
|
|
|
var sampleDecision = new ImportDecision<LocalTrack>(
|
|
|
|
new LocalTrack
|
2017-09-04 02:20:56 +00:00
|
|
|
{
|
2019-02-16 14:49:24 +00:00
|
|
|
Artist = fileDecision.Item.Artist,
|
|
|
|
Album = fileDecision.Item.Album,
|
|
|
|
Tracks = new List<Track> { fileDecision.Item.Tracks.First() },
|
2017-12-24 05:03:38 +00:00
|
|
|
Path = @"C:\Test\Music\Alien Ant Farm\Alien Ant Farm - 01 - Pilot.mp3".AsOsAgnostic(),
|
2017-09-04 02:20:56 +00:00
|
|
|
Quality = new QualityModel(Quality.MP3_256),
|
|
|
|
Size = 80.Megabytes()
|
|
|
|
});
|
2013-10-14 16:22:16 +00:00
|
|
|
|
2019-02-16 14:49:24 +00:00
|
|
|
var all = new List<ImportDecision<LocalTrack>>();
|
2013-10-14 16:22:16 +00:00
|
|
|
all.Add(fileDecision);
|
|
|
|
all.Add(sampleDecision);
|
|
|
|
|
2014-04-19 15:09:22 +00:00
|
|
|
var results = Subject.Import(all, false);
|
2013-10-14 16:22:16 +00:00
|
|
|
|
2014-08-22 00:57:23 +00:00
|
|
|
results.Should().HaveCount(all.Count);
|
|
|
|
results.Should().ContainSingle(d => d.Result == ImportResultType.Imported);
|
2019-02-16 14:49:24 +00:00
|
|
|
results.Should().ContainSingle(d => d.Result == ImportResultType.Imported && d.ImportDecision.Item.Size == fileDecision.Item.Size);
|
2013-10-14 16:22:16 +00:00
|
|
|
}
|
2016-10-18 19:17:50 +00:00
|
|
|
|
|
|
|
[Test]
|
2017-09-26 02:31:52 +00:00
|
|
|
public void should_copy_when_cannot_move_files_downloads()
|
2016-10-18 19:17:50 +00:00
|
|
|
{
|
2020-11-16 21:34:49 +00:00
|
|
|
Subject.Import(new List<ImportDecision<LocalTrack>> { _approvedDecisions.First() }, true, new DownloadClientItem { Title = "Alien.Ant.Farm-Truant", CanMoveFiles = false, DownloadClientInfo = _clientInfo });
|
2016-10-18 19:17:50 +00:00
|
|
|
|
|
|
|
Mocker.GetMock<IUpgradeMediaFiles>()
|
2019-02-16 14:49:24 +00:00
|
|
|
.Verify(v => v.UpgradeTrackFile(It.IsAny<TrackFile>(), _approvedDecisions.First().Item, true), Times.Once());
|
2016-10-18 19:17:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void should_use_override_importmode()
|
|
|
|
{
|
2020-11-16 21:34:49 +00:00
|
|
|
Subject.Import(new List<ImportDecision<LocalTrack>> { _approvedDecisions.First() }, true, new DownloadClientItem { Title = "Alien.Ant.Farm-Truant", CanMoveFiles = false, DownloadClientInfo = _clientInfo }, ImportMode.Move);
|
2016-10-18 19:17:50 +00:00
|
|
|
|
|
|
|
Mocker.GetMock<IUpgradeMediaFiles>()
|
2019-02-16 14:49:24 +00:00
|
|
|
.Verify(v => v.UpgradeTrackFile(It.IsAny<TrackFile>(), _approvedDecisions.First().Item, false), Times.Once());
|
2016-10-18 19:17:50 +00:00
|
|
|
}
|
2018-01-26 03:01:53 +00:00
|
|
|
|
|
|
|
[Test]
|
2019-04-04 08:20:47 +00:00
|
|
|
public void should_delete_existing_trackfiles_with_the_same_path()
|
2018-01-26 03:01:53 +00:00
|
|
|
{
|
|
|
|
Mocker.GetMock<IMediaFileService>()
|
2019-06-08 19:13:58 +00:00
|
|
|
.Setup(s => s.GetFileWithPath(It.IsAny<string>()))
|
|
|
|
.Returns(Builder<TrackFile>.CreateNew().Build());
|
2018-01-26 03:01:53 +00:00
|
|
|
|
2019-04-04 08:20:47 +00:00
|
|
|
var track = _approvedDecisions.First();
|
|
|
|
track.Item.ExistingFile = true;
|
|
|
|
Subject.Import(new List<ImportDecision<LocalTrack>> { track }, false);
|
2018-01-26 03:01:53 +00:00
|
|
|
|
|
|
|
Mocker.GetMock<IMediaFileService>()
|
|
|
|
.Verify(v => v.Delete(It.IsAny<TrackFile>(), DeleteMediaFileReason.ManualOverride), Times.Once());
|
|
|
|
}
|
2013-07-12 05:57:24 +00:00
|
|
|
}
|
2016-10-18 19:17:50 +00:00
|
|
|
}
|