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.
150 lines
5.4 KiB
150 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 ExperienceTest
|
|
{
|
|
[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 exp = new ExperienceEntity()
|
|
{
|
|
Id = "100",
|
|
Title = "Software Engineer",
|
|
CompanyName = "CGI",
|
|
StartDate = new DateTime(2020, 9, 1),
|
|
EndDate = new DateTime(2022, 6, 1),
|
|
IsCurrent = true
|
|
};
|
|
await context.Experiences.AddAsync(exp);
|
|
await context.SaveChangesAsync();
|
|
|
|
var inBaseExp = await context.Experiences.FirstOrDefaultAsync(e => e.Id == "100");
|
|
Assert.IsNotNull(inBaseExp);
|
|
Assert.AreEqual("100", inBaseExp.Id);
|
|
Assert.AreEqual("Software Engineer", inBaseExp.Title);
|
|
Assert.AreEqual("CGI", inBaseExp.CompanyName);
|
|
Assert.AreEqual(new DateTime(2020, 9, 1), inBaseExp.StartDate);
|
|
Assert.AreEqual(new DateTime(2022, 6, 1), inBaseExp.EndDate);
|
|
Assert.IsTrue(inBaseExp.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 exp = new ExperienceEntity()
|
|
{
|
|
Id = "100",
|
|
Title = "Software Engineer",
|
|
CompanyName = "CGI",
|
|
StartDate = new DateTime(2020, 9, 1),
|
|
EndDate = new DateTime(2022, 6, 1),
|
|
IsCurrent = true
|
|
};
|
|
await context.Experiences.AddAsync(exp);
|
|
await context.SaveChangesAsync();
|
|
|
|
var inBaseExp = await context.Experiences.FirstOrDefaultAsync(e => e.Id == "100");
|
|
Assert.IsNotNull(inBaseExp);
|
|
|
|
context.Experiences.Remove(exp);
|
|
await context.SaveChangesAsync();
|
|
|
|
var inBaseExp2 = await context.Experiences.FirstOrDefaultAsync(e => e.Id == "100");
|
|
Assert.IsNull(inBaseExp2);
|
|
}
|
|
}
|
|
|
|
[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 exp = new ExperienceEntity()
|
|
{
|
|
Id = "100",
|
|
Title = "Software Engineer",
|
|
CompanyName = "CGI",
|
|
StartDate = new DateTime(2020, 9, 1),
|
|
EndDate = new DateTime(2022, 6, 1),
|
|
IsCurrent = true
|
|
};
|
|
|
|
await context.Experiences.AddAsync(exp);
|
|
await context.SaveChangesAsync();
|
|
|
|
var inBaseExp = await context.Experiences.FirstOrDefaultAsync(e => e.Id == "100");
|
|
Assert.IsNotNull(inBaseExp);
|
|
|
|
inBaseExp.Title = "Software Engineer II";
|
|
inBaseExp.CompanyName = "CGI II";
|
|
inBaseExp.StartDate = new DateTime(2021, 9, 1);
|
|
inBaseExp.EndDate = new DateTime(2023, 6, 1);
|
|
inBaseExp.IsCurrent = false;
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
var inBaseExp2 = await context.Experiences.FirstOrDefaultAsync(e => e.Id == "100");
|
|
Assert.IsNotNull(inBaseExp2);
|
|
Assert.AreEqual("Software Engineer II", inBaseExp2.Title);
|
|
Assert.AreEqual("CGI II", inBaseExp2.CompanyName);
|
|
Assert.AreEqual(new DateTime(2021, 9, 1), inBaseExp2.StartDate);
|
|
Assert.AreEqual(new DateTime(2023, 6, 1), inBaseExp2.EndDate);
|
|
Assert.IsFalse(inBaseExp2.IsCurrent);
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ToString_ReturnsCorrectFormat()
|
|
{
|
|
// Arrange
|
|
var experience = new ExperienceEntity
|
|
{
|
|
Id = "100",
|
|
Title = "Software Engineer",
|
|
CompanyName = "CGI",
|
|
StartDate = new DateTime(2020, 9, 1),
|
|
EndDate = new DateTime(2022, 6, 1),
|
|
IsCurrent = true
|
|
};
|
|
|
|
// Act
|
|
var result = experience.ToString();
|
|
|
|
// Assert
|
|
var expected = "\tTitle: Software Engineer, Company: CGI, Start Date: 01/09/2020, End Date: 01/06/2022, Is Current: True";
|
|
Assert.AreEqual(expected, result);
|
|
}
|
|
} |