From 2f1aff3c025a607779c301a24b42de53efd69d85 Mon Sep 17 00:00:00 2001 From: nathan boileau Date: Wed, 8 Feb 2023 17:25:11 +0100 Subject: [PATCH] Test unit : Limite de ajout --- Sources/EFLol/ChampionEntity.cs | 5 ++ Sources/TestUnitaire/TestEfLol.cs | 117 ++++++++++++++++-------------- 2 files changed, 69 insertions(+), 53 deletions(-) diff --git a/Sources/EFLol/ChampionEntity.cs b/Sources/EFLol/ChampionEntity.cs index 8c8ac7f..5638ce5 100644 --- a/Sources/EFLol/ChampionEntity.cs +++ b/Sources/EFLol/ChampionEntity.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -9,7 +10,11 @@ namespace EFLol public class ChampionEntity { public int Id { get; set; } + + [MaxLength(30, ErrorMessage = "Name cannot be longer than 30 characters.")] public string Name { get; set; } + + [MaxLength(256, ErrorMessage = "Bio cannot be longer than 256 characters.")] public string Bio { get; set; } } } diff --git a/Sources/TestUnitaire/TestEfLol.cs b/Sources/TestUnitaire/TestEfLol.cs index e7ec753..4d07f9a 100644 --- a/Sources/TestUnitaire/TestEfLol.cs +++ b/Sources/TestUnitaire/TestEfLol.cs @@ -1,52 +1,67 @@ -using EFLol; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; - -namespace TestUnitaire -{ - public class TestEfLol - { +using EFLol; +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; + +namespace TestUnitaire +{ + public class TestEfLol + { [Theory] - [InlineData("zeus","dieu")] - [InlineData("zeus", "dieu")] - [InlineData("zeus", "dieu")] - public async Task TestAddInMemory(String name, String bio) - { - // Arrange - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - // Act - using (var context = new ChampionContext(options)) - { - await context.Database.EnsureCreatedAsync(); - var Dieu = new ChampionEntity - { - Name = name, - Bio = bio - }; - - ChampionEntity found = await context.Champions.SingleOrDefaultAsync(c => c.Name == "Zeus"); - Assert.Null(found); - - await context.Champions.AddAsync(Dieu); - await context.SaveChangesAsync(); - - found = await context.Champions.SingleOrDefaultAsync(c => c.Name == name); - Assert.NotNull(found); - - Assert.Equal(1, await context.Champions.CountAsync()); - Assert.Equal(name, found.Name); - } - } - - [Fact] + [InlineData("Zeus", "Dieu de la foudre", true)] + [InlineData("Hades", "Dieu des enfers", true)] + [InlineData("Aphrodite", "Déesse de l'amour", true)] + [InlineData("AresAresAresAresAresAresAresAresAresAres", + "Dieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerre" + + "Dieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerre" + + "Dieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerre", false)] + public async Task TestAddInMemory(String name, String bio, bool expected) + { + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + + var options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + + using (var context = new ChampionContext(options)) + { + await context.Database.EnsureCreatedAsync(); + var Dieu = new ChampionEntity + { + Name = name, + Bio = bio + }; + + ChampionEntity found = await context.Champions.SingleOrDefaultAsync(c => c.Name == "Zeus"); + Assert.Null(found); + + await context.Champions.AddAsync(Dieu); + await context.SaveChangesAsync(); + + found = await context.Champions.SingleOrDefaultAsync(c => c.Name == name); + Assert.NotNull(found); + + Assert.Equal(1, await context.Champions.CountAsync()); + Assert.Equal(name, found.Name); + + // Test if the max length of the name is respected (30) and the max length of the bio is respected (256) + if (expected) + { + Assert.True(found.Name.Length <= 30); + Assert.True(found.Bio.Length <= 256); + } + else + { + Assert.False(found.Bio.Length <= 256); + Assert.False(found.Name.Length <= 30); + } + } + + } + + [Fact] public void ModifyTestInMemory() - {//connection must be opened to use In-memory database + { var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); @@ -96,9 +111,5 @@ namespace TestUnitaire Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count()); } } - } -} - - - - + } +} \ No newline at end of file