Skip to content

Commit

Permalink
Merge pull request #5 from Fazzani/feature/add-more-unit-tests
Browse files Browse the repository at this point in the history
feat: Add more unit tests
  • Loading branch information
Fazzani authored Dec 31, 2024
2 parents f4d0126 + 9c0cbb9 commit 9e8d9e9
Show file tree
Hide file tree
Showing 17 changed files with 257 additions and 49 deletions.
3 changes: 2 additions & 1 deletion .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ Describe the big picture of your changes here to communicate to the maintainers

## Types of changes

What types of changes does your code introduce to Appium?
What types of changes does your code introduce to Proxarr ?
_Put an `x` in the boxes that apply_

- [ ] Bugfix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] Documentation Update (if none of the other choices apply)
- [ ] CI/CD or unit tests improvements

## Further comments

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
hide_complexity: true
indicators: true
output: both
thresholds: '45 45'
thresholds: '95 95'

- name: Add Coverage PR Comment
uses: marocchino/sticky-pull-request-comment@v2
Expand Down
21 changes: 17 additions & 4 deletions src/Proxarr.Api.Tests/Proxarr.Api.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,26 @@
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<AssemblyAttribute Include="System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="coverlet.collector" Version="6.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.msbuild" Version="6.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
27 changes: 21 additions & 6 deletions src/Proxarr.Api.Tests/QualifierControllerTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using FluentAssertions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Moq;
using Proxarr.Api.Controllers;
Expand Down Expand Up @@ -33,7 +34,21 @@ public async Task Qualified_ShouldReturnOk_WhenEventTypeIsTest()
var result = await _controller.Qualified(media, cancellationToken);

// Assert
Assert.IsType<OkResult>(result);
result.Should().BeOfType<OkResult>();
}

[Fact]
public async Task Qualified_ShouldReturnOk_WhenEventTypeMovieAdded()
{
// Arrange
var media = new MovieAdded { EventType = "MovieAdded", ApplicationUrl = "http://localhost", Movie=default };
var cancellationToken = new CancellationToken();

// Act
var result = await _controller.Qualified(media, cancellationToken);

// Assert
result.Should().BeOfType<OkResult>();
}

[Fact]
Expand All @@ -49,7 +64,7 @@ public async Task Qualified_ShouldReturnNotFound_WhenMovieNotFound()
var result = await _controller.Qualified(movie, cancellationToken);

// Assert
Assert.IsType<NotFoundResult>(result);
result.Should().BeOfType<NotFoundResult>();
}

[Fact]
Expand All @@ -65,7 +80,7 @@ public async Task Qualified_ShouldReturnNotFound_WhenTvNotFound()
var result = await _controller.Qualified(tvAdded, cancellationToken);

// Assert
Assert.IsType<NotFoundResult>(result);
result.Should().BeOfType<NotFoundResult>();
}

[Fact]
Expand All @@ -79,7 +94,7 @@ public async Task Qualified_ShouldReturnBadRequest_WhenMediaIsInvalid()
var result = await _controller.Qualified(media, cancellationToken);

// Assert
Assert.IsType<BadRequestResult>(result);
result.Should().BeOfType<BadRequestResult>();
}

[Fact]
Expand All @@ -95,7 +110,7 @@ public async Task Qualified_ShouldReturnOk_WhenMediaIsValid()
var result = await _controller.Qualified(tvAdded, cancellationToken);

// Assert
Assert.IsType<OkResult>(result);
result.Should().BeOfType<OkResult>();
}
}
}
78 changes: 75 additions & 3 deletions src/Proxarr.Api.Tests/RadarrServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using TMDbLib.Objects.General;
using TMDbLib.Objects.Movies;
using Movie = Proxarr.Api.Models.Movie;
using FluentAssertions;

namespace Proxarr.Api.Tests
{
Expand All @@ -32,7 +33,7 @@ public RadarrServiceTests()
var appConfig = new AppConfiguration
{
Clients = [new ClientConfiguration { Application = "Sonarr", BaseUrl = "http://localhost", ApiKey = "fake_api_key" }],
WatchProviders = "US:Netflix, FR: Amazon Prime Video",
WatchProviders = "US:Netflix, FR: Amazon Prime Video, US:Netflix with ads",
};

_appConfigMock.Setup(x => x.Value).Returns(appConfig);
Expand All @@ -42,7 +43,14 @@ public RadarrServiceTests()
}

[Fact]
public async Task Qualify_ShouldReturnNotFound_WhenMovieNotFound()
public async Task Ctor_ShouldThrowArgNullEx_WhenAppConfigurationNull()
{
Action act = () => new RadarrService(null, null, null, null);
act.Should().Throw<ArgumentNullException>().WithParameterName("appConfiguration");
}

[Fact]
public async Task Qualify_ShouldReturnNotFound_WhenMovieNotFoundIntoRadarr()
{
// Arrange
var movieAdded = new MovieAdded
Expand All @@ -66,6 +74,31 @@ public async Task Qualify_ShouldReturnNotFound_WhenMovieNotFound()
Assert.Equal("NotFound", result);
}

[Fact]
public async Task Qualify_ShouldReturnNotFound_WhenMovieNotFoundIntoTMDB()
{
// Arrange
var movieAdded = new MovieAdded
{
ApplicationUrl = "http://localhost",
EventType = "FULL_SCAN",
Movie = new Movie { Id = 1, TmdbId = 123, Title = "Test Movie" }
};
var cancellationToken = new CancellationToken();

_tmdbClientMock.Setup(x => x.GetMovieAsync(123, MovieMethods.WatchProviders, cancellationToken))
.ReturnsAsync((TMDbLib.Objects.Movies.Movie)null);

_radarrClientMock.Setup(x => x.MovieGET2Async(1, cancellationToken))
.ReturnsAsync((MovieResource)null);

// Act
var result = await _radarrService.Qualify(movieAdded, cancellationToken);

// Assert
Assert.Equal("NotFound", result);
}

[Fact]
public async Task Qualify_ShouldUpdateTags_WhenMovieFound()
{
Expand All @@ -82,7 +115,7 @@ public async Task Qualify_ShouldUpdateTags_WhenMovieFound()
{
Results = new Dictionary<string, WatchProviders>
{
{ "US", new WatchProviders { Free = [new WatchProviderItem { ProviderName = "Netflix" }] } }
{ "US", new WatchProviders { Free = [new WatchProviderItem { ProviderName = "Netflix" }], FlatRate = [new WatchProviderItem { ProviderName = "Netflix with Ads" }] } }
}
};

Expand Down Expand Up @@ -200,5 +233,44 @@ public async Task Qualify_Should_BeTagged_When_MatchedWatchProvider()
Assert.Single(movieResource.Tags);
Assert.Contains(movieResource.Tags, x => x == _qualifyTag.Id);
}

[Fact]
public async Task Qualify_Should_BeTagged_When_NoWatchProviders()
{
// Arrange
var tvAdded = new MovieAdded
{
ApplicationUrl = "http://localhost",
EventType = "MovieAdded",
Movie = new Movie { Id = 1, TmdbId = 123, Title = "Test Movie" },
};
var cancellationToken = new CancellationToken();


var movieResource = new MovieResource { Id = 1, Title = "Test Series", Tags = [] };

_tmdbClientMock.Setup(x => x.GetMovieAsync(123, MovieMethods.WatchProviders, cancellationToken))
.ReturnsAsync(new TMDbLib.Objects.Movies.Movie());

_radarrClientMock.Setup(x => x.MovieGET2Async(1, cancellationToken))
.ReturnsAsync(movieResource);

_radarrClientMock.Setup(x => x.TagAllAsync(cancellationToken))
.ReturnsAsync([_qualifyTag]);

var tagResource = new TagResource { Id = 1, Label = "Netflix" };

_radarrClientMock.Setup(x => x.TagPOSTAsync(It.IsAny<TagResource>(), cancellationToken))
.ReturnsAsync(tagResource);

// Act
var result = await _radarrService.Qualify(tvAdded, cancellationToken);

// Assert
_radarrClientMock.Verify(x => x.MoviePUTAsync(false, "1", movieResource, cancellationToken), Times.Once);
Assert.Empty(result);
Assert.Single(movieResource.Tags);
Assert.Contains(movieResource.Tags, x => x == _qualifyTag.Id);
}
}
}
35 changes: 35 additions & 0 deletions src/Proxarr.Api.Tests/ScheduledServiceExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Proxarr.Api.Core;
using Proxarr.Api.HostedServices;

namespace Proxarr.Api.Tests
{
public class ScheduledServiceExtensionsTests
{
[Fact]
public void AddCronJob_ShouldThrowNullException_WhenOptionsNull()
{
IServiceCollection services = new ServiceCollection();
Action act = () => services.AddCronJob<FullScanHostedService>(null);
act.Should().Throw<ArgumentNullException>().WithParameterName("options").WithMessage("Please provide Schedule Configurations. (Parameter 'options')");
}

[Fact]
public void AddCronJob_ShouldThrowNullException_WhenCronIsNull()
{
IServiceCollection services = new ServiceCollection();
Action act = () => services.AddCronJob<FullScanHostedService>(x => x.CronExpression = string.Empty);
act.Should().Throw<ArgumentNullException>().WithParameterName("options").WithMessage("Empty Cron Expression is not allowed. (Parameter 'options')");
}

[Fact]
public void AddCronJob_ShouldAddHostedService()
{
IServiceCollection services = new ServiceCollection();
Action act = () => services.AddCronJob<FullScanHostedService>(x => x.CronExpression = "* * * * *");
act.Should().NotThrow();
}
}
}
Loading

0 comments on commit 9e8d9e9

Please sign in to comment.