Test unitaire EF add

pull/2/head
nathan boileau 2 years ago
parent 62e23ce924
commit 3c642ff5e3

@ -7,7 +7,7 @@ using Microsoft.EntityFrameworkCore;
namespace EFLol namespace EFLol
{ {
internal class ChampionContext : DbContext public class ChampionContext : DbContext
{ {
public DbSet<ChampionEntity> Champions { get; set; } public DbSet<ChampionEntity> Champions { get; set; }

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace EFLol namespace EFLol
{ {
internal class ChampionEntity public class ChampionEntity
{ {
public int Id { get; set; } public int Id { get; set; }
public string Name { get; set; } public string Name { get; set; }

@ -10,8 +10,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace EFLol.Migrations namespace EFLol.Migrations
{ {
[DbContext(typeof(ChampionContext))] [DbContext(typeof(ChampionContext))]
[Migration("20230201162158_FirstMig")] [Migration("20230204101816_migration1")]
partial class FirstMig partial class migration1
{ {
/// <inheritdoc /> /// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder) protected override void BuildTargetModel(ModelBuilder modelBuilder)

@ -5,7 +5,7 @@
namespace EFLol.Migrations namespace EFLol.Migrations
{ {
/// <inheritdoc /> /// <inheritdoc />
public partial class FirstMig : Migration public partial class migration1 : Migration
{ {
/// <inheritdoc /> /// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder) protected override void Up(MigrationBuilder migrationBuilder)

@ -11,4 +11,8 @@ using (var context = new ChampionContext())
Console.WriteLine("Adding Zeus to the database..."); Console.WriteLine("Adding Zeus to the database...");
context.Champions.Add(Zeus); context.Champions.Add(Zeus);
context.SaveChanges(); context.SaveChanges();
} }

Binary file not shown.

@ -28,10 +28,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "apiLOL", "apiLOL\apiLOL.csp
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFLol", "EFLol\EFLol.csproj", "{7AEE66D2-490B-4049-B9D3-C629D7F78DA7}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFLol", "EFLol\EFLol.csproj", "{7AEE66D2-490B-4049-B9D3-C629D7F78DA7}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestUnitaireAPiLol", "TestUnitaireAPiLol\TestUnitaireAPiLol.csproj", "{AD7F3A69-AC84-42CA-BD7E-FB037DAB1581}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestUnitaire", "TestUnitaire\TestUnitaire.csproj", "{5702F240-97BD-4757-918C-6E485C57D0EC}"
ProjectSection(ProjectDependencies) = postProject ProjectSection(ProjectDependencies) = postProject
{2960F9BA-49DE-494D-92E3-CE5A794BA1A9} = {2960F9BA-49DE-494D-92E3-CE5A794BA1A9}
{65B824B5-FADB-4C89-8B4B-D541B62B7DCA} = {65B824B5-FADB-4C89-8B4B-D541B62B7DCA} {65B824B5-FADB-4C89-8B4B-D541B62B7DCA} = {65B824B5-FADB-4C89-8B4B-D541B62B7DCA}
{7AEE66D2-490B-4049-B9D3-C629D7F78DA7} = {7AEE66D2-490B-4049-B9D3-C629D7F78DA7}
{B01D7EF2-2D64-409A-A29A-61FB7BB7A9DB} = {B01D7EF2-2D64-409A-A29A-61FB7BB7A9DB} {B01D7EF2-2D64-409A-A29A-61FB7BB7A9DB} = {B01D7EF2-2D64-409A-A29A-61FB7BB7A9DB}
EndProjectSection EndProjectSection
EndProject EndProject
@ -65,10 +65,10 @@ Global
{7AEE66D2-490B-4049-B9D3-C629D7F78DA7}.Debug|Any CPU.Build.0 = Debug|Any CPU {7AEE66D2-490B-4049-B9D3-C629D7F78DA7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7AEE66D2-490B-4049-B9D3-C629D7F78DA7}.Release|Any CPU.ActiveCfg = Release|Any CPU {7AEE66D2-490B-4049-B9D3-C629D7F78DA7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7AEE66D2-490B-4049-B9D3-C629D7F78DA7}.Release|Any CPU.Build.0 = Release|Any CPU {7AEE66D2-490B-4049-B9D3-C629D7F78DA7}.Release|Any CPU.Build.0 = Release|Any CPU
{AD7F3A69-AC84-42CA-BD7E-FB037DAB1581}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {5702F240-97BD-4757-918C-6E485C57D0EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AD7F3A69-AC84-42CA-BD7E-FB037DAB1581}.Debug|Any CPU.Build.0 = Debug|Any CPU {5702F240-97BD-4757-918C-6E485C57D0EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AD7F3A69-AC84-42CA-BD7E-FB037DAB1581}.Release|Any CPU.ActiveCfg = Release|Any CPU {5702F240-97BD-4757-918C-6E485C57D0EC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AD7F3A69-AC84-42CA-BD7E-FB037DAB1581}.Release|Any CPU.Build.0 = Release|Any CPU {5702F240-97BD-4757-918C-6E485C57D0EC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

@ -0,0 +1,37 @@
using apiLOL;
using apiLOL.Controllers;
using Microsoft.AspNetCore.Mvc;
using StubLib;
namespace TestUnitaire
{
public class TestAPILol
{
[Fact]
public void Test1()
{
}
[Fact]
public void TestPostChampion()
{
// Arrange
var data = new StubData();
var controller = new ControllerChampions(new StubData());
var champDTO = new ChampionDTO("Charles", "Charles est un champion de League of Legends");
// Act
var result = controller.Post(champDTO);
data.ChampionsMgr.AddItem(champDTO.ToModel());
var nbItem = data.ChampionsMgr.GetNbItems();
Task<int> nbItemTask = nbItem;
// Assert
Assert.IsType<OkResult>(result);
// Verify that the champions is added to the stub
Assert.Equal(7, nbItemTask.Result);
}
}
}

@ -0,0 +1,40 @@

using EFLol;
namespace TestUnitaire
{
public class TestEfLol
{
[Fact]
public void TestAddIntoDB()
{
// Arrange
ChampionEntity Zeus = new ChampionEntity
{
Name = "Zeus",
Bio = "Zeus is the king of the gods."
};
// Act
var context = new ChampionContext();
context.Champions.Add(Zeus);
context.SaveChanges();
// Assert
var champion = context.Champions.FirstOrDefault(c => c.Name == "Zeus");
if (champion == null)
{
Assert.True(false, "Champion not found in database.");
}
Assert.NotNull(champion);
Assert.Equal("Zeus", champion.Name);
Assert.Equal("Zeus is the king of the gods.", champion.Bio);
}
}
}

@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\apiLOL\apiLOL.csproj" />
<ProjectReference Include="..\EFLol\EFLol.csproj" />
<ProjectReference Include="..\StubLib\StubLib.csproj" />
</ItemGroup>
</Project>

@ -0,0 +1 @@
global using Xunit;

@ -3,9 +3,9 @@ using apiLOL.Controllers;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using StubLib; using StubLib;
namespace TestUnitaireAPiLol namespace TestUnitaire
{ {
public class UnitTest1 public class TestAPILol
{ {
[Fact] [Fact]
public void Test1() public void Test1()

@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestUnitaire
{
public class TestEFLol
{
public void TestAddIntoDB()
{
// Arrange
ChampionEntity Zeus = new ChampionEntity
{
Name = "Zeus",
Bio = "Zeus is the king of the gods."
};
// Act
using (var context = new ChampionContext())
{
Console.WriteLine("Adding Zeus to the database...");
context.Champions.Add(Zeus);
context.SaveChanges();
}
// Assert
using (var context = new ChampionContext())
{
var champion = context.Champions.FirstOrDefault(c => c.Name == "Zeus");
if (champion == null)
{
Assert.True(false, "Champion not found in database.");
}
Assert.NotNull(champion);
Assert.Equal("Zeus", champion.Name);
Assert.Equal("Zeus is the king of the gods.", champion.Bio);
}
}
public void TestDeleteFromDB()
{
// Act
}
}
}
Loading…
Cancel
Save