You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
OptifitWebService/UnitTestEF/Entities/FormationTest.cs

145 lines
5.4 KiB

using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
namespace UnitTestEF.Entities;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Infrastructure;
using Infrastructure.Entities;
[TestClass]
public class FormationTest
{
[TestMethod]
public async Task AddFormationTest()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<AlumniDbContext>()
.UseSqlite(connection)
.Options;
using (var context = new AlumniDbContext(options))
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
var newFormation = new FormationEntity {
Id = "100",
SchoolName = "IUT",
Name = "BUT Informatique",
StartDate = new DateTime(2020, 9, 1),
EndDate = new DateTime(2022, 6, 1),
IsCurrent = true
};
await context.Formations.AddAsync(newFormation);
await context.SaveChangesAsync();
var inBaseFormation = await context.Formations.FirstOrDefaultAsync(f => f.Id == "100");
Assert.IsNotNull(inBaseFormation);
Assert.AreEqual("100", inBaseFormation.Id);
Assert.AreEqual("IUT", inBaseFormation.SchoolName);
Assert.AreEqual("BUT Informatique", inBaseFormation.Name);
Assert.AreEqual(new DateTime(2020, 9, 1), inBaseFormation.StartDate);
Assert.AreEqual(new DateTime(2022, 6, 1), inBaseFormation.EndDate);
Assert.IsTrue(inBaseFormation.IsCurrent);
}
}
[TestMethod]
public async Task DeleteFormationTest()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<AlumniDbContext>()
.UseSqlite(connection)
.Options;
using (var context = new AlumniDbContext(options))
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
var newFormation = new FormationEntity {
Id = "100",
SchoolName = "IUT",
Name = "BUT Informatique",
StartDate = new DateTime(2020, 9, 1),
EndDate = new DateTime(2022, 6, 1),
IsCurrent = true
};
await context.Formations.AddAsync(newFormation);
await context.SaveChangesAsync();
var inBaseFormation = await context.Formations.FirstOrDefaultAsync(f => f.Id == "100");
Assert.IsNotNull(inBaseFormation);
context.Formations.Remove(newFormation);
await context.SaveChangesAsync();
var formationAfterDelete = await context.Formations.FirstOrDefaultAsync(f => f.Id == "100");
Assert.IsNull(formationAfterDelete);
}
}
[TestMethod]
public async Task UpdateFormationTest()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<AlumniDbContext>()
.UseSqlite(connection)
.Options;
using (var context = new AlumniDbContext(options))
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
var newFormation = new FormationEntity {
Id = "100",
SchoolName = "IUT",
Name = "BUT Informatique",
StartDate = new DateTime(2020, 9, 1),
EndDate = new DateTime(2022, 6, 1),
IsCurrent = true
};
await context.Formations.AddAsync(newFormation);
await context.SaveChangesAsync();
var inBaseFormation = await context.Formations.FirstOrDefaultAsync(f => f.Id == "100");
Assert.IsNotNull(inBaseFormation);
inBaseFormation.SchoolName = "IUT2";
inBaseFormation.Name = "BUT Informatique2";
inBaseFormation.StartDate = new DateTime(2020, 9, 2);
inBaseFormation.EndDate = new DateTime(2022, 6, 2);
inBaseFormation.IsCurrent = false;
await context.SaveChangesAsync();
var formationAfterUpdate = await context.Formations.FirstOrDefaultAsync(f => f.Id == "100");
Assert.IsNotNull(formationAfterUpdate);
Assert.AreEqual("IUT2", formationAfterUpdate.SchoolName);
Assert.AreEqual("BUT Informatique2", formationAfterUpdate.Name);
Assert.AreEqual(new DateTime(2020, 9, 2), formationAfterUpdate.StartDate);
Assert.AreEqual(new DateTime(2022, 6, 2), formationAfterUpdate.EndDate);
Assert.IsFalse(formationAfterUpdate.IsCurrent);
}
}
[TestMethod]
public void ToString_ReturnsCorrectFormat()
{
// Arrange
var formation = new FormationEntity
{
Id = "100",
SchoolName = "IUT",
Name = "BUT Informatique",
StartDate = new DateTime(2020, 9, 1),
EndDate = new DateTime(2022, 6, 1),
IsCurrent = true
};
// Act
var result = formation.ToString();
// Assert
var expected = "\tFormation : BUT Informatique \tSchool's name : IUT";
Assert.AreEqual(expected, result);
}
}