récupération des modifications
continuous-integration/drone/push Build is passing Details

API
Damien NORTIER 1 year ago
parent dbab6e4fc9
commit 665f017e83

@ -1,265 +0,0 @@
// <auto-generated />
using System;
using DbConnectionLibrairie;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace DbConnectionLibrairie.Migrations
{
[DbContext(typeof(MyDbContext))]
[Migration("20240226094705_m1")]
partial class m1
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.2");
modelBuilder.Entity("Entities.AdministratorEntity", b =>
{
b.Property<uint?>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("HashedPassword")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Username")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Administrators");
});
modelBuilder.Entity("Entities.AnswerEntity", b =>
{
b.Property<uint?>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Content")
.IsRequired()
.HasColumnType("TEXT");
b.Property<uint>("IdQuestion")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.HasIndex("IdQuestion");
b.ToTable("Answers");
});
modelBuilder.Entity("Entities.ChapterEntity", b =>
{
b.Property<uint?>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Chapters");
});
modelBuilder.Entity("Entities.LobbyEntity", b =>
{
b.Property<uint?>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<uint?>("IdCreator")
.HasColumnType("INTEGER");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.Property<uint>("NbPlayers")
.HasColumnType("INTEGER");
b.Property<string>("Password")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("IdCreator");
b.ToTable("Lobbies");
});
modelBuilder.Entity("Entities.LobbyEntityPlayerEntity", b =>
{
b.Property<uint>("IdLobby")
.HasColumnType("INTEGER");
b.Property<uint>("IdPlayer")
.HasColumnType("INTEGER");
b.Property<uint>("MaxScore")
.HasColumnType("INTEGER");
b.HasKey("IdLobby", "IdPlayer");
b.HasIndex("IdPlayer");
b.ToTable("Use");
});
modelBuilder.Entity("Entities.PlayerEntity", b =>
{
b.Property<uint?>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("HashedPassword")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Nickname")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Players");
});
modelBuilder.Entity("Entities.PlayerEntityChapterEntity", b =>
{
b.Property<uint>("IdPlayer")
.HasColumnType("INTEGER");
b.Property<uint>("IdChapter")
.HasColumnType("INTEGER");
b.Property<uint>("MaxScore")
.HasColumnType("INTEGER");
b.HasKey("IdPlayer", "IdChapter");
b.HasIndex("IdChapter");
b.ToTable("Play");
});
modelBuilder.Entity("Entities.QuestionEntity", b =>
{
b.Property<uint?>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Content")
.IsRequired()
.HasColumnType("TEXT");
b.Property<byte>("Difficulty")
.HasColumnType("INTEGER");
b.Property<uint?>("IdAnswerGood")
.HasColumnType("INTEGER");
b.Property<uint?>("IdChapter")
.HasColumnType("INTEGER");
b.Property<uint>("NbFalls")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.HasIndex("IdAnswerGood");
b.HasIndex("IdChapter");
b.ToTable("Questions");
});
modelBuilder.Entity("Entities.AnswerEntity", b =>
{
b.HasOne("Entities.QuestionEntity", "Question")
.WithMany()
.HasForeignKey("IdQuestion")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Question");
});
modelBuilder.Entity("Entities.LobbyEntity", b =>
{
b.HasOne("Entities.PlayerEntity", "Creator")
.WithMany()
.HasForeignKey("IdCreator");
b.Navigation("Creator");
});
modelBuilder.Entity("Entities.LobbyEntityPlayerEntity", b =>
{
b.HasOne("Entities.LobbyEntity", "Lobby")
.WithMany()
.HasForeignKey("IdLobby")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Entities.PlayerEntity", "Player")
.WithMany()
.HasForeignKey("IdPlayer")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Lobby");
b.Navigation("Player");
});
modelBuilder.Entity("Entities.PlayerEntityChapterEntity", b =>
{
b.HasOne("Entities.ChapterEntity", "Chapter")
.WithMany()
.HasForeignKey("IdChapter")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Entities.PlayerEntity", "Player")
.WithMany()
.HasForeignKey("IdPlayer")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Chapter");
b.Navigation("Player");
});
modelBuilder.Entity("Entities.QuestionEntity", b =>
{
b.HasOne("Entities.AnswerEntity", "AnswerGood")
.WithMany()
.HasForeignKey("IdAnswerGood");
b.HasOne("Entities.ChapterEntity", "Chapter")
.WithMany()
.HasForeignKey("IdChapter");
b.Navigation("AnswerGood");
b.Navigation("Chapter");
});
#pragma warning restore 612, 618
}
}
}

@ -1,237 +0,0 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace DbConnectionLibrairie.Migrations
{
/// <inheritdoc />
public partial class m1 : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Administrators",
columns: table => new
{
Id = table.Column<uint>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Username = table.Column<string>(type: "TEXT", nullable: false),
HashedPassword = table.Column<string>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Administrators", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Chapters",
columns: table => new
{
Id = table.Column<uint>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Name = table.Column<string>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Chapters", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Players",
columns: table => new
{
Id = table.Column<uint>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Nickname = table.Column<string>(type: "TEXT", nullable: false),
HashedPassword = table.Column<string>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Players", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Lobbies",
columns: table => new
{
Id = table.Column<uint>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Name = table.Column<string>(type: "TEXT", nullable: false),
Password = table.Column<string>(type: "TEXT", nullable: false),
NbPlayers = table.Column<uint>(type: "INTEGER", nullable: false),
IdCreator = table.Column<uint>(type: "INTEGER", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Lobbies", x => x.Id);
table.ForeignKey(
name: "FK_Lobbies_Players_IdCreator",
column: x => x.IdCreator,
principalTable: "Players",
principalColumn: "Id");
});
migrationBuilder.CreateTable(
name: "Play",
columns: table => new
{
IdChapter = table.Column<uint>(type: "INTEGER", nullable: false),
IdPlayer = table.Column<uint>(type: "INTEGER", nullable: false),
MaxScore = table.Column<uint>(type: "INTEGER", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Play", x => new { x.IdPlayer, x.IdChapter });
table.ForeignKey(
name: "FK_Play_Chapters_IdChapter",
column: x => x.IdChapter,
principalTable: "Chapters",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_Play_Players_IdPlayer",
column: x => x.IdPlayer,
principalTable: "Players",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "Use",
columns: table => new
{
IdLobby = table.Column<uint>(type: "INTEGER", nullable: false),
IdPlayer = table.Column<uint>(type: "INTEGER", nullable: false),
MaxScore = table.Column<uint>(type: "INTEGER", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Use", x => new { x.IdLobby, x.IdPlayer });
table.ForeignKey(
name: "FK_Use_Lobbies_IdLobby",
column: x => x.IdLobby,
principalTable: "Lobbies",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_Use_Players_IdPlayer",
column: x => x.IdPlayer,
principalTable: "Players",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "Answers",
columns: table => new
{
Id = table.Column<uint>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Content = table.Column<string>(type: "TEXT", nullable: false),
IdQuestion = table.Column<uint>(type: "INTEGER", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Answers", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Questions",
columns: table => new
{
Id = table.Column<uint>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Content = table.Column<string>(type: "TEXT", nullable: false),
Difficulty = table.Column<byte>(type: "INTEGER", nullable: false),
NbFalls = table.Column<uint>(type: "INTEGER", nullable: false),
IdChapter = table.Column<uint>(type: "INTEGER", nullable: true),
IdAnswerGood = table.Column<uint>(type: "INTEGER", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Questions", x => x.Id);
table.ForeignKey(
name: "FK_Questions_Answers_IdAnswerGood",
column: x => x.IdAnswerGood,
principalTable: "Answers",
principalColumn: "Id");
table.ForeignKey(
name: "FK_Questions_Chapters_IdChapter",
column: x => x.IdChapter,
principalTable: "Chapters",
principalColumn: "Id");
});
migrationBuilder.CreateIndex(
name: "IX_Answers_IdQuestion",
table: "Answers",
column: "IdQuestion");
migrationBuilder.CreateIndex(
name: "IX_Lobbies_IdCreator",
table: "Lobbies",
column: "IdCreator");
migrationBuilder.CreateIndex(
name: "IX_Play_IdChapter",
table: "Play",
column: "IdChapter");
migrationBuilder.CreateIndex(
name: "IX_Questions_IdAnswerGood",
table: "Questions",
column: "IdAnswerGood");
migrationBuilder.CreateIndex(
name: "IX_Questions_IdChapter",
table: "Questions",
column: "IdChapter");
migrationBuilder.CreateIndex(
name: "IX_Use_IdPlayer",
table: "Use",
column: "IdPlayer");
migrationBuilder.AddForeignKey(
name: "FK_Answers_Questions_IdQuestion",
table: "Answers",
column: "IdQuestion",
principalTable: "Questions",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Answers_Questions_IdQuestion",
table: "Answers");
migrationBuilder.DropTable(
name: "Administrators");
migrationBuilder.DropTable(
name: "Play");
migrationBuilder.DropTable(
name: "Use");
migrationBuilder.DropTable(
name: "Lobbies");
migrationBuilder.DropTable(
name: "Players");
migrationBuilder.DropTable(
name: "Questions");
migrationBuilder.DropTable(
name: "Answers");
migrationBuilder.DropTable(
name: "Chapters");
}
}
}

@ -1,262 +0,0 @@
// <auto-generated />
using System;
using DbConnectionLibrairie;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace DbConnectionLibrairie.Migrations
{
[DbContext(typeof(MyDbContext))]
partial class MyDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.2");
modelBuilder.Entity("Entities.AdministratorEntity", b =>
{
b.Property<uint?>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("HashedPassword")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Username")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Administrators");
});
modelBuilder.Entity("Entities.AnswerEntity", b =>
{
b.Property<uint?>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Content")
.IsRequired()
.HasColumnType("TEXT");
b.Property<uint>("IdQuestion")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.HasIndex("IdQuestion");
b.ToTable("Answers");
});
modelBuilder.Entity("Entities.ChapterEntity", b =>
{
b.Property<uint?>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Chapters");
});
modelBuilder.Entity("Entities.LobbyEntity", b =>
{
b.Property<uint?>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<uint?>("IdCreator")
.HasColumnType("INTEGER");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.Property<uint>("NbPlayers")
.HasColumnType("INTEGER");
b.Property<string>("Password")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("IdCreator");
b.ToTable("Lobbies");
});
modelBuilder.Entity("Entities.LobbyEntityPlayerEntity", b =>
{
b.Property<uint>("IdLobby")
.HasColumnType("INTEGER");
b.Property<uint>("IdPlayer")
.HasColumnType("INTEGER");
b.Property<uint>("MaxScore")
.HasColumnType("INTEGER");
b.HasKey("IdLobby", "IdPlayer");
b.HasIndex("IdPlayer");
b.ToTable("Use");
});
modelBuilder.Entity("Entities.PlayerEntity", b =>
{
b.Property<uint?>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("HashedPassword")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Nickname")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Players");
});
modelBuilder.Entity("Entities.PlayerEntityChapterEntity", b =>
{
b.Property<uint>("IdPlayer")
.HasColumnType("INTEGER");
b.Property<uint>("IdChapter")
.HasColumnType("INTEGER");
b.Property<uint>("MaxScore")
.HasColumnType("INTEGER");
b.HasKey("IdPlayer", "IdChapter");
b.HasIndex("IdChapter");
b.ToTable("Play");
});
modelBuilder.Entity("Entities.QuestionEntity", b =>
{
b.Property<uint?>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Content")
.IsRequired()
.HasColumnType("TEXT");
b.Property<byte>("Difficulty")
.HasColumnType("INTEGER");
b.Property<uint?>("IdAnswerGood")
.HasColumnType("INTEGER");
b.Property<uint?>("IdChapter")
.HasColumnType("INTEGER");
b.Property<uint>("NbFalls")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.HasIndex("IdAnswerGood");
b.HasIndex("IdChapter");
b.ToTable("Questions");
});
modelBuilder.Entity("Entities.AnswerEntity", b =>
{
b.HasOne("Entities.QuestionEntity", "Question")
.WithMany()
.HasForeignKey("IdQuestion")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Question");
});
modelBuilder.Entity("Entities.LobbyEntity", b =>
{
b.HasOne("Entities.PlayerEntity", "Creator")
.WithMany()
.HasForeignKey("IdCreator");
b.Navigation("Creator");
});
modelBuilder.Entity("Entities.LobbyEntityPlayerEntity", b =>
{
b.HasOne("Entities.LobbyEntity", "Lobby")
.WithMany()
.HasForeignKey("IdLobby")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Entities.PlayerEntity", "Player")
.WithMany()
.HasForeignKey("IdPlayer")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Lobby");
b.Navigation("Player");
});
modelBuilder.Entity("Entities.PlayerEntityChapterEntity", b =>
{
b.HasOne("Entities.ChapterEntity", "Chapter")
.WithMany()
.HasForeignKey("IdChapter")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Entities.PlayerEntity", "Player")
.WithMany()
.HasForeignKey("IdPlayer")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Chapter");
b.Navigation("Player");
});
modelBuilder.Entity("Entities.QuestionEntity", b =>
{
b.HasOne("Entities.AnswerEntity", "AnswerGood")
.WithMany()
.HasForeignKey("IdAnswerGood");
b.HasOne("Entities.ChapterEntity", "Chapter")
.WithMany()
.HasForeignKey("IdChapter");
b.Navigation("AnswerGood");
b.Navigation("Chapter");
});
#pragma warning restore 612, 618
}
}
}

@ -22,7 +22,7 @@ namespace Entities
[Required]
[ForeignKey(nameof(Question))]
public uint IdQuestion { get; set; }
public uint? IdQuestion { get; set; }
public QuestionEntity? Question { get; set; }
}

@ -27,6 +27,6 @@ namespace Entities
[ForeignKey(nameof(Creator))]
public uint? IdCreator { get; set; }
public PlayerEntity Creator { get; set; } = null!;
public PlayerEntity? Creator { get; set; }
}
}

@ -1,4 +1,5 @@
using DbConnectionLibrairie;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
@ -23,12 +24,17 @@ namespace UnitTestsEntityManagers
/// </summary>
public AbstractUnitTestEM()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var opt = new DbContextOptionsBuilder<MyDbContext>()
.UseSqlite("DataSource=:memory:")
.UseSqlite(connection)
.Options;
dbContext = new MyDbContext(opt);
dbContext.Database.EnsureCreated();
httpClient = new HttpClient();
}

@ -1,129 +1,140 @@
using DbConnectionLibrairie;
using Entities;
using EntityManagers;
using Newtonsoft.Json;
namespace UnitTestsEntityManagers
{
public class UnitTestAnswerManager : AbstractUnitTestEM
{
IEnumerable<AnswerEntity> answers = JsonConvert
.DeserializeObject<List<AnswerEntity>>
(File.ReadAllTextAsync("../fake-Answers.json").Result)!; // 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 async void TestAjouterAnswer()
{
var answerToAdd = new AnswerEntity { Id = 0, Content = "damien" };
var a = await mgr.addAnswer(answerToAdd);
// 1) normal insertion
// WF : it work perfectally
// and a is the same as answerToAdd
Assert.NotNull(a);
Assert.Equal(answerToAdd.Content, a.Content);
Assert.Equal(answerToAdd.Id, a.Id);
answerToAdd = new AnswerEntity { Id = 5, Content = "damien" };
a = await mgr.addAnswer(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((uint)1, a.Id);
answerToAdd = new AnswerEntity { Id = 7, Content = "châteîgne" };
a = await mgr.addAnswer(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 = await mgr.addAnswer(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 async Task TestSupprimerAnswer()
{
// remember that we have only 2 answers in the database :
// 1) (0, "châteigne")
// 2) (1, "damien")
var a = await mgr.removeAnswer(2);
// 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(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((uint)0, a.Id);
Assert.Equal("châteigne", a.Content);
a = await mgr.removeAnswer(1);
// 1) same thing with the id 1 just
// to clean 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((uint)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 async Task TestGetNbElement()
{
Assert.Equal(0, mgr.getNbElements()); // just to be sure
Assert.NotNull(answers); // just to be sure
int count = 0;
foreach (var answer in answers)
{
await mgr.addAnswer(answer);
count++;
Assert.Equal(count, mgr.getNbElements());
}
}
// getAnswers
// modifierAnswer
// supprimerAnswer (les 2)
// getAnswersByIdQuestion
}
using DbConnectionLibrairie;
using Entities;
using EntityManagers;
namespace UnitTestsEntityManagers
{
public class UnitTestAnswerManager : AbstractUnitTestEM
{
AnswerEntityManager mgr;
public UnitTestAnswerManager()
: base()
{
mgr = new AnswerEntityManager(dbContext);
}
/// <summary>
/// test of the 'ajouterAnswer' method of an AnswerManager
/// </summary>
[Fact]
public async void TestAddAnswer()
{
var answerToAdd = new AnswerEntity { Id = 0, Content = "damien" };
var a = await mgr.addAnswer(answerToAdd);
// 1) normal insertion
// WF : it work perfectally
// and a is the same as answerToAdd
Assert.NotNull(a);
Assert.Equal(answerToAdd.Content, a.Content);
Assert.Equal(answerToAdd.Id, a.Id);
answerToAdd = new AnswerEntity { Id = 5, Content = "damien" };
a = await mgr.addAnswer(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((uint)1, a.Id);
answerToAdd = new AnswerEntity { Id = 7, Content = "châteîgne" };
a = await mgr.addAnswer(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 = await mgr.addAnswer(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 async Task TestRemoveAnswer()
{
// remember that we have only 2 answers in the database :
// 1) (0, "châteigne")
// 2) (1, "damien")
var a = await mgr.removeAnswer(2);
// 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(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((uint)0, a.Id);
Assert.Equal("châteigne", a.Content);
a = await mgr.removeAnswer(1);
// 1) same thing with the id 1 just
// to clean 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((uint)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 async Task TestGetNbElement()
{
Assert.Equal(0, mgr.getNbElements()); // 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.getNbElements()); // ok, it's incremented
}
}
public static IEnumerable<Object?[]> TestGetAnswer_Datas()
{
uint max = 0;
foreach (var item in fakeAnswers.datas)
{
yield return new Object[] { item.Id, item };
if(max < item.Id) max = item.Id;
}
yield return new Object[] { max+1, null };
}
[Theory]
[MemberData(nameof(TestGetAnswer_Datas))]
public async Task TestGetAnswer(uint id, AnswerEntity? waiting)
{
Assert.Equal(waiting, await mgr.getAnswer(id));
}
// modifierAnswer
// supprimerAnswer (les 2)
// getAnswersByIdQuestion
}
}

@ -12,7 +12,7 @@
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
</ItemGroup>
@ -21,6 +21,7 @@
<ProjectReference Include="..\DbConnectionLibrairie\DbConnectionLibrairie.csproj" />
<ProjectReference Include="..\Entities\Entities.csproj" />
<ProjectReference Include="..\EntityManagers\EntityManagers.csproj" />
<ProjectReference Include="..\FakeDatas\FakeDatas.csproj" />
</ItemGroup>
<ItemGroup>

@ -27,7 +27,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ServiceManagers", "ServiceM
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApi", "WebApi\WebApi.csproj", "{7A65FECF-F4F5-4D88-853D-F21A3BAA5E39}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FakeDatas", "FakeDatas\FakeDatas.csproj", "{3C930FB5-2F1E-463E-978B-F0B5C7A58307}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FakeDatas", "FakeDatas\FakeDatas.csproj", "{3C930FB5-2F1E-463E-978B-F0B5C7A58307}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution

Loading…
Cancel
Save