parent
541edc9749
commit
28d5fbe409
@ -0,0 +1,96 @@
|
||||
using System.Linq;
|
||||
using FizzWare.NBuilder;
|
||||
using FluentAssertions;
|
||||
using Marr.Data;
|
||||
using NUnit.Framework;
|
||||
using NzbDrone.Core.MediaFiles;
|
||||
using NzbDrone.Core.MediaFiles.TrackImport.Specifications;
|
||||
using NzbDrone.Core.Parser.Model;
|
||||
using NzbDrone.Core.Test.Framework;
|
||||
using NzbDrone.Core.Music;
|
||||
|
||||
namespace NzbDrone.Core.Test.MediaFiles.TrackImport.Specifications
|
||||
{
|
||||
[TestFixture]
|
||||
public class SameFileSpecificationFixture : CoreTest<SameFileSpecification>
|
||||
{
|
||||
private LocalTrack _localTrack;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_localTrack = Builder<LocalTrack>.CreateNew()
|
||||
.With(l => l.Size = 150.Megabytes())
|
||||
.Build();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_be_accepted_if_no_existing_file()
|
||||
{
|
||||
_localTrack.Tracks = Builder<Track>.CreateListOfSize(1)
|
||||
.TheFirst(1)
|
||||
.With(e => e.TrackFileId = 0)
|
||||
.BuildList();
|
||||
|
||||
Subject.IsSatisfiedBy(_localTrack).Accepted.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_be_accepted_if_multiple_existing_files()
|
||||
{
|
||||
_localTrack.Tracks = Builder<Track>.CreateListOfSize(2)
|
||||
.TheFirst(1)
|
||||
.With(e => e.TrackFileId = 1)
|
||||
.With(e => e.TrackFile = new LazyLoaded<TrackFile>(
|
||||
new TrackFile
|
||||
{
|
||||
Size = _localTrack.Size
|
||||
}))
|
||||
.TheNext(1)
|
||||
.With(e => e.TrackFileId = 2)
|
||||
.With(e => e.TrackFile = new LazyLoaded<TrackFile>(
|
||||
new TrackFile
|
||||
{
|
||||
Size = _localTrack.Size
|
||||
}))
|
||||
.Build()
|
||||
.ToList();
|
||||
|
||||
Subject.IsSatisfiedBy(_localTrack).Accepted.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_be_accepted_if_file_size_is_different()
|
||||
{
|
||||
_localTrack.Tracks = Builder<Track>.CreateListOfSize(1)
|
||||
.TheFirst(1)
|
||||
.With(e => e.TrackFileId = 1)
|
||||
.With(e => e.TrackFile = new LazyLoaded<TrackFile>(
|
||||
new TrackFile
|
||||
{
|
||||
Size = _localTrack.Size + 100.Megabytes()
|
||||
}))
|
||||
.Build()
|
||||
.ToList();
|
||||
|
||||
Subject.IsSatisfiedBy(_localTrack).Accepted.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_be_reject_if_file_size_is_the_same()
|
||||
{
|
||||
_localTrack.Tracks = Builder<Track>.CreateListOfSize(1)
|
||||
.TheFirst(1)
|
||||
.With(e => e.TrackFileId = 1)
|
||||
.With(e => e.TrackFile = new LazyLoaded<TrackFile>(
|
||||
new TrackFile
|
||||
{
|
||||
Size = _localTrack.Size
|
||||
}))
|
||||
.Build()
|
||||
.ToList();
|
||||
|
||||
Subject.IsSatisfiedBy(_localTrack).Accepted.Should().BeFalse();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
using System.Linq;
|
||||
using NLog;
|
||||
using NzbDrone.Core.DecisionEngine;
|
||||
using NzbDrone.Core.Download;
|
||||
using NzbDrone.Core.Parser.Model;
|
||||
|
||||
namespace NzbDrone.Core.MediaFiles.TrackImport.Specifications
|
||||
{
|
||||
public class SameFileSpecification : IImportDecisionEngineSpecification
|
||||
{
|
||||
private readonly Logger _logger;
|
||||
|
||||
public SameFileSpecification(Logger logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Decision IsSatisfiedBy(LocalTrack localTrack)
|
||||
{
|
||||
var trackFiles = localTrack.Tracks.Where(e => e.TrackFileId != 0).Select(e => e.TrackFile).ToList();
|
||||
|
||||
if (trackFiles.Count == 0)
|
||||
{
|
||||
_logger.Debug("No existing track file, skipping");
|
||||
return Decision.Accept();
|
||||
}
|
||||
|
||||
if (trackFiles.Count > 1)
|
||||
{
|
||||
_logger.Debug("More than one existing track file, skipping.");
|
||||
return Decision.Accept();
|
||||
}
|
||||
|
||||
if (trackFiles.First().Value.Size == localTrack.Size)
|
||||
{
|
||||
_logger.Debug("'{0}' Has the same filesize as existing file", localTrack.Path);
|
||||
return Decision.Reject("Has the same filesize as existing file");
|
||||
}
|
||||
|
||||
return Decision.Accept();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue