From 3c642ff5e3c0cc2e7723138795ad3ec50c5ca797 Mon Sep 17 00:00:00 2001 From: nathan boileau Date: Sat, 4 Feb 2023 11:19:52 +0100 Subject: [PATCH] Test unitaire EF add --- Sources/EFLol/ChampionContext.cs | 2 +- Sources/EFLol/ChampionEntity.cs | 2 +- ... => 20230204101816_migration1.Designer.cs} | 4 +- ...rstMig.cs => 20230204101816_migration1.cs} | 2 +- Sources/EFLol/Program.cs | 6 ++- Sources/EFLol/loldb.db | Bin 20480 -> 20480 bytes Sources/LeagueOfLegends.sln | 12 ++--- Sources/TestUnitaire/TestAPILol.cs | 37 +++++++++++++ Sources/TestUnitaire/TestEfLol.cs | 40 ++++++++++++++ Sources/TestUnitaire/TestUnitaire.csproj | 30 +++++++++++ Sources/TestUnitaire/Usings.cs | 1 + .../{UnitTest1.cs => TestAPILol.cs} | 4 +- Sources/TestUnitaireAPiLol/TestEFLol.cs | 49 ++++++++++++++++++ ...taireAPiLol.csproj => TestUnitaire.csproj} | 0 14 files changed, 175 insertions(+), 14 deletions(-) rename Sources/EFLol/Migrations/{20230201162158_FirstMig.Designer.cs => 20230204101816_migration1.Designer.cs} (94%) rename Sources/EFLol/Migrations/{20230201162158_FirstMig.cs => 20230204101816_migration1.cs} (95%) create mode 100644 Sources/TestUnitaire/TestAPILol.cs create mode 100644 Sources/TestUnitaire/TestEfLol.cs create mode 100644 Sources/TestUnitaire/TestUnitaire.csproj create mode 100644 Sources/TestUnitaire/Usings.cs rename Sources/TestUnitaireAPiLol/{UnitTest1.cs => TestAPILol.cs} (93%) create mode 100644 Sources/TestUnitaireAPiLol/TestEFLol.cs rename Sources/TestUnitaireAPiLol/{TestUnitaireAPiLol.csproj => TestUnitaire.csproj} (100%) diff --git a/Sources/EFLol/ChampionContext.cs b/Sources/EFLol/ChampionContext.cs index d1333c0..813108d 100644 --- a/Sources/EFLol/ChampionContext.cs +++ b/Sources/EFLol/ChampionContext.cs @@ -7,7 +7,7 @@ using Microsoft.EntityFrameworkCore; namespace EFLol { - internal class ChampionContext : DbContext + public class ChampionContext : DbContext { public DbSet Champions { get; set; } diff --git a/Sources/EFLol/ChampionEntity.cs b/Sources/EFLol/ChampionEntity.cs index e9fe819..8c8ac7f 100644 --- a/Sources/EFLol/ChampionEntity.cs +++ b/Sources/EFLol/ChampionEntity.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace EFLol { - internal class ChampionEntity + public class ChampionEntity { public int Id { get; set; } public string Name { get; set; } diff --git a/Sources/EFLol/Migrations/20230201162158_FirstMig.Designer.cs b/Sources/EFLol/Migrations/20230204101816_migration1.Designer.cs similarity index 94% rename from Sources/EFLol/Migrations/20230201162158_FirstMig.Designer.cs rename to Sources/EFLol/Migrations/20230204101816_migration1.Designer.cs index 29614aa..6210419 100644 --- a/Sources/EFLol/Migrations/20230201162158_FirstMig.Designer.cs +++ b/Sources/EFLol/Migrations/20230204101816_migration1.Designer.cs @@ -10,8 +10,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace EFLol.Migrations { [DbContext(typeof(ChampionContext))] - [Migration("20230201162158_FirstMig")] - partial class FirstMig + [Migration("20230204101816_migration1")] + partial class migration1 { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) diff --git a/Sources/EFLol/Migrations/20230201162158_FirstMig.cs b/Sources/EFLol/Migrations/20230204101816_migration1.cs similarity index 95% rename from Sources/EFLol/Migrations/20230201162158_FirstMig.cs rename to Sources/EFLol/Migrations/20230204101816_migration1.cs index 59c3ac5..c50961f 100644 --- a/Sources/EFLol/Migrations/20230201162158_FirstMig.cs +++ b/Sources/EFLol/Migrations/20230204101816_migration1.cs @@ -5,7 +5,7 @@ namespace EFLol.Migrations { /// - public partial class FirstMig : Migration + public partial class migration1 : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) diff --git a/Sources/EFLol/Program.cs b/Sources/EFLol/Program.cs index 775f405..38f62aa 100644 --- a/Sources/EFLol/Program.cs +++ b/Sources/EFLol/Program.cs @@ -11,4 +11,8 @@ using (var context = new ChampionContext()) Console.WriteLine("Adding Zeus to the database..."); context.Champions.Add(Zeus); context.SaveChanges(); -} \ No newline at end of file +} + + + + diff --git a/Sources/EFLol/loldb.db b/Sources/EFLol/loldb.db index ef35bd0418c784d5fbba46faa9a9afb18701dc67..4764763624ff79b428cd6258fd26a7406b960d36 100644 GIT binary patch delta 223 zcmZozz}T>WQ71UiC$l6~AuYcsH?c&)m_dMniHX5ML4kpRfeDCNffxpuCh8cAGBN1I zOY!pmU|`|h%fNSue=Q$B@7~RV0-n5+_wt4?GEWxbJH*I1`6}N{MyAQ@`NLW6GVtHs zEU0jqUssWl*JBmZLt{>PgI d9d7a~%P`w>;?%(la+m;6;Y0q36C~MCxd0WcH2wep delta 281 zcmZozz}T>WQ71UiC$l6~AuYcsH?c&)m_dMnk&(ecL4kpRfgOlhffxqZCh8cAvNPz# zOY!pmU|`|h%fP#r?-Ku7KK{*u0-n5+_wt4?vQHM`JH*I3`6}N{Mz+c8`NLW6Gw|Qv zENF0*Ut6A$*;?Glz{uFZ$iUFh%*fExBHk^tsJO&8Gu>RzK+lK^XcHs nbItemTask = nbItem; + + // Assert + Assert.IsType(result); + // Verify that the champions is added to the stub + Assert.Equal(7, nbItemTask.Result); + + } + } +} \ No newline at end of file diff --git a/Sources/TestUnitaire/TestEfLol.cs b/Sources/TestUnitaire/TestEfLol.cs new file mode 100644 index 0000000..a6fec54 --- /dev/null +++ b/Sources/TestUnitaire/TestEfLol.cs @@ -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); + } + } +} + + + + diff --git a/Sources/TestUnitaire/TestUnitaire.csproj b/Sources/TestUnitaire/TestUnitaire.csproj new file mode 100644 index 0000000..e625409 --- /dev/null +++ b/Sources/TestUnitaire/TestUnitaire.csproj @@ -0,0 +1,30 @@ + + + + net6.0 + enable + enable + + false + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + + diff --git a/Sources/TestUnitaire/Usings.cs b/Sources/TestUnitaire/Usings.cs new file mode 100644 index 0000000..8c927eb --- /dev/null +++ b/Sources/TestUnitaire/Usings.cs @@ -0,0 +1 @@ +global using Xunit; \ No newline at end of file diff --git a/Sources/TestUnitaireAPiLol/UnitTest1.cs b/Sources/TestUnitaireAPiLol/TestAPILol.cs similarity index 93% rename from Sources/TestUnitaireAPiLol/UnitTest1.cs rename to Sources/TestUnitaireAPiLol/TestAPILol.cs index ab3973d..b2a1758 100644 --- a/Sources/TestUnitaireAPiLol/UnitTest1.cs +++ b/Sources/TestUnitaireAPiLol/TestAPILol.cs @@ -3,9 +3,9 @@ using apiLOL.Controllers; using Microsoft.AspNetCore.Mvc; using StubLib; -namespace TestUnitaireAPiLol +namespace TestUnitaire { - public class UnitTest1 + public class TestAPILol { [Fact] public void Test1() diff --git a/Sources/TestUnitaireAPiLol/TestEFLol.cs b/Sources/TestUnitaireAPiLol/TestEFLol.cs new file mode 100644 index 0000000..491f46e --- /dev/null +++ b/Sources/TestUnitaireAPiLol/TestEFLol.cs @@ -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 + + } + + } +} diff --git a/Sources/TestUnitaireAPiLol/TestUnitaireAPiLol.csproj b/Sources/TestUnitaireAPiLol/TestUnitaire.csproj similarity index 100% rename from Sources/TestUnitaireAPiLol/TestUnitaireAPiLol.csproj rename to Sources/TestUnitaireAPiLol/TestUnitaire.csproj