tests unitaires qui seront probablement abandonnés

API
Damien NORTIER 1 year ago
parent 8bbe150571
commit 62b3993325

@ -1,49 +0,0 @@
using DbConnectionLibrairie;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using StubbedDbContextLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
namespace UnitTestsEntityManagers
{
/// <summary>
/// an abstract class to init the dbContext
/// (every unit tests need to implement it)
/// </summary>
public abstract class AbstractUnitTestEM
{
protected DbContextOptions<MyDbContext> opt;
/// <summary>
/// constructor of the class :
/// initialise the database context
/// </summary>
public AbstractUnitTestEM()
{
}
protected void creerOpt(string provider, string connectionString)
{
if(provider == "sqlite")
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
opt = new DbContextOptionsBuilder<MyDbContext>()
.UseSqlite(connection)
.Options;
}
else
{
throw new Exception("provider unknown");
}
}
}
}

@ -1,20 +1,28 @@
using DbConnectionLibrairie; using DbConnectionLibrairie;
using Entities; using Entities;
using EntityManagers; using EntityManagers;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using OrderCriterias; using OrderCriterias;
using StubbedDbContextLibrary; using StubbedDbContextLibrary;
namespace UnitTestsEntityManagers namespace UnitTestsEntityManagers
{ {
public class UnitTestAnswerManager : AbstractUnitTestEM public class UnitTestAnswerManager
{ {
/// <summary> /// <summary>
/// test of the 'ajouterAnswer' method of an AnswerManager /// test of the 'ajouterAnswer' method of an AnswerManager
/// </summary> /// </summary>
[Fact] [Fact]
public async void TestAddAnswer() public async void TestAddAnswer()
{ {
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var opt = new DbContextOptionsBuilder<MyDbContext>()
.UseSqlite(connection)
.Options;
using (var context = new MyDbContext(opt)) using (var context = new MyDbContext(opt))
{ {
await context.Database.EnsureCreatedAsync(); await context.Database.EnsureCreatedAsync();
@ -35,7 +43,7 @@ namespace UnitTestsEntityManagers
// and since it's the second answer // and since it's the second answer
// that we add, his id equal 2 // that we add, his id equal 2
Assert.Equal(answerToAdd.Content, a.Content); Assert.Equal(answerToAdd.Content, a.Content);
Assert.Equal((uint)2, a.Id); Assert.Equal((int)2, a.Id);
answerToAdd = new AnswerEntity { Id = 7, Content = "chateîgne" }; answerToAdd = new AnswerEntity { Id = 7, Content = "chateîgne" };
a = await manager.addAnswer(answerToAdd); a = await manager.addAnswer(answerToAdd);
@ -44,7 +52,7 @@ namespace UnitTestsEntityManagers
// have the same content so the content of 'a' is "châteigne" // have the same content so the content of 'a' is "châteigne"
// and his id is 1 // and his id is 1
Assert.Equal("chateîgne", a.Content); Assert.Equal("chateîgne", a.Content);
Assert.Equal((uint)1, a.Id); Assert.Equal((int)1, a.Id);
answerToAdd = new AnswerEntity { Id = 7, Content = "CHATEÎGNE" }; answerToAdd = new AnswerEntity { Id = 7, Content = "CHATEÎGNE" };
a = await manager.addAnswer(answerToAdd); a = await manager.addAnswer(answerToAdd);
@ -54,7 +62,7 @@ namespace UnitTestsEntityManagers
// already have the same content so the content // already have the same content so the content
// of 'a' is "chateîgne" and his id is 1 // of 'a' is "chateîgne" and his id is 1
Assert.Equal("chateîgne", a.Content); Assert.Equal("chateîgne", a.Content);
Assert.Equal((uint)1, a.Id); Assert.Equal((int)1, a.Id);
} }
} }
@ -64,46 +72,52 @@ namespace UnitTestsEntityManagers
[Fact] [Fact]
public async Task TestRemoveAnswerById() public async Task TestRemoveAnswerById()
{ {
using (var context = new StubbedDbContext(opt)) var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var opt = new DbContextOptionsBuilder<MyDbContext>()
.UseSqlite(connection)
.Options;
using (var context = new StubbedDbContext())
{ {
await context.Database.EnsureCreatedAsync(); await context.Database.EnsureCreatedAsync();
var mgr = new AnswerEntityManager(context); var mgr = new AnswerEntityManager(context);
// remember that we have only 2 answers in the database : var a = await mgr.removeAnswer(context.Answers.Select(a => a.Id).Max() + 1);
// 1) (1, "châteigne")
// 2) (2, "damien")
var a = await mgr.removeAnswer(3);
// 1) with an id greater or equal // 1) with an id greater or equal
// to the number of element // to the number of element
// WF : it don't works so 'a' is null, // WF : it don't works so 'a' is null,
Assert.Null(a); Assert.Null(a);
a = await mgr.removeAnswer(1); a = await mgr.removeAnswer(context.Answers.Select(a => a.Id).Min());
// 1) with an id that belongs to an answer // 1) with an id that belongs to an answer
// WF : it works so 'a' is not null, // WF : it works so 'a' is not null,
// and since we've delete the answer with // and since we've delete the answer with
// the id 1, the content is "châteigne" // the id 1, the content is "châteigne"
Assert.NotNull(a); Assert.NotNull(a);
Assert.Equal((uint)1, a.Id); Assert.Equal(context.Answers.Select(a => a.Id).Min(), a.Id);
Assert.Equal("châteigne", a.Content); Assert.Equal(context.Answers.Single(a => a.Id == context.Answers.Select(a => a.Id).Min()).Content, a.Content);
a = await mgr.removeAnswer(2); a = await mgr.removeAnswer(2);
} }
} }
// /!\ WARNING : since there was 2 answers added to the base,
// id index while now start at 3 (even though we've delete those)
/// <summary> /// <summary>
/// test of the 'getNbElement' method of an AnswerManager /// test of the 'getNbElement' method of an AnswerManager
/// </summary> /// </summary>
[Fact] [Fact]
public async Task TestGetNbElement() public async Task TestGetNbElement()
{ {
using (var context = new StubbedDbContext(opt)) var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var opt = new DbContextOptionsBuilder<MyDbContext>()
.UseSqlite(connection)
.Options;
using (var context = new StubbedDbContext())
{ {
await context.Database.EnsureCreatedAsync(); await context.Database.EnsureCreatedAsync();
var mgr = new AnswerEntityManager(context); var mgr = new AnswerEntityManager(context);
Assert.Equal(0, mgr.getNbElements()); // just to be sure Assert.Equal(0, mgr.getNbAnswers()); // just to be sure
Assert.NotNull(fakeAnswers.datas); // just to be sure Assert.NotNull(fakeAnswers.datas); // just to be sure
@ -112,7 +126,7 @@ namespace UnitTestsEntityManagers
{ {
await mgr.addAnswer(answer); await mgr.addAnswer(answer);
count++; count++;
Assert.Equal(count, mgr.getNbElements()); // ok, it's incremented Assert.Equal(count, mgr.getNbAnswers()); // ok, it's incremented
} }
} }
} }
@ -124,13 +138,13 @@ namespace UnitTestsEntityManagers
public static IEnumerable<Object?[]> TestGetAnswer_Datas() public static IEnumerable<Object?[]> TestGetAnswer_Datas()
{ {
var datas = fakeAnswers.datas; var datas = fakeAnswers.datas;
uint max = 0; int max = 0;
foreach (var item in datas) foreach (var item in datas)
{ {
yield return new Object[] { item.Id, item }; yield return new Object[] { item.Id, item };
if(max < item.Id) max = item.Id; if(max < item.Id) max = item.Id;
} }
yield return new Object[] { max+1, null }; yield return new Object[] { max+1, null! };
} }
/// <summary> /// <summary>
/// test of the 'getAnswer' method of an AnswerManager /// test of the 'getAnswer' method of an AnswerManager
@ -140,9 +154,17 @@ namespace UnitTestsEntityManagers
/// <returns>nothing but a Task</returns> /// <returns>nothing but a Task</returns>
[Theory] [Theory]
[MemberData(nameof(TestGetAnswer_Datas))] [MemberData(nameof(TestGetAnswer_Datas))]
public async Task TestGetAnswer(uint id, AnswerEntity? waiting) public async Task TestGetAnswer(int id, AnswerEntity? waiting)
{ {
using (var context = new StubbedDbContext(opt)) var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var opt = new DbContextOptionsBuilder<MyDbContext>()
.UseSqlite(connection)
.Options;
using (var context = new StubbedDbContext())
{ {
await context.Database.EnsureCreatedAsync(); await context.Database.EnsureCreatedAsync();
var mgr = new AnswerEntityManager(context); var mgr = new AnswerEntityManager(context);
@ -217,13 +239,20 @@ namespace UnitTestsEntityManagers
[MemberData(nameof(TestGetSomeAnswers_Datas))] [MemberData(nameof(TestGetSomeAnswers_Datas))]
public async Task TestGetSomeAnswers(int num, int count, IEnumerable<AnswerEntity>? waiting, AnswerOrderCriteria orderCriteria = AnswerOrderCriteria.ById) public async Task TestGetSomeAnswers(int num, int count, IEnumerable<AnswerEntity>? waiting, AnswerOrderCriteria orderCriteria = AnswerOrderCriteria.ById)
{ {
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var opt = new DbContextOptionsBuilder<MyDbContext>()
.UseSqlite(connection)
.Options;
using (var context = new StubbedDbContext(opt)) using (var context = new StubbedDbContext())
{ {
await context.Database.EnsureCreatedAsync(); await context.Database.EnsureCreatedAsync();
var mgr = new AnswerEntityManager(context); var mgr = new AnswerEntityManager(context);
var nbPages = count == 0 ? -1 : mgr.getNbElements() / count; var nbPages = count == 0 ? -1 : mgr.getNbAnswers() / count;
var tmp = await mgr.getAnswers(num, count, orderCriteria); var tmp = await mgr.getAnswers(num, count, orderCriteria);
Assert.Equal(nbPages, tmp.nbPages); Assert.Equal(nbPages, tmp.nbPages);

Loading…
Cancel
Save