Skip to content

Commit

Permalink
Fixed: Respect import list search setting when monitoring existing items
Browse files Browse the repository at this point in the history
  • Loading branch information
ta264 committed Aug 3, 2022
1 parent b168bdd commit c339645
Show file tree
Hide file tree
Showing 2 changed files with 297 additions and 118 deletions.
233 changes: 221 additions & 12 deletions src/NzbDrone.Core.Test/ImportListTests/ImportListSyncServiceFixture.cs
@@ -1,9 +1,12 @@
using System.Collections.Generic;
using System.Linq;
using FizzWare.NBuilder;
using Moq;
using NUnit.Framework;
using NzbDrone.Core.ImportLists;
using NzbDrone.Core.ImportLists.Exclusions;
using NzbDrone.Core.IndexerSearch;
using NzbDrone.Core.Messaging.Commands;
using NzbDrone.Core.MetadataSource;
using NzbDrone.Core.Music;
using NzbDrone.Core.Parser.Model;
Expand Down Expand Up @@ -85,18 +88,32 @@ private void WithSecondBook()
_importListReports.Add(importListItem2);
}

private void WithExistingArtist()
private void WithExistingArtist(bool monitored)
{
Mocker.GetMock<IArtistService>()
.Setup(v => v.FindById(_importListReports.First().ArtistMusicBrainzId))
.Returns(new Artist { Id = 1, ForeignArtistId = _importListReports.First().ArtistMusicBrainzId });
.Returns(new Artist { Id = 1, ForeignArtistId = _importListReports.First().ArtistMusicBrainzId, Monitored = monitored });
}

private void WithExistingAlbum()
private void WithExistingAlbum(bool monitored)
{
var album = Builder<Album>.CreateNew()
.With(x => x.Id = 1)
.With(x => x.ForeignAlbumId = _importListReports.First().AlbumMusicBrainzId)
.With(x => x.Monitored = monitored)
.Build();

var artist = Builder<Artist>.CreateNew()
.With(x => x.Monitored = monitored)
.With(x => x.ForeignArtistId = _importListReports.First().ArtistMusicBrainzId)
.With(x => x.Albums = new List<Album> { album })
.Build();

album.Artist = artist;

Mocker.GetMock<IAlbumService>()
.Setup(v => v.FindById(_importListReports.First().AlbumMusicBrainzId))
.Returns(new Album { Id = 1, ForeignAlbumId = _importListReports.First().AlbumMusicBrainzId });
.Returns(album);
}

private void WithExcludedArtist()
Expand Down Expand Up @@ -125,11 +142,11 @@ private void WithExcludedAlbum()
});
}

private void WithMonitorType(ImportListMonitorType monitor)
private void WithListSettings(ImportListMonitorType monitor = ImportListMonitorType.EntireArtist, bool shouldMonitorExisting = false, bool shouldSearch = true)
{
Mocker.GetMock<IImportListFactory>()
.Setup(v => v.Get(It.IsAny<int>()))
.Returns(new ImportListDefinition { ShouldMonitor = monitor });
.Returns(new ImportListDefinition { ShouldMonitor = monitor, ShouldMonitorExisting = shouldMonitorExisting, ShouldSearch = shouldSearch });
}

[Test]
Expand Down Expand Up @@ -191,7 +208,7 @@ public void should_not_search_if_all_info()
public void should_not_add_if_existing_artist()
{
WithArtistId();
WithExistingArtist();
WithExistingArtist(false);

Subject.Execute(new ImportListSyncCommand());

Expand All @@ -203,7 +220,7 @@ public void should_not_add_if_existing_artist()
public void should_not_add_if_existing_album()
{
WithAlbumId();
WithExistingAlbum();
WithExistingAlbum(false);

Subject.Execute(new ImportListSyncCommand());

Expand All @@ -216,7 +233,7 @@ public void should_add_if_existing_artist_but_new_album()
{
WithAlbumId();
WithArtistId();
WithExistingArtist();
WithExistingArtist(false);

Subject.Execute(new ImportListSyncCommand());

Expand All @@ -230,7 +247,7 @@ public void should_add_if_existing_artist_but_new_album()
public void should_add_if_not_existing_artist(ImportListMonitorType monitor, bool expectedArtistMonitored)
{
WithArtistId();
WithMonitorType(monitor);
WithListSettings(monitor);

Subject.Execute(new ImportListSyncCommand());

Expand All @@ -244,7 +261,8 @@ public void should_add_if_not_existing_artist(ImportListMonitorType monitor, boo
public void should_add_if_not_existing_album(ImportListMonitorType monitor, bool expectedAlbumMonitored)
{
WithAlbumId();
WithMonitorType(monitor);
WithArtistId();
WithListSettings(monitor);

Subject.Execute(new ImportListSyncCommand());

Expand All @@ -268,6 +286,7 @@ public void should_not_add_artist_if_excluded_artist()
public void should_not_add_album_if_excluded_album()
{
WithAlbumId();
WithArtistId();
WithExcludedAlbum();

Subject.Execute(new ImportListSyncCommand());
Expand Down Expand Up @@ -298,7 +317,7 @@ public void should_add_two_albums(ImportListMonitorType monitor, int expectedAlb
WithAlbumId();
WithSecondBook();
WithArtistId();
WithMonitorType(monitor);
WithListSettings(monitor);

Subject.Execute(new ImportListSyncCommand());

Expand All @@ -311,5 +330,195 @@ public void should_add_two_albums(ImportListMonitorType monitor, int expectedAlb
false,
true));
}

[TestCase(ImportListMonitorType.SpecificAlbum)]
[TestCase(ImportListMonitorType.EntireArtist)]
public void should_monitor_existing_unmonitored_album(ImportListMonitorType monitorType)
{
WithAlbumId();
WithArtistId();
WithExistingAlbum(false);

WithListSettings(monitorType, true);

Subject.Execute(new ImportListSyncCommand());

Mocker.GetMock<IAlbumService>()
.Verify(v => v.SetAlbumMonitored(1, true));
}

[TestCase(ImportListMonitorType.SpecificAlbum)]
[TestCase(ImportListMonitorType.EntireArtist)]
public void should_not_monitor_existing_monitored_album(ImportListMonitorType monitorType)
{
WithAlbumId();
WithArtistId();
WithExistingAlbum(true);

WithListSettings(monitorType, true);

Subject.Execute(new ImportListSyncCommand());

Mocker.GetMock<IAlbumService>()
.Verify(v => v.SetAlbumMonitored(1, true), Times.Never);
}

[TestCase(ImportListMonitorType.SpecificAlbum, false)]
[TestCase(ImportListMonitorType.EntireArtist, false)]
[TestCase(ImportListMonitorType.None, false)]
[TestCase(ImportListMonitorType.None, true)]

public void should_not_monitor_existing_unmonitored_album(ImportListMonitorType monitorType, bool shouldMonitorExisting)
{
WithAlbumId();
WithArtistId();
WithExistingAlbum(false);

WithListSettings(monitorType, shouldMonitorExisting);

Subject.Execute(new ImportListSyncCommand());

Mocker.GetMock<IAlbumService>()
.Verify(v => v.SetAlbumMonitored(1, true), Times.Never);
}

[Test]
public void should_search_specific_existing_unmonitored_album()
{
WithAlbumId();
WithArtistId();
WithExistingAlbum(false);

WithListSettings(ImportListMonitorType.SpecificAlbum, true, true);

Subject.Execute(new ImportListSyncCommand());

Mocker.GetMock<IManageCommandQueue>()
.Verify(v => v.Push<Command>(It.Is<AlbumSearchCommand>(x => x.AlbumIds.Count == 1 && x.AlbumIds.Contains(1)), CommandPriority.Normal, CommandTrigger.Unspecified));
}

[Test]
public void should_not_search_specific_existing_unmonitored_album()
{
WithAlbumId();
WithArtistId();
WithExistingAlbum(false);

WithListSettings(ImportListMonitorType.SpecificAlbum, true, false);

Subject.Execute(new ImportListSyncCommand());

Mocker.GetMock<IManageCommandQueue>()
.Verify(v => v.Push<Command>(It.Is<AlbumSearchCommand>(x => x.AlbumIds.Count == 1 && x.AlbumIds.Contains(1)), CommandPriority.Normal, CommandTrigger.Unspecified), Times.Never);
}

[Test]
public void should_search_all_artist_albums()
{
WithAlbumId();
WithArtistId();
WithExistingAlbum(false);

WithListSettings(ImportListMonitorType.EntireArtist, true, true);

Subject.Execute(new ImportListSyncCommand());

Mocker.GetMock<IManageCommandQueue>()
.Verify(v => v.Push<Command>(It.Is<MissingAlbumSearchCommand>(x => x.ArtistId == 1), CommandPriority.Normal, CommandTrigger.Unspecified));
}

[Test]
public void should_not_search_all_artist_albums()
{
WithAlbumId();
WithArtistId();
WithExistingAlbum(false);

WithListSettings(ImportListMonitorType.EntireArtist, true, false);

Subject.Execute(new ImportListSyncCommand());

Mocker.GetMock<IManageCommandQueue>()
.Verify(v => v.Push<Command>(It.Is<MissingAlbumSearchCommand>(x => x.ArtistId == 1), CommandPriority.Normal, CommandTrigger.Unspecified), Times.Never);
}

[TestCase(ImportListMonitorType.SpecificAlbum)]
[TestCase(ImportListMonitorType.EntireArtist)]
[TestCase(ImportListMonitorType.None)]

public void should_monitor_existing_unmonitored_artist(ImportListMonitorType monitorType)
{
WithArtistId();
WithExistingArtist(false);

WithListSettings(monitorType, true);

Subject.Execute(new ImportListSyncCommand());

Mocker.GetMock<IArtistService>()
.Verify(v => v.UpdateArtist(It.Is<Artist>(a => a.Monitored), true));
}

[TestCase(ImportListMonitorType.SpecificAlbum)]
[TestCase(ImportListMonitorType.EntireArtist)]
[TestCase(ImportListMonitorType.None)]

public void should_not_monitor_existing_monitored_artist(ImportListMonitorType monitorType)
{
WithArtistId();
WithExistingArtist(true);

WithListSettings(monitorType, true);

Subject.Execute(new ImportListSyncCommand());

Mocker.GetMock<IArtistService>()
.Verify(v => v.UpdateArtist(It.IsAny<Artist>(), It.IsAny<bool>()), Times.Never);
}

[TestCase(ImportListMonitorType.SpecificAlbum)]
[TestCase(ImportListMonitorType.EntireArtist)]
[TestCase(ImportListMonitorType.None)]

public void should_not_monitor_existing_unmonitored_artist(ImportListMonitorType monitorType)
{
WithArtistId();
WithExistingArtist(false);

WithListSettings(monitorType, false);

Subject.Execute(new ImportListSyncCommand());

Mocker.GetMock<IArtistService>()
.Verify(v => v.UpdateArtist(It.IsAny<Artist>(), It.IsAny<bool>()), Times.Never);
}

[Test]
public void should_search_unmonitored_artist()
{
WithArtistId();
WithExistingArtist(false);

WithListSettings(ImportListMonitorType.EntireArtist, true, true);

Subject.Execute(new ImportListSyncCommand());

Mocker.GetMock<IManageCommandQueue>()
.Verify(v => v.Push<Command>(It.Is<MissingAlbumSearchCommand>(x => x.ArtistId == 1), CommandPriority.Normal, CommandTrigger.Unspecified));
}

[Test]
public void should_not_search_unmonitored_artist()
{
WithArtistId();
WithExistingArtist(false);

WithListSettings(ImportListMonitorType.EntireArtist, true, false);

Subject.Execute(new ImportListSyncCommand());

Mocker.GetMock<IManageCommandQueue>()
.Verify(v => v.Push<Command>(It.IsAny<MissingAlbumSearchCommand>(), CommandPriority.Normal, CommandTrigger.Unspecified), Times.Never);
}
}
}

0 comments on commit c339645

Please sign in to comment.