105 lines
4.1 KiB
C#
105 lines
4.1 KiB
C#
using ManagerService.Controllers;
|
|
using ManagerService.Data;
|
|
using ManagerService.DTOs;
|
|
using ManagerService.Tests.Infrastructure;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Xunit;
|
|
|
|
namespace ManagerService.Tests.Controllers
|
|
{
|
|
public class ConfigurationControllerTests
|
|
{
|
|
private static IConfiguration BuildConfig() =>
|
|
new ConfigurationBuilder()
|
|
.AddInMemoryCollection(new Dictionary<string, string?>
|
|
{
|
|
["ConnectionStrings:TabletDb"] = "mongodb://localhost:27017",
|
|
["OpenWeatherApiKey"] = ""
|
|
})
|
|
.Build();
|
|
|
|
private static ConfigurationController BuildController(MyInfoMateDbContext db) =>
|
|
new ConfigurationController(BuildConfig(), NullLogger<ConfigurationController>.Instance, db);
|
|
|
|
// ── GET ──────────────────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public void Get_FiltersToInstance()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
db.Configurations.AddRange(
|
|
new Configuration { Id = "c1", InstanceId = "inst-a", Label = "A", Title = new List<TranslationDTO>() },
|
|
new Configuration { Id = "c2", InstanceId = "inst-b", Label = "B", Title = new List<TranslationDTO>() }
|
|
);
|
|
db.SaveChanges();
|
|
|
|
var result = BuildController(db).Get("inst-a");
|
|
|
|
var ok = Assert.IsType<OkObjectResult>(result);
|
|
var list = Assert.IsAssignableFrom<System.Collections.IEnumerable>(ok.Value);
|
|
Assert.Single(list.Cast<object>());
|
|
}
|
|
|
|
// ── CREATE ───────────────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public void Create_ValidDto_Persists()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
|
|
var result = BuildController(db).Create(new ConfigurationDTO
|
|
{
|
|
instanceId = "inst-a",
|
|
label = "My Config",
|
|
languages = new List<string> { "FR" }
|
|
});
|
|
|
|
Assert.IsType<OkObjectResult>(result);
|
|
Assert.Equal(1, db.Configurations.Count());
|
|
Assert.Equal("My Config", db.Configurations.First().Label);
|
|
}
|
|
|
|
// ── UPDATE ───────────────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public void Update_UnknownId_Returns404()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
|
|
var result = BuildController(db).Update(new ConfigurationDTO { id = "unknown", label = "X" });
|
|
|
|
Assert.IsType<NotFoundObjectResult>(result);
|
|
}
|
|
|
|
// ── DELETE ───────────────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public void Delete_ExistingConfig_Returns202()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
db.Configurations.Add(new Configuration { Id = "c1", InstanceId = "inst-a", Label = "A", Title = new List<TranslationDTO>() });
|
|
db.SaveChanges();
|
|
|
|
var result = BuildController(db).Delete("c1");
|
|
|
|
var obj = Assert.IsType<ObjectResult>(result);
|
|
Assert.Equal(202, obj.StatusCode);
|
|
Assert.Equal(0, db.Configurations.Count());
|
|
}
|
|
|
|
[Fact]
|
|
public void Delete_UnknownId_Returns404()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
|
|
var result = BuildController(db).Delete("unknown");
|
|
|
|
Assert.IsType<NotFoundObjectResult>(result);
|
|
}
|
|
}
|
|
}
|