Test unit : Limite de ajout

pull/2/head
nathan boileau 2 years ago
parent 43a5e6c351
commit 2f1aff3c02

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -9,7 +10,11 @@ namespace EFLol
public class ChampionEntity public class ChampionEntity
{ {
public int Id { get; set; } public int Id { get; set; }
[MaxLength(30, ErrorMessage = "Name cannot be longer than 30 characters.")]
public string Name { get; set; } public string Name { get; set; }
[MaxLength(256, ErrorMessage = "Bio cannot be longer than 256 characters.")]
public string Bio { get; set; } public string Bio { get; set; }
} }
} }

@ -1,52 +1,67 @@
using EFLol; using EFLol;
using Microsoft.Data.Sqlite; using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace TestUnitaire namespace TestUnitaire
{ {
public class TestEfLol public class TestEfLol
{ {
[Theory] [Theory]
[InlineData("zeus","dieu")] [InlineData("Zeus", "Dieu de la foudre", true)]
[InlineData("zeus", "dieu")] [InlineData("Hades", "Dieu des enfers", true)]
[InlineData("zeus", "dieu")] [InlineData("Aphrodite", "Déesse de l'amour", true)]
public async Task TestAddInMemory(String name, String bio) [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" +
// Arrange "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" +
var connection = new SqliteConnection("DataSource=:memory:"); "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)]
connection.Open(); public async Task TestAddInMemory(String name, String bio, bool expected)
{
var options = new DbContextOptionsBuilder<ChampionContext>() var connection = new SqliteConnection("DataSource=:memory:");
.UseSqlite(connection) connection.Open();
.Options;
var options = new DbContextOptionsBuilder<ChampionContext>()
// Act .UseSqlite(connection)
using (var context = new ChampionContext(options)) .Options;
{
await context.Database.EnsureCreatedAsync(); using (var context = new ChampionContext(options))
var Dieu = new ChampionEntity {
{ await context.Database.EnsureCreatedAsync();
Name = name, var Dieu = new ChampionEntity
Bio = bio {
}; Name = name,
Bio = bio
ChampionEntity found = await context.Champions.SingleOrDefaultAsync(c => c.Name == "Zeus"); };
Assert.Null(found);
ChampionEntity found = await context.Champions.SingleOrDefaultAsync(c => c.Name == "Zeus");
await context.Champions.AddAsync(Dieu); Assert.Null(found);
await context.SaveChangesAsync();
await context.Champions.AddAsync(Dieu);
found = await context.Champions.SingleOrDefaultAsync(c => c.Name == name); await context.SaveChangesAsync();
Assert.NotNull(found);
found = await context.Champions.SingleOrDefaultAsync(c => c.Name == name);
Assert.Equal(1, await context.Champions.CountAsync()); Assert.NotNull(found);
Assert.Equal(name, found.Name);
} Assert.Equal(1, await context.Champions.CountAsync());
} Assert.Equal(name, found.Name);
[Fact] // 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() public void ModifyTestInMemory()
{//connection must be opened to use In-memory database {
var connection = new SqliteConnection("DataSource=:memory:"); var connection = new SqliteConnection("DataSource=:memory:");
connection.Open(); connection.Open();
@ -96,9 +111,5 @@ namespace TestUnitaire
Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count()); Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count());
} }
} }
} }
} }
Loading…
Cancel
Save