manager-service/ManagerService.Tests/Controllers/SectionMapControllerTests.cs

143 lines
5.1 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.Collections.Generic;
using System.Linq;
using Xunit;
namespace ManagerService.Tests.Controllers
{
public class SectionMapControllerTests
{
private static SectionMapController BuildController(MyInfoMateDbContext db)
{
var controller = new SectionMapController(
FakeMongoConfig.Create(),
NullLogger<SectionMapController>.Instance,
db);
FakeUser.SetUser(controller, FakeUser.Create("Manager.contenteditor", "inst-test"));
return controller;
}
private static GeoPoint EmptyGeoPoint() => new GeoPoint
{
Title = new List<TranslationDTO>(),
Description = new List<TranslationDTO>(),
Contents = new List<ContentDTO>(),
Schedules = new List<TranslationDTO>(),
Prices = new List<TranslationDTO>(),
Phone = new List<TranslationDTO>(),
Email = new List<TranslationDTO>(),
Site = new List<TranslationDTO>()
};
private static GeoPointDTO EmptyGeoPointDTO() => new GeoPointDTO
{
title = new List<TranslationDTO>(),
description = new List<TranslationDTO>(),
contents = new List<ContentDTO>(),
schedules = new List<TranslationDTO>(),
prices = new List<TranslationDTO>(),
phone = new List<TranslationDTO>(),
email = new List<TranslationDTO>(),
site = new List<TranslationDTO>()
};
// ── GET POINTS ───────────────────────────────────────────────────────
[Fact]
public void GetAllGeoPoints_ReturnsPointsForSection()
{
using var db = DbContextFactory.Create();
var section = new SectionMap
{
Id = "sm-1",
InstanceId = "inst-test",
Label = "Map",
Type = SectionType.Map,
ConfigurationId = "c1",
Title = new List<TranslationDTO>(),
Description = new List<TranslationDTO>(),
MapPoints = new List<GeoPoint> { EmptyGeoPoint(), EmptyGeoPoint() },
MapCategories = new List<CategorieDTO>()
};
db.Sections.Add(section);
db.SaveChanges();
var result = BuildController(db).GetAllGeoPointsFromSection("sm-1");
var ok = Assert.IsType<OkObjectResult>(result);
var points = Assert.IsAssignableFrom<IEnumerable<GeoPointDTO>>(ok.Value);
Assert.Equal(2, points.Count());
}
[Fact]
public void GetAllGeoPoints_UnknownSection_Returns404()
{
using var db = DbContextFactory.Create();
var result = BuildController(db).GetAllGeoPointsFromSection("unknown");
Assert.IsType<NotFoundObjectResult>(result);
}
// ── CREATE POINT ─────────────────────────────────────────────────────
[Fact]
public void Create_ValidPoint_Persists()
{
using var db = DbContextFactory.Create();
db.Sections.Add(new SectionMap
{
Id = "sm-1",
InstanceId = "inst-test",
Label = "Map",
Type = SectionType.Map,
ConfigurationId = "c1",
Title = new List<TranslationDTO>(),
Description = new List<TranslationDTO>(),
MapPoints = new List<GeoPoint>(),
MapCategories = new List<CategorieDTO>()
});
db.SaveChanges();
var result = BuildController(db).Create("sm-1", EmptyGeoPointDTO());
Assert.IsType<OkObjectResult>(result);
Assert.Equal(1, db.GeoPoints.Count());
}
// ── DELETE POINT ─────────────────────────────────────────────────────
[Fact]
public void Delete_ExistingGeoPoint_Returns202()
{
using var db = DbContextFactory.Create();
db.GeoPoints.Add(EmptyGeoPoint());
db.SaveChanges();
int id = (int)db.GeoPoints.First().Id!;
var result = BuildController(db).Delete(id);
var obj = Assert.IsType<ObjectResult>(result);
Assert.Equal(202, obj.StatusCode);
Assert.Equal(0, db.GeoPoints.Count());
}
[Fact]
public void Delete_UnknownGeoPoint_Returns404()
{
using var db = DbContextFactory.Create();
var result = BuildController(db).Delete(999);
Assert.IsType<NotFoundObjectResult>(result);
}
}
}