196 lines
7.2 KiB
C#
196 lines
7.2 KiB
C#
using ManagerService.Controllers;
|
|
using ManagerService.Data;
|
|
using ManagerService.Data.SubSection;
|
|
using ManagerService.DTOs;
|
|
using ManagerService.Tests.Infrastructure;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Xunit;
|
|
|
|
namespace ManagerService.Tests.Controllers
|
|
{
|
|
public class SectionAgendaControllerTests
|
|
{
|
|
private SectionAgendaController BuildController(MyInfoMateDbContext db)
|
|
{
|
|
var cfg = new ConfigurationBuilder().Build();
|
|
return new SectionAgendaController(cfg, NullLogger<SectionAgendaController>.Instance, db);
|
|
}
|
|
|
|
private SectionAgenda MakeSection(MyInfoMateDbContext db, string id = "sa1")
|
|
{
|
|
var section = new SectionAgenda
|
|
{
|
|
Id = id,
|
|
Label = "Agenda test",
|
|
Title = new List<TranslationDTO>(),
|
|
Description = new List<TranslationDTO>(),
|
|
ConfigurationId = "conf1",
|
|
InstanceId = "inst1",
|
|
Type = SectionType.Agenda,
|
|
AgendaResourceIds = new List<TranslationDTO>(),
|
|
EventAgendas = new List<EventAgenda>()
|
|
};
|
|
db.Sections.Add(section);
|
|
return section;
|
|
}
|
|
|
|
private EventAgenda MakeEvent(string sectionId, DateTime? dateFrom)
|
|
{
|
|
return new EventAgenda
|
|
{
|
|
Label = new List<TranslationDTO>(),
|
|
Description = new List<TranslationDTO>(),
|
|
SectionAgendaId = sectionId,
|
|
DateFrom = dateFrom
|
|
};
|
|
}
|
|
|
|
// ── GET ALL ──────────────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public void GetAllEventAgendaFromSection_UnknownSection_Returns404()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
|
|
var result = BuildController(db).GetAllEventAgendaFromSection("unknown");
|
|
|
|
Assert.IsType<NotFoundObjectResult>(result);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetAllEventAgendaFromSection_ReturnsAllEvents()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
MakeSection(db);
|
|
db.EventAgendas.Add(MakeEvent("sa1", DateTime.Today.AddDays(-5)));
|
|
db.EventAgendas.Add(MakeEvent("sa1", DateTime.Today.AddDays(5)));
|
|
db.SaveChanges();
|
|
|
|
var result = BuildController(db).GetAllEventAgendaFromSection("sa1");
|
|
|
|
var ok = Assert.IsType<OkObjectResult>(result);
|
|
var events = Assert.IsAssignableFrom<IEnumerable<EventAgendaDTO>>(ok.Value);
|
|
Assert.Equal(2, events.Count());
|
|
}
|
|
|
|
// ── GET UPCOMING ─────────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public void GetUpcomingEventAgendas_FiltersOutPastEvents()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
MakeSection(db);
|
|
db.EventAgendas.Add(MakeEvent("sa1", DateTime.Today.AddDays(-1))); // passé → filtré
|
|
db.EventAgendas.Add(MakeEvent("sa1", DateTime.Today)); // aujourd'hui → inclus
|
|
db.EventAgendas.Add(MakeEvent("sa1", DateTime.Today.AddDays(1))); // futur → inclus
|
|
db.SaveChanges();
|
|
|
|
var result = BuildController(db).GetUpcomingEventAgendas("sa1");
|
|
|
|
var ok = Assert.IsType<OkObjectResult>(result);
|
|
var events = Assert.IsAssignableFrom<IEnumerable<EventAgendaDTO>>(ok.Value);
|
|
Assert.Equal(2, events.Count());
|
|
}
|
|
|
|
[Fact]
|
|
public void GetUpcomingEventAgendas_NullDateFrom_IsIncluded()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
MakeSection(db);
|
|
db.EventAgendas.Add(MakeEvent("sa1", null)); // sans date → inclus
|
|
db.EventAgendas.Add(MakeEvent("sa1", DateTime.Today.AddDays(-1))); // passé → filtré
|
|
db.SaveChanges();
|
|
|
|
var result = BuildController(db).GetUpcomingEventAgendas("sa1");
|
|
|
|
var ok = Assert.IsType<OkObjectResult>(result);
|
|
var events = Assert.IsAssignableFrom<IEnumerable<EventAgendaDTO>>(ok.Value);
|
|
Assert.Equal(1, events.Count());
|
|
}
|
|
|
|
[Fact]
|
|
public void GetUpcomingEventAgendas_UnknownSection_Returns404()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
|
|
var result = BuildController(db).GetUpcomingEventAgendas("unknown");
|
|
|
|
Assert.IsType<NotFoundObjectResult>(result);
|
|
}
|
|
|
|
// ── CREATE ───────────────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public void CreateEventAgenda_ValidDto_Persists()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
MakeSection(db);
|
|
db.SaveChanges();
|
|
|
|
var dto = new EventAgendaDTO
|
|
{
|
|
label = new List<TranslationDTO>(),
|
|
description = new List<TranslationDTO>(),
|
|
sectionAgendaId = "sa1"
|
|
};
|
|
|
|
var result = BuildController(db).CreateEventAgenda("sa1", dto);
|
|
|
|
Assert.IsType<OkObjectResult>(result);
|
|
Assert.Equal(1, db.EventAgendas.Count());
|
|
}
|
|
|
|
[Fact]
|
|
public void CreateEventAgenda_UnknownSection_Returns500()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
var dto = new EventAgendaDTO
|
|
{
|
|
label = new List<TranslationDTO>(),
|
|
description = new List<TranslationDTO>(),
|
|
sectionAgendaId = "unknown"
|
|
};
|
|
|
|
// La section n'existe pas → KeyNotFoundException → ObjectResult 500
|
|
var result = BuildController(db).CreateEventAgenda("unknown", dto);
|
|
|
|
var status = Assert.IsType<ObjectResult>(result);
|
|
Assert.Equal(500, status.StatusCode);
|
|
}
|
|
|
|
// ── DELETE ───────────────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public void DeleteEventAgenda_ExistingEvent_Returns202()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
MakeSection(db);
|
|
var ev = MakeEvent("sa1", null);
|
|
db.EventAgendas.Add(ev);
|
|
db.SaveChanges();
|
|
var id = ev.Id;
|
|
|
|
var result = BuildController(db).DeleteEventAgenda(id);
|
|
|
|
var status = Assert.IsType<ObjectResult>(result);
|
|
Assert.Equal(202, status.StatusCode);
|
|
Assert.Equal(0, db.EventAgendas.Count());
|
|
}
|
|
|
|
[Fact]
|
|
public void DeleteEventAgenda_UnknownId_Returns404()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
|
|
var result = BuildController(db).DeleteEventAgenda(9999);
|
|
|
|
Assert.IsType<NotFoundObjectResult>(result);
|
|
}
|
|
}
|
|
}
|