add tactics tests
continuous-integration/drone/push Build is passing Details

tests
maxime 1 year ago
parent 5e142f4604
commit 4a54e1fe6f

@ -16,7 +16,8 @@ public class TacticController(ITacticService service, IContextAccessor accessor)
public record UpdateNameRequest( public record UpdateNameRequest(
[StringLength(50, MinimumLength = 1)] [StringLength(50, MinimumLength = 1)]
[Name] [Name]
string Name); string Name
);
[HttpPut("/tactics/{tacticId:int}/name")] [HttpPut("/tactics/{tacticId:int}/name")]
[Authorize] [Authorize]
@ -81,12 +82,11 @@ public class TacticController(ITacticService service, IContextAccessor accessor)
{ {
var userId = accessor.CurrentUserId(HttpContext); var userId = accessor.CurrentUserId(HttpContext);
var courtType = req.CourtType switch if (!Enum.TryParse<CourtType>(req.CourtType, true, out var courtType))
{ {
"PLAIN" => CourtType.Plain, // unreachable if called by ASP
"HALF" => CourtType.Half, throw new ArgumentOutOfRangeException("for req.CourtType");
_ => throw new ArgumentOutOfRangeException() //unreachable }
};
var id = await service.AddTactic(userId, req.Name, courtType); var id = await service.AddTactic(userId, req.Name, courtType);
return new CreateNewResponse(id); return new CreateNewResponse(id);
@ -118,10 +118,10 @@ public class TacticController(ITacticService service, IContextAccessor accessor)
var json = await service.GetTacticStepContent(tacticId, stepId); var json = await service.GetTacticStepContent(tacticId, stepId);
return json != null ? Ok(JsonSerializer.Deserialize<object>(json)) : NotFound(); return json != null ? Ok(JsonSerializer.Deserialize<object>(json)) : NotFound();
} }
[HttpDelete("/tactics/{tacticId:int}/steps/{stepId:int}")] [HttpDelete("/tactics/{tacticId:int}/steps/{stepId:int}")]
[Authorize] [Authorize]
public async Task<IActionResult> RemoveStepContent(int tacticId, int stepId) public async Task<IActionResult> RemoveStep(int tacticId, int stepId)
{ {
var userId = accessor.CurrentUserId(HttpContext); var userId = accessor.CurrentUserId(HttpContext);
@ -140,15 +140,14 @@ public class TacticController(ITacticService service, IContextAccessor accessor)
[HttpPut("/tactics/{tacticId:int}/steps/{stepId:int}")] [HttpPut("/tactics/{tacticId:int}/steps/{stepId:int}")]
[Authorize] [Authorize]
public async Task<IActionResult> SaveStepContent(int tacticId, int stepId, [FromBody] SaveStepContentRequest req) public async Task<IActionResult> SaveStepContent(int tacticId, int stepId, [FromBody] SaveStepContentRequest req)
{ {
var userId = accessor.CurrentUserId(HttpContext); var userId = accessor.CurrentUserId(HttpContext);
if (!await service.HasAnyRights(userId, tacticId)) if (!await service.HasAnyRights(userId, tacticId))
{ {
return Unauthorized(); return Unauthorized();
} }
await service.SetTacticStepContent(tacticId, stepId, JsonSerializer.Serialize(req.Content)); var found = await service.SetTacticStepContent(tacticId, stepId, JsonSerializer.Serialize(req.Content));
return Ok(); return found ? Ok() : NotFound();
} }
} }

@ -1,3 +1,4 @@
using System.ComponentModel.DataAnnotations;
using Model; using Model;
namespace AppContext.Entities; namespace AppContext.Entities;
@ -15,5 +16,6 @@ public class TacticStepEntity
public required int? ParentId { get; set; } public required int? ParentId { get; set; }
public TacticStepEntity? Parent { get; set; } public TacticStepEntity? Parent { get; set; }
[MaxLength(2_000_000)]
public required string JsonContent { get; set; } public required string JsonContent { get; set; }
} }

@ -1,4 +1,3 @@
using System.Collections;
using AppContext.Entities; using AppContext.Entities;
using Converters; using Converters;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@ -59,7 +58,8 @@ public class DbTacticService(AppContext.AppContext context) : ITacticService
return false; return false;
entity.Name = name; entity.Name = name;
return await context.SaveChangesAsync() > 0; await context.SaveChangesAsync();
return true;
} }
public async Task<bool> SetTacticStepContent(int tacticId, int stepId, string json) public async Task<bool> SetTacticStepContent(int tacticId, int stepId, string json)
@ -70,7 +70,8 @@ public class DbTacticService(AppContext.AppContext context) : ITacticService
return false; return false;
entity.JsonContent = json; entity.JsonContent = json;
return await context.SaveChangesAsync() > 0; await context.SaveChangesAsync();
return true;
} }
public async Task<string?> GetTacticStepContent(int tacticId, int stepId) public async Task<string?> GetTacticStepContent(int tacticId, int stepId)
@ -108,6 +109,11 @@ public class DbTacticService(AppContext.AppContext context) : ITacticService
public async Task<int?> AddTacticStep(int tacticId, int parentStepId, string initialJson) public async Task<int?> AddTacticStep(int tacticId, int parentStepId, string initialJson)
{ {
var parentExists = context.TacticSteps.Any(t => t.TacticId == tacticId && t.Id == parentStepId);
if (!parentExists)
return null;
var tactic = await context.Tactics.FirstOrDefaultAsync(t => t.Id == tacticId); var tactic = await context.Tactics.FirstOrDefaultAsync(t => t.Id == tacticId);
if (tactic == null) if (tactic == null)
{ {

@ -39,7 +39,7 @@ public class StubAppContext(DbContextOptions<AppContext> options) : AppContext(o
.HasKey("TeamId", "UserId"); .HasKey("TeamId", "UserId");
builder.Entity<TacticEntity>() builder.Entity<TacticEntity>()
.HasData(new TacticEntity() .HasData(new TacticEntity
{ {
Id = 1, Id = 1,
Name = "New tactic", Name = "New tactic",
@ -52,7 +52,7 @@ public class StubAppContext(DbContextOptions<AppContext> options) : AppContext(o
.HasData(new TacticStepEntity .HasData(new TacticStepEntity
{ {
Id = 1, Id = 1,
JsonContent = "{}", JsonContent = "{\"components\": []}",
TacticId = 1, TacticId = 1,
ParentId = null ParentId = null
}); });

@ -1,8 +1,8 @@
using API.Controllers; using API.Controllers;
using API.DTO; using API.DTO;
using AppContext.Entities;
using DbServices; using DbServices;
using FluentAssertions; using FluentAssertions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.Sqlite; using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Model; using Model;
@ -39,7 +39,7 @@ public class TacticsControllerTest
var tactic = await context.Tactics.FindAsync(1); var tactic = await context.Tactics.FindAsync(1);
tactic.Name.Should().BeEquivalentTo("Stade de France"); tactic.Name.Should().BeEquivalentTo("Stade de France");
result = await controller.UpdateName(-1, new("Stade de France")); result = await controller.UpdateName(-1, new("Stade de France"));
result.Should().BeEquivalentTo(controller.Unauthorized()); result.Should().BeEquivalentTo(controller.Unauthorized());
} }
@ -49,18 +49,92 @@ public class TacticsControllerTest
{ {
var (controller, context) = GetController(1); var (controller, context) = GetController(1);
var result = await controller.GetTacticInfo(1); var result = await controller.GetTacticInfo(1);
result.Should().BeEquivalentTo(controller.Ok(new Tactic(1, "New tactic", 1, CourtType.Plain, new DateTime(2024, 5, 31)).ToDto())); result.Should()
.BeEquivalentTo(controller.Ok(new Tactic(1, "New tactic", 1, CourtType.Plain, new DateTime(2024, 5, 31))
.ToDto()));
result = await controller.GetTacticInfo(100); result = await controller.GetTacticInfo(100);
result.Should().BeEquivalentTo(controller.Unauthorized()); result.Should().BeEquivalentTo(controller.Unauthorized());
} }
[Fact] [Fact]
public async void GetTacticStepsRoot() public async void GetTacticStepsRootTest()
{ {
var (controller, context) = GetController(1); var (controller, context) = GetController(1);
var result = await controller.GetTacticStepsRoot(1); var result = await controller.GetTacticStepsRoot(1);
result.Should().BeEquivalentTo(controller.Ok(new TacticController.GetTacticStepsTreeResponse(new TacticStep(1, null, [], "{}").ToDto()))); result.Should()
.BeEquivalentTo(controller.Ok(
new TacticController.GetTacticStepsTreeResponse(new TacticStep(1, null, [], "{}").ToDto())));
}
[Fact]
public async void CreateNewTest()
{
var (controller, context) = GetController(1);
var result = await controller.CreateNew(new("Test Tactic", "pLaIn"));
result.Should().BeEquivalentTo(new TacticController.CreateNewResponse(2));
var tactic = await context.Tactics.FirstOrDefaultAsync(e => e.Id == 2);
tactic.Should().NotBeNull();
tactic!.Name.Should().BeEquivalentTo("Test Tactic");
tactic.OwnerId.Should().Be(1);
tactic.Type.Should().Be(CourtType.Plain);
}
[Fact]
public async void AddStepTest()
{
var (controller, context) = GetController(1);
var result = await controller.AddStep(1, new(1, "{components: []}"));
result.Should().BeEquivalentTo(controller.Ok(new TacticController.AddStepResponse(2)));
var tactic = await context.TacticSteps.FirstOrDefaultAsync(e => e.Id == 2);
tactic.Should().NotBeNull();
tactic!.Id.Should().Be(2);
tactic.ParentId.Should().Be(1);
tactic.TacticId.Should().Be(1);
// if tactic does not exists
result = await controller.AddStep(100, new(1, "hello"));
result.Should().BeEquivalentTo(controller.NotFound());
// if step does not exists
result = await controller.AddStep(1, new(10, "hello"));
result.Should().BeEquivalentTo(controller.NotFound());
}
[Fact]
public async void RemoveStepTest()
{
var (controller, context) = GetController(1);
var result = await controller.RemoveStep(1, 1);
result.Should().BeEquivalentTo(controller.Ok());
var tactic = await context.TacticSteps.FirstOrDefaultAsync(e => e.Id == 1);
tactic.Should().BeNull();
// if tactic does not exists
result = await controller.RemoveStep(100, 1);
result.Should().BeEquivalentTo(controller.Unauthorized());
// if step does not exists
result = await controller.RemoveStep(1, 10);
result.Should().BeEquivalentTo(controller.NotFound());
}
[Fact]
public async void GetStepContentTest()
{
var (controller, context) = GetController(1);
(await controller.GetStepContent(1, 1)).Should().BeAssignableTo(controller.Ok("").GetType());
(await controller.GetStepContent(10, 1)).Should().BeEquivalentTo(controller.Unauthorized());
(await controller.GetStepContent(1, 10)).Should().BeEquivalentTo(controller.NotFound());
}
[Fact]
public async void SaveStepContentTest()
{
var (controller, context) = GetController(1);
(await controller.SaveStepContent(1, 1, new(new object()))).Should().BeEquivalentTo(controller.Ok());
(await controller.SaveStepContent(10, 1, new (new object()))).Should().BeEquivalentTo(controller.Unauthorized());
(await controller.SaveStepContent(1, 10, new (new object()))).Should().BeEquivalentTo(controller.NotFound());
} }
} }

@ -1,4 +1,5 @@
using API.Controllers; using API.Controllers;
using API.DTO;
using DbServices; using DbServices;
using FluentAssertions; using FluentAssertions;
using Microsoft.Data.Sqlite; using Microsoft.Data.Sqlite;
@ -26,7 +27,7 @@ public class UsersControllerTest
new DbTacticService(context), new DbTacticService(context),
new ManualContextAccessor(userId) new ManualContextAccessor(userId)
); );
return controller; return controller;
} }
@ -44,6 +45,9 @@ public class UsersControllerTest
{ {
var controller = GetUserController(1); var controller = GetUserController(1);
var result = await controller.GetUserData(); var result = await controller.GetUserData();
result.Should().BeEquivalentTo(new UsersController.GetUserDataResponse([], [])); result.Should().BeEquivalentTo(new UsersController.GetUserDataResponse(
[],
[new TacticDto(1, "New tactic", 1, "PLAIN", 1717106400000L)]
));
} }
} }
Loading…
Cancel
Save