You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Readarr/src/NzbDrone.Core.Test/MediaFiles/ImportApprovedTracksFixture.cs

265 lines
11 KiB

using System.Collections.Generic;
using System.IO;
using System.Linq;
using FizzWare.NBuilder;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using NzbDrone.Core.Books;
using NzbDrone.Core.DecisionEngine;
using NzbDrone.Core.Download;
using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.MediaFiles.BookImport;
using NzbDrone.Core.MediaFiles.Events;
using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.Profiles.Qualities;
using NzbDrone.Core.Qualities;
using NzbDrone.Core.RootFolders;
using NzbDrone.Core.Test.Framework;
11 years ago
using NzbDrone.Test.Common;
namespace NzbDrone.Core.Test.MediaFiles
{
[TestFixture]
public class ImportApprovedTracksFixture : CoreTest<ImportApprovedBooks>
{
private List<ImportDecision<LocalBook>> _rejectedDecisions;
private List<ImportDecision<LocalBook>> _approvedDecisions;
private DownloadClientItem _downloadClientItem;
private DownloadClientItemClientInfo _clientInfo;
[SetUp]
public void Setup()
{
_rejectedDecisions = new List<ImportDecision<LocalBook>>();
_approvedDecisions = new List<ImportDecision<LocalBook>>();
var author = Builder<Author>.CreateNew()
.With(e => e.QualityProfile = new QualityProfile { Items = Qualities.QualityFixture.GetDefaultQualities() })
.With(s => s.Path = @"C:\Test\Music\Alien Ant Farm".AsOsAgnostic())
.Build();
var book = Builder<Book>.CreateNew()
.With(e => e.Author = author)
.Build();
var edition = Builder<Edition>.CreateNew()
.With(e => e.Book = book)
.With(e => e.Monitored = true)
.Build();
book.Editions = new List<Edition> { edition };
var rootFolder = Builder<RootFolder>.CreateNew()
.With(r => r.IsCalibreLibrary = false)
.Build();
_rejectedDecisions.Add(new ImportDecision<LocalBook>(new LocalBook(), new Rejection("Rejected!")));
_rejectedDecisions.Add(new ImportDecision<LocalBook>(new LocalBook(), new Rejection("Rejected!")));
_rejectedDecisions.Add(new ImportDecision<LocalBook>(new LocalBook(), new Rejection("Rejected!")));
_approvedDecisions.Add(new ImportDecision<LocalBook>(
new LocalBook
4 years ago
{
Author = author,
Book = book,
Edition = edition,
Part = 1,
Path = Path.Combine(author.Path, "Alien Ant Farm - 01 - Pilot.mp3"),
Quality = new QualityModel(Quality.MP3),
4 years ago
FileTrackInfo = new ParsedTrackInfo
{
4 years ago
ReleaseGroup = "DRONE"
}
}));
Mocker.GetMock<IUpgradeMediaFiles>()
.Setup(s => s.UpgradeBookFile(It.IsAny<BookFile>(), It.IsAny<LocalBook>(), It.IsAny<bool>()))
.Returns(new BookFileMoveResult());
_clientInfo = Builder<DownloadClientItemClientInfo>.CreateNew().Build();
_downloadClientItem = Builder<DownloadClientItem>.CreateNew().With(x => x.DownloadClientInfo = _clientInfo).Build();
Whole album matching and fingerprinting (#592) * Cache result of GetAllArtists * Fixed: Manual import not respecting album import notifications * Fixed: partial album imports stay in queue, prompting manual import * Fixed: Allow release if tracks are missing * Fixed: Be tolerant of missing/extra "The" at start of artist name * Improve manual import UI * Omit video tracks from DB entirely * Revert "faster test packaging in build.sh" This reverts commit 2723e2a7b86bcbff9051fd2aced07dd807b4bcb7. -u and -T are not supported on macOS * Fix tests on linux and macOS * Actually lint on linux On linux yarn runs scripts with sh not bash so ** doesn't recursively glob * Match whole albums * Option to disable fingerprinting * Rip out MediaInfo * Don't split up things that have the same album selected in manual import * Try to speed up IndentificationService * More speedups * Some fixes and increase power of recording id * Fix NRE when no tags * Fix NRE when some (but not all) files in a directory have missing tags * Bump taglib, tidy up tag parsing * Add a health check * Remove media info setting * Tags -> audioTags * Add some tests where tags are null * Rename history events * Add missing method to interface * Reinstate MediaInfo tags and update info with artist scan Also adds migration to remove old format media info * This file no longer exists * Don't penalise year if missing from tags * Formatting improvements * Use correct system newline * Switch to the netstandard2.0 library to support net 461 * TagLib.File is IDisposable so should be in a using * Improve filename matching and add tests * Neater logging of parsed tags * Fix disk scan tests for new media info update * Fix quality detection source * Fix Inexact Artist/Album match * Add button to clear track mapping * Fix warning * Pacify eslint * Use \ not / * Fix UI updates * Fix media covers Prevent localizing URL propaging back to the metadata object * Reduce database overhead broadcasting UI updates * Relax timings a bit to make test pass * Remove irrelevant tests * Test framework for identification service * Fix PreferMissingToBadMatch test case * Make fingerprinting more robust * More logging * Penalize unknown media format and country * Prefer USA to UK * Allow Data CD * Fix exception if fingerprinting fails for all files * Fix tests * Fix NRE * Allow apostrophes and remove accents in filename aggregation * Address codacy issues * Cope with old versions of fpcalc and suggest upgrade * fpcalc health check passes if fingerprinting disabled * Get the Artist meta with the artist * Fix the mapper so that lazy loaded lists will be populated on Join And therefore we can join TrackFiles on Tracks by default and avoid an extra query * Rename subtitle -> lyric * Tidy up MediaInfoFormatter
6 years ago
Mocker.GetMock<IMediaFileService>()
.Setup(s => s.GetFilesByBook(It.IsAny<int>()))
4 years ago
.Returns(new List<BookFile>());
Mocker.GetMock<IRootFolderService>()
.Setup(s => s.GetBestRootFolder(It.IsAny<string>()))
.Returns(rootFolder);
Mocker.GetMock<IEditionService>()
.Setup(s => s.SetMonitored(edition))
.Returns(new List<Edition> { edition });
}
[Test]
public void should_not_import_any_if_there_are_no_approved_decisions()
{
Subject.Import(_rejectedDecisions, false).Where(i => i.Result == ImportResultType.Imported).Should().BeEmpty();
4 years ago
Mocker.GetMock<IMediaFileService>().Verify(v => v.Add(It.IsAny<BookFile>()), Times.Never());
}
[Test]
public void should_import_each_approved()
{
4 years ago
Subject.Import(_approvedDecisions, false).Should().HaveCount(1);
}
[Test]
public void should_only_import_approved()
{
var all = new List<ImportDecision<LocalBook>>();
all.AddRange(_rejectedDecisions);
all.AddRange(_approvedDecisions);
var result = Subject.Import(all, false);
result.Should().HaveCount(all.Count);
result.Where(i => i.Result == ImportResultType.Imported).Should().HaveCount(_approvedDecisions.Count);
}
[Test]
public void should_only_import_each_track_once()
{
var all = new List<ImportDecision<LocalBook>>();
all.AddRange(_approvedDecisions);
all.Add(new ImportDecision<LocalBook>(_approvedDecisions.First().Item));
var result = Subject.Import(all, false);
result.Where(i => i.Result == ImportResultType.Imported).Should().HaveCount(_approvedDecisions.Count);
}
[Test]
public void should_move_new_downloads()
{
Subject.Import(new List<ImportDecision<LocalBook>> { _approvedDecisions.First() }, true);
Mocker.GetMock<IUpgradeMediaFiles>()
.Verify(v => v.UpgradeBookFile(It.IsAny<BookFile>(), _approvedDecisions.First().Item, false),
Times.Once());
}
[Test]
public void should_publish_TrackImportedEvent_for_new_downloads()
{
Subject.Import(new List<ImportDecision<LocalBook>> { _approvedDecisions.First() }, true);
Mocker.GetMock<IEventAggregator>()
.Verify(v => v.PublishEvent(It.IsAny<TrackImportedEvent>()), Times.Once());
}
[Test]
public void should_not_move_existing_files()
{
var track = _approvedDecisions.First();
track.Item.ExistingFile = true;
Subject.Import(new List<ImportDecision<LocalBook>> { track }, false);
Mocker.GetMock<IUpgradeMediaFiles>()
.Verify(v => v.UpgradeBookFile(It.IsAny<BookFile>(), _approvedDecisions.First().Item, false),
Times.Never());
}
[Test]
public void should_import_higher_quality_files_first()
{
var lqDecision = _approvedDecisions.First();
lqDecision.Item.Quality = new QualityModel(Quality.MOBI);
lqDecision.Item.Size = 10.Megabytes();
var hqDecision = new ImportDecision<LocalBook>(
new LocalBook
{
Author = lqDecision.Item.Author,
Book = lqDecision.Item.Book,
Edition = lqDecision.Item.Edition,
Part = 1,
Path = @"C:\Test\Music\Alien Ant Farm\Alien Ant Farm - 01 - Pilot.mp3".AsOsAgnostic(),
Quality = new QualityModel(Quality.AZW3),
Size = 1.Megabytes(),
FileTrackInfo = new ParsedTrackInfo
{
ReleaseGroup = "DRONE"
}
});
var all = new List<ImportDecision<LocalBook>>();
all.Add(lqDecision);
all.Add(hqDecision);
var results = Subject.Import(all, false);
results.Should().HaveCount(all.Count);
results.Should().ContainSingle(d => d.Result == ImportResultType.Imported);
results.Should().ContainSingle(d => d.Result == ImportResultType.Imported && d.ImportDecision.Item.Size == hqDecision.Item.Size);
}
[Test]
public void should_import_larger_files_for_same_quality_first()
{
var fileDecision = _approvedDecisions.First();
Whole album matching and fingerprinting (#592) * Cache result of GetAllArtists * Fixed: Manual import not respecting album import notifications * Fixed: partial album imports stay in queue, prompting manual import * Fixed: Allow release if tracks are missing * Fixed: Be tolerant of missing/extra "The" at start of artist name * Improve manual import UI * Omit video tracks from DB entirely * Revert "faster test packaging in build.sh" This reverts commit 2723e2a7b86bcbff9051fd2aced07dd807b4bcb7. -u and -T are not supported on macOS * Fix tests on linux and macOS * Actually lint on linux On linux yarn runs scripts with sh not bash so ** doesn't recursively glob * Match whole albums * Option to disable fingerprinting * Rip out MediaInfo * Don't split up things that have the same album selected in manual import * Try to speed up IndentificationService * More speedups * Some fixes and increase power of recording id * Fix NRE when no tags * Fix NRE when some (but not all) files in a directory have missing tags * Bump taglib, tidy up tag parsing * Add a health check * Remove media info setting * Tags -> audioTags * Add some tests where tags are null * Rename history events * Add missing method to interface * Reinstate MediaInfo tags and update info with artist scan Also adds migration to remove old format media info * This file no longer exists * Don't penalise year if missing from tags * Formatting improvements * Use correct system newline * Switch to the netstandard2.0 library to support net 461 * TagLib.File is IDisposable so should be in a using * Improve filename matching and add tests * Neater logging of parsed tags * Fix disk scan tests for new media info update * Fix quality detection source * Fix Inexact Artist/Album match * Add button to clear track mapping * Fix warning * Pacify eslint * Use \ not / * Fix UI updates * Fix media covers Prevent localizing URL propaging back to the metadata object * Reduce database overhead broadcasting UI updates * Relax timings a bit to make test pass * Remove irrelevant tests * Test framework for identification service * Fix PreferMissingToBadMatch test case * Make fingerprinting more robust * More logging * Penalize unknown media format and country * Prefer USA to UK * Allow Data CD * Fix exception if fingerprinting fails for all files * Fix tests * Fix NRE * Allow apostrophes and remove accents in filename aggregation * Address codacy issues * Cope with old versions of fpcalc and suggest upgrade * fpcalc health check passes if fingerprinting disabled * Get the Artist meta with the artist * Fix the mapper so that lazy loaded lists will be populated on Join And therefore we can join TrackFiles on Tracks by default and avoid an extra query * Rename subtitle -> lyric * Tidy up MediaInfoFormatter
6 years ago
fileDecision.Item.Size = 1.Gigabytes();
var sampleDecision = new ImportDecision<LocalBook>(
new LocalBook
{
Author = fileDecision.Item.Author,
Book = fileDecision.Item.Book,
Edition = fileDecision.Item.Edition,
Part = 1,
Path = @"C:\Test\Music\Alien Ant Farm\Alien Ant Farm - 01 - Pilot.mp3".AsOsAgnostic(),
Quality = new QualityModel(Quality.MP3),
Size = 80.Megabytes()
});
var all = new List<ImportDecision<LocalBook>>();
all.Add(fileDecision);
all.Add(sampleDecision);
var results = Subject.Import(all, false);
results.Should().HaveCount(all.Count);
results.Should().ContainSingle(d => d.Result == ImportResultType.Imported);
Whole album matching and fingerprinting (#592) * Cache result of GetAllArtists * Fixed: Manual import not respecting album import notifications * Fixed: partial album imports stay in queue, prompting manual import * Fixed: Allow release if tracks are missing * Fixed: Be tolerant of missing/extra "The" at start of artist name * Improve manual import UI * Omit video tracks from DB entirely * Revert "faster test packaging in build.sh" This reverts commit 2723e2a7b86bcbff9051fd2aced07dd807b4bcb7. -u and -T are not supported on macOS * Fix tests on linux and macOS * Actually lint on linux On linux yarn runs scripts with sh not bash so ** doesn't recursively glob * Match whole albums * Option to disable fingerprinting * Rip out MediaInfo * Don't split up things that have the same album selected in manual import * Try to speed up IndentificationService * More speedups * Some fixes and increase power of recording id * Fix NRE when no tags * Fix NRE when some (but not all) files in a directory have missing tags * Bump taglib, tidy up tag parsing * Add a health check * Remove media info setting * Tags -> audioTags * Add some tests where tags are null * Rename history events * Add missing method to interface * Reinstate MediaInfo tags and update info with artist scan Also adds migration to remove old format media info * This file no longer exists * Don't penalise year if missing from tags * Formatting improvements * Use correct system newline * Switch to the netstandard2.0 library to support net 461 * TagLib.File is IDisposable so should be in a using * Improve filename matching and add tests * Neater logging of parsed tags * Fix disk scan tests for new media info update * Fix quality detection source * Fix Inexact Artist/Album match * Add button to clear track mapping * Fix warning * Pacify eslint * Use \ not / * Fix UI updates * Fix media covers Prevent localizing URL propaging back to the metadata object * Reduce database overhead broadcasting UI updates * Relax timings a bit to make test pass * Remove irrelevant tests * Test framework for identification service * Fix PreferMissingToBadMatch test case * Make fingerprinting more robust * More logging * Penalize unknown media format and country * Prefer USA to UK * Allow Data CD * Fix exception if fingerprinting fails for all files * Fix tests * Fix NRE * Allow apostrophes and remove accents in filename aggregation * Address codacy issues * Cope with old versions of fpcalc and suggest upgrade * fpcalc health check passes if fingerprinting disabled * Get the Artist meta with the artist * Fix the mapper so that lazy loaded lists will be populated on Join And therefore we can join TrackFiles on Tracks by default and avoid an extra query * Rename subtitle -> lyric * Tidy up MediaInfoFormatter
6 years ago
results.Should().ContainSingle(d => d.Result == ImportResultType.Imported && d.ImportDecision.Item.Size == fileDecision.Item.Size);
}
[Test]
public void should_copy_when_cannot_move_files_downloads()
{
Subject.Import(new List<ImportDecision<LocalBook>> { _approvedDecisions.First() }, true, new DownloadClientItem { Title = "Alien.Ant.Farm-Truant", CanMoveFiles = false, DownloadClientInfo = _clientInfo });
Mocker.GetMock<IUpgradeMediaFiles>()
.Verify(v => v.UpgradeBookFile(It.IsAny<BookFile>(), _approvedDecisions.First().Item, true), Times.Once());
}
[Test]
public void should_use_override_importmode()
{
Subject.Import(new List<ImportDecision<LocalBook>> { _approvedDecisions.First() }, true, new DownloadClientItem { Title = "Alien.Ant.Farm-Truant", CanMoveFiles = false, DownloadClientInfo = _clientInfo }, ImportMode.Move);
Mocker.GetMock<IUpgradeMediaFiles>()
.Verify(v => v.UpgradeBookFile(It.IsAny<BookFile>(), _approvedDecisions.First().Item, false), Times.Once());
}
[Test]
public void should_delete_existing_trackfiles_with_the_same_path()
{
Mocker.GetMock<IMediaFileService>()
.Setup(s => s.GetFileWithPath(It.IsAny<string>()))
4 years ago
.Returns(Builder<BookFile>.CreateNew().Build());
var track = _approvedDecisions.First();
track.Item.ExistingFile = true;
Subject.Import(new List<ImportDecision<LocalBook>> { track }, false);
Mocker.GetMock<IMediaFileService>()
4 years ago
.Verify(v => v.Delete(It.IsAny<BookFile>(), DeleteMediaFileReason.ManualOverride), Times.Once());
}
}
}