187 lines
6.8 KiB
C#
187 lines
6.8 KiB
C#
using Manager.DTOs;
|
|
using ManagerService.Controllers;
|
|
using ManagerService.Data;
|
|
using ManagerService.Data.SubSection;
|
|
using ManagerService.DTOs;
|
|
using ManagerService.Tests.Infrastructure;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Xunit;
|
|
using static ManagerService.Data.SubSection.SectionEvent;
|
|
|
|
namespace ManagerService.Tests.Controllers
|
|
{
|
|
public class SectionEventControllerTests
|
|
{
|
|
private static SectionEventController BuildController(MyInfoMateDbContext db)
|
|
{
|
|
var controller = new SectionEventController(
|
|
FakeMongoConfig.Create(),
|
|
NullLogger<SectionEventController>.Instance,
|
|
db);
|
|
FakeUser.SetUser(controller, FakeUser.Create("Manager.contenteditor", "inst-test"));
|
|
return controller;
|
|
}
|
|
|
|
private static List<TranslationDTO> EmptyTranslations() => new List<TranslationDTO>();
|
|
|
|
// ── PROGRAMME BLOCK ──────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public void CreateProgrammeBlock_ValidRequest_Persists()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
db.Sections.Add(new SectionEvent
|
|
{
|
|
Id = "se-1",
|
|
InstanceId = "inst-test",
|
|
Label = "Event",
|
|
Type = SectionType.Event,
|
|
ConfigurationId = "c1",
|
|
Title = EmptyTranslations(),
|
|
Description = EmptyTranslations(),
|
|
Programme = new List<ProgrammeBlock>(),
|
|
ParcoursIds = new List<string>()
|
|
});
|
|
db.SaveChanges();
|
|
|
|
var result = BuildController(db).CreateProgrammeBlock("se-1", new ProgrammeBlockDTO
|
|
{
|
|
title = EmptyTranslations(),
|
|
description = EmptyTranslations(),
|
|
startTime = DateTime.UtcNow,
|
|
endTime = DateTime.UtcNow.AddHours(1)
|
|
});
|
|
|
|
Assert.IsType<OkObjectResult>(result);
|
|
Assert.Equal(1, db.ProgrammeBlocks.Count());
|
|
}
|
|
|
|
[Fact]
|
|
public void CreateProgrammeBlock_UnknownSection_Returns404()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
|
|
var result = BuildController(db).CreateProgrammeBlock("unknown", new ProgrammeBlockDTO
|
|
{
|
|
title = EmptyTranslations(),
|
|
startTime = DateTime.UtcNow,
|
|
endTime = DateTime.UtcNow.AddHours(1)
|
|
});
|
|
|
|
// KeyNotFoundException attrapée par catch(Exception) → 500
|
|
var obj = Assert.IsType<ObjectResult>(result);
|
|
Assert.Equal(500, obj.StatusCode);
|
|
}
|
|
|
|
[Fact]
|
|
public void UpdateProgrammeBlock_ExistingBlock_UpdatesFields()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
db.ProgrammeBlocks.Add(new ProgrammeBlock
|
|
{
|
|
Id = "pb-1",
|
|
Title = EmptyTranslations(),
|
|
Description = EmptyTranslations(),
|
|
StartTime = DateTime.UtcNow,
|
|
EndTime = DateTime.UtcNow.AddHours(1),
|
|
MapAnnotations = new List<MapAnnotation>()
|
|
});
|
|
db.SaveChanges();
|
|
|
|
var newStart = DateTime.UtcNow.AddHours(2);
|
|
var result = BuildController(db).UpdateProgrammeBlock(new ProgrammeBlockDTO
|
|
{
|
|
id = "pb-1",
|
|
title = EmptyTranslations(),
|
|
description = EmptyTranslations(),
|
|
startTime = newStart,
|
|
endTime = newStart.AddHours(1)
|
|
});
|
|
|
|
Assert.IsType<OkObjectResult>(result);
|
|
Assert.Equal(newStart, db.ProgrammeBlocks.First().StartTime);
|
|
}
|
|
|
|
[Fact]
|
|
public void DeleteProgrammeBlock_ExistingBlock_Returns202()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
db.ProgrammeBlocks.Add(new ProgrammeBlock
|
|
{
|
|
Id = "pb-1",
|
|
Title = EmptyTranslations(),
|
|
Description = EmptyTranslations(),
|
|
StartTime = DateTime.UtcNow,
|
|
EndTime = DateTime.UtcNow.AddHours(1),
|
|
MapAnnotations = new List<MapAnnotation>()
|
|
});
|
|
db.SaveChanges();
|
|
|
|
var result = BuildController(db).DeleteProgrammeBlock("pb-1");
|
|
|
|
var obj = Assert.IsType<ObjectResult>(result);
|
|
Assert.Equal(202, obj.StatusCode);
|
|
Assert.Equal(0, db.ProgrammeBlocks.Count());
|
|
}
|
|
|
|
// ── MAP ANNOTATION ───────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public void CreateMapAnnotation_ValidRequest_Persists()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
db.ProgrammeBlocks.Add(new ProgrammeBlock
|
|
{
|
|
Id = "pb-1",
|
|
Title = EmptyTranslations(),
|
|
Description = EmptyTranslations(),
|
|
StartTime = DateTime.UtcNow,
|
|
EndTime = DateTime.UtcNow.AddHours(1),
|
|
MapAnnotations = new List<MapAnnotation>()
|
|
});
|
|
db.SaveChanges();
|
|
|
|
var result = BuildController(db).CreateMapAnnotation("pb-1", new MapAnnotationDTO
|
|
{
|
|
label = EmptyTranslations(),
|
|
type = EmptyTranslations(),
|
|
geometryType = GeometryType.Point,
|
|
geometry = new GeometryDTO { type = "Point", coordinates = new List<double> { 4.35, 50.85 } }
|
|
});
|
|
|
|
Assert.IsType<OkObjectResult>(result);
|
|
Assert.Equal(1, db.MapAnnotations.Count());
|
|
}
|
|
|
|
[Fact]
|
|
public void UpdateMapAnnotation_ExistingAnnotation_UpdatesLabel()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
db.MapAnnotations.Add(new MapAnnotation
|
|
{
|
|
Id = "ma-1",
|
|
Label = EmptyTranslations(),
|
|
Type = EmptyTranslations()
|
|
});
|
|
db.SaveChanges();
|
|
|
|
var newLabel = new List<TranslationDTO> { new TranslationDTO { language = "FR", value = "Updated" } };
|
|
var result = BuildController(db).UpdateMapAnnotation(new MapAnnotationDTO
|
|
{
|
|
id = "ma-1",
|
|
label = newLabel,
|
|
type = EmptyTranslations(),
|
|
geometryType = GeometryType.Point,
|
|
geometry = null // UpdateMapAnnotation handles null geometry
|
|
});
|
|
|
|
Assert.IsType<OkObjectResult>(result);
|
|
Assert.Equal("Updated", db.MapAnnotations.First().Label.First().value);
|
|
}
|
|
}
|
|
}
|