commit
90dab5667b
@ -0,0 +1,4 @@
|
||||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
||||
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=711b0308_002Dc8c6_002D4423_002Db315_002D82f61e82bdb5/@EntryIndexedValue"><SessionState ContinuousTestingMode="0" IsActive="True" Name="alumniTest" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session">
|
||||
<Solution />
|
||||
</SessionState></s:String></wpf:ResourceDictionary>
|
@ -0,0 +1,8 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Infrastructure.Base;
|
||||
|
||||
public abstract class EntityBase
|
||||
{
|
||||
[Key] public string? Id { get; set; } = Guid.NewGuid().ToString();
|
||||
}
|
@ -0,0 +1,157 @@
|
||||
using Infrastructure.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Infrastructure.Stub;
|
||||
|
||||
public class StubbedContext : AlumniDbContext
|
||||
{
|
||||
public StubbedContext(DbContextOptions<AlumniDbContext> options) : base(options)
|
||||
{
|
||||
}
|
||||
|
||||
public StubbedContext(): this(new DbContextOptionsBuilder<AlumniDbContext>()
|
||||
.UseSqlite("DataSource=:memory:")
|
||||
.Options)
|
||||
{
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
var eventEntities = new List<EventEntity>(
|
||||
[
|
||||
new EventEntity
|
||||
{
|
||||
Id = GenerateGuid().ToString(),
|
||||
Title = "Laser Game",
|
||||
Description = "Venez vous amuser et faire du networking lors d'une partie de Laser Game avec vos camarades de promo et d'anciens étudiants !",
|
||||
Date = GenerateRandomDate(),
|
||||
nbPlaces = GenerateRandomInt(1, 100)
|
||||
},
|
||||
new EventEntity
|
||||
{
|
||||
Id = GenerateGuid().ToString(),
|
||||
Title = "Afterwork Raclette",
|
||||
Description = "Venez déguster de la raclette et boire un verre lors de notre afterwork recherche de stage et alternance !",
|
||||
Date = GenerateRandomDate(),
|
||||
nbPlaces = GenerateRandomInt(1, 100)
|
||||
},
|
||||
new EventEntity
|
||||
{
|
||||
Id = GenerateGuid().ToString(),
|
||||
Title = "Conférence : Développeur & IA ?",
|
||||
Description = "Venez assister à une conférence qui vous permettra de découvrir comment l'IA doit être prise en compte dans le développement d'applications !",
|
||||
Date = GenerateRandomDate(),
|
||||
nbPlaces = GenerateRandomInt(1, 100)
|
||||
}
|
||||
]
|
||||
);
|
||||
|
||||
var alumniEntities = new List<User>(
|
||||
[
|
||||
new User
|
||||
{
|
||||
Id = GenerateGuid().ToString(),
|
||||
FirstName = "Test",
|
||||
LastName = "Test",
|
||||
Email = "test@gmail.com",
|
||||
EntryYear = "2021",
|
||||
Password = "1234567890",
|
||||
Role = "ADMIN"
|
||||
},
|
||||
new User
|
||||
{
|
||||
Id = GenerateGuid().ToString(),
|
||||
FirstName = "Test2",
|
||||
LastName = "Test2",
|
||||
Email = "test2@gmail.com",
|
||||
EntryYear = "2021",
|
||||
Password = "1234567890",
|
||||
Role = "USER",
|
||||
}
|
||||
]
|
||||
);
|
||||
var experienceEntities = new List<ExperienceEntity>(
|
||||
[
|
||||
new ExperienceEntity
|
||||
{
|
||||
Id = GenerateGuid().ToString(),
|
||||
Title = "Data Scientist",
|
||||
StartDate = GenerateRandomDate(),
|
||||
CompanyName = "Michelin",
|
||||
IsCurrent = GenerateRandomBool()
|
||||
},
|
||||
new ExperienceEntity
|
||||
{
|
||||
Id = GenerateGuid().ToString(),
|
||||
Title = "Développeur C# .NET",
|
||||
StartDate = GenerateRandomDate(),
|
||||
CompanyName = "CGI",
|
||||
IsCurrent = GenerateRandomBool()
|
||||
}
|
||||
]
|
||||
);
|
||||
var formationEntities = new List<FormationEntity>(
|
||||
|
||||
[
|
||||
new FormationEntity
|
||||
{
|
||||
Id = GenerateGuid().ToString(),
|
||||
SchoolName = "IUT Clermont-Ferrand",
|
||||
Name = "BUT Informatique",
|
||||
StartDate = GenerateRandomDate(),
|
||||
IsCurrent = GenerateRandomBool()
|
||||
},
|
||||
new FormationEntity
|
||||
{
|
||||
Id = GenerateGuid().ToString(),
|
||||
SchoolName = "Isima",
|
||||
Name = "Ingénieur Informatique",
|
||||
StartDate = GenerateRandomDate(),
|
||||
IsCurrent = GenerateRandomBool()
|
||||
}
|
||||
]
|
||||
);
|
||||
|
||||
modelBuilder.Entity<ExperienceEntity>().HasData(experienceEntities);
|
||||
modelBuilder.Entity<FormationEntity>().HasData(formationEntities);
|
||||
modelBuilder.Entity<User>().HasData(alumniEntities);
|
||||
modelBuilder.Entity<EventEntity>().HasData(eventEntities);
|
||||
}
|
||||
|
||||
|
||||
// Methods to generate random data to stub the database
|
||||
public Guid GenerateGuid()
|
||||
{
|
||||
return Guid.NewGuid();
|
||||
}
|
||||
|
||||
public string GenerateRandomString(int length)
|
||||
{
|
||||
var random = new Random();
|
||||
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
return new string(Enumerable.Repeat(chars, length)
|
||||
.Select(s => s[random.Next(s.Length)]).ToArray());
|
||||
}
|
||||
|
||||
public int GenerateRandomInt(int min, int max)
|
||||
{
|
||||
var random = new Random();
|
||||
return random.Next(min, max);
|
||||
}
|
||||
|
||||
public DateTime GenerateRandomDate()
|
||||
{
|
||||
var random = new Random();
|
||||
var start = new DateTime(1995, 1, 1);
|
||||
var range = (DateTime.Today - start).Days;
|
||||
return start.AddDays(random.Next(range));
|
||||
}
|
||||
|
||||
public bool GenerateRandomBool()
|
||||
{
|
||||
var random = new Random();
|
||||
return random.Next(0, 2) > 0;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
<div align = center>
|
||||
<h1>SAE Alica</h1>
|
||||
<h2>Entity Framework - API dotnet</h2>
|
||||
</div>
|
||||
|
||||
|
||||
<div align = center>
|
||||
|
||||
|
||||
|
||||
---
|
||||

|
||||

|
||||

|
||||

|
||||

|
||||
|
||||
</div>
|
||||
|
||||
# Table des matières
|
||||
[Présentation](#présentation) | [Répartition du Git](#répartition-du-git) | [Prérequis](#prérequis-pour-le-projet-entity-framework) | [Getting Started](#premiers-pas) | [Fabriqué avec](#fabriqué-avec) | [Contributeurs](#contributeurs) |
|
||||
|
||||
|
||||
## Présentation
|
||||
|
||||
### Récapitulatif du Projet
|
||||
|
||||
Ce repot représente le rendu en API et Entity Framework, dans le cadre de notre SAe Alica.
|
||||
<br>
|
||||
En ce qui concerne la notation, notre travaille se trouve sur la branche **dev**
|
||||
|
||||
## Prérequis pour le projet Entity Framework
|
||||
|
||||
* [Visual Studio](https://visualstudio.microsoft.com/) - Environnement de développement intégré (IDE) recommandé pour les projets .NET si vous êtes sur Window
|
||||
* [Rider](https://www.jetbrains.com/fr-fr/rider/) - Environnement de développement intégré (IDE) recommandé pour les projets .NET si vous êtes sur MAC
|
||||
* [Code First](https://codefirst.iut.uca.fr/) - Outil de versioning
|
||||
|
||||
## Premiers Pas
|
||||
#### Warning : Le développement se faire sur la branche **dev**
|
||||
1. Cloner le dépôt Git : *git clone 'https://codefirst.iut.uca.fr/git/sae.alica/API_dotnet.git'*
|
||||
2. Aller dans le dossier API_dotnet : *cd API_dotnet/*
|
||||
3. Changer de branche afin d'avoir la dernière version du projet : *git checkout dev*
|
||||
4. Verifier bien que vous êtes sur la branche dev : *git branch*
|
||||
5. Lancer le projet avec votre IDE (Rider ou Visual Studio Code)
|
||||
6. Lancer la solution
|
||||
7. Profiter des fonctionnalités de l'API via swagger
|
||||
|
||||
|
||||
## Fabriqué avec
|
||||
* [Visual Studio](https://visualstudio.microsoft.com/) - Environnement de développement intégré (IDE) recommandé pour les projets .NET
|
||||
* [C#](https://learn.microsoft.com/fr-fr/dotnet/csharp/) - Documentation C#
|
||||
* [Entity Framework](https://learn.microsoft.com/fr-fr/ef/) - Documentation EF
|
||||
* [CodeFirst](https://codefirst.iut.uca.fr/) - Gitea
|
||||
|
||||
|
||||
## Contributeurs
|
||||
* [Léo TUAILLON](https://codefirst.iut.uca.fr/git/leo.tuaillon)
|
||||
* [Thomas MUZARD](https://codefirst.iut.uca.fr/git/thomas.muzard)
|
After Width: | Height: | Size: 10 KiB |
@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<ActiveDebugProfile>https</ActiveDebugProfile>
|
||||
<Controller_SelectedScaffolderID>MinimalApiScaffolder</Controller_SelectedScaffolderID>
|
||||
<Controller_SelectedScaffolderCategoryPath>root/Common/Api</Controller_SelectedScaffolderCategoryPath>
|
||||
<WebStackScaffolding_ControllerDialogWidth>650</WebStackScaffolding_ControllerDialogWidth>
|
||||
</PropertyGroup>
|
||||
</Project>
|
@ -0,0 +1,6 @@
|
||||
@API_dotnet_HostAddress = http://localhost:5166
|
||||
|
||||
GET {{API_dotnet_HostAddress}}/weatherforecast/
|
||||
Accept: application/json
|
||||
|
||||
###
|
@ -0,0 +1,45 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Server.Dto.Response;
|
||||
using Asp.Versioning;
|
||||
using Server.IServices;
|
||||
using Shared.Criteria;
|
||||
|
||||
namespace Server.Controller.v1;
|
||||
|
||||
[ApiController]
|
||||
[ApiVersion("1.0")]
|
||||
[Route("api/v{version:apiVersion}/alumni-restricted")]
|
||||
public class AlumniRestrictedController : ControllerBase
|
||||
{
|
||||
private readonly ILogger<AlumniRestrictedController> _logger;
|
||||
private IAlumnisService _dataServices;
|
||||
|
||||
public AlumniRestrictedController(ILogger<AlumniRestrictedController> logger, IAlumnisService dataServices)
|
||||
{
|
||||
_logger = logger;
|
||||
_dataServices = dataServices;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[ProducesResponseType(typeof(IEnumerable<ResponseAlumniDto>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[RequireHttps]
|
||||
public async Task<IActionResult> GetAlumniRestricted([FromQuery] string? lastname, [FromQuery] int page = 1, [FromQuery] int size = 5,
|
||||
[FromQuery] AlumniOrderCriteria orderCriteria = AlumniOrderCriteria.None, [FromQuery] bool ascending = true)
|
||||
{
|
||||
var alumni = await _dataServices.GetAlumnisRestricted(lastname, page, size, orderCriteria, ascending);
|
||||
if( alumni.Count == 0) return NoContent();
|
||||
return Ok(alumni.Alumnis);
|
||||
}
|
||||
|
||||
|
||||
[HttpGet("{id}")]
|
||||
[ProducesResponseType(typeof(ResponseAlumniDto), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[RequireHttps]
|
||||
public IActionResult GetAlumniRestrictedById(string id)
|
||||
{
|
||||
var alumni = _dataServices.GetAlumniRestrictedById(id);
|
||||
return alumni.Result == null ? NotFound() : Ok(alumni.Result);
|
||||
}
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Server.Dto.Request;
|
||||
using Server.Dto.Response;
|
||||
using Asp.Versioning;
|
||||
using Server.IServices;
|
||||
using Shared.Criteria;
|
||||
|
||||
namespace Server.Controller.v1;
|
||||
|
||||
[ApiController]
|
||||
[ApiVersion("1.0")]
|
||||
[Route("api/v{version:apiVersion}/[controller]")]
|
||||
public class AlumnisController : ControllerBase
|
||||
{
|
||||
private readonly ILogger<AlumnisController> _logger;
|
||||
private IAlumnisService _dataServices;
|
||||
|
||||
public AlumnisController(ILogger<AlumnisController> logger, IAlumnisService dataServices)
|
||||
{
|
||||
_logger = logger;
|
||||
_dataServices = dataServices;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[ProducesResponseType(typeof(IEnumerable<ResponseAlumniDto>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[RequireHttps]
|
||||
public IActionResult GetAlumni([FromQuery] string? lastname, [FromQuery] int page = 1, [FromQuery] int size = 5,
|
||||
[FromQuery] AlumniOrderCriteria orderCriteria = AlumniOrderCriteria.None, [FromQuery] bool ascending = true)
|
||||
{
|
||||
var alumni = _dataServices.GetAlumnis(lastname, page, size, orderCriteria, ascending);
|
||||
return alumni.Result.Count == 0 ? NoContent() : Ok(alumni.Result.Alumnis);
|
||||
}
|
||||
|
||||
|
||||
[HttpPost()]
|
||||
[ProducesResponseType(typeof(ResponseAlumniDto), StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[RequireHttps]
|
||||
public IActionResult CreateAlumni([FromBody] RequestAlumniDto alumniDto)
|
||||
{
|
||||
var alumni = _dataServices.CreateAlumni(alumniDto);
|
||||
return alumni.Result == null ? BadRequest() : CreatedAtAction(nameof(CreateAlumni), alumni);
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
[ProducesResponseType(typeof(ResponseAlumniDto), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
|
||||
[RequireHttps]
|
||||
public IActionResult GetAlumniById(string id)
|
||||
{
|
||||
var alumni = _dataServices.GetAlumniById(id);
|
||||
return alumni.Result == null ? NotFound() : Ok(alumni);
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
|
||||
USER $APP_UID
|
||||
WORKDIR /app
|
||||
EXPOSE 8080
|
||||
EXPOSE 8081
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
WORKDIR /src
|
||||
COPY ["Server/Server.csproj", "Server/"]
|
||||
COPY ["Infrastructure/Infrastructure.csproj", "Infrastructure/"]
|
||||
COPY ["Shared/Shared.csproj", "Shared/"]
|
||||
COPY ["UnitTestEF/UnitTestEF.csproj", "UnitTestEF/"]
|
||||
COPY ["TestAPI/TestAPI.csproj", "TestAPI/"]
|
||||
RUN dotnet restore "Server/Server.csproj"
|
||||
COPY . .
|
||||
WORKDIR "/src/Server"
|
||||
RUN dotnet build "Server.csproj" -c $BUILD_CONFIGURATION -o /app/build
|
||||
|
||||
FROM build AS publish
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
RUN dotnet publish "Server.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
|
||||
|
||||
FROM base AS final
|
||||
WORKDIR /app
|
||||
COPY --from=publish /app/publish .
|
||||
ENTRYPOINT ["dotnet", "Server.dll"]
|
@ -0,0 +1,30 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Server.Dto.Request;
|
||||
|
||||
public class RequestAlumniDto
|
||||
{
|
||||
[EmailAddress(ErrorMessage = "Invalid Email")]
|
||||
[Required(ErrorMessage = "Email is required")]
|
||||
public string Email { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "Password is required")]
|
||||
public string Password { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "EntryYear is required")]
|
||||
public string EntryYear { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "FirstName is required")]
|
||||
public string FirstName { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "LastName is required")]
|
||||
public string LastName { get; set; }
|
||||
|
||||
public string? LinkedinUrl { get; set; }
|
||||
|
||||
public string? GithubUrl { get; set; }
|
||||
|
||||
public string? PortfolioUrl { get; set; }
|
||||
|
||||
public string? ImageUrl { get; set; }
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Server.Dto.Request;
|
||||
|
||||
public class RequestEventDto
|
||||
{
|
||||
[Required(ErrorMessage = "AlumniId is required")]
|
||||
public string AlumniId { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "Title is required")]
|
||||
public string Title { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "Description is required")]
|
||||
public string Description { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "Date is required")]
|
||||
public DateTime Date { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "NumberOfParticipants is required")]
|
||||
public int nbMaxRegistrations { get; set; }
|
||||
|
||||
public string? ImageUrl { get; set; }
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Server.Dto.Request;
|
||||
|
||||
public class RequestExperienceDto
|
||||
{
|
||||
[Required(ErrorMessage = "AlumniId is required")]
|
||||
public string AlumniId { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "Title of post is required")]
|
||||
public string Title { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "StartingDate is required")]
|
||||
public DateTime StartingDate { get; set; }
|
||||
|
||||
public DateTime? EndingDate { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "Company's name is required")]
|
||||
public string CompanyName { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "CurrentFormation is required")]
|
||||
public bool CurrentJob { get; set; }
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Server.Dto.Request;
|
||||
|
||||
public class RequestFormationDto
|
||||
{
|
||||
[Required(ErrorMessage = "AlumniId is required")]
|
||||
public string AlumniId { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "Name of formation is required")]
|
||||
public string Name { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "StartingDate is required")]
|
||||
public DateTime StartingDate { get; set; }
|
||||
|
||||
public DateTime? EndingDate { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "School's name is required")]
|
||||
public string SchoolName { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "CurrentFormation is required")]
|
||||
public bool CurrentFormation { get; set; }
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
using Shared.Enums;
|
||||
|
||||
namespace Server.Dto.Response;
|
||||
|
||||
public class ResponseAlumniDto
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string Email { get; set; }
|
||||
public ERole ERole { get; set; }
|
||||
public string EntryYear { get; set; }
|
||||
public string FirstName { get; set; }
|
||||
public string LastName { get; set; }
|
||||
public string? LinkedinUrl { get; set; }
|
||||
public string? GithubUrl { get; set; }
|
||||
public string? PortfolioUrl { get; set; }
|
||||
public string? ImageUrl { get; set; }
|
||||
public IEnumerable<ResponseExperienceDto> Experiences { get; set; } = new List<ResponseExperienceDto>();
|
||||
public IEnumerable<ResponseFormationDto> Formations { get; set; } = new List<ResponseFormationDto>();
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
namespace Server.Dto.Response;
|
||||
|
||||
public class ResponseEventDto
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string? AlumniId { get; set; }
|
||||
public string Title { get; set; }
|
||||
public string? ImageUrl { get; set; }
|
||||
public string Description { get; set; }
|
||||
public DateTime Date { get; set; }
|
||||
public int nbMaxRegistrations { get; set; }
|
||||
public int nbRegistrations { get; set; }
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
namespace Server.Dto.Response;
|
||||
|
||||
public class ResponseExperienceDto
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string? AlumniId { get; set; }
|
||||
public string Title { get; set; }
|
||||
public DateTime StartingDate { get; set; }
|
||||
public DateTime? EndingDate { get; set; }
|
||||
public string CompanyName { get; set; }
|
||||
public bool CurrentJob { get; set; }
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
namespace Server.Dto.Response;
|
||||
|
||||
public class ResponseFormationDto
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string? AlumniId { get; set; }
|
||||
public string Name { get; set; }
|
||||
public DateTime StartingDate { get; set; }
|
||||
public DateTime? EndingDate { get; set; }
|
||||
public string SchoolName { get; set; }
|
||||
public bool CurrentFormation { get; set; }
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
namespace Server.Dto.Response;
|
||||
|
||||
public class ResponseRestrictedAlumniDto
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string FirstName { get; set; }
|
||||
public string LastName { get; set; }
|
||||
public string? LinkedinUrl { get; set; }
|
||||
public string? ImageUrl { get; set; }
|
||||
}
|
Binary file not shown.
@ -0,0 +1,17 @@
|
||||
using Server.Dto.Request;
|
||||
using Server.Dto.Response;
|
||||
using Shared.Criteria;
|
||||
|
||||
namespace Server.IServices;
|
||||
|
||||
public interface IAlumnisService
|
||||
{
|
||||
Task<(long Count, IEnumerable<ResponseRestrictedAlumniDto> Alumnis)> GetAlumnisRestricted(string? lastname, int page, int size, AlumniOrderCriteria orderCriteria = AlumniOrderCriteria.None, bool ascending = true);
|
||||
Task<ResponseRestrictedAlumniDto?> GetAlumniRestrictedById(string id);
|
||||
Task<ResponseAlumniDto?> GetAlumniById(string id);
|
||||
Task<(long Count, IEnumerable<ResponseAlumniDto> Alumnis)> GetAlumnis
|
||||
(string? lastname, int page, int size, AlumniOrderCriteria orderCriteria = AlumniOrderCriteria.None,
|
||||
bool ascending = true);
|
||||
|
||||
Task<ResponseAlumniDto?> CreateAlumni(RequestAlumniDto alumni);
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
using Server.Dto.Request;
|
||||
using Server.Dto.Response;
|
||||
using Shared.Criteria;
|
||||
|
||||
namespace Server.IServices;
|
||||
|
||||
public interface IEventsService
|
||||
{
|
||||
Task<ResponseEventDto?> GetEventById(string id);
|
||||
|
||||
Task<(long Count, IEnumerable<ResponseEventDto> Events)> GetEvents
|
||||
(string? title, int page, int size, EventOrderCriteria orderCriteria = EventOrderCriteria.Date,
|
||||
bool ascending = true);
|
||||
|
||||
Task<ResponseEventDto?> CreateEvent(RequestEventDto evt);
|
||||
Task<ResponseEventDto?> UpdateEvent(string id, RequestEventDto evt);
|
||||
Task<bool> DeleteEvent(string id);
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
using Server.Dto.Request;
|
||||
using Server.Dto.Response;
|
||||
using Shared.Criteria;
|
||||
|
||||
namespace Server.IServices;
|
||||
|
||||
public interface IExperiencesService
|
||||
{
|
||||
Task<ResponseExperienceDto?> GetExperienceById(string id);
|
||||
|
||||
Task<(long Count, IEnumerable<ResponseExperienceDto> Experiences)> GetExperiences
|
||||
(string? title, int page, int size,
|
||||
ExperienceOrderCriteria orderCriteria = ExperienceOrderCriteria.EndDate, bool ascending = true);
|
||||
|
||||
Task<ResponseExperienceDto?> CreateExperience(RequestExperienceDto experience);
|
||||
Task<ResponseExperienceDto?> UpdateExperience(string id, RequestExperienceDto experience);
|
||||
Task<bool> DeleteExperience(string id);
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
using Server.Dto.Request;
|
||||
using Server.Dto.Response;
|
||||
using Shared.Criteria;
|
||||
|
||||
namespace Server.IServices;
|
||||
|
||||
public interface IFormationsService
|
||||
{
|
||||
Task<ResponseFormationDto?> GetFormationById(string id);
|
||||
|
||||
Task<(long Count, IEnumerable<ResponseFormationDto> Formations)> GetFormations
|
||||
(string? name, int page, int size, FormationOrderCriteria orderCriteria, bool ascending = true);
|
||||
|
||||
Task<ResponseFormationDto?> CreateFormation(RequestFormationDto formation);
|
||||
Task<ResponseFormationDto?> UpdateFormation(string id, RequestFormationDto formation);
|
||||
Task<bool> DeleteFormation(string id);
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
using Infrastructure.Entities;
|
||||
using Server.Dto.Request;
|
||||
using Server.Dto.Response;
|
||||
using Shared.Enums;
|
||||
|
||||
namespace Server.Mappers;
|
||||
|
||||
public static class AlumnisMappers
|
||||
{
|
||||
public static ResponseAlumniDto ToDto(this User alumni)
|
||||
{
|
||||
return new ResponseAlumniDto
|
||||
{
|
||||
Id = alumni.Id,
|
||||
Email = alumni.Email,
|
||||
ERole = alumni.Role.ToERole(),
|
||||
EntryYear = alumni.EntryYear,
|
||||
FirstName = alumni.FirstName,
|
||||
LastName = alumni.LastName,
|
||||
LinkedinUrl = alumni.Linkedin,
|
||||
GithubUrl = alumni.Github,
|
||||
PortfolioUrl = alumni.WebSite,
|
||||
Experiences = alumni.Experiences.Select(e => e.ToDto()),
|
||||
Formations = alumni.Formations.Select(f => f.ToDto())
|
||||
};
|
||||
}
|
||||
|
||||
public static ResponseRestrictedAlumniDto ToDtoRestricted(this User alumni)
|
||||
{
|
||||
return new ResponseRestrictedAlumniDto
|
||||
{
|
||||
Id = alumni.Id,
|
||||
FirstName = alumni.FirstName,
|
||||
LastName = alumni.LastName,
|
||||
LinkedinUrl = alumni.Linkedin
|
||||
};
|
||||
}
|
||||
|
||||
public static User ToEntity(this RequestAlumniDto alumni)
|
||||
{
|
||||
return new User
|
||||
{
|
||||
Email = alumni.Email,
|
||||
Password = alumni.Password,
|
||||
EntryYear = alumni.EntryYear,
|
||||
FirstName = alumni.FirstName,
|
||||
LastName = alumni.LastName,
|
||||
Linkedin = alumni.LinkedinUrl,
|
||||
Github = alumni.GithubUrl,
|
||||
WebSite = alumni.PortfolioUrl,
|
||||
Role = "USER"
|
||||
};
|
||||
}
|
||||
|
||||
public static ERole ToERole(this string role)
|
||||
{
|
||||
return role switch
|
||||
{
|
||||
"ADMIN" => ERole.ADMIN,
|
||||
"MODERATOR" => ERole.MODERATOR,
|
||||
"USER" => ERole.USER
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
using Infrastructure.Entities;
|
||||
using Server.Dto.Request;
|
||||
using Server.Dto.Response;
|
||||
|
||||
namespace Server.Mappers;
|
||||
|
||||
public static class EventsMappers
|
||||
{
|
||||
public static ResponseEventDto ToDto(this EventEntity evt)
|
||||
{
|
||||
return new ResponseEventDto
|
||||
{
|
||||
Id = evt.Id,
|
||||
Title = evt.Title,
|
||||
Description = evt.Description,
|
||||
Date = evt.Date,
|
||||
nbMaxRegistrations = evt.nbPlaces,
|
||||
nbRegistrations = evt.Participants.Count
|
||||
};
|
||||
}
|
||||
|
||||
public static EventEntity ToEntity(this RequestEventDto evt)
|
||||
{
|
||||
return new EventEntity
|
||||
{
|
||||
Title = evt.Title,
|
||||
Description = evt.Description,
|
||||
Date = evt.Date,
|
||||
nbPlaces = evt.nbMaxRegistrations
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
using Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Server.IServices;
|
||||
using Asp.Versioning;
|
||||
using Server.Services;
|
||||
using Microsoft.OpenApi.Models;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
|
||||
builder.Services.AddScoped<IAlumnisService, AlumniService>();
|
||||
builder.Services.AddScoped<IExperiencesService, ExperienceServices>();
|
||||
builder.Services.AddScoped<IFormationsService, FormationServices>();
|
||||
builder.Services.AddScoped<IEventsService, EventServices>();
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddDbContext<AlumniDbContext>(options =>
|
||||
options.UseSqlite("Data Source=FirstTest.db"));
|
||||
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
//allow us to have lowercase urls (/alica instead of /Alica)
|
||||
builder.Services.AddRouting(options => options.LowercaseUrls = true);
|
||||
|
||||
// Add API's versionning
|
||||
builder.Services.AddApiVersioning(options =>
|
||||
{
|
||||
options.DefaultApiVersion = new ApiVersion(1, 0);
|
||||
options.AssumeDefaultVersionWhenUnspecified = true;
|
||||
options.ReportApiVersions = true;
|
||||
})
|
||||
.AddApiExplorer(options =>
|
||||
{
|
||||
options.GroupNameFormat = "'v'VVV";
|
||||
options.SubstituteApiVersionInUrl = true;
|
||||
});
|
||||
|
||||
builder.Services.AddSwaggerGen(c =>
|
||||
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Alica API First Version", Version = "v1" })
|
||||
);
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
// app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
@ -0,0 +1,36 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<StartWorkingDirectory>$(MSBuildProjectDirectory)</StartWorkingDirectory>
|
||||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.1" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.1" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.1" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Infrastructure\Infrastructure.csproj" />
|
||||
<ProjectReference Include="..\Shared\Shared.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="..\.dockerignore">
|
||||
<Link>.dockerignore</Link>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<ActiveDebugProfile>https</ActiveDebugProfile>
|
||||
<Controller_SelectedScaffolderID>ApiControllerEmptyScaffolder</Controller_SelectedScaffolderID>
|
||||
<Controller_SelectedScaffolderCategoryPath>root/Common/Api</Controller_SelectedScaffolderCategoryPath>
|
||||
</PropertyGroup>
|
||||
</Project>
|
@ -0,0 +1,83 @@
|
||||
using Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Server.Dto.Request;
|
||||
using Server.Dto.Response;
|
||||
using Server.IServices;
|
||||
using Server.Mappers;
|
||||
using Shared.Criteria;
|
||||
|
||||
namespace Server.Services;
|
||||
|
||||
public class AlumniService(AlumniDbContext context) : IAlumnisService
|
||||
{
|
||||
public Task<(long Count, IEnumerable<ResponseRestrictedAlumniDto> Alumnis)> GetAlumnisRestricted(string? lastname, int page, int size,
|
||||
AlumniOrderCriteria orderCriteria = AlumniOrderCriteria.None, bool ascending = true)
|
||||
{
|
||||
var query = context.Alumni.Skip((page-1) * size).Take(size);
|
||||
if (lastname != null)
|
||||
{
|
||||
query = context.Alumni.Where(e => e.LastName.Contains(lastname)).Skip((page-1) * size).Take(size);
|
||||
}
|
||||
switch (orderCriteria)
|
||||
{
|
||||
case AlumniOrderCriteria.Name:
|
||||
query = ascending ? query.OrderBy(e => e.LastName) : query.OrderByDescending(e => e.LastName);
|
||||
break;
|
||||
case AlumniOrderCriteria.None:
|
||||
break;
|
||||
}
|
||||
var count = query.LongCount();
|
||||
return Task.FromResult((count, query.AsEnumerable().Select(e => e.ToDtoRestricted())));
|
||||
}
|
||||
|
||||
public Task<ResponseRestrictedAlumniDto?> GetAlumniRestrictedById(string id)
|
||||
{
|
||||
var result = context.Alumni.FirstOrDefault(e => e.Id == id);
|
||||
return Task.FromResult(result?.ToDtoRestricted());
|
||||
}
|
||||
|
||||
public Task<ResponseAlumniDto?> GetAlumniById(string id)
|
||||
{
|
||||
var result = context.Alumni
|
||||
.Include(a => a.Experiences)
|
||||
.Include(a => a.Formations)
|
||||
.FirstOrDefault(e => e.Id == id);
|
||||
return Task.FromResult(result?.ToDto());
|
||||
}
|
||||
|
||||
public Task<(long Count, IEnumerable<ResponseAlumniDto> Alumnis)> GetAlumnis(string? lastname, int page, int size,
|
||||
AlumniOrderCriteria orderCriteria = AlumniOrderCriteria.None,
|
||||
bool ascending = true)
|
||||
{
|
||||
var query = context.Alumni
|
||||
.Include(a => a.Experiences)
|
||||
.Include(a => a.Formations)
|
||||
.Skip((page-1) * size)
|
||||
.Take(size);
|
||||
if (lastname != null)
|
||||
{
|
||||
query = context.Alumni.Where(e => e.LastName.Contains(lastname))
|
||||
.Include(a => a.Experiences)
|
||||
.Include(a => a.Formations)
|
||||
.Skip((page-1) * size)
|
||||
.Take(size);
|
||||
}
|
||||
switch (orderCriteria)
|
||||
{
|
||||
case AlumniOrderCriteria.Name:
|
||||
query = ascending ? query.OrderBy(e => e.LastName) : query.OrderByDescending(e => e.LastName);
|
||||
break;
|
||||
case AlumniOrderCriteria.None:
|
||||
break;
|
||||
}
|
||||
var count = query.LongCount();
|
||||
return Task.FromResult((count, query.AsEnumerable().Select(e => e.ToDto())));
|
||||
}
|
||||
|
||||
public Task<ResponseAlumniDto?> CreateAlumni(RequestAlumniDto alumni)
|
||||
{
|
||||
var result = context.Alumni.AddAsync(alumni.ToEntity());
|
||||
context.SaveChangesAsync();
|
||||
return Task.FromResult(result.Result?.Entity.ToDto());
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
namespace Shared.Criteria;
|
||||
|
||||
public enum AlumniOrderCriteria
|
||||
{
|
||||
Name,
|
||||
None
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
namespace Shared.Criteria;
|
||||
|
||||
public enum EventOrderCriteria
|
||||
{
|
||||
Title,
|
||||
Date,
|
||||
NbPlaces
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
namespace Shared.Criteria;
|
||||
|
||||
public enum ExperienceOrderCriteria
|
||||
{
|
||||
Title,
|
||||
StartDate,
|
||||
EndDate
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
namespace Shared.Criteria;
|
||||
|
||||
public enum FormationOrderCriteria
|
||||
{
|
||||
Name,
|
||||
StartDate,
|
||||
EndDate,
|
||||
SchoolName,
|
||||
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
namespace Shared.Enums;
|
||||
|
||||
public enum EContract
|
||||
{
|
||||
CDI,
|
||||
CDD,
|
||||
INTERNSHIP,
|
||||
APPRENTICESHIP
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
namespace Shared.Enums;
|
||||
|
||||
public enum ELevel
|
||||
{
|
||||
JUNIOR,
|
||||
SENIOR,
|
||||
INDEFERENT
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
namespace Shared.Enums;
|
||||
|
||||
public enum ERole
|
||||
{
|
||||
ADMIN,
|
||||
MODERATOR,
|
||||
USER
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
namespace Shared.Enums;
|
||||
|
||||
public enum EStudies
|
||||
{
|
||||
BAC_2,
|
||||
BAC_3,
|
||||
BAC_5,
|
||||
INDIFERENT
|
||||
}
|
@ -0,0 +1,130 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using Server.Controller.v1;
|
||||
using Server.Dto.Response;
|
||||
using Server.IServices;
|
||||
using Shared.Criteria;
|
||||
|
||||
namespace TestAPI.ControllerTests
|
||||
{
|
||||
public class AlumniControllerTest
|
||||
{
|
||||
private readonly Mock<ILogger<AlumnisController>> _loggerMock = new Mock<ILogger<AlumnisController>>();
|
||||
private readonly Mock<IAlumnisService> _alumnisServiceMock = new Mock<IAlumnisService>();
|
||||
|
||||
[Fact]
|
||||
public void GetAlumni_NoAlumni_ReturnsNoContent()
|
||||
{
|
||||
// Arrange
|
||||
_alumnisServiceMock.Setup(service => service.GetAlumnis(null, 1, 5, AlumniOrderCriteria.None, true))
|
||||
.ReturnsAsync((0, Enumerable.Empty<ResponseAlumniDto>()));
|
||||
|
||||
var controller = new AlumnisController(_loggerMock.Object, _alumnisServiceMock.Object);
|
||||
|
||||
// Act
|
||||
var result = controller.GetAlumni(null);
|
||||
|
||||
// Assert
|
||||
var actionResult = Assert.IsType<NoContentResult>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetAlumni_AlumniExists_ReturnsOk()
|
||||
{
|
||||
// Arrange
|
||||
var alumniList = new List<ResponseAlumniDto>
|
||||
{
|
||||
new ResponseAlumniDto { Id = "1", FirstName = "John", LastName = "Doe" }
|
||||
};
|
||||
_alumnisServiceMock.Setup(service => service.GetAlumnis(null, 1, 5, AlumniOrderCriteria.None, true))
|
||||
.ReturnsAsync((alumniList.Count, alumniList));
|
||||
|
||||
var controller = new AlumnisController(_loggerMock.Object, _alumnisServiceMock.Object);
|
||||
|
||||
// Act
|
||||
var result = controller.GetAlumni(null);
|
||||
|
||||
// Assert
|
||||
var actionResult = Assert.IsType<OkObjectResult>(result);
|
||||
var returnValue = Assert.IsAssignableFrom<IEnumerable<ResponseAlumniDto>>(actionResult.Value);
|
||||
Assert.Single(returnValue);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetAlumniById_AlumniExists_ReturnsOk()
|
||||
{
|
||||
// Arrange
|
||||
var alumniId = "1";
|
||||
var alumniDto = new ResponseAlumniDto { Id = alumniId };
|
||||
_alumnisServiceMock.Setup(service => service.GetAlumniById(alumniId)).ReturnsAsync(alumniDto);
|
||||
|
||||
var controller = new AlumnisController(_loggerMock.Object, _alumnisServiceMock.Object);
|
||||
|
||||
// Act
|
||||
var result = controller.GetAlumniById(alumniId);
|
||||
|
||||
// Assert
|
||||
var actionResult = Assert.IsType<OkObjectResult>(result);
|
||||
var returnValue = Assert.IsType<Task<ResponseAlumniDto>>(actionResult.Value);
|
||||
Assert.Equal(alumniId, returnValue.Result.Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetAlumniById_AlumniDoesNotExist_ReturnsNotFound()
|
||||
{
|
||||
// Arrange
|
||||
var alumniId = "1";
|
||||
_alumnisServiceMock.Setup(service => service.GetAlumniById(alumniId)).ReturnsAsync((ResponseAlumniDto)null);
|
||||
|
||||
var controller = new AlumnisController(_loggerMock.Object, _alumnisServiceMock.Object);
|
||||
|
||||
// Act
|
||||
var result = controller.GetAlumniById(alumniId);
|
||||
|
||||
// Assert
|
||||
var actionResult = Assert.IsType<NotFoundResult>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetAlumni_ByLastName_NoAlumni_ReturnsNoContent()
|
||||
{
|
||||
// Arrange
|
||||
var lastName = "Doe";
|
||||
_alumnisServiceMock.Setup(service => service.GetAlumnis(lastName, 1, 5, AlumniOrderCriteria.None, true))
|
||||
.ReturnsAsync((0, new List<ResponseAlumniDto>()));
|
||||
|
||||
var controller = new AlumnisController(_loggerMock.Object, _alumnisServiceMock.Object);
|
||||
|
||||
// Act
|
||||
var result = controller.GetAlumni(lastName);
|
||||
|
||||
// Assert
|
||||
var actionResult = Assert.IsType<NoContentResult>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetAlumni_ByLastName_AlumniExist_ReturnsOk()
|
||||
{
|
||||
// Arrange
|
||||
var lastName = "Doe";
|
||||
var alumniList = new List<ResponseAlumniDto>
|
||||
{
|
||||
new ResponseAlumniDto { Id = "1", LastName = "Doe" }
|
||||
};
|
||||
_alumnisServiceMock.Setup(service => service.GetAlumnis(lastName, 1, 5, AlumniOrderCriteria.None, true))
|
||||
.ReturnsAsync((alumniList.Count, alumniList));
|
||||
|
||||
var controller = new AlumnisController(_loggerMock.Object, _alumnisServiceMock.Object);
|
||||
|
||||
// Act
|
||||
var result = controller.GetAlumni(lastName);
|
||||
|
||||
// Assert
|
||||
var actionResult = Assert.IsType<OkObjectResult>(result);
|
||||
var returnValue = Assert.IsAssignableFrom<IEnumerable<ResponseAlumniDto>>(actionResult.Value);
|
||||
Assert.Single(returnValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,101 @@
|
||||
using Xunit;
|
||||
using Moq;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using Infrastructure;
|
||||
using Infrastructure.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Server.Dto.Request;
|
||||
using Server.Services;
|
||||
using Shared.Criteria;
|
||||
|
||||
public class AlumniServiceTests
|
||||
{
|
||||
private readonly Mock<AlumniDbContext> _mockContext;
|
||||
private readonly AlumniService _alumniService;
|
||||
|
||||
public AlumniServiceTests()
|
||||
{
|
||||
var alumni = new List<User>
|
||||
{
|
||||
new User { Id = "1", LastName = "Alumni1" },
|
||||
new User { Id = "2", LastName = "Alumni2" }
|
||||
}.AsQueryable();
|
||||
|
||||
var mockSet = new Mock<DbSet<User>>();
|
||||
mockSet.As<IQueryable<User>>().Setup(m => m.Provider).Returns(alumni.Provider);
|
||||
mockSet.As<IQueryable<User>>().Setup(m => m.Expression).Returns(alumni.Expression);
|
||||
mockSet.As<IQueryable<User>>().Setup(m => m.ElementType).Returns(alumni.ElementType);
|
||||
mockSet.As<IQueryable<User>>().Setup(m => m.GetEnumerator()).Returns(alumni.GetEnumerator());
|
||||
|
||||
_mockContext = new Mock<AlumniDbContext>();
|
||||
_mockContext.Setup(c => c.Alumni).Returns(mockSet.Object);
|
||||
|
||||
_alumniService = new AlumniService(_mockContext.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAlumniById_ReturnsNull_WhenIdDoesNotExist()
|
||||
{
|
||||
var result = await _alumniService.GetAlumniById("3");
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task GetAlumnis_ReturnsEmpty_WhenLastNameDoesNotExist()
|
||||
{
|
||||
var result = await _alumniService.GetAlumnis("Alumni3", 1, 1, AlumniOrderCriteria.Name, true);
|
||||
Assert.Empty(result.Alumnis);
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task GetAlumnisRestricted_NoMatchingLastName_ReturnsEmpty()
|
||||
{
|
||||
// Arrange
|
||||
string lastName = "NonExistingLastName";
|
||||
var page = 1;
|
||||
var size = 5;
|
||||
|
||||
// Act
|
||||
var result = await _alumniService.GetAlumnisRestricted(lastName, page, size);
|
||||
|
||||
// Assert
|
||||
Assert.Empty(result.Alumnis);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAlumnisRestricted_MatchingLastName_ReturnsAlumni()
|
||||
{
|
||||
// Arrange
|
||||
string lastName = "Alumni1";
|
||||
var page = 1;
|
||||
var size = 5;
|
||||
|
||||
// Act
|
||||
var result = await _alumniService.GetAlumnisRestricted(lastName, page, size);
|
||||
|
||||
// Assert
|
||||
Assert.NotEmpty(result.Alumnis);
|
||||
Assert.Equal(1, result.Alumnis.Count());
|
||||
Assert.Equal("Alumni1", result.Alumnis.First().LastName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAlumnisRestricted_PaginationWorksCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
string lastName = null;
|
||||
var page = 2;
|
||||
var size = 1;
|
||||
|
||||
// Act
|
||||
var result = await _alumniService.GetAlumnisRestricted(lastName, page, size);
|
||||
|
||||
// Assert
|
||||
Assert.NotEmpty(result.Alumnis);
|
||||
Assert.Single(result.Alumnis);
|
||||
Assert.Equal("Alumni2", result.Alumnis.First().LastName);
|
||||
}
|
||||
}
|
@ -0,0 +1,97 @@
|
||||
using Xunit;
|
||||
using Moq;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using Infrastructure;
|
||||
using Infrastructure.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Server.Dto.Request;
|
||||
using Server.Services;
|
||||
using Shared.Criteria;
|
||||
|
||||
public class EventServicesTests
|
||||
{
|
||||
private readonly Mock<AlumniDbContext> _mockContext;
|
||||
private readonly EventServices _eventServices;
|
||||
|
||||
public EventServicesTests()
|
||||
{
|
||||
var events = new List<EventEntity>
|
||||
{
|
||||
new EventEntity { Id = "1", Title = "Event1", Description = "Description1", Date = DateTime.Now, nbPlaces = 100 },
|
||||
new EventEntity { Id = "2", Title = "Event2", Description = "Description2", Date = DateTime.Now.AddDays(1), nbPlaces = 200 }
|
||||
}.AsQueryable();
|
||||
|
||||
var mockSet = new Mock<DbSet<EventEntity>>();
|
||||
mockSet.As<IQueryable<EventEntity>>().Setup(m => m.Provider).Returns(events.Provider);
|
||||
mockSet.As<IQueryable<EventEntity>>().Setup(m => m.Expression).Returns(events.Expression);
|
||||
mockSet.As<IQueryable<EventEntity>>().Setup(m => m.ElementType).Returns(events.ElementType);
|
||||
mockSet.As<IQueryable<EventEntity>>().Setup(m => m.GetEnumerator()).Returns(events.GetEnumerator());
|
||||
|
||||
_mockContext = new Mock<AlumniDbContext>();
|
||||
_mockContext.Setup(c => c.Events).Returns(mockSet.Object);
|
||||
|
||||
_eventServices = new EventServices(_mockContext.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetEventById_ReturnsEvent_WhenIdExists()
|
||||
{
|
||||
var result = await _eventServices.GetEventById("1");
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal("1", result.Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetEventById_ReturnsNull_WhenIdDoesNotExist()
|
||||
{
|
||||
var result = await _eventServices.GetEventById("3");
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetEvents_ReturnsEvents_WhenTitleExists()
|
||||
{
|
||||
var result = await _eventServices.GetEvents("Event1", 1, 1, EventOrderCriteria.Date, true);
|
||||
Assert.Single(result.Events);
|
||||
Assert.Equal("Event1", result.Events.First().Title);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetEvents_ReturnsEmpty_WhenTitleDoesNotExist()
|
||||
{
|
||||
var result = await _eventServices.GetEvents("Event3", 1, 1, EventOrderCriteria.Date, true);
|
||||
Assert.Empty(result.Events);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateEvent_ReturnsUpdatedEvent_WhenIdExists()
|
||||
{
|
||||
var eventDto = new RequestEventDto { Title = "UpdatedEvent", Description = "UpdatedDescription", Date = DateTime.Now, nbMaxRegistrations = 150 };
|
||||
var result = await _eventServices.UpdateEvent("1", eventDto);
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal("UpdatedEvent", result.Title);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateEvent_ReturnsNull_WhenIdDoesNotExist()
|
||||
{
|
||||
var eventDto = new RequestEventDto { Title = "UpdatedEvent", Description = "UpdatedDescription", Date = DateTime.Now, nbMaxRegistrations = 150 };
|
||||
var result = await _eventServices.UpdateEvent("3", eventDto);
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteEvent_ReturnsTrue_WhenIdExists()
|
||||
{
|
||||
var result = await _eventServices.DeleteEvent("1");
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteEvent_ReturnsFalse_WhenIdDoesNotExist()
|
||||
{
|
||||
var result = await _eventServices.DeleteEvent("3");
|
||||
Assert.False(result);
|
||||
}
|
||||
}
|
@ -0,0 +1,93 @@
|
||||
using Moq;
|
||||
using Infrastructure;
|
||||
using Infrastructure.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Server.Dto.Request;
|
||||
using Server.Services;
|
||||
|
||||
public class ExperienceServicesTests
|
||||
{
|
||||
private readonly Mock<AlumniDbContext> _mockContext;
|
||||
private readonly ExperienceServices _experienceServices;
|
||||
|
||||
public ExperienceServicesTests()
|
||||
{
|
||||
var experiences = new List<ExperienceEntity>
|
||||
{
|
||||
new ExperienceEntity { Id = "1", Title = "Experience1", CompanyName = "Company1", StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(30), IsCurrent = true },
|
||||
new ExperienceEntity { Id = "2", Title = "Experience2", CompanyName = "Company2", StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(30), IsCurrent = false }
|
||||
}.AsQueryable();
|
||||
|
||||
var mockSet = new Mock<DbSet<ExperienceEntity>>();
|
||||
mockSet.As<IQueryable<ExperienceEntity>>().Setup(m => m.Provider).Returns(experiences.Provider);
|
||||
mockSet.As<IQueryable<ExperienceEntity>>().Setup(m => m.Expression).Returns(experiences.Expression);
|
||||
mockSet.As<IQueryable<ExperienceEntity>>().Setup(m => m.ElementType).Returns(experiences.ElementType);
|
||||
mockSet.As<IQueryable<ExperienceEntity>>().Setup(m => m.GetEnumerator()).Returns(experiences.GetEnumerator());
|
||||
|
||||
_mockContext = new Mock<AlumniDbContext>();
|
||||
_mockContext.Setup(c => c.Experiences).Returns(mockSet.Object);
|
||||
|
||||
_experienceServices = new ExperienceServices(_mockContext.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetExperienceById_ReturnsExperience_WhenIdExists()
|
||||
{
|
||||
var result = await _experienceServices.GetExperienceById("1");
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal("1", result.Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetExperienceById_ReturnsNull_WhenIdDoesNotExist()
|
||||
{
|
||||
var result = await _experienceServices.GetExperienceById("3");
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetExperiences_ReturnsExperiences_WhenTitleExists()
|
||||
{
|
||||
var result = await _experienceServices.GetExperiences("Experience1", 1, 1);
|
||||
Assert.Single(result.Experiences);
|
||||
Assert.Equal("Experience1", result.Experiences.First().Title);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetExperiences_ReturnsEmpty_WhenTitleDoesNotExist()
|
||||
{
|
||||
var result = await _experienceServices.GetExperiences("Experience3", 1, 1);
|
||||
Assert.Empty(result.Experiences);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateExperience_ReturnsUpdatedExperience_WhenIdExists()
|
||||
{
|
||||
var experience = new RequestExperienceDto { Title = "UpdatedExperience", CompanyName = "UpdatedCompany", StartingDate = DateTime.Now, EndingDate = DateTime.Now.AddDays(30), CurrentJob = false };
|
||||
var result = await _experienceServices.UpdateExperience("1", experience);
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal("UpdatedExperience", result.Title);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateExperience_ReturnsNull_WhenIdDoesNotExist()
|
||||
{
|
||||
var experience = new RequestExperienceDto { Title = "UpdatedExperience", CompanyName = "UpdatedCompany", StartingDate = DateTime.Now, EndingDate = DateTime.Now.AddDays(30), CurrentJob = false };
|
||||
var result = await _experienceServices.UpdateExperience("3", experience);
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteExperience_ReturnsTrue_WhenIdExists()
|
||||
{
|
||||
var result = await _experienceServices.DeleteExperience("1");
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteExperience_ReturnsFalse_WhenIdDoesNotExist()
|
||||
{
|
||||
var result = await _experienceServices.DeleteExperience("3");
|
||||
Assert.False(result);
|
||||
}
|
||||
}
|
@ -0,0 +1,143 @@
|
||||
using Moq;
|
||||
using Infrastructure;
|
||||
using Infrastructure.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Server.Dto.Request;
|
||||
using Server.Services;
|
||||
|
||||
public class FormationServicesTests
|
||||
{
|
||||
private readonly Mock<AlumniDbContext> _mockContext;
|
||||
private readonly FormationServices _formationServices;
|
||||
|
||||
public FormationServicesTests()
|
||||
{
|
||||
var formations = new List<FormationEntity>
|
||||
{
|
||||
new FormationEntity { Id = "1", Name = "Formation1", SchoolName = "School1", StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(30), IsCurrent = true },
|
||||
new FormationEntity { Id = "2", Name = "Formation2", SchoolName = "School2", StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(30), IsCurrent = true }
|
||||
}.AsQueryable();
|
||||
|
||||
var mockSet = new Mock<DbSet<FormationEntity>>();
|
||||
mockSet.As<IQueryable<FormationEntity>>().Setup(m => m.Provider).Returns(formations.Provider);
|
||||
mockSet.As<IQueryable<FormationEntity>>().Setup(m => m.Expression).Returns(formations.Expression);
|
||||
mockSet.As<IQueryable<FormationEntity>>().Setup(m => m.ElementType).Returns(formations.ElementType);
|
||||
mockSet.As<IQueryable<FormationEntity>>().Setup(m => m.GetEnumerator()).Returns(formations.GetEnumerator());
|
||||
|
||||
_mockContext = new Mock<AlumniDbContext>();
|
||||
_mockContext.Setup(c => c.Formations).Returns(mockSet.Object);
|
||||
|
||||
_formationServices = new FormationServices(_mockContext.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetFormationById_ReturnsFormation_WhenIdExists()
|
||||
{
|
||||
var result = await _formationServices.GetFormationById("1");
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal("1", result.Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetFormationById_ReturnsNull_WhenIdDoesNotExist()
|
||||
{
|
||||
var result = await _formationServices.GetFormationById("3");
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetFormations_ReturnsFormations_WhenNameExists()
|
||||
{
|
||||
var result = await _formationServices.GetFormations("Formation1", 1, 1);
|
||||
Assert.Single(result.Formations);
|
||||
Assert.Equal("Formation1", result.Formations.First().Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetFormations_ReturnsEmpty_WhenNameDoesNotExist()
|
||||
{
|
||||
var result = await _formationServices.GetFormations("Formation3", 1, 1);
|
||||
Assert.Empty(result.Formations);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateFormation_ReturnsUpdatedFormation_WhenIdExists()
|
||||
{
|
||||
var formation = new RequestFormationDto { Name = "UpdatedFormation", SchoolName = "UpdatedSchool", StartingDate = DateTime.Now, EndingDate = DateTime.Now.AddDays(30), CurrentFormation = false };
|
||||
var result = await _formationServices.UpdateFormation("1", formation);
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal("UpdatedFormation", result.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateFormation_ReturnsNull_WhenIdDoesNotExist()
|
||||
{
|
||||
var formation = new RequestFormationDto { Name = "UpdatedFormation", SchoolName = "UpdatedSchool", StartingDate = DateTime.Now, EndingDate = DateTime.Now.AddDays(30), CurrentFormation = false };
|
||||
var result = await _formationServices.UpdateFormation("3", formation);
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteFormation_ReturnsTrue_WhenIdExists()
|
||||
{
|
||||
var result = await _formationServices.DeleteFormation("1");
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteFormation_ReturnsFalse_WhenIdDoesNotExist()
|
||||
{
|
||||
var result = await _formationServices.DeleteFormation("3");
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetFormations_NoName_ReturnsFormations()
|
||||
{
|
||||
// Arrange
|
||||
string name = null;
|
||||
var page = 1;
|
||||
var size = 5;
|
||||
|
||||
// Act
|
||||
var result = await _formationServices.GetFormations(name, page, size);
|
||||
|
||||
// Assert
|
||||
Assert.NotEmpty(result.Formations);
|
||||
Assert.Equal(2, result.Formations.Count());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetFormations_MatchingName_ReturnsFilteredFormations()
|
||||
{
|
||||
// Arrange
|
||||
string name = "Formation1";
|
||||
var page = 1;
|
||||
var size = 5;
|
||||
|
||||
// Act
|
||||
var result = await _formationServices.GetFormations(name, page, size);
|
||||
|
||||
// Assert
|
||||
Assert.Single(result.Formations);
|
||||
Assert.Equal("Formation1", result.Formations.First().Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetFormations_PaginationWorksCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
string name = null;
|
||||
var page = 2;
|
||||
var size = 1;
|
||||
|
||||
// Act
|
||||
var result = await _formationServices.GetFormations(name, page, size);
|
||||
|
||||
// Assert
|
||||
Assert.NotEmpty(result.Formations);
|
||||
Assert.Single(result.Formations);
|
||||
Assert.Equal("Formation2", result.Formations.First().Name);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
|
||||
<PackageReference Include="Moq" Version="4.20.70"/>
|
||||
<PackageReference Include="xunit" Version="2.5.3"/>
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Server\Server.csproj"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit"/>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -0,0 +1,194 @@
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace UnitTestEF.Entities;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Infrastructure;
|
||||
using Infrastructure.Entities;
|
||||
|
||||
[TestClass]
|
||||
public class AlumniTest
|
||||
{
|
||||
[TestMethod]
|
||||
public async Task AddAlumniTest()
|
||||
{
|
||||
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 alumnis = new List<User>(
|
||||
[
|
||||
new User
|
||||
{
|
||||
Id = "1",
|
||||
FirstName = "Test",
|
||||
LastName = "Test",
|
||||
Email = "test@gmail.com",
|
||||
EntryYear = "2021",
|
||||
Password = "1234567890",
|
||||
Role = "ADMIN"
|
||||
},
|
||||
new User
|
||||
{
|
||||
Id = "2",
|
||||
FirstName = "Test2",
|
||||
LastName = "Test2",
|
||||
Email = "test2@gmail.com",
|
||||
EntryYear = "2021",
|
||||
Password = "1234567890",
|
||||
Role = "USER",
|
||||
}
|
||||
]
|
||||
);
|
||||
|
||||
await context.Alumni.AddRangeAsync(alumnis);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
Assert.AreEqual(2, await context.Alumni.CountAsync());
|
||||
|
||||
var inBaseAlumni = await context.Alumni.FirstOrDefaultAsync(a => a.Id == "1");
|
||||
Assert.IsNotNull(inBaseAlumni);
|
||||
Assert.AreEqual("1", inBaseAlumni.Id);
|
||||
Assert.AreEqual("Test", inBaseAlumni.FirstName);
|
||||
Assert.AreEqual("Test", inBaseAlumni.LastName);
|
||||
Assert.AreEqual("test@gmail.com", inBaseAlumni.Email);
|
||||
Assert.AreEqual("2021", inBaseAlumni.EntryYear);
|
||||
Assert.AreEqual("1234567890", inBaseAlumni.Password);
|
||||
Assert.AreEqual("ADMIN", inBaseAlumni.Role);
|
||||
|
||||
var inBaseAlumni2 = await context.Alumni.FirstOrDefaultAsync(a => a.Id == "2");
|
||||
Assert.IsNotNull(inBaseAlumni2);
|
||||
Assert.AreEqual("2", inBaseAlumni2.Id);
|
||||
Assert.AreEqual("Test2", inBaseAlumni2.FirstName);
|
||||
Assert.AreEqual("Test2", inBaseAlumni2.LastName);
|
||||
Assert.AreEqual("test2@gmail.com", inBaseAlumni2.Email);
|
||||
Assert.AreEqual("2021", inBaseAlumni2.EntryYear);
|
||||
Assert.AreEqual("1234567890", inBaseAlumni2.Password);
|
||||
Assert.AreEqual("USER", inBaseAlumni2.Role);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task DeleteAlumniTest()
|
||||
{
|
||||
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 alumni = new User
|
||||
{
|
||||
Id = "1",
|
||||
FirstName = "Test",
|
||||
LastName = "Test",
|
||||
Email = "test@gmail.com",
|
||||
EntryYear = "2021",
|
||||
Password = "1234567890",
|
||||
Role = "ADMIN"
|
||||
};
|
||||
|
||||
await context.Alumni.AddAsync(alumni);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
Assert.AreEqual(1, await context.Alumni.CountAsync());
|
||||
|
||||
var inBaseAlumni = await context.Alumni.FirstOrDefaultAsync(a => a.Id == "1");
|
||||
Assert.IsNotNull(inBaseAlumni);
|
||||
|
||||
context.Alumni.Remove(alumni);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
Assert.AreEqual(0, await context.Alumni.CountAsync());
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task UpdateAlumniTest()
|
||||
{
|
||||
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 alumni = new User
|
||||
{
|
||||
Id = "1",
|
||||
FirstName = "Test",
|
||||
LastName = "Test",
|
||||
Email = "test@gmail.com",
|
||||
EntryYear = "2021",
|
||||
Password = "1234567890",
|
||||
Role = "ADMIN"
|
||||
};
|
||||
|
||||
await context.Alumni.AddAsync(alumni);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
Assert.AreEqual(1, await context.Alumni.CountAsync());
|
||||
|
||||
var inBaseAlumni = await context.Alumni.FirstOrDefaultAsync(a => a.Id == "1");
|
||||
Assert.IsNotNull(inBaseAlumni);
|
||||
|
||||
inBaseAlumni.FirstName = "Test2";
|
||||
inBaseAlumni.LastName = "Test2";
|
||||
inBaseAlumni.Email = "test2@gmail.com";
|
||||
inBaseAlumni.EntryYear = "2022";
|
||||
inBaseAlumni.Password = "0987654321";
|
||||
inBaseAlumni.Role = "USER";
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
var alumniAfterUpdate = await context.Alumni.FirstOrDefaultAsync(a => a.Id == "1");
|
||||
Assert.IsNotNull(alumniAfterUpdate);
|
||||
Assert.AreEqual("1", alumniAfterUpdate.Id);
|
||||
Assert.AreEqual("Test2", alumniAfterUpdate.FirstName);
|
||||
Assert.AreEqual("Test2", alumniAfterUpdate.LastName);
|
||||
Assert.AreEqual("test2@gmail.com", alumniAfterUpdate.Email);
|
||||
Assert.AreEqual("2022", alumniAfterUpdate.EntryYear);
|
||||
Assert.AreEqual("0987654321", alumniAfterUpdate.Password);
|
||||
Assert.AreEqual("USER", alumniAfterUpdate.Role);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ToString_ReturnsCorrectFormat()
|
||||
{
|
||||
// Arrange
|
||||
var alumni = new User
|
||||
{
|
||||
Id = "1",
|
||||
FirstName = "Test",
|
||||
LastName = "Test",
|
||||
Email = "test@gmail.com",
|
||||
EntryYear = "2021",
|
||||
Password = "1234567890",
|
||||
Role = "ADMIN",
|
||||
Experiences = new List<ExperienceEntity>(),
|
||||
Formations = new List<FormationEntity>(),
|
||||
Events = new List<EventEntity>()
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = alumni.ToString();
|
||||
|
||||
// Assert
|
||||
var expected = "\t------------Alumni Id : 1 ------------- :\n\tFirstname : Test Lastname : Test Email : test@gmail.com Role : ADMIN\n\tExperiences : 0 \tFormations : 0 \t Events : 0";
|
||||
Assert.AreEqual(expected, result);
|
||||
}
|
||||
}
|
@ -0,0 +1,211 @@
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace UnitTestEF.Entities;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Infrastructure;
|
||||
using Infrastructure.Entities;
|
||||
|
||||
[TestClass]
|
||||
public class EventTest
|
||||
{
|
||||
[TestMethod]
|
||||
public async Task AddEventTest()
|
||||
{
|
||||
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 newEvent = new EventEntity
|
||||
{
|
||||
Id = "1",
|
||||
Title = "A Test Event",
|
||||
Description = "A Test Description",
|
||||
Date = new DateTime(2020, 9, 1),
|
||||
nbPlaces = 50
|
||||
};
|
||||
await context.Events.AddAsync(newEvent);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
Assert.AreEqual(1, await context.Events.CountAsync());
|
||||
|
||||
var inBaseEvent = await context.Events.FirstOrDefaultAsync(e => e.Id == "1");
|
||||
Assert.IsNotNull(inBaseEvent);
|
||||
Assert.AreEqual("1", inBaseEvent.Id);
|
||||
Assert.AreEqual("A Test Event", inBaseEvent.Title);
|
||||
Assert.AreEqual("A Test Description", inBaseEvent.Description);
|
||||
Assert.AreEqual(new DateTime(2020, 9, 1), inBaseEvent.Date);
|
||||
Assert.AreEqual(50, inBaseEvent.nbPlaces);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task RemoveEventTest()
|
||||
{
|
||||
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 newEvent = new EventEntity
|
||||
{
|
||||
Id = "1",
|
||||
Title = "A Test Event",
|
||||
Description = "A Test Description",
|
||||
Date = new DateTime(2020, 9, 1),
|
||||
nbPlaces = 50
|
||||
};
|
||||
await context.Events.AddAsync(newEvent);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
var inBaseEvent = await context.Events.FirstOrDefaultAsync(e => e.Id == "1");
|
||||
Assert.IsNotNull(inBaseEvent);
|
||||
|
||||
context.Events.Remove(newEvent);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
var eventAfterDelete = await context.Events.FirstOrDefaultAsync(e => e.Id == "1");
|
||||
Assert.IsNull(eventAfterDelete);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task UpdateEventTest()
|
||||
{
|
||||
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 newEvent = new EventEntity
|
||||
{
|
||||
Id = "1",
|
||||
Title = "A Test Event",
|
||||
Description = "A Test Description",
|
||||
Date = new DateTime(2020, 9, 1),
|
||||
nbPlaces = 50
|
||||
};
|
||||
await context.Events.AddAsync(newEvent);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
var inBaseEvent = await context.Events.FirstOrDefaultAsync(e => e.Id == "1");
|
||||
Assert.IsNotNull(inBaseEvent);
|
||||
|
||||
inBaseEvent.Title = "A New Title";
|
||||
inBaseEvent.Description = "A New Description";
|
||||
inBaseEvent.Date = new DateTime(2020, 9, 2);
|
||||
inBaseEvent.nbPlaces = 100;
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
var eventAfterUpdate = await context.Events.FirstOrDefaultAsync(e => e.Id == "1");
|
||||
Assert.IsNotNull(eventAfterUpdate);
|
||||
Assert.AreEqual("1", eventAfterUpdate.Id);
|
||||
Assert.AreEqual("A New Title", eventAfterUpdate.Title);
|
||||
Assert.AreEqual("A New Description", eventAfterUpdate.Description);
|
||||
Assert.AreEqual(new DateTime(2020, 9, 2), eventAfterUpdate.Date);
|
||||
Assert.AreEqual(100, eventAfterUpdate.nbPlaces);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task EventWithParticipantsTest()
|
||||
{
|
||||
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 newEvent = new EventEntity
|
||||
{
|
||||
Id = "1",
|
||||
Title = "A Test Event",
|
||||
Description = "A Test Description",
|
||||
Date = new DateTime(2020, 9, 1),
|
||||
nbPlaces = 50
|
||||
};
|
||||
await context.Events.AddAsync(newEvent);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
var inBaseEvent = await context.Events.FirstOrDefaultAsync(e => e.Id == "1");
|
||||
Assert.IsNotNull(inBaseEvent);
|
||||
|
||||
var participants = new List<User>(
|
||||
[
|
||||
new User
|
||||
{
|
||||
Id = "1",
|
||||
FirstName = "Test",
|
||||
LastName = "Test",
|
||||
Email = "test@gmail.com",
|
||||
EntryYear = "2021",
|
||||
Password = "1234567890",
|
||||
Role = "ADMIN"
|
||||
},
|
||||
new User
|
||||
{
|
||||
Id = "2",
|
||||
FirstName = "Test2",
|
||||
LastName = "Test2",
|
||||
Email = "test2@gmail.com",
|
||||
EntryYear = "2021",
|
||||
Password = "1234567890",
|
||||
Role = "USER",
|
||||
}
|
||||
]
|
||||
);
|
||||
|
||||
foreach (var p in participants)
|
||||
{
|
||||
inBaseEvent.Participants.Add(p);
|
||||
}
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
var eventWithParticipants = await context.Events.Include(e => e.Participants).FirstOrDefaultAsync(e => e.Id == "1");
|
||||
Assert.IsNotNull(eventWithParticipants);
|
||||
Assert.AreEqual(2, eventWithParticipants.Participants.Count);
|
||||
|
||||
foreach (var p in eventWithParticipants.Participants)
|
||||
{
|
||||
Assert.IsTrue(participants.Contains(p));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ToString_ReturnsCorrectFormat()
|
||||
{
|
||||
// Arrange
|
||||
var eventEntity = new EventEntity
|
||||
{
|
||||
Title = "Test Event",
|
||||
Description = "This is a test event",
|
||||
Date = new DateTime(2022, 1, 1),
|
||||
nbPlaces = 100
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = eventEntity.ToString();
|
||||
|
||||
// Assert
|
||||
var expected = "\tTitle: Test Event, Date: 01/01/2022 00:00:00, nbPlaces: 100";
|
||||
Assert.AreEqual(expected, result);
|
||||
}
|
||||
}
|
@ -0,0 +1,150 @@
|
||||
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);
|
||||
}
|
||||
}
|
@ -0,0 +1,145 @@
|
||||
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);
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
global using Microsoft.VisualStudio.TestTools.UnitTesting;
|
@ -0,0 +1,24 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
<RootNamespace>UnitTestEF</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0"/>
|
||||
<PackageReference Include="MSTest.TestAdapter" Version="3.0.4"/>
|
||||
<PackageReference Include="MSTest.TestFramework" Version="3.0.4"/>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Infrastructure\Infrastructure.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
Loading…
Reference in new issue