envoie de ce que j'ai fait (les tests)
continuous-integration/drone/push Build is passing Details

API
Damien NORTIER 1 year ago
parent 70f7d6d316
commit e1518a8e17

@ -6,6 +6,9 @@ using OrderCriterias;
namespace EntityManagers
{
/// <summary>
/// a manager for answer entity
/// </summary>
public class AnswerEntityManager : IAnswerManager<AnswerEntity>
{
private MyDbContext dbContext;
@ -36,12 +39,20 @@ namespace EntityManagers
public IEnumerable<AnswerEntity> getAnswers(int nb, int count, AnswerOrderCriteria orderCriteria = AnswerOrderCriteria.ById)
{
return dbContext.Answers.OrderBy(a => a.Id).Skip((nb - 1) * count).Take(count).ToListAsync().Result;
if ((nb - 1) * count >= getNbElement()) throw new Exception("too many page skiped");
if (orderCriteria == AnswerOrderCriteria.ById)
{
return dbContext.Answers.OrderBy(a => a.Id).Skip((nb - 1) * count).Take(count).ToListAsync().Result;
}
else
{
return dbContext.Answers.OrderBy(a => a.Content).Skip((nb - 1) * count).Take(count).ToListAsync().Result;
}
}
public AnswerEntity? modifierAnswer(long id, AnswerEntity answer)
{
var tmp = dbContext.Answers.Where(a => a.Id == id).FirstOrDefaultAsync().Result;
var tmp = getAnswer(id);
if (tmp == null) return null;
tmp.Content = answer.Content;
dbContext.SaveChangesAsync();
@ -50,7 +61,7 @@ namespace EntityManagers
public AnswerEntity? supprimerAnswer(long id)
{
var tmp = dbContext.Answers.Where(a => a.Id == id).FirstOrDefaultAsync().Result;
var tmp = getAnswer(id);
if (tmp == null) return null;
dbContext.Answers.Remove(tmp);
dbContext.SaveChangesAsync();

@ -1,29 +1,29 @@
using OrderCriterias;
namespace ManagerInterfaces
{
using OrderCriterias;
namespace ManagerInterfaces
{
/// <summary>
/// All methods to handle answers
/// </summary>
/// <typeparam name="T">a DTO or Entity type answer</typeparam>
public interface IAnswerManager<T>
{
/// <typeparam name="T">a DTO or Entity type answer</typeparam>
public interface IAnswerManager<T>
{
/// <summary>
/// get the number of T element
/// </summary>
/// <returns>the number of T element</returns>
abstract int getNbElement();
/// <returns>the number of T element</returns>
abstract int getNbElement();
/// <summary>
/// get a part of all answers
/// </summary>
/// <param name="nb">the actual page</param>
/// <param name="count">number of T element in a page</param>
/// <param name="orderCriteria">the order criteria</param>
/// <returns>
/// all T element of the database for
/// this page (or null if (nb-1)*count
/// is outside boundaries (0, getNbElement()-1)
/// </returns>
/// <returns>
/// all T element of the database for
/// this page (or null if (nb-1)*count
/// is outside boundaries (0, getNbElement()-1)
/// </returns>
public IEnumerable<T>? getAnswers(int nb, int count, AnswerOrderCriteria orderCriteria = AnswerOrderCriteria.ById);
/// <summary>
/// get a T element with an id
@ -60,7 +60,7 @@ namespace ManagerInterfaces
/// <returns>
/// the T element (modified) that corresponde
/// to the id or null if there isn't any
/// </returns>
public T ajouterAnswer(T answer);
}
}
/// </returns>
public T ajouterAnswer(T answer);
}
}

@ -0,0 +1,41 @@
using DbConnectionLibrairie;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UnitTestsEntityManagers
{
/// <summary>
/// an abstract class to init the dbContext
/// (every unit tests need to implement it)
/// </summary>
public abstract class AbstractUnitTestEM
{
protected MyDbContext dbContext;
/// <summary>
/// constructor of the class :
/// initialise the dbContext
/// </summary>
public AbstractUnitTestEM()
{
var opt = new DbContextOptionsBuilder<MyDbContext>()
.UseSqlite("DataSource=:memory:")
.Options;
dbContext = new MyDbContext(opt);
}
/// <summary>
/// destructor of the class :
/// dispose the database context
/// </summary>
~AbstractUnitTestEM()
{
dbContext.DisposeAsync();
}
}
}

@ -0,0 +1,152 @@
using DbConnectionLibrairie;
using Entities;
using EntityManagers;
using static System.Net.WebRequestMethods;
using Xunit.Abstractions;
using System.Text.Json;
using System.Net.Http.Json;
using Newtonsoft.Json;
namespace UnitTestsEntityManagers
{
public class UnitTestAnswerManager : AbstractUnitTestEM
{
IEnumerable<AnswerEntity> answers = JsonConvert
.DeserializeObject<IEnumerable<AnswerEntity>>
("../FakeDatas/fake-Answer.json")!; // if this is null, we don't want to continue
AnswerEntityManager mgr;
public UnitTestAnswerManager()
: base()
{
mgr = new AnswerEntityManager(dbContext);
}
/// <summary>
/// test of the 'ajouterAnswer' method of an AnswerManager
/// </summary>
[Fact]
public void TestAjouterAnswer()
{
var answerToAdd = new AnswerEntity { Id = -1, Content = "châteîgne" };
var a = mgr.ajouterAnswer(answerToAdd);
// 1) with an id less than 0
// WF : it works so 'a' is not null,
// his content is equal to the
// content of 'answerToAdd'
// and since it's the first answer
// that we add, his id equal 0
Assert.NotNull(a);
Assert.Equal(answerToAdd.Content, a.Content);
Assert.NotEqual(0, a.Id);
answerToAdd = new AnswerEntity { Id = 5, Content = "damien" };
a = mgr.ajouterAnswer(answerToAdd);
// 2) with a random id greater than 0
// WF : it works so 'a' is not null,
// his content is equal to the
// content of 'answerToAdd'
// and since it's the second answer
// that we add, his id equal 1
Assert.NotNull(a);
Assert.Equal(answerToAdd.Content, a.Content);
Assert.NotEqual(1, a.Id);
answerToAdd = new AnswerEntity { Id = 7, Content = "châteîgne" };
a = mgr.ajouterAnswer(answerToAdd);
// 3) with a content that we already have added
// WF : it don't works so 'a' is null
Assert.Null(a);
answerToAdd = new AnswerEntity { Id = 7, Content = "CHÂTEÎGNE" };
a = mgr.ajouterAnswer(answerToAdd);
// 3) with a content that we already have added
// but in upperCase instead of lowerCase
// WF : it don't works so 'a' is null
Assert.Null(a);
}
/// <summary>
/// test of the 'supprimerAnswer' method of an AnswerManager
/// </summary>
[Fact]
public void TestSupprimerAnswer()
{
// remember that we have only 2 answers in the database :
// 1) (0, "châteigne")
// 2) (1, "damien")
var a = mgr.supprimerAnswer(-3);
// 1) with an id less than 0
// WF : it don't work because there's no
// negative id so 'a' is null
Assert.Null(a);
a = mgr.supprimerAnswer(3);
// 2) with an id greater or equal
// to the number of element
// WF : it don't works so 'a' is null,
Assert.Null(a);
a = mgr.supprimerAnswer(0);
// 1) with an id that belongs to an answer
// WF : it works so 'a' is not null,
// and since we've delete the answer with
// the id 0, the content is "châteigne"
Assert.NotNull(a);
Assert.Equal(0, a.Id);
Assert.Equal("châteigne", a.Content);
a = mgr.supprimerAnswer(0);
// 1) same thing with the id 1 just
// for cleaning the database
// WF : it works so 'a' is not null,
// and since we've delete the answer with
// the id 1, the content is "damien"
Assert.NotNull(a);
Assert.Equal(0, a.Id);
Assert.Equal("damien", a.Content);
// now, the database should be clean
}
// /!\ WARNING : since there was 2 answers added to the base,
// id index while now start at 2 (even though we've delete those)
/// <summary>
/// test of the 'getNbElement' method of an AnswerManager
/// </summary>
[Fact]
public void TestGetNbElement()
{
Assert.Equal(0, mgr.getNbElement()); // just to be sure
Assert.NotNull(answers); // just to be sure
int count = 0;
foreach (var answer in answers)
{
mgr.ajouterAnswer(answer);
count++;
Assert.Equal(count, mgr.getNbElement());
}
}
[Fact]
public void testGetAnswer()
{
// with an id which correspond of
// an answer which is in the database
foreach (var a in answers)
{
var tmp = mgr.getAnswer(a.Id+2);
Assert.NotNull(tmp);
Assert.Equal(a.Content, tmp.Content);
Assert.Equal(a.Id, tmp.Id);
}
}
// getAnswers
// modifierAnswer
}
}

@ -0,0 +1,29 @@
<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="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DbConnectionLibrairie\DbConnectionLibrairie.csproj" />
<ProjectReference Include="..\Entities\Entities.csproj" />
<ProjectReference Include="..\EntityManagers\EntityManagers.csproj" />
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
</Project>

@ -3,8 +3,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34607.119
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApplication", "WebApplication\WebApplication.csproj", "{9F05B995-3079-4905-A9B1-7B3E8621ECC1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Entities", "Entities\Entities.csproj", "{19A1AA55-0FDF-427F-97EA-157E816C93CE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DbConnectionLibrairie", "DbConnectionLibrairie\DbConnectionLibrairie.csproj", "{8224E470-B008-4738-88FD-7DEDCAA4AE44}"
@ -13,9 +11,16 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ManagerInterfaces", "Manage
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DTOs", "DTOs\DTOs.csproj", "{F911181D-6194-4CA9-A302-7A055652E5FA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EntityManagers", "EntityManagers\EntityManagers.csproj", "{FAAF96CF-33DD-406A-B42D-E5768D3C46BB}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EntityManagers", "EntityManagers\EntityManagers.csproj", "{FAAF96CF-33DD-406A-B42D-E5768D3C46BB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OrderCriterias", "OrderCriterias\OrderCriterias.csproj", "{EE565F87-6811-46C8-A03B-6550D692873A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTestsEntityManagers", "UnitTestsEntityManagers\UnitTestsEntityManagers.csproj", "{6E2E46BB-91F0-4F5C-B378-A54E4F80ED0C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OrderCriterias", "OrderCriterias\OrderCriterias.csproj", "{EE565F87-6811-46C8-A03B-6550D692873A}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "FakeDatas", "FakeDatas", "{AB3977C6-DAF2-4E93-B2C4-BB33A9CAFC71}"
ProjectSection(SolutionItems) = preProject
FakeDatas\fake-Answers.json = FakeDatas\fake-Answers.json
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -23,10 +28,6 @@ Global
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{9F05B995-3079-4905-A9B1-7B3E8621ECC1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9F05B995-3079-4905-A9B1-7B3E8621ECC1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9F05B995-3079-4905-A9B1-7B3E8621ECC1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9F05B995-3079-4905-A9B1-7B3E8621ECC1}.Release|Any CPU.Build.0 = Release|Any CPU
{19A1AA55-0FDF-427F-97EA-157E816C93CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{19A1AA55-0FDF-427F-97EA-157E816C93CE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{19A1AA55-0FDF-427F-97EA-157E816C93CE}.Release|Any CPU.ActiveCfg = Release|Any CPU
@ -51,6 +52,10 @@ Global
{EE565F87-6811-46C8-A03B-6550D692873A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EE565F87-6811-46C8-A03B-6550D692873A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EE565F87-6811-46C8-A03B-6550D692873A}.Release|Any CPU.Build.0 = Release|Any CPU
{6E2E46BB-91F0-4F5C-B378-A54E4F80ED0C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6E2E46BB-91F0-4F5C-B378-A54E4F80ED0C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6E2E46BB-91F0-4F5C-B378-A54E4F80ED0C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6E2E46BB-91F0-4F5C-B378-A54E4F80ED0C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

@ -1,33 +0,0 @@
using Microsoft.AspNetCore.Mvc;
namespace WebApplication.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
}

@ -1,25 +0,0 @@
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
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();

@ -1,41 +0,0 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:62623",
"sslPort": 44358
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5228",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7256;http://localhost:5228",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

@ -1,13 +0,0 @@
namespace WebApplication
{
public class WeatherForecast
{
public DateOnly Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string? Summary { get; set; }
}
}

@ -1,13 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
</Project>

@ -1,6 +0,0 @@
@WebApplication_HostAddress = http://localhost:5228
GET {{WebApplication_HostAddress}}/weatherforecast/
Accept: application/json
###

@ -1,8 +0,0 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

@ -1,9 +0,0 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Loading…
Cancel
Save