From 8052b7c1ad411b1c694064633f42b45198ae72bf Mon Sep 17 00:00:00 2001 From: skuill Date: Sun, 10 Dec 2023 01:54:40 +0400 Subject: [PATCH] #6 Added LyricScraperClient Unit Tests. Fixed host configuration without logging. --- .../Providers/AZLyrics/AZLyricsProvider.cs | 12 ++ .../Providers/Genius/GeniusProvider.cs | 12 ++ .../Providers/LyricFind/LyricFindProvider.cs | 12 ++ .../Musixmatch/MusixmatchProvider.cs | 12 ++ .../SongLyrics/SongLyricsProvider.cs | 14 +++ .../LyricsScraperClientTests.cs | 112 ++++++++++++++++++ .../AZLyrics/AZLyricsUriConverterTests.cs | 9 ++ 7 files changed, 183 insertions(+) create mode 100644 Tests/LyricsScraperNET.UnitTest/LyricsScraperClientTests.cs diff --git a/LyricsScraperNET/Providers/AZLyrics/AZLyricsProvider.cs b/LyricsScraperNET/Providers/AZLyrics/AZLyricsProvider.cs index 385fa97..c2a349b 100644 --- a/LyricsScraperNET/Providers/AZLyrics/AZLyricsProvider.cs +++ b/LyricsScraperNET/Providers/AZLyrics/AZLyricsProvider.cs @@ -40,6 +40,18 @@ public AZLyricsProvider(ILogger logger, IOptionsSnapshot options) + : this(null, options.Value) + { + Ensure.ArgumentNotNull(options, nameof(options)); + } + #endregion public override IExternalProviderOptions Options { get; } diff --git a/LyricsScraperNET/Providers/Genius/GeniusProvider.cs b/LyricsScraperNET/Providers/Genius/GeniusProvider.cs index ef6ba2e..515404c 100644 --- a/LyricsScraperNET/Providers/Genius/GeniusProvider.cs +++ b/LyricsScraperNET/Providers/Genius/GeniusProvider.cs @@ -49,6 +49,18 @@ public GeniusProvider(ILogger logger, IOptionsSnapshot options) + : this(null, options.Value) + { + Ensure.ArgumentNotNull(options, nameof(options)); + } + #endregion public override IExternalProviderOptions Options { get; } diff --git a/LyricsScraperNET/Providers/LyricFind/LyricFindProvider.cs b/LyricsScraperNET/Providers/LyricFind/LyricFindProvider.cs index 482f31e..549c064 100644 --- a/LyricsScraperNET/Providers/LyricFind/LyricFindProvider.cs +++ b/LyricsScraperNET/Providers/LyricFind/LyricFindProvider.cs @@ -39,6 +39,18 @@ public LyricFindProvider(ILogger logger, IOptionsSnapshot options) + : this(null, options.Value) + { + Ensure.ArgumentNotNull(options, nameof(options)); + } + #endregion public override IExternalProviderOptions Options { get; } diff --git a/LyricsScraperNET/Providers/Musixmatch/MusixmatchProvider.cs b/LyricsScraperNET/Providers/Musixmatch/MusixmatchProvider.cs index 49bc688..01747fa 100644 --- a/LyricsScraperNET/Providers/Musixmatch/MusixmatchProvider.cs +++ b/LyricsScraperNET/Providers/Musixmatch/MusixmatchProvider.cs @@ -59,6 +59,18 @@ public MusixmatchProvider(ILogger logger, IOptionsSnapshot options) + : this(null, options.Value) + { + Ensure.ArgumentNotNull(options, nameof(options)); + } + #endregion public override IExternalProviderOptions Options { get; } diff --git a/LyricsScraperNET/Providers/SongLyrics/SongLyricsProvider.cs b/LyricsScraperNET/Providers/SongLyrics/SongLyricsProvider.cs index 6677da4..f517b5a 100644 --- a/LyricsScraperNET/Providers/SongLyrics/SongLyricsProvider.cs +++ b/LyricsScraperNET/Providers/SongLyrics/SongLyricsProvider.cs @@ -21,6 +21,7 @@ public sealed class SongLyricsProvider : ExternalProviderBase private const string NotExistLyricPattern = "We do not have the lyrics for (.*) yet."; + #region Constructors public SongLyricsProvider() { @@ -42,6 +43,19 @@ public SongLyricsProvider(ILogger logger, IOptionsSnapshot options) + : this(null, options.Value) + { + Ensure.ArgumentNotNull(options, nameof(options)); + } + + #endregion public override IExternalProviderOptions Options { get; } diff --git a/Tests/LyricsScraperNET.UnitTest/LyricsScraperClientTests.cs b/Tests/LyricsScraperNET.UnitTest/LyricsScraperClientTests.cs new file mode 100644 index 0000000..25291cd --- /dev/null +++ b/Tests/LyricsScraperNET.UnitTest/LyricsScraperClientTests.cs @@ -0,0 +1,112 @@ +using LyricsScraperNET.Models.Requests; +using LyricsScraperNET.Providers.Models; +using Moq; +using Xunit; + +namespace LyricsScraperNET.UnitTest +{ + public class LyricsScraperClientTests + { + [Fact] + public async void SearchLyric_WithDisabledClient_ShouldReturnEmptySearchResult() + { + // Arrange + var lyricsScraperClient = GetLyricsScraperClient(); + var searchRequestMock = new Mock(); + var externalProviderTypes = GetExternalProviderTypes(); + + // Act + lyricsScraperClient.Disable(); + var searchResult = lyricsScraperClient.SearchLyric(searchRequestMock.Object); + var searchResultAsync = await lyricsScraperClient.SearchLyricAsync(searchRequestMock.Object); + + // Assert + Assert.False(lyricsScraperClient.IsEnabled); + foreach (var providerType in externalProviderTypes) + { + Assert.False(lyricsScraperClient[providerType].IsEnabled); + } + Assert.NotNull(searchResult); + Assert.True(searchResult.IsEmpty()); + Assert.NotNull(searchResultAsync); + Assert.True(searchResultAsync.IsEmpty()); + } + + [Fact] + public async void SearchLyric_DefaultClient_ShouldReturnEmptySearchResult() + { + // Arrange + var lyricsScraperClient = new LyricsScraperClient(); + var searchRequestMock = new Mock(); + + // Act + var searchResult = lyricsScraperClient.SearchLyric(searchRequestMock.Object); + var searchResultAsync = await lyricsScraperClient.SearchLyricAsync(searchRequestMock.Object); + + // Assert + Assert.False(lyricsScraperClient.IsEnabled); + Assert.NotNull(searchResult); + Assert.True(searchResult.IsEmpty()); + Assert.NotNull(searchResultAsync); + Assert.True(searchResultAsync.IsEmpty()); + } + + [Fact] + public void Indexer_DefaultClient_ShouldReturnEmptyExternalProvider() + { + // Arrange + var lyricsScraperClient = new LyricsScraperClient(); + + // Act + var externalProvider = lyricsScraperClient[ExternalProviderType.Genius]; + + // Assert + Assert.Null(externalProvider); + } + + [Fact] + public void Indexer_ConfiguredClient_ShouldReturnEmptyExternalProvider() + { + // Arrange + var lyricsScraperClient = GetLyricsScraperClient(); + + // Act + var externalProvider = lyricsScraperClient[ExternalProviderType.Genius]; + + // Assert + Assert.Null(externalProvider); + } + + [Fact] + public async void Enable_WithDisabledClient_ShouldBeEnabled() + { + // Arrange + var lyricsScraperClient = GetLyricsScraperClient(); + var searchRequestMock = new Mock(); + var externalProviderTypes = GetExternalProviderTypes(); + + // Act + lyricsScraperClient.Disable(); + lyricsScraperClient.Enable(); + + // Assert + Assert.True(lyricsScraperClient.IsEnabled); + foreach (var providerType in externalProviderTypes) + { + Assert.True(lyricsScraperClient[providerType].IsEnabled); + } + } + + private ExternalProviderType[] GetExternalProviderTypes() + { + return new[] { ExternalProviderType.AZLyrics, ExternalProviderType.SongLyrics }; + } + + private ILyricsScraperClient GetLyricsScraperClient() + { + return new LyricsScraperClient() + .WithAZLyrics() + .WithSongLyrics(); + } + } +} diff --git a/Tests/LyricsScraperNET.UnitTest/Providers/AZLyrics/AZLyricsUriConverterTests.cs b/Tests/LyricsScraperNET.UnitTest/Providers/AZLyrics/AZLyricsUriConverterTests.cs index b1d465b..591fe52 100644 --- a/Tests/LyricsScraperNET.UnitTest/Providers/AZLyrics/AZLyricsUriConverterTests.cs +++ b/Tests/LyricsScraperNET.UnitTest/Providers/AZLyrics/AZLyricsUriConverterTests.cs @@ -8,7 +8,16 @@ public class AZLyricsUriConverterTests { [Theory] [InlineData("The Devil Wears Prada", "You Can't Spell Crap Without 'C'", "http://www.azlyrics.com/lyrics/devilwearsprada/youcantspellcrapwithoutc.html")] + [InlineData("The Devil Wears Prada", "You Can't Spell \"Crap\" Without \"C\"", "http://www.azlyrics.com/lyrics/devilwearsprada/youcantspellcrapwithoutc.html")] [InlineData(" Young Thug ", " Rich Nigga Shit ", "http://www.azlyrics.com/lyrics/youngthug/richniggashit.html")] + [InlineData("Attack Attack!", "I Swear I'll Change", "http://www.azlyrics.com/lyrics/attackattack/iswearillchange.html")] + [InlineData("Attack Attack!", "\"I Swear I'll Change\"", "http://www.azlyrics.com/lyrics/attackattack/iswearillchange.html")] + [InlineData("Against Me!", "Stop!", "http://www.azlyrics.com/lyrics/againstme/stop.html")] + [InlineData("Bryan Adams", "Summer of '69", "http://www.azlyrics.com/lyrics/bryanadams/summerof69.html")] + [InlineData("Bob Dylan", "Leopard-Skin Pill-Box Hat", "http://www.azlyrics.com/lyrics/bobdylan/leopardskinpillboxhat.html")] + [InlineData("Mac DeMarco", "Me and Jon, Hanging On", "http://www.azlyrics.com/lyrics/macdemarco/meandjonhangingon.html")] + [InlineData("Of Mice & Men", "You're Not Alone", "http://www.azlyrics.com/lyrics/ofmicemen/yourenotalone.html")] + [InlineData("Of Mice & Men", "O.G. Loko", "http://www.azlyrics.com/lyrics/ofmicemen/ogloko.html")] public void GetLyricUri_MultipleInputs_ShouldBeParse(string artistName, string songName, string expectedUri) { // Arrange