-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added functionality to approve TV requests (#10)
- Loading branch information
Showing
12 changed files
with
548 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
src/PlexRequests.Core.UnitTests/Helpers/RequestHelperTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using System; | ||
using AutoFixture; | ||
using FluentAssertions; | ||
using PlexRequests.Core.Helpers; | ||
using PlexRequests.Repository.Enums; | ||
using PlexRequests.Repository.Models; | ||
using TestStack.BDDfy; | ||
using Xunit; | ||
|
||
namespace PlexRequests.Core.UnitTests.Helpers | ||
{ | ||
public class RequestHelperTests | ||
{ | ||
private readonly RequestHelper _underTest; | ||
|
||
private readonly Fixture _fixture; | ||
|
||
private Request _request; | ||
private Func<RequestStatuses> _commandAction; | ||
|
||
public RequestHelperTests() | ||
{ | ||
_fixture = new Fixture(); | ||
|
||
_underTest = new RequestHelper(); | ||
} | ||
|
||
[Theory] | ||
[InlineData(RequestStatuses.Approved)] | ||
[InlineData(RequestStatuses.Rejected)] | ||
[InlineData(RequestStatuses.Completed)] | ||
[InlineData(RequestStatuses.PendingApproval)] | ||
private void AggregateStatus_Sets_Status_When_All_Of_Same_Status(RequestStatuses status) | ||
{ | ||
this.Given(x => x.GivenAllRequestEpisodesOfStatus(status)) | ||
.When(x => x.WhenACommandActionIsCreated()) | ||
.Then(x => x.ThenOverallStatusIsCorrect(status)) | ||
.BDDfy(); | ||
} | ||
|
||
[Theory] | ||
[InlineData(RequestStatuses.Approved)] | ||
[InlineData(RequestStatuses.Rejected)] | ||
[InlineData(RequestStatuses.PendingApproval)] | ||
private void AggregateStatus_Sets_Status_To_PartialComplete_When_At_least_One_Episode_Complete(RequestStatuses allOtherEpisodeStatuses) | ||
{ | ||
this.Given(x => x.GivenOneEpisodeOfStatus(RequestStatuses.Completed, allOtherEpisodeStatuses)) | ||
.When(x => x.WhenACommandActionIsCreated()) | ||
.Then(x => x.ThenOverallStatusIsCorrect(RequestStatuses.PartialCompletion)) | ||
.BDDfy(); | ||
} | ||
|
||
[Theory] | ||
[InlineData(RequestStatuses.Rejected)] | ||
[InlineData(RequestStatuses.PendingApproval)] | ||
private void AggregateStatus_Sets_Status_To_PartialApproval_When_At_least_One_Episode_Approved(RequestStatuses allOtherEpisodeStatuses) | ||
{ | ||
this.Given(x => x.GivenOneEpisodeOfStatus(RequestStatuses.Approved, allOtherEpisodeStatuses)) | ||
.When(x => x.WhenACommandActionIsCreated()) | ||
.Then(x => x.ThenOverallStatusIsCorrect(RequestStatuses.PartialApproval)) | ||
.BDDfy(); | ||
} | ||
|
||
private void GivenAllRequestEpisodesOfStatus(RequestStatuses status) | ||
{ | ||
_request = _fixture.Create<Request>(); | ||
|
||
SetEpisodeStatuses(status); | ||
} | ||
|
||
private void GivenOneEpisodeOfStatus(RequestStatuses status, RequestStatuses allOtherEpisodeStatus) | ||
{ | ||
_request = _fixture.Create<Request>(); | ||
|
||
SetEpisodeStatuses(allOtherEpisodeStatus); | ||
|
||
_request.Seasons[0].Episodes[0].Status = status; | ||
} | ||
|
||
private void WhenACommandActionIsCreated() | ||
{ | ||
_commandAction = () => _underTest.CalculateAggregatedStatus(_request); | ||
} | ||
|
||
private void ThenOverallStatusIsCorrect(RequestStatuses expectedStatus) | ||
{ | ||
var actualStatus = _commandAction(); | ||
|
||
actualStatus.Should().Be(expectedStatus); | ||
} | ||
|
||
private void SetEpisodeStatuses(RequestStatuses status) | ||
{ | ||
foreach (var season in _request.Seasons) | ||
{ | ||
foreach (var episode in season.Episodes) | ||
{ | ||
episode.Status = status; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using PlexRequests.Repository.Enums; | ||
using PlexRequests.Repository.Models; | ||
|
||
namespace PlexRequests.Core.Helpers | ||
{ | ||
public interface IRequestHelper | ||
{ | ||
RequestStatuses CalculateAggregatedStatus(Request request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
using System.Linq; | ||
using PlexRequests.Repository.Enums; | ||
using PlexRequests.Repository.Models; | ||
|
||
namespace PlexRequests.Core.Helpers | ||
{ | ||
public class RequestHelper : IRequestHelper | ||
{ | ||
public RequestStatuses CalculateAggregatedStatus(Request request) | ||
{ | ||
var possibleStatus = Enum.GetValues(typeof(RequestStatuses)).Cast<RequestStatuses>(); | ||
var statusCounts = possibleStatus.ToDictionary(status => status, count => 0); | ||
|
||
foreach (var season in request.Seasons) | ||
{ | ||
foreach (var episode in season.Episodes) | ||
{ | ||
statusCounts[episode.Status]++; | ||
} | ||
} | ||
|
||
var totalStatusCounts = statusCounts.Sum(x => x.Value); | ||
var overallStatus = RequestStatuses.PendingApproval; | ||
|
||
foreach (var (status, count) in statusCounts.OrderBy(x => x.Key)) | ||
{ | ||
if (count == totalStatusCounts) | ||
{ | ||
overallStatus = status; | ||
break; | ||
} | ||
|
||
if (count == 0 || status == RequestStatuses.Rejected) | ||
{ | ||
continue; | ||
} | ||
|
||
overallStatus = status == RequestStatuses.Approved ? RequestStatuses.PartialApproval : RequestStatuses.PartialCompletion; | ||
} | ||
|
||
return overallStatus; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.