From 454e3e0aed7bce8e289e1218d0b27282587fed8c Mon Sep 17 00:00:00 2001 From: Louwar Date: Wed, 22 Feb 2023 12:02:42 +0100 Subject: [PATCH] Add test EF --- Sources/ConsoleDB/Program.cs | 4 +- Sources/EFlib/EFChampion.cs | 14 ++- Sources/EFlib/EFChampionContext.cs | 41 ------- Sources/EFlib/EFSkin.cs | 17 +++ Sources/EFlib/SQLiteContext.cs | 9 +- Sources/LeagueOfLegends.sln | 28 +++-- Sources/StubEF/StubEF.csproj | 19 ++++ Sources/StubEF/StubEFChampions.cs | 29 +++++ Sources/TestEF/TestEF.csproj | 29 +++++ Sources/TestEF/UnitTestChampion.cs | 133 +++++++++++++++++++++++ Sources/TestEF/Usings.cs | 1 + Sources/TestProject1/TestProject1.csproj | 23 ---- Sources/TestProject1/UnitTest1.cs | 59 ---------- Sources/TestProject1/Usings.cs | 1 - Sources/TestProject2/TestProject2.csproj | 24 ++++ Sources/TestProject2/UnitTest1.cs | 11 ++ Sources/TestProject2/Usings.cs | 1 + 17 files changed, 307 insertions(+), 136 deletions(-) delete mode 100644 Sources/EFlib/EFChampionContext.cs create mode 100644 Sources/EFlib/EFSkin.cs create mode 100644 Sources/StubEF/StubEF.csproj create mode 100644 Sources/StubEF/StubEFChampions.cs create mode 100644 Sources/TestEF/TestEF.csproj create mode 100644 Sources/TestEF/UnitTestChampion.cs create mode 100644 Sources/TestEF/Usings.cs delete mode 100644 Sources/TestProject1/TestProject1.csproj delete mode 100644 Sources/TestProject1/UnitTest1.cs delete mode 100644 Sources/TestProject1/Usings.cs create mode 100644 Sources/TestProject2/TestProject2.csproj create mode 100644 Sources/TestProject2/UnitTest1.cs create mode 100644 Sources/TestProject2/Usings.cs diff --git a/Sources/ConsoleDB/Program.cs b/Sources/ConsoleDB/Program.cs index bbe5f63..8426e71 100644 --- a/Sources/ConsoleDB/Program.cs +++ b/Sources/ConsoleDB/Program.cs @@ -12,7 +12,7 @@ EFChampion boss1 = new EFChampion { Name = "bigBoss", Bio = "KingOfMetal", Icon EFChampion boss2 = new EFChampion { Name = "Soon", Bio = "Indomptable", Icon = "vide" }; EFChampion boss3 = new EFChampion { Name = "doctorWho", Bio = "Le silence", Icon="vide" }; -using (var context = new EFChampionContext()) +using (var context = new SQLiteContext()) { // Crée des EFChampion et les insère dans la base Console.WriteLine("Creates and inserts new EFChampion"); @@ -22,7 +22,7 @@ using (var context = new EFChampionContext()) context.SaveChanges(); } -using (var context = new EFChampionContext()) +using (var context = new SQLiteContext()) { foreach (var n in context.Champions) { diff --git a/Sources/EFlib/EFChampion.cs b/Sources/EFlib/EFChampion.cs index 45f656d..7a344ad 100644 --- a/Sources/EFlib/EFChampion.cs +++ b/Sources/EFlib/EFChampion.cs @@ -9,11 +9,21 @@ public string Name { get; set; } public string Bio { get; set; } public string Icon { get; set; } + + //public Dictionary Characteristics { get; set; } //public int this - + /**** Méthodes ****/ - //public Champion() { } + public EFChampion() + {} + public EFChampion(int id, string name, string bio, string icon) + { + Id = id; + Name = name; + Bio = bio; + Icon = icon; + } } diff --git a/Sources/EFlib/EFChampionContext.cs b/Sources/EFlib/EFChampionContext.cs deleted file mode 100644 index c839dc9..0000000 --- a/Sources/EFlib/EFChampionContext.cs +++ /dev/null @@ -1,41 +0,0 @@ -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace EFlib -{ - public class EFChampionContext : DbContext - { - - /**** Attributs ****/ - public DbSet Champions { get; set; } - - /**** Méthodes ****/ - public EFChampionContext() - { } - - public EFChampionContext(DbContextOptions options) - : base(options) - { } - - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - { - if (!optionsBuilder.IsConfigured) - { - optionsBuilder.UseSqlite($"Data Source=projet.dbloulou.db"); - } - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - optionsBuilder.UseSqlite(connection); - } - /* - var options = new DbContextOptionsBuilder().UseSqlite($"Data Source=LeNomDe.MaBase.db").Options; - var context = new TheDbContext(options); //ou une autre classe dérivant de TheDbContext - await context.Database.EnsureCreatedAsync(); //pour créer la base si elle n'existe pas déjà - */ - } -} diff --git a/Sources/EFlib/EFSkin.cs b/Sources/EFlib/EFSkin.cs new file mode 100644 index 0000000..714f763 --- /dev/null +++ b/Sources/EFlib/EFSkin.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EFlib +{ + internal class EFSkin + { + /**** Attributs ****/ + public string Name { get; set; } + public string Description { get; set; } + public string Icon { get; set; } + public string Float { get; set; } + } +} diff --git a/Sources/EFlib/SQLiteContext.cs b/Sources/EFlib/SQLiteContext.cs index 89f60b3..50a4b47 100644 --- a/Sources/EFlib/SQLiteContext.cs +++ b/Sources/EFlib/SQLiteContext.cs @@ -12,8 +12,15 @@ namespace EFlib { /**** Attributs ****/ public DbSet Champions { get; set; } - + + /**** Méthodes ****/ + public SQLiteContext() + { } + + public SQLiteContext(DbContextOptions options) + : base(options) + { } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlite($"Data Source=projet.dbloulou.db"); diff --git a/Sources/LeagueOfLegends.sln b/Sources/LeagueOfLegends.sln index d2f4f1a..5665c80 100644 --- a/Sources/LeagueOfLegends.sln +++ b/Sources/LeagueOfLegends.sln @@ -21,9 +21,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleEF", "ConsoleDB\Cons EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "API", "API\API.csproj", "{37E43647-A760-467F-A3C3-71E25149D1BF}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleAPI", "ConsoleAPI\ConsoleAPI.csproj", "{1BC3389F-495C-4889-8969-BD566F1512AF}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleAPI", "ConsoleAPI\ConsoleAPI.csproj", "{1BC3389F-495C-4889-8969-BD566F1512AF}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestProject1", "TestProject1\TestProject1.csproj", "{C6B89B38-761F-4B84-A3C7-BAC7FE0C603B}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StubEF", "StubEF\StubEF.csproj", "{9AA1A21B-90EE-4D6C-B47B-8FDA701078EF}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestProject2", "TestProject2\TestProject2.csproj", "{48213B2E-9BFA-4735-852C-E4D401E7CED5}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestEF", "TestEF\TestEF.csproj", "{84B0BF2B-17C8-403B-9A26-2310A2A368F9}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -63,10 +67,18 @@ Global {1BC3389F-495C-4889-8969-BD566F1512AF}.Debug|Any CPU.Build.0 = Debug|Any CPU {1BC3389F-495C-4889-8969-BD566F1512AF}.Release|Any CPU.ActiveCfg = Release|Any CPU {1BC3389F-495C-4889-8969-BD566F1512AF}.Release|Any CPU.Build.0 = Release|Any CPU - {C6B89B38-761F-4B84-A3C7-BAC7FE0C603B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {C6B89B38-761F-4B84-A3C7-BAC7FE0C603B}.Debug|Any CPU.Build.0 = Debug|Any CPU - {C6B89B38-761F-4B84-A3C7-BAC7FE0C603B}.Release|Any CPU.ActiveCfg = Release|Any CPU - {C6B89B38-761F-4B84-A3C7-BAC7FE0C603B}.Release|Any CPU.Build.0 = Release|Any CPU + {9AA1A21B-90EE-4D6C-B47B-8FDA701078EF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9AA1A21B-90EE-4D6C-B47B-8FDA701078EF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9AA1A21B-90EE-4D6C-B47B-8FDA701078EF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9AA1A21B-90EE-4D6C-B47B-8FDA701078EF}.Release|Any CPU.Build.0 = Release|Any CPU + {48213B2E-9BFA-4735-852C-E4D401E7CED5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {48213B2E-9BFA-4735-852C-E4D401E7CED5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {48213B2E-9BFA-4735-852C-E4D401E7CED5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {48213B2E-9BFA-4735-852C-E4D401E7CED5}.Release|Any CPU.Build.0 = Release|Any CPU + {84B0BF2B-17C8-403B-9A26-2310A2A368F9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {84B0BF2B-17C8-403B-9A26-2310A2A368F9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {84B0BF2B-17C8-403B-9A26-2310A2A368F9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {84B0BF2B-17C8-403B-9A26-2310A2A368F9}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -74,7 +86,9 @@ Global GlobalSection(NestedProjects) = preSolution {1889FA6E-B7C6-416E-8628-9449FB9070B9} = {C76D0C23-1FFA-4963-93CD-E12BD643F030} {B01D7EF2-2D64-409A-A29A-61FB7BB7A9DB} = {2C607793-B163-4731-A4D1-AFE8A7C4C170} - {C6B89B38-761F-4B84-A3C7-BAC7FE0C603B} = {C76D0C23-1FFA-4963-93CD-E12BD643F030} + {9AA1A21B-90EE-4D6C-B47B-8FDA701078EF} = {2C607793-B163-4731-A4D1-AFE8A7C4C170} + {48213B2E-9BFA-4735-852C-E4D401E7CED5} = {C76D0C23-1FFA-4963-93CD-E12BD643F030} + {84B0BF2B-17C8-403B-9A26-2310A2A368F9} = {C76D0C23-1FFA-4963-93CD-E12BD643F030} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {92F3083D-793F-4552-8A9A-0AD6534159C9} diff --git a/Sources/StubEF/StubEF.csproj b/Sources/StubEF/StubEF.csproj new file mode 100644 index 0000000..b4c70c5 --- /dev/null +++ b/Sources/StubEF/StubEF.csproj @@ -0,0 +1,19 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + + + diff --git a/Sources/StubEF/StubEFChampions.cs b/Sources/StubEF/StubEFChampions.cs new file mode 100644 index 0000000..a86956e --- /dev/null +++ b/Sources/StubEF/StubEFChampions.cs @@ -0,0 +1,29 @@ +using Microsoft.EntityFrameworkCore; +using EFlib; + +namespace StubEF +{ + public class StubEFChampions : SQLiteContext + { + /**** Attributs ****/ + + + /**** Méthodes ****/ + public StubEFChampions(DbContextOptions options) + : base(options) + { } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + modelBuilder.Entity().HasData( + new EFChampion (1, "Akali", "maBio1", "monIcon1" ), + new EFChampion (2, "Aatrox", "maBio2", "monIcon2" ), + new EFChampion (3, "Ahri", "maBio3", "monIcon3" ), + new EFChampion (4, "Akshan", "maBio4", "monIcon4"), + new EFChampion (5, "Bard", "maBio5", "monIcon5"), + new EFChampion (6, "Alistar", "maBio6", "monIcon6") + ); + } + } +} diff --git a/Sources/TestEF/TestEF.csproj b/Sources/TestEF/TestEF.csproj new file mode 100644 index 0000000..24a595b --- /dev/null +++ b/Sources/TestEF/TestEF.csproj @@ -0,0 +1,29 @@ + + + + net6.0 + enable + enable + + false + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + diff --git a/Sources/TestEF/UnitTestChampion.cs b/Sources/TestEF/UnitTestChampion.cs new file mode 100644 index 0000000..9844bb6 --- /dev/null +++ b/Sources/TestEF/UnitTestChampion.cs @@ -0,0 +1,133 @@ +using EFlib; +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; +using StubEF; +using Xunit; + +namespace TestEF +{ + public class UniTestChampion + { + [Fact] + public async Task GetChampion_Test() + { + //connection must be opened to use In-memory database + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + + var options = new DbContextOptionsBuilder().UseSqlite(connection).Options; + + using (var context = new StubEFChampions(options)) + { + //context.Database.OpenConnection(); + await context.Database.EnsureCreatedAsync(); //pour créer la base si elle n'existe pas déjà + + var champs = context.Champions.SingleOrDefault(c=> c.Id == 1); + + Assert.NotNull(champs); + Assert.Equal(1, champs.Id); + } + } + /* + [Fact] + public void Modify_Test() + { + //connection must be opened to use In-memory database + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + + var options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + + //prepares the database with one instance of the context + using (var context = new NounoursContext(options)) + { + //context.Database.OpenConnection(); + context.Database.EnsureCreated(); + + Nounours chewie = new Nounours { Nom = "Chewbacca" }; + Nounours yoda = new Nounours { Nom = "Yoda" }; + Nounours ewok = new Nounours { Nom = "Ewok" }; + + context.Nounours.Add(chewie); + context.Nounours.Add(yoda); + context.Nounours.Add(ewok); + context.SaveChanges(); + } + + //uses another instance of the context to do the tests + using (var context = new NounoursContext(options)) + { + context.Database.EnsureCreated(); + + string nameToFind = "ew"; + Assert.Equal(2, context.Nounours.Where(n => n.Nom.ToLower().Contains(nameToFind)).Count()); + nameToFind = "wo"; + Assert.Equal(1, context.Nounours.Where(n => n.Nom.ToLower().Contains(nameToFind)).Count()); + var ewok = context.Nounours.Where(n => n.Nom.ToLower().Contains(nameToFind)).First(); + ewok.Nom = "Wicket"; + context.SaveChanges(); + } + + //uses another instance of the context to do the tests + using (var context = new NounoursContext(options)) + { + context.Database.EnsureCreated(); + + string nameToFind = "ew"; + Assert.Equal(1, context.Nounours.Where(n => n.Nom.ToLower().Contains(nameToFind)).Count()); + nameToFind = "wick"; + Assert.Equal(1, context.Nounours.Where(n => n.Nom.ToLower().Contains(nameToFind)).Count()); + } + }*/ + /* + [SetUp] + public void Setup() + { + } + + + [Test] + public void TestGET() + { + //Arrange + + //Act + var championResult = ChampionController.get(); + + //Assert + var objectResult = championResult as OkObjectResult; + //vérifie que c’est un ok 200 et un objet + Assert.IsNotNull(objectResult); + + var champions = objectResult?.Value as IEnumerable; + Assert.IsNotNull(champions); + + Assert.AreEqual(champions.Count(), (await StubData.championsMgr.getItems(0, 5)).Count()); + } + + [Test] + public void TestPOST() + { + //Arrange + var championDto = new ChampionDto() + { + Name = "Darius" + }; + + //Act + var championResult = await ChampionController.post(championDto); + + //Assert + var objectResult = championResult as CreatedAtActionResult; + Assert.IsNotNull(objectResult); + + var champions = objectResult?.Value as ChampionDto; + Assert.IsNotNull(champions); + + }*/ + + + } +} \ No newline at end of file diff --git a/Sources/TestEF/Usings.cs b/Sources/TestEF/Usings.cs new file mode 100644 index 0000000..8c927eb --- /dev/null +++ b/Sources/TestEF/Usings.cs @@ -0,0 +1 @@ +global using Xunit; \ No newline at end of file diff --git a/Sources/TestProject1/TestProject1.csproj b/Sources/TestProject1/TestProject1.csproj deleted file mode 100644 index 859e432..0000000 --- a/Sources/TestProject1/TestProject1.csproj +++ /dev/null @@ -1,23 +0,0 @@ - - - - net6.0 - enable - enable - - false - - - - - - - - - - - - - - - diff --git a/Sources/TestProject1/UnitTest1.cs b/Sources/TestProject1/UnitTest1.cs deleted file mode 100644 index 31b1209..0000000 --- a/Sources/TestProject1/UnitTest1.cs +++ /dev/null @@ -1,59 +0,0 @@ -using API.Controllers; -using API.Dto; -using Microsoft.AspNetCore.Mvc; -using StubLib; -using System.Collections.Generic; - -namespace TestProject1 -{ - public class Tests - { - [SetUp] - public void Setup() - { - } - - - /*[Test] - public void TestGET() - { - //Arrange - - //Act - var championResult = ChampionController.get(); - - //Assert - var objectResult = championResult as OkObjectResult; - //vérifie que c’est un ok 200 et un objet - Assert.IsNotNull(objectResult); - - var champions = objectResult?.Value as IEnumerable; - Assert.IsNotNull(champions); - - Assert.AreEqual(champions.Count(), (await StubData.championsMgr.getItems(0, 5)).Count()); - } - - [Test] - public void TestPOST() - { - //Arrange - var championDto = new ChampionDto() - { - Name = "Darius" - }; - - //Act - var championResult = await ChampionController.post(championDto); - - //Assert - var objectResult = championResult as CreatedAtActionResult; - Assert.IsNotNull(objectResult); - - var champions = objectResult?.Value as ChampionDto; - Assert.IsNotNull(champions); - - }*/ - - - } -} \ No newline at end of file diff --git a/Sources/TestProject1/Usings.cs b/Sources/TestProject1/Usings.cs deleted file mode 100644 index cefced4..0000000 --- a/Sources/TestProject1/Usings.cs +++ /dev/null @@ -1 +0,0 @@ -global using NUnit.Framework; \ No newline at end of file diff --git a/Sources/TestProject2/TestProject2.csproj b/Sources/TestProject2/TestProject2.csproj new file mode 100644 index 0000000..c5d1063 --- /dev/null +++ b/Sources/TestProject2/TestProject2.csproj @@ -0,0 +1,24 @@ + + + + net6.0 + enable + enable + + false + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + diff --git a/Sources/TestProject2/UnitTest1.cs b/Sources/TestProject2/UnitTest1.cs new file mode 100644 index 0000000..2e7a3d1 --- /dev/null +++ b/Sources/TestProject2/UnitTest1.cs @@ -0,0 +1,11 @@ +namespace TestProject2 +{ + public class UnitTest1 + { + [Fact] + public void Test1() + { + + } + } +} \ No newline at end of file diff --git a/Sources/TestProject2/Usings.cs b/Sources/TestProject2/Usings.cs new file mode 100644 index 0000000..8c927eb --- /dev/null +++ b/Sources/TestProject2/Usings.cs @@ -0,0 +1 @@ +global using Xunit; \ No newline at end of file