Skip to content

Commit

Permalink
#6 Added LyricScraperClient Unit Tests. Fixed host configuration with…
Browse files Browse the repository at this point in the history
…out logging.
  • Loading branch information
skuill committed Dec 9, 2023
1 parent 496ee37 commit 8052b7c
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 0 deletions.
12 changes: 12 additions & 0 deletions LyricsScraperNET/Providers/AZLyrics/AZLyricsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ public AZLyricsProvider(ILogger<AZLyricsProvider> logger, IOptionsSnapshot<AZLyr
Ensure.ArgumentNotNull(options, nameof(options));
}

public AZLyricsProvider(AZLyricsOptions options)
: this(null, options)
{
Ensure.ArgumentNotNull(options, nameof(options));
}

public AZLyricsProvider(IOptionsSnapshot<AZLyricsOptions> options)
: this(null, options.Value)
{
Ensure.ArgumentNotNull(options, nameof(options));
}

#endregion

public override IExternalProviderOptions Options { get; }
Expand Down
12 changes: 12 additions & 0 deletions LyricsScraperNET/Providers/Genius/GeniusProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ public GeniusProvider(ILogger<GeniusProvider> logger, IOptionsSnapshot<GeniusOpt
Ensure.ArgumentNotNull(options, nameof(options));
}

public GeniusProvider(GeniusOptions options)
: this(null, options)
{
Ensure.ArgumentNotNull(options, nameof(options));
}

public GeniusProvider(IOptionsSnapshot<GeniusOptions> options)
: this(null, options.Value)
{
Ensure.ArgumentNotNull(options, nameof(options));
}

#endregion

public override IExternalProviderOptions Options { get; }
Expand Down
12 changes: 12 additions & 0 deletions LyricsScraperNET/Providers/LyricFind/LyricFindProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ public LyricFindProvider(ILogger<LyricFindProvider> logger, IOptionsSnapshot<Lyr
Ensure.ArgumentNotNull(options, nameof(options));
}

public LyricFindProvider(LyricFindOptions options)
: this(null, options)
{
Ensure.ArgumentNotNull(options, nameof(options));
}

public LyricFindProvider(IOptionsSnapshot<LyricFindOptions> options)
: this(null, options.Value)
{
Ensure.ArgumentNotNull(options, nameof(options));
}

#endregion

public override IExternalProviderOptions Options { get; }
Expand Down
12 changes: 12 additions & 0 deletions LyricsScraperNET/Providers/Musixmatch/MusixmatchProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ public MusixmatchProvider(ILogger<MusixmatchProvider> logger, IOptionsSnapshot<M
Ensure.ArgumentNotNull(options, nameof(options));
}

public MusixmatchProvider(MusixmatchOptions options)
: this(null, options)
{
Ensure.ArgumentNotNull(options, nameof(options));
}

public MusixmatchProvider(IOptionsSnapshot<MusixmatchOptions> options)
: this(null, options.Value)
{
Ensure.ArgumentNotNull(options, nameof(options));
}

#endregion

public override IExternalProviderOptions Options { get; }
Expand Down
14 changes: 14 additions & 0 deletions LyricsScraperNET/Providers/SongLyrics/SongLyricsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -42,6 +43,19 @@ public SongLyricsProvider(ILogger<SongLyricsProvider> logger, IOptionsSnapshot<S
Ensure.ArgumentNotNull(options, nameof(options));
}

public SongLyricsProvider(SongLyricsOptions options)
: this(null, options)
{
Ensure.ArgumentNotNull(options, nameof(options));
}

public SongLyricsProvider(IOptionsSnapshot<SongLyricsOptions> options)
: this(null, options.Value)
{
Ensure.ArgumentNotNull(options, nameof(options));
}

#endregion

public override IExternalProviderOptions Options { get; }

Expand Down
112 changes: 112 additions & 0 deletions Tests/LyricsScraperNET.UnitTest/LyricsScraperClientTests.cs
Original file line number Diff line number Diff line change
@@ -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<SearchRequest>();
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<SearchRequest>();

// 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<SearchRequest>();
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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8052b7c

Please sign in to comment.