From 1d374366269803890410a301abc997205c99fa87 Mon Sep 17 00:00:00 2001 From: clchieu Date: Fri, 15 Mar 2024 16:15:21 +0100 Subject: [PATCH] Ajout de tests pour la blacklist et gestion des erreurs dans le constructeur --- API_SQLuedo/Model/BlackList.cs | 3 ++ .../EntitiesTests/TestBlackListEntity.cs | 54 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 API_SQLuedo/TestEF/EntitiesTests/TestBlackListEntity.cs diff --git a/API_SQLuedo/Model/BlackList.cs b/API_SQLuedo/Model/BlackList.cs index 002b5ba..bce0cf0 100644 --- a/API_SQLuedo/Model/BlackList.cs +++ b/API_SQLuedo/Model/BlackList.cs @@ -7,6 +7,9 @@ public class BlackList public BlackList(string email, DateOnly expirationDate) { + if (email is null or "") + throw new ArgumentException("Email cannot be null or empty"); + ArgumentOutOfRangeException.ThrowIfLessThan(expirationDate, DateOnly.FromDateTime(DateTime.Now)); Email = email; ExpirationDate = expirationDate; } diff --git a/API_SQLuedo/TestEF/EntitiesTests/TestBlackListEntity.cs b/API_SQLuedo/TestEF/EntitiesTests/TestBlackListEntity.cs new file mode 100644 index 0000000..abc2ad6 --- /dev/null +++ b/API_SQLuedo/TestEF/EntitiesTests/TestBlackListEntity.cs @@ -0,0 +1,54 @@ +using Model; + +namespace TestEF.EntitiesTests; + +public class TestBlackListEntity +{ + [Fact] + public void Constructor_ShouldSetProperties_WhenCalledWithTwoParameters() + { + // Arrange + string email = "test@example.com"; + DateOnly expirationDate = DateOnly.FromDateTime(DateTime.Now.AddDays(10)); + + // Act + var blackList = new BlackList(email, expirationDate); + + // Assert + Assert.Equal(email, blackList.Email); + Assert.Equal(expirationDate, blackList.ExpirationDate); + } + + [Fact] + public void Constructor_ShouldThrowArgumentException_WhenEmailIsEmpty() + { + // Arrange + string email = string.Empty; + DateOnly expirationDate = DateOnly.FromDateTime(DateTime.Now.AddDays(10)); + + // Act & Assert + Assert.Throws(() => new BlackList(email, expirationDate)); + } + + [Fact] + public void Constructor_ShouldThrowArgumentException_WhenEmailIsNull() + { + // Arrange + string email = null; + DateOnly expirationDate = DateOnly.FromDateTime(DateTime.Now.AddDays(10)); + + // Act & Assert + Assert.Throws(() => new BlackList(email, expirationDate)); + } + + [Fact] + public void Constructor_ShouldThrowArgumentOutOfRangeException_WhenExpirationDateIsInThePast() + { + // Arrange + string email = "test@example.com"; + DateOnly expirationDate = DateOnly.FromDateTime(DateTime.Now.AddDays(-1)); + + // Act & Assert + Assert.Throws(() => new BlackList(email, expirationDate)); + } +} \ No newline at end of file