feat : début des tests unitaires version console

API
Damien NORTIER 1 year ago
parent b8bf972765
commit 8bbe150571

@ -0,0 +1,379 @@
using DbConnectionLibrairie;
using Entities;
using EntityManagers;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using OrderCriterias;
using StubbedDbContextLibrary;
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var opt = new DbContextOptionsBuilder<StubbedDbContext>()
.UseSqlite(connection)
.Options;
using var context = new StubbedDbContext();
/// <summary>
/// test of the 'addAnswer' method of an AnswerManager
/// </summary>
async Task TestAddAnswer()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var opt = new DbContextOptionsBuilder<MyDbContext>()
.UseSqlite(connection)
.Options;
using (var dbContext = new MyDbContext(opt))
{
await dbContext.Database.EnsureCreatedAsync();
var manager = new AnswerEntityManager(dbContext);
var answerToAdd = new AnswerEntity { Id = 1, Content = "chateîgne" };
var a = await manager.addAnswer(answerToAdd);
// 1) normal insertion
// WF : a is the same
// as answerToAdd
if (a.Content == answerToAdd.Content && a.Id == answerToAdd.Id)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("test 1 AnswerEntityManager.addAnswer OK");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("test 1 AnswerEntityManager.addAnswer KO");
Console.ResetColor();
}
answerToAdd = new AnswerEntity { Id = 5, Content = "damien" };
a = await manager.addAnswer(answerToAdd);
// 2) with a random id greater than 0
// WF : 'a' content is equal to the
// content of 'answerToAdd'
// and since it's the second answer
// that we add, his id equal 2
if (a.Content == answerToAdd.Content && a.Id == 2)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("test 2 AnswerEntityManager.addAnswer OK");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("test 2 AnswerEntityManager.addAnswer KO");
Console.ResetColor();
}
answerToAdd = new AnswerEntity { Id = 7, Content = "chateîgne" };
a = await manager.addAnswer(answerToAdd);
// 3) with a content that we already have added
// WF : the function return the answer which already
// have the same content so the content of 'a' is "châteigne"
// and his id is 1
if (a.Content == "chateîgne" && a.Id == 1)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("test 3 AnswerEntityManager.addAnswer OK");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("test 3 AnswerEntityManager.addAnswer KO");
Console.ResetColor();
}
answerToAdd = new AnswerEntity { Id = 7, Content = "CHATEÎGNE" };
a = await manager.addAnswer(answerToAdd);
// 3) with a content that we already have added
// but in upperCase instead of lowerCase
// WF : the function return the answer which
// already have the same content so the content
// of 'a' is "chateîgne" and his id is 1
if (a.Content == "chateîgne" && a.Id == 1)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("test 4 AnswerEntityManager.addAnswer OK");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("test 4 AnswerEntityManager.addAnswer KO");
Console.ResetColor();
}
}
}
/// <summary>
/// test of the 'supprimerAnswer' method of an AnswerManager
/// </summary>
async Task TestRemoveAnswerById(MyDbContext context)
{
var mgr = new AnswerEntityManager(context);
var a = await mgr.removeAnswer(context.Answers.Select(a => a.Id).Max() + 1);
// 1) with an id greater or equal
// to the number of element
// WF : it don't works so 'a' is null,
if(a == null)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("test 1 AnswerEntityManager.RemoveAnswer OK");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("test 1 AnswerEntityManager.RemoveAnswer KO");
Console.ResetColor();
}
a = await mgr.removeAnswer(context.Answers.Select(a => a.Id).Min());
// 1) with an id that is allowed to an answer
// WF : it works so 'a' is not null,
// and since we've delete the answer with
// the id 1, the content is "châteigne"
if (a.Content == context.Answers.Single(a => a.Id == context.Answers.Select(a => a.Id).Min()).Content
&& a.Id == context.Answers.Select(a => a.Id).Min())
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("test 2 AnswerEntityManager.RemoveAnswer OK");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("test 2 AnswerEntityManager.RemoveAnswer KO");
Console.ResetColor();
}
}
/// <summary>
/// test of the 'getNbElement' method of an AnswerManager
/// </summary>
async Task TestGetNbAnswers(MyDbContext context)
{
await context.Database.EnsureCreatedAsync();
var mgr = new AnswerEntityManager(context);
if (mgr.getNbAnswers() != 0 || fakeAnswers.datas == null) throw new Exception("not ready");
int count = 0;
foreach (var answer in fakeAnswers.datas)
{
await mgr.addAnswer(answer);
count++;
if (mgr.getNbAnswers() == count)
{ // ok, it's incremented
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"test {count} AnswerEntityManager.addAnswer OK");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"test {count} AnswerEntityManager.addAnswer KO");
Console.ResetColor();
}
}
}
/// <summary>
/// member data for the 'TestGetAnswer' test method
/// </summary>
/// <returns>a set of all inline datas for the test method</returns>
IEnumerable<Object?[]> TestGetAnswer_Datas()
{
var datas = fakeAnswers.datas;
int max = 0;
List<Object?[]> tmp = new List<Object?[]>();
foreach (var item in datas)
{
tmp.Add(new Object[] { item.Id, item });
if (max < item.Id) max = item.Id;
}
tmp.Add(new Object[] { max + 1, null! });
return tmp;
}
/// <summary>
/// test of the 'getAnswer' method of an AnswerManager
/// </summary>
/// <param name="id">identifiant of the answer to get</param>
/// <param name="waiting">answer expected</param>
/// <returns>nothing but a Task</returns>
async Task TestGetAnswer(MyDbContext context, int numTest, int id, AnswerEntity? waiting)
{
await context.Database.EnsureCreatedAsync();
var mgr = new AnswerEntityManager(context);
var tmp = await mgr.getAnswer(id + 2);
if (tmp?.Content == waiting?.Content)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"test {numTest} AnswerEntityManager.addAnswer OK");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"test {numTest} AnswerEntityManager.addAnswer KO");
Console.ResetColor();
}
}
/// <summary>
/// member data for the 'TestGetAnswer' test method
/// </summary>
/// <returns>a set of all inline datas for the test method</returns>
static IEnumerable<Object?[]> TestGetSomeAnswers_Datas()
{
var datas = fakeAnswers.datas;
var count = datas.Count();
// remind that to add answers, we haven't ordered this collection
// but we just have done a foreach
return new List<Object?[]>
{
// 1 : 0 answer from the page 1 then, -1 answer from this page
// WF : count < 1 so we got null
new object?[] { 1, 0, null },
new object?[] { 1, -1, null },
// 2 : 1 answers from the page 0 then, 1 answers from the page -1
// WF : page < 1 so we got null
new object?[] { 0, 1, null },
new object?[] { -1, 1, null },
// 3 : 10 answers from the page 1
// WF : the first 10 element of datas
new object?[] { 1, 10, datas.Take(10) },
// 4 : 10 elements from the page 1 order by id
// WF : the first 10 element of datas order by id
new object?[] { 1, 10, datas.OrderBy(e => e.Id).Take(10), AnswerOrderCriteria.ById },
// 5 : 10 elements from the page 1 order by content
// WF : the first 10 element of datas order by content
new object?[] { 1, 10, datas.OrderBy(e => e.Content).Take(10), AnswerOrderCriteria.ById },
// 6 : repeat 3, 4 and 5 with the page 2
// WF : the 10 next answer from each case
new object?[] { 2, 10, datas.Skip(10).Take(10) },
new object?[] { 1, 10, datas.OrderBy(e => e.Id).Skip(10).Take(10), AnswerOrderCriteria.ById },
new object?[] { 1, 10, datas.OrderBy(e => e.Content).Skip(10).Take(10), AnswerOrderCriteria.ByContent },
// 7 : count/4 elements from the page 4
// WF : the lasts count/4 elements of datas
new object?[] { 4, count / 4, datas.TakeLast(count / 4) },
// 8 : count/4 elements from the page 5
// WF : null since num (4) >= count / (count/4)
new object?[] { 5, count / 4, null },
// 9 : 10 elements from the page 4
// WF : since there's only 31 elements in fake datas,
// we got the last element
new object?[] { 4, 10, datas.TakeLast(1) }
};
}
/// <summary>
/// test of the 'getAnswer' method of an AnswerManager
/// </summary>
/// <param name="num">the page number</param>
/// <param name="count">page elements number</param>
/// <param name="waiting">set of answers expected</param>
/// <param name="orderCriteria">the order criteria</param>
/// <returns>nothing but a Task</returns>
async Task TestGetSomeAnswers(MyDbContext context, int numTest, int num, int count, IEnumerable<AnswerEntity>? waiting, AnswerOrderCriteria orderCriteria = AnswerOrderCriteria.ById)
{
await context.Database.EnsureCreatedAsync();
var mgr = new AnswerEntityManager(context);
var nbPages = count == 0 ? -1 : mgr.getNbAnswers() / count;
var tmp = await mgr.getAnswers(num, count, orderCriteria);
if(tmp.nbPages == nbPages){
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"test {numTest} AnswerEntityManager.getSomeAnswers OK");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"test {numTest} AnswerEntityManager.getSomeAnswers KO");
Console.ResetColor();
}
if (waiting == null && tmp.answers == null)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"test {numTest + 1} AnswerEntityManager.getSomeAnswers OK");
Console.ResetColor();
}
else if (waiting != null && tmp.answers != null)
{
if (waiting?.Count() == 0)
{
if(tmp.answers == null)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"test {numTest + 1} AnswerEntityManager.getSomeAnswers OK");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"test {numTest + 1} AnswerEntityManager.getSomeAnswers KO");
Console.ResetColor();
}
}
else
{
var nbFautes = 0;
for (var i = 0; i < count; i++)
{
var e1 = waiting.ElementAt(i);
var e2 = tmp.answers.ElementAt(i);
if(e2.Content != e1.Content || e2.IdQuestion != e1.IdQuestion) nbFautes++;
}
if (nbFautes == 0)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"test {numTest + 1} AnswerEntityManager.getSomeAnswers OK");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"test {numTest + 1} AnswerEntityManager.getSomeAnswers KO");
Console.ResetColor();
}
}
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"test {numTest + 1} AnswerEntityManager.getSomeAnswers KO");
Console.ResetColor();
}
}
{
await TestAddAnswer();
await TestRemoveAnswerById(context);
await TestGetNbAnswers(context);
var tmp = TestGetAnswer_Datas();
for (int i = 0; i < tmp.Count(); i++)
{
await TestGetAnswer(context, i, (int)tmp.ElementAt(i).ElementAt(0)!, (AnswerEntity)tmp.ElementAt(i).ElementAt(1)!);
}
tmp = TestGetSomeAnswers_Datas();
for (int i = 0; i < tmp.Count(); i++)
{
await TestGetSomeAnswers(context, i, (int)tmp.ElementAt(i).ElementAt(0)!, (int)tmp.ElementAt(i).ElementAt(1)!,
(IEnumerable<AnswerEntity>?)tmp.ElementAt(i).ElementAtOrDefault(2),
(AnswerOrderCriteria)(tmp.ElementAt(i).ElementAtOrDefault(i) ?? AnswerOrderCriteria.ById)
);
}
}
// removeAnswer by element
// updateAnswer

@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DbConnectionLibrairie\DbConnectionLibrairie.csproj" />
<ProjectReference Include="..\Entities\Entities.csproj" />
<ProjectReference Include="..\EntityManagers\EntityManagers.csproj" />
<ProjectReference Include="..\StubbedDbContextLibrary\StubbedDbContextLibrary.csproj" />
</ItemGroup>
</Project>

@ -29,7 +29,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApi", "WebApi\WebApi.csp
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FakeDatas", "FakeDatas\FakeDatas.csproj", "{3C930FB5-2F1E-463E-978B-F0B5C7A58307}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StubbedDbContextLibrary", "StubbedDbContextLibrary\StubbedDbContextLibrary.csproj", "{6A0D9093-EAA4-45A0-8813-ED5BB4E1EA3E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StubbedDbContextLibrary", "StubbedDbContextLibrary\StubbedDbContextLibrary.csproj", "{6A0D9093-EAA4-45A0-8813-ED5BB4E1EA3E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestEntityManagers", "TestEntityManagers\TestEntityManagers.csproj", "{F0758B69-FD20-4BC4-BE0F-5868DB8BF74E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -93,6 +95,10 @@ Global
{6A0D9093-EAA4-45A0-8813-ED5BB4E1EA3E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6A0D9093-EAA4-45A0-8813-ED5BB4E1EA3E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6A0D9093-EAA4-45A0-8813-ED5BB4E1EA3E}.Release|Any CPU.Build.0 = Release|Any CPU
{F0758B69-FD20-4BC4-BE0F-5868DB8BF74E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F0758B69-FD20-4BC4-BE0F-5868DB8BF74E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F0758B69-FD20-4BC4-BE0F-5868DB8BF74E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F0758B69-FD20-4BC4-BE0F-5868DB8BF74E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

Loading…
Cancel
Save