196 lines
7.1 KiB
C#
196 lines
7.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.Configuration;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Xunit;
|
|
|
|
namespace ManagerService.Tests.Controllers
|
|
{
|
|
public class SectionQuizControllerTests
|
|
{
|
|
private SectionQuizController BuildController(MyInfoMateDbContext db)
|
|
{
|
|
var cfg = new ConfigurationBuilder().Build();
|
|
return new SectionQuizController(cfg, NullLogger<SectionQuizController>.Instance, db);
|
|
}
|
|
|
|
private SectionQuiz MakeQuizSection(string id = "sq1")
|
|
{
|
|
return new SectionQuiz
|
|
{
|
|
Id = id,
|
|
Label = "Quiz test",
|
|
Title = new List<TranslationDTO>(),
|
|
Description = new List<TranslationDTO>(),
|
|
ConfigurationId = "conf1",
|
|
InstanceId = "inst1",
|
|
Type = SectionType.Quiz,
|
|
QuizQuestions = new List<QuizQuestion>(),
|
|
QuizBadLevel = new List<TranslationAndResourceDTO>(),
|
|
QuizMediumLevel = new List<TranslationAndResourceDTO>(),
|
|
QuizGoodLevel = new List<TranslationAndResourceDTO>(),
|
|
QuizGreatLevel = new List<TranslationAndResourceDTO>()
|
|
};
|
|
}
|
|
|
|
private QuizQuestion MakeQuestion(string sectionId, int order, string? label = null)
|
|
{
|
|
return new QuizQuestion
|
|
{
|
|
SectionQuizId = sectionId,
|
|
Order = order,
|
|
Label = new List<TranslationAndResourceDTO>(),
|
|
Responses = new List<ResponseDTO>()
|
|
};
|
|
}
|
|
|
|
// ── CREATE ───────────────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public void Create_AutoSetsOrderToCount()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
var section = MakeQuizSection();
|
|
db.Sections.Add(section);
|
|
// 2 questions déjà dans la section
|
|
db.QuizQuestions.AddRange(
|
|
MakeQuestion("sq1", 0),
|
|
MakeQuestion("sq1", 1)
|
|
);
|
|
db.SaveChanges();
|
|
|
|
var dto = new QuestionDTO
|
|
{
|
|
label = new List<TranslationAndResourceDTO>(),
|
|
responses = new List<ResponseDTO>()
|
|
};
|
|
|
|
var result = BuildController(db).Create("sq1", dto);
|
|
|
|
var ok = Assert.IsType<OkObjectResult>(result);
|
|
// La nouvelle question doit avoir l'ordre = 2 (count avant ajout)
|
|
var question = db.QuizQuestions.OrderByDescending(q => q.Order).First();
|
|
Assert.Equal(2, question.Order);
|
|
}
|
|
|
|
[Fact]
|
|
public void Create_UnknownSection_Returns500()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
|
|
var dto = new QuestionDTO
|
|
{
|
|
label = new List<TranslationAndResourceDTO>(),
|
|
responses = new List<ResponseDTO>()
|
|
};
|
|
|
|
var result = BuildController(db).Create("unknown", dto);
|
|
|
|
var status = Assert.IsType<ObjectResult>(result);
|
|
Assert.Equal(500, status.StatusCode);
|
|
}
|
|
|
|
// ── UPDATE / REORDER ─────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public void Update_ChangeOrder_ReordersAllQuestionsFrom0()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
var section = MakeQuizSection();
|
|
db.Sections.Add(section);
|
|
var q0 = MakeQuestion("sq1", 0);
|
|
var q1 = MakeQuestion("sq1", 1);
|
|
var q2 = MakeQuestion("sq1", 2);
|
|
db.QuizQuestions.AddRange(q0, q1, q2);
|
|
db.SaveChanges();
|
|
|
|
// Déplace q2 (order=2) à la position 0
|
|
var dto = new QuestionDTO
|
|
{
|
|
id = q2.Id,
|
|
order = 0,
|
|
label = new List<TranslationAndResourceDTO>(),
|
|
responses = new List<ResponseDTO>()
|
|
};
|
|
BuildController(db).Update(dto);
|
|
|
|
var questions = db.QuizQuestions.OrderBy(q => q.Order).ToList();
|
|
// Après réordonnancement : q2 doit être en position 0
|
|
Assert.Equal(q2.Id, questions[0].Id);
|
|
// Les ordres doivent être continus depuis 0
|
|
Assert.Equal(new[] { 0, 1, 2 }, questions.Select(q => q.Order).ToArray());
|
|
}
|
|
|
|
[Fact]
|
|
public void Update_SameOrder_UpdatesFieldsWithoutReorder()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
var section = MakeQuizSection();
|
|
db.Sections.Add(section);
|
|
var q0 = MakeQuestion("sq1", 0);
|
|
db.QuizQuestions.Add(q0);
|
|
db.SaveChanges();
|
|
|
|
var dto = new QuestionDTO
|
|
{
|
|
id = q0.Id,
|
|
order = 0, // même ordre
|
|
label = new List<TranslationAndResourceDTO> { new TranslationAndResourceDTO { language = "fr", value = "Nouvelle question" } },
|
|
responses = new List<ResponseDTO>()
|
|
};
|
|
BuildController(db).Update(dto);
|
|
|
|
// Le label est mis à jour (via l'objet retourné — la valeur est dans le DTO en retour)
|
|
// On vérifie juste que ça s'est exécuté sans erreur
|
|
Assert.Equal(1, db.QuizQuestions.Count());
|
|
}
|
|
|
|
[Fact]
|
|
public void Update_UnknownQuestion_Returns404()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
|
|
var dto = new QuestionDTO { id = 9999, order = 0, label = new List<TranslationAndResourceDTO>(), responses = new List<ResponseDTO>() };
|
|
|
|
var result = BuildController(db).Update(dto);
|
|
|
|
Assert.IsType<NotFoundObjectResult>(result);
|
|
}
|
|
|
|
// ── DELETE ───────────────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public void Delete_ExistingQuestion_Returns202()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
var section = MakeQuizSection();
|
|
db.Sections.Add(section);
|
|
var q = MakeQuestion("sq1", 0);
|
|
db.QuizQuestions.Add(q);
|
|
db.SaveChanges();
|
|
var qId = q.Id;
|
|
|
|
var result = BuildController(db).Delete(qId);
|
|
|
|
var status = Assert.IsType<ObjectResult>(result);
|
|
Assert.Equal(202, status.StatusCode);
|
|
Assert.Equal(0, db.QuizQuestions.Count());
|
|
}
|
|
|
|
[Fact]
|
|
public void Delete_UnknownId_Returns404()
|
|
{
|
|
using var db = DbContextFactory.Create();
|
|
|
|
var result = BuildController(db).Delete(9999);
|
|
|
|
Assert.IsType<NotFoundObjectResult>(result);
|
|
}
|
|
}
|
|
}
|