Skip to content

Commit

Permalink
add test for CompetitiveEventAccountingType service and controller, u…
Browse files Browse the repository at this point in the history
…pdate tests for CompetitiveEvent service
  • Loading branch information
Tetyana-K committed Dec 13, 2024
1 parent 07fa775 commit d3609b9
Show file tree
Hide file tree
Showing 5 changed files with 356 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
using Moq;
using NUnit.Framework;
using OutOfSchool.BusinessLogic;
using OutOfSchool.BusinessLogic.Enums;
using OutOfSchool.BusinessLogic.Models.CompetitiveEvent;
using OutOfSchool.BusinessLogic.Services;
using OutOfSchool.WebApi.Controllers.V1;

namespace OutOfSchool.WebApi.Tests.Controllers;

[TestFixture]
public class CompetitiveEventAccountingTypeControllerTests
{
private CompetitiveEventAccountingTypeController controller;
private Mock<ICompetitiveEventAccountingTypeService> service;
private IMapper mapper;

[SetUp]
public void Setup()
{
service = new Mock<ICompetitiveEventAccountingTypeService>();
var localizer = new Mock<IStringLocalizer<SharedResource>>();
controller = new CompetitiveEventAccountingTypeController(service.Object);
}

[Test]
public async Task GetAll_WhenValidLocalization_ReturnsOkWithResult()
{
// Arrange
var localization = LocalizationType.Ua;
var accountingTypesDto = FakeCompetitiveEventAccountingTypesDto();

service.Setup(s => s.GetAll(localization)).ReturnsAsync(accountingTypesDto);

// Act
var result = await controller.GetAll();

// Assert
Assert.IsInstanceOf<OkObjectResult>(result);
var okResult = result as OkObjectResult;
Assert.IsNotNull(okResult);
Assert.AreEqual(StatusCodes.Status200OK, okResult.StatusCode);
Assert.AreEqual(accountingTypesDto, okResult.Value);
}

[Test]
public async Task GetAll_WhenEmptyCollection_ReturnsNoContent()
{
// Arrange
service.Setup(s => s.GetAll(It.IsAny<LocalizationType>()))
.ReturnsAsync(new List<CompetitiveEventAccountingTypeDto>());

// Act
var result = await controller.GetAll().ConfigureAwait(false);

// Assert
Assert.IsInstanceOf<NoContentResult>(result);
}

private IEnumerable<CompetitiveEventAccountingTypeDto> FakeCompetitiveEventAccountingTypesDto()
{
return new List<CompetitiveEventAccountingTypeDto>()
{
new CompetitiveEventAccountingTypeDto { Id = 1, Title = "Тип 1"},
new CompetitiveEventAccountingTypeDto { Id = 2, Title = "Тип 2"},
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
using Moq;
using NUnit.Framework;
using OutOfSchool.BusinessLogic;
using OutOfSchool.BusinessLogic.Models.CompetitiveEvent;
using OutOfSchool.BusinessLogic.Services;
using OutOfSchool.BusinessLogic.Util;
using OutOfSchool.BusinessLogic.Util.Mapping;
using OutOfSchool.Tests.Common;
using OutOfSchool.Services.Enums;
using OutOfSchool.WebApi.Controllers.V1;

namespace OutOfSchool.WebApi.Tests.Controllers;
Expand All @@ -25,13 +22,6 @@ internal class CompetitiveEventControllerTests
private Mock<ICompetitiveEventService> competitiveEventService;
private Mock<IStringLocalizer<SharedResource>> localizer;
private IEnumerable<CompetitiveEventDto> competitiveEvents;
IMapper mapper;

[OneTimeSetUp]
public void OneTimeSetup()
{
this.mapper = TestHelper.CreateMapperInstanceOfProfileTypes<CommonProfile, MappingProfile>(); // is it enough?
}

[SetUp]
public void Setup()
Expand All @@ -44,7 +34,6 @@ public void Setup()
controller = new CompetitiveEventController(
competitiveEventService.Object,
localizer.Object);

}

[Test]
Expand Down Expand Up @@ -90,11 +79,14 @@ public async Task Create_WhenModelIsValid_ReturnsCreatedAtActionResult()
{
new JudgeDto()
{
FirstName ="Judge A"
FirstName ="Judge A",
Description = "Description A",
IsChiefJudge = true,
},
new JudgeDto()
{
FirstName ="Judge B"
FirstName ="Judge B",
Description = "Description B"
},
}
};
Expand All @@ -116,18 +108,48 @@ public async Task Create_WhenModelIsValid_ReturnsCreatedAtActionResult()
var competitiveEventResult = result.Value as CompetitiveEventDto;
Assert.That(result, Is.Not.Null);

Assert.AreEqual(inputDto.Title, competitiveEventResult.Title);
Assert.AreEqual(inputDto.ScheduledStartTime, competitiveEventResult.ScheduledStartTime);
Assert.AreEqual(inputDto.ScheduledEndTime, competitiveEventResult.ScheduledEndTime);

// var expectedDto = mapper.Map<CompetitiveEventDto>(inputDto); // does not work mapping
// AssertCompetitiveEventPropertiesAreEqual(expectedDto, competitiveEventResult);

AssertCompetitiveEventPropertiesAreEqual(inputDto, competitiveEventResult);
AssertJudgesAreEqual(inputDto.Judges, competitiveEventResult.Judges);

Assert.AreEqual((int)HttpStatusCode.Created, result.StatusCode);
}

[Test]
public async Task Create_WhenDtoIsNull_ReturnsBadRequest()
{
// Act
var result = await controller.Create(null);

// Assert
Assert.IsInstanceOf<BadRequestObjectResult>(result);
var badRequestResult = result as BadRequestObjectResult;
Assert.IsNotNull(badRequestResult);
Assert.AreEqual("The request body is empty.", badRequestResult.Value);
}

[Test]
public async Task Create_WhenJudgesAreInvalid_ReturnsBadRequest()
{
// Arrange
var invalidDto = new CompetitiveEventCreateDto
{
Judges = new List<JudgeDto>
{
new JudgeDto { IsChiefJudge = true },
new JudgeDto { IsChiefJudge = true },
},
};

// Act
var result = await controller.Create(invalidDto);

// Assert
Assert.IsInstanceOf<BadRequestObjectResult>(result);
var badRequestResult = result as BadRequestObjectResult;
Assert.IsNotNull(badRequestResult);
Assert.AreEqual("A competitive event can have no more than one chief judge.", badRequestResult.Value);
}

[Test]
public async Task Update_WhenValidDto_ReturnsOkObjectResult()
{
Expand All @@ -141,8 +163,8 @@ public async Task Update_WhenValidDto_ReturnsOkObjectResult()
ScheduledEndTime = DateTime.UtcNow.AddHours(2),
Judges = new List<JudgeDto>
{
new JudgeDto { FirstName = "Judge A", LastName = "LastName A" },
new JudgeDto { FirstName = "Judge B", LastName = "LastName B" }
new JudgeDto { FirstName = "Judge A", LastName = "LastName A", Gender = Gender.Male },
new JudgeDto { FirstName = "Judge B", LastName = "LastName B", Gender = Gender.Female }
}
};
competitiveEventService.Setup(s => s.Update(inputDto)).ReturnsAsync(new CompetitiveEventDto
Expand All @@ -163,13 +185,7 @@ public async Task Update_WhenValidDto_ReturnsOkObjectResult()
var competitiveEventResult = (result as OkObjectResult).Value as CompetitiveEventDto;
Assert.That(competitiveEventResult, Is.Not.Null);

Assert.AreEqual(inputDto.Title, competitiveEventResult.Title);
Assert.AreEqual(inputDto.ScheduledStartTime, competitiveEventResult.ScheduledStartTime);
Assert.AreEqual(inputDto.ScheduledEndTime, competitiveEventResult.ScheduledEndTime);

// var expectedDto = mapper.Map<CompetitiveEventDto>(inputDto); // does not work mapping : could not find needed map

//AssertCompetitiveEventPropertiesAreEqual(expectedDto, competitiveEventResult);
AssertCompetitiveEventPropertiesAreEqual(inputDto, competitiveEventResult);
AssertJudgesAreEqual(inputDto.Judges, competitiveEventResult.Judges);
}

Expand All @@ -187,7 +203,25 @@ public async Task Delete_WhenIdIsValid_ReturnsNoContentResult()
Assert.IsInstanceOf<NoContentResult>(response);
}

private void AssertCompetitiveEventPropertiesAreEqual(CompetitiveEventDto expected, CompetitiveEventDto actual)
[Test]
public async Task Delete_WhenEntityExists_DeletesEntityAndReturnsNoContent()
{
// Arrange
var id = Guid.NewGuid();
var existingEvent = new CompetitiveEventDto { Id = id, Title = "Test Event" };

competitiveEventService.Setup(s => s.GetById(id)).ReturnsAsync(existingEvent);
competitiveEventService.Setup(s => s.Delete(id)).Returns(Task.CompletedTask);

// Act
var result = await controller.Delete(id);

// Assert
Assert.IsInstanceOf<NoContentResult>(result);
competitiveEventService.Verify(s => s.Delete(id), Times.Once);
}

private void AssertCompetitiveEventPropertiesAreEqual(CompetitiveEventCreateDto expected, CompetitiveEventDto actual)
{
Assert.That(actual, Is.Not.Null, "CompetitiveEventDto should not be null");
Assert.AreEqual(expected.Title, actual.Title, "Title mismatch");
Expand All @@ -204,8 +238,12 @@ private void AssertJudgesAreEqual(List<JudgeDto> expectedJudges, List<JudgeDto>
{
Assert.AreEqual(expectedJudges[i].FirstName, actualJudges[i].FirstName, $"Judge #{i + 1} FirstName mismatch");
Assert.AreEqual(expectedJudges[i].LastName, actualJudges[i].LastName, $"Judge #{i + 1} LastName mismatch");
Assert.AreEqual(expectedJudges[i].Gender, actualJudges[i].Gender, $"Judge #{i + 1} Gender mismatch");
Assert.AreEqual(expectedJudges[i].MiddleName, actualJudges[i].MiddleName, $"Judge #{i + 1} MiddleName mismatch");
Assert.AreEqual(expectedJudges[i].Description, actualJudges[i].Description, $"Judge #{i + 1} Description mismatch");
}
}

private IEnumerable<CompetitiveEventDto> FakeCompetitiveEvents()
{
return new List<CompetitiveEventDto>()
Expand All @@ -217,8 +255,8 @@ private IEnumerable<CompetitiveEventDto> FakeCompetitiveEvents()
Description = "Test1",
Judges = new List<JudgeDto>
{
new JudgeDto { Id = Guid.NewGuid(), FirstName = "Judge A", LastName = "LastName A" },
new JudgeDto { Id = Guid.NewGuid(), FirstName = "Judge B", LastName = "LastName B" }
new JudgeDto { Id = Guid.NewGuid(), FirstName = "Judge A", MiddleName="A", LastName = "LastName A", Gender = Gender.Male },
new JudgeDto { Id = Guid.NewGuid(), FirstName = "Judge B", MiddleName = "B", LastName = "LastName B", Gender = Gender.Female }
}
},
new CompetitiveEventDto
Expand All @@ -228,9 +266,9 @@ private IEnumerable<CompetitiveEventDto> FakeCompetitiveEvents()
Description = "Test2",
Judges = new List<JudgeDto>
{
new JudgeDto { Id = Guid.NewGuid(), FirstName = "Judge C", LastName = "LastName C" },
new JudgeDto { Id = Guid.NewGuid(), FirstName = "Judge D", LastName = "LastName D" },
new JudgeDto { Id = Guid.NewGuid(), FirstName = "Judge E", LastName = "LastName E" },
new JudgeDto { Id = Guid.NewGuid(), FirstName = "Judge C", LastName = "LastName C", Gender = Gender.Male },
new JudgeDto { Id = Guid.NewGuid(), FirstName = "Judge D", LastName = "LastName D", Gender = Gender.Female },
new JudgeDto { Id = Guid.NewGuid(), FirstName = "Judge E", LastName = "LastName E", Gender = Gender.Male },
}
},
new CompetitiveEventDto
Expand All @@ -240,7 +278,7 @@ private IEnumerable<CompetitiveEventDto> FakeCompetitiveEvents()
Description = "Test3",
Judges = new List<JudgeDto>
{
new JudgeDto { Id = Guid.NewGuid(), FirstName = "Judge F", LastName = "LastName F" },
new JudgeDto { Id = Guid.NewGuid(), FirstName = "Judge F", LastName = "LastName F", Description= "Description F" },
}
},
};
Expand Down
Loading

0 comments on commit d3609b9

Please sign in to comment.