diff --git a/src/NzbDrone.Core.Test/MusicTests/ArtistNameSlugValidatorFixture.cs b/src/NzbDrone.Core.Test/MusicTests/ArtistNameSlugValidatorFixture.cs new file mode 100644 index 000000000..1a96907d3 --- /dev/null +++ b/src/NzbDrone.Core.Test/MusicTests/ArtistNameSlugValidatorFixture.cs @@ -0,0 +1,76 @@ +using System.Collections.Generic; +using System.Linq; +using FizzWare.NBuilder; +using FluentAssertions; +using FluentValidation.Validators; +using NUnit.Framework; +using NzbDrone.Common.Extensions; +using NzbDrone.Core.Test.Framework; +using NzbDrone.Core.Music; +using NzbDrone.Test.Common; + +namespace NzbDrone.Core.Test.TvTests +{ + [TestFixture] + public class ArtistNameSlugValidatorFixture : CoreTest + { + private List _artist; + private TestValidator _validator; + + [SetUp] + public void Setup() + { + _artist = Builder.CreateListOfSize(1) + .Build() + .ToList(); + + _validator = new TestValidator + { + v => v.RuleFor(s => s.NameSlug).SetValidator(Subject) + }; + + Mocker.GetMock() + .Setup(s => s.GetAllArtists()) + .Returns(_artist); + } + + [Test] + public void should_not_be_valid_if_there_is_an_existing_artist_with_the_same_title_slug() + { + var series = Builder.CreateNew() + .With(s => s.Id = 100) + .With(s => s.NameSlug = _artist.First().NameSlug) + .Build(); + + _validator.Validate(series).IsValid.Should().BeFalse(); + } + + [Test] + public void should_be_valid_if_there_is_not_an_existing_artist_with_the_same_title_slug() + { + var series = Builder.CreateNew() + .With(s => s.NameSlug = "MyNameSlug") + .Build(); + + _validator.Validate(series).IsValid.Should().BeTrue(); + } + + [Test] + public void should_be_valid_if_there_is_an_existing_artist_with_a_null_title_slug() + { + _artist.First().NameSlug = null; + + var series = Builder.CreateNew() + .With(s => s.NameSlug = "MyNameSlug") + .Build(); + + _validator.Validate(series).IsValid.Should().BeTrue(); + } + + [Test] + public void should_be_valid_when_updating_an_existing_artist() + { + _validator.Validate(_artist.First().JsonClone()).IsValid.Should().BeTrue(); + } + } +} diff --git a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index df686403e..bf7907dd5 100644 --- a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -283,6 +283,7 @@ + diff --git a/src/NzbDrone.Core/Music/ArtistSlugValidator.cs b/src/NzbDrone.Core/Music/ArtistSlugValidator.cs index 1d894fee2..af5f1ecd9 100644 --- a/src/NzbDrone.Core/Music/ArtistSlugValidator.cs +++ b/src/NzbDrone.Core/Music/ArtistSlugValidator.cs @@ -1,7 +1,8 @@ -using FluentValidation.Validators; +using System.Linq; +using FluentValidation.Validators; +using NzbDrone.Common.Extensions; using System; using System.Collections.Generic; -using System.Linq; using System.Text; namespace NzbDrone.Core.Music @@ -23,7 +24,9 @@ namespace NzbDrone.Core.Music dynamic instance = context.ParentContext.InstanceToValidate; var instanceId = (int)instance.Id; - return !_artistService.GetAllArtists().Exists(s => s.NameSlug.Equals(context.PropertyValue.ToString()) && s.Id != instanceId); + return !_artistService.GetAllArtists().Where(s => s.NameSlug.IsNotNullOrWhiteSpace()) + .ToList() + .Exists(s => s.NameSlug.Equals(context.PropertyValue.ToString()) && s.Id != instanceId); } } } diff --git a/src/NzbDrone.Test.Common/NzbDrone.Test.Common.csproj b/src/NzbDrone.Test.Common/NzbDrone.Test.Common.csproj index 1d4d29214..4c6e70778 100644 --- a/src/NzbDrone.Test.Common/NzbDrone.Test.Common.csproj +++ b/src/NzbDrone.Test.Common/NzbDrone.Test.Common.csproj @@ -107,6 +107,7 @@ + diff --git a/src/NzbDrone.Test.Common/TestValidator.cs b/src/NzbDrone.Test.Common/TestValidator.cs new file mode 100644 index 000000000..e6851b01f --- /dev/null +++ b/src/NzbDrone.Test.Common/TestValidator.cs @@ -0,0 +1,16 @@ +using System; +using FluentValidation; + +namespace NzbDrone.Test.Common +{ + public class TestValidator : InlineValidator + { + public TestValidator(params Action>[] actions) + { + foreach (var action in actions) + { + action(this); + } + } + } +}