using DbConnectionLibrairie;
using Entities;
using EntityManagers;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using OrderCriterias;
using StubbedDbContextLibrary;
namespace UnitTestsEntityManagers
{
public class UnitTestAnswerManager
{
///
/// test of the 'ajouterAnswer' method of an AnswerManager
///
[Fact]
public async void TestAddAnswer()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var opt = new DbContextOptionsBuilder()
.UseSqlite(connection)
.Options;
using (var context = new MyDbContext(opt))
{
await context.Database.EnsureCreatedAsync();
var manager = new AnswerEntityManager(context);
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
Assert.Equal(answerToAdd.Content, a.Content);
Assert.Equal(answerToAdd.Id, a.Id);
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
Assert.Equal(answerToAdd.Content, a.Content);
Assert.Equal((int)2, a.Id);
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
Assert.Equal("chateîgne", a.Content);
Assert.Equal((int)1, a.Id);
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
Assert.Equal("chateîgne", a.Content);
Assert.Equal((int)1, a.Id);
}
}
///
/// test of the 'supprimerAnswer' method of an AnswerManager
///
[Fact]
public async Task TestRemoveAnswerById()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var opt = new DbContextOptionsBuilder()
.UseSqlite(connection)
.Options;
using (var context = new StubbedDbContext())
{
await context.Database.EnsureCreatedAsync();
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,
Assert.Null(a);
a = await mgr.removeAnswer(context.Answers.Select(a => a.Id).Min());
// 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 1, the content is "châteigne"
Assert.NotNull(a);
Assert.Equal(context.Answers.Select(a => a.Id).Min(), a.Id);
Assert.Equal(context.Answers.Single(a => a.Id == context.Answers.Select(a => a.Id).Min()).Content, a.Content);
a = await mgr.removeAnswer(2);
}
}
///
/// test of the 'getNbElement' method of an AnswerManager
///
[Fact]
public async Task TestGetNbElement()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var opt = new DbContextOptionsBuilder()
.UseSqlite(connection)
.Options;
using (var context = new StubbedDbContext())
{
await context.Database.EnsureCreatedAsync();
var mgr = new AnswerEntityManager(context);
Assert.Equal(0, mgr.getNbAnswers()); // just to be sure
Assert.NotNull(fakeAnswers.datas); // just to be sure
int count = 0;
foreach (var answer in fakeAnswers.datas)
{
await mgr.addAnswer(answer);
count++;
Assert.Equal(count, mgr.getNbAnswers()); // ok, it's incremented
}
}
}
///
/// member data for the 'TestGetAnswer' test method
///
/// a set of all inline datas for the test method
public static IEnumerable