2022-07-11 09:36:39 -05:00
|
|
|
using FluentAssertions;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Moq;
|
2022-07-08 16:32:25 -05:00
|
|
|
using server.Controllers.v1;
|
2022-07-11 09:36:39 -05:00
|
|
|
using server.Models;
|
2022-07-08 16:32:25 -05:00
|
|
|
using server.Persistence.Repositories;
|
2022-07-11 09:36:39 -05:00
|
|
|
using System.Net;
|
2022-07-08 16:32:25 -05:00
|
|
|
|
2022-07-11 12:46:12 -05:00
|
|
|
namespace server.tests.v1.UnitTests
|
2022-07-08 16:32:25 -05:00
|
|
|
{
|
|
|
|
|
public class SeasonsControllerUnitTests
|
|
|
|
|
{
|
|
|
|
|
private readonly Mock<ISeasonRepository> _mockRepo;
|
|
|
|
|
private readonly SeasonsController _controller;
|
|
|
|
|
|
|
|
|
|
public SeasonsControllerUnitTests()
|
|
|
|
|
{
|
|
|
|
|
_mockRepo = new Mock<ISeasonRepository>();
|
|
|
|
|
_controller = new SeasonsController(_mockRepo.Object);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetSeasonsAsync_AllSeasons_Returns200StatusCodeWithSeasonsCollection()
|
|
|
|
|
{
|
2022-07-11 09:36:39 -05:00
|
|
|
var seasons = new List<Season> { new Season(), new Season() };
|
|
|
|
|
|
|
|
|
|
_mockRepo
|
|
|
|
|
.Setup(repo => repo.GetSeasonsAsync())
|
|
|
|
|
.ReturnsAsync(seasons);
|
|
|
|
|
|
|
|
|
|
var response = await _controller.GetSeasonsAsync() as ObjectResult;
|
|
|
|
|
|
|
|
|
|
var data = response.Value as List<Season>;
|
|
|
|
|
|
|
|
|
|
_mockRepo.Verify(repo => repo.GetSeasonsAsync(), Times.Once());
|
|
|
|
|
|
|
|
|
|
response.Should().NotBeNull();
|
|
|
|
|
response.Should().BeOfType<OkObjectResult>();
|
|
|
|
|
response.StatusCode.Should().Be((int)HttpStatusCode.OK);
|
|
|
|
|
|
|
|
|
|
data.Should().NotBeNull();
|
|
|
|
|
data.Should().BeOfType<List<Season>>();
|
|
|
|
|
data.Should().HaveCount(2);
|
2022-07-08 16:32:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetSeasonsAsync_RepoThrowsException_Returns500StatusCodeWithProblemDetails()
|
|
|
|
|
{
|
2022-07-11 09:36:39 -05:00
|
|
|
_mockRepo
|
|
|
|
|
.Setup(repo => repo.GetSeasonsAsync())
|
|
|
|
|
.Throws(new Exception());
|
|
|
|
|
|
|
|
|
|
var response = await _controller.GetSeasonsAsync() as ObjectResult;
|
|
|
|
|
|
|
|
|
|
var details = response.Value;
|
|
|
|
|
|
|
|
|
|
_mockRepo.Verify(repo => repo.GetSeasonsAsync(), Times.Once());
|
|
|
|
|
|
|
|
|
|
response.Should().NotBeNull();
|
|
|
|
|
response.Should().BeOfType<ObjectResult>();
|
|
|
|
|
response.StatusCode.Should().Be((int)HttpStatusCode.InternalServerError);
|
|
|
|
|
|
|
|
|
|
details.Should().NotBeNull();
|
|
|
|
|
details.Should().BeOfType<ProblemDetails>();
|
2022-07-08 16:32:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetSeasonByNumberAsync_ValidSeasonNumber_Returns200StatusCodeWithSeason()
|
|
|
|
|
{
|
2022-07-11 11:10:52 -05:00
|
|
|
var seasonNumber = 1;
|
|
|
|
|
|
|
|
|
|
_mockRepo
|
|
|
|
|
.Setup(repo => repo.GetSeasonByNumberAsync(seasonNumber))
|
|
|
|
|
.ReturnsAsync(new Season());
|
|
|
|
|
|
|
|
|
|
var response = await _controller.GetSeasonByNumberAsync(seasonNumber) as ObjectResult;
|
|
|
|
|
|
|
|
|
|
var season = response.Value;
|
|
|
|
|
|
|
|
|
|
_mockRepo.Verify(repo => repo.GetSeasonByNumberAsync(It.IsAny<int>()), Times.Once());
|
|
|
|
|
|
|
|
|
|
response.Should().NotBeNull();
|
|
|
|
|
response.Should().BeOfType<OkObjectResult>();
|
|
|
|
|
response.StatusCode.Should().Be((int)HttpStatusCode.OK);
|
|
|
|
|
|
|
|
|
|
season.Should().NotBeNull();
|
2022-07-11 11:31:53 -05:00
|
|
|
season.Should().BeOfType<Season>();
|
2022-07-08 16:32:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetSeasonByNumberAsync_ValidSeasonNumberForNonExistentSeason_Returns404StatusCodeWithProblemDetails()
|
|
|
|
|
{
|
2022-07-11 11:10:52 -05:00
|
|
|
var seasonNumber = 20;
|
|
|
|
|
|
|
|
|
|
_mockRepo
|
|
|
|
|
.Setup(repo => repo.GetSeasonByNumberAsync(seasonNumber))
|
|
|
|
|
.ReturnsAsync(null as Season);
|
|
|
|
|
|
|
|
|
|
var response = await _controller.GetSeasonByNumberAsync(seasonNumber) as ObjectResult;
|
|
|
|
|
|
|
|
|
|
var details = response.Value;
|
|
|
|
|
|
|
|
|
|
_mockRepo.Verify(repo => repo.GetSeasonByNumberAsync(It.IsAny<int>()), Times.Once());
|
|
|
|
|
|
|
|
|
|
response.Should().NotBeNull();
|
|
|
|
|
response.Should().BeOfType<ObjectResult>();
|
|
|
|
|
response.StatusCode.Should().Be((int)HttpStatusCode.NotFound);
|
|
|
|
|
|
|
|
|
|
details.Should().NotBeNull();
|
|
|
|
|
details.Should().BeOfType<ProblemDetails>();
|
2022-07-08 16:32:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetSeasonByNumberAsync_RepoThrowsException_Returns500StatusCodeWithProblemDetails()
|
|
|
|
|
{
|
2022-07-11 09:36:39 -05:00
|
|
|
var seasonNumber = 1;
|
|
|
|
|
|
|
|
|
|
_mockRepo
|
|
|
|
|
.Setup(repo => repo.GetSeasonByNumberAsync(seasonNumber))
|
|
|
|
|
.Throws(new Exception());
|
|
|
|
|
|
|
|
|
|
var response = await _controller.GetSeasonByNumberAsync(seasonNumber) as ObjectResult;
|
|
|
|
|
|
|
|
|
|
var details = response.Value;
|
|
|
|
|
|
|
|
|
|
_mockRepo.Verify(repo => repo.GetSeasonByNumberAsync(It.IsAny<int>()), Times.Once());
|
|
|
|
|
|
|
|
|
|
response.Should().NotBeNull();
|
|
|
|
|
response.Should().BeOfType<ObjectResult>();
|
|
|
|
|
response.StatusCode.Should().Be((int)HttpStatusCode.InternalServerError);
|
|
|
|
|
|
|
|
|
|
details.Should().NotBeNull();
|
|
|
|
|
details.Should().BeOfType<ProblemDetails>();
|
2022-07-08 16:32:25 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|