From a88c59b0a37c45dfa8a66713adbd4d7bd85f1dcd Mon Sep 17 00:00:00 2001 From: nathan boileau Date: Wed, 8 Feb 2023 15:44:02 +0100 Subject: [PATCH 01/12] POST methode --- Sources/apiLOL/Controllers/ControllerChampions.cs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Sources/apiLOL/Controllers/ControllerChampions.cs b/Sources/apiLOL/Controllers/ControllerChampions.cs index 00e2396..6446010 100644 --- a/Sources/apiLOL/Controllers/ControllerChampions.cs +++ b/Sources/apiLOL/Controllers/ControllerChampions.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Mvc; +using System.Security.Cryptography; +using Microsoft.AspNetCore.Mvc; using Model; using StubLib; using System.Xml.Linq; @@ -12,8 +13,8 @@ namespace apiLOL.Controllers public class ControllerChampions : Controller { private readonly IDataManager data; - - + + public ControllerChampions(IDataManager manager) { @@ -35,7 +36,7 @@ namespace apiLOL.Controllers [Route("{name}")] public async Task GetChampion(string name) { - var champs = (await data.ChampionsMgr.GetItemsByName(name,0,1)).First(); + var champs = (await data.ChampionsMgr.GetItemsByName(name, 0, 1)).First(); return Ok(champs.ToDTO()); } @@ -47,6 +48,9 @@ namespace apiLOL.Controllers Champion tmp = champDTO.ToModel(); data.ChampionsMgr.AddItem(tmp); return Ok(); + + //return CreatedAtAction(nameof(GetChampion), new { Id = 1 }, + // await data.ChampionsMgr.AddItem(champDTO.ToModel())); } // PUT api//5 @@ -62,4 +66,4 @@ namespace apiLOL.Controllers } } -} +} \ No newline at end of file -- 2.36.3 From 1135c0078d9a188d9896b26b37c9cf30bad66f4b Mon Sep 17 00:00:00 2001 From: Nathan BOILEAU Date: Wed, 8 Feb 2023 15:47:52 +0100 Subject: [PATCH 02/12] =?UTF-8?q?Mise=20=C3=A0=20jour=20de=20'.drone.yml'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .drone.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.drone.yml b/.drone.yml index ace3152..ec592b6 100644 --- a/.drone.yml +++ b/.drone.yml @@ -54,7 +54,7 @@ steps: path: /docs commands: #- cd Documentation/doxygen - #- doxygen Doxyfile + #- doxygen Doxyfile - /entrypoint.sh when: branch: -- 2.36.3 From 3f37575dfcd4812731a76314e1f8bbdd1fb2880b Mon Sep 17 00:00:00 2001 From: nathan boileau Date: Wed, 8 Feb 2023 16:05:31 +0100 Subject: [PATCH 03/12] Modif gitignore : no push sonar --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 6c2aa92..5913f3d 100644 --- a/.gitignore +++ b/.gitignore @@ -428,4 +428,5 @@ FodyWeavers.xsd *.sln.iml *.db -*Migrations \ No newline at end of file +*Migrations +*.sonarqube \ No newline at end of file -- 2.36.3 From d1a38d234346ee0842792edafab505971f4b06d4 Mon Sep 17 00:00:00 2001 From: "bastien.ollier@etu.uca.fr" Date: Wed, 8 Feb 2023 16:15:15 +0100 Subject: [PATCH 04/12] change fact to theorie --- Sources/TestUnitaire/TestEfLol.cs | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/Sources/TestUnitaire/TestEfLol.cs b/Sources/TestUnitaire/TestEfLol.cs index 00b1923..ddaf938 100644 --- a/Sources/TestUnitaire/TestEfLol.cs +++ b/Sources/TestUnitaire/TestEfLol.cs @@ -6,8 +6,11 @@ namespace TestUnitaire { public class TestEfLol { - [Fact] - public async Task TestAddInMemory() + [Theory] + [InlineData("zeus","dieu")] + [InlineData("zeus", "dieu")] + [InlineData("zeus", "dieu")] + public async Task TestAddInMemory(String name, String bio) { // Arrange var connection = new SqliteConnection("DataSource=:memory:"); @@ -21,29 +24,23 @@ namespace TestUnitaire using (var context = new ChampionContext(options)) { await context.Database.EnsureCreatedAsync(); - var Zeus = new ChampionEntity - { - Name = "Zeus", - Bio = "Zeus is the king of the gods." - }; - var Poseidon = new ChampionEntity + var Dieu = new ChampionEntity { - Name = "Poseidon", - Bio = "Poseidon is the king of the sea." + Name = name, + Bio = bio }; ChampionEntity found = await context.Champions.SingleOrDefaultAsync(c => c.Name == "Zeus"); Assert.Null(found); - await context.Champions.AddAsync(Zeus); - await context.Champions.AddAsync(Poseidon); + await context.Champions.AddAsync(Dieu); await context.SaveChangesAsync(); - found = await context.Champions.SingleOrDefaultAsync(c => c.Name == "Zeus"); + found = await context.Champions.SingleOrDefaultAsync(c => c.Name == name); Assert.NotNull(found); - Assert.Equal(2, await context.Champions.CountAsync()); - Assert.Equal("Zeus", found.Name); + Assert.Equal(1, await context.Champions.CountAsync()); + Assert.Equal(name, found.Name); } } -- 2.36.3 From 43a5e6c35173b612fd3f72e40f68b855302ea910 Mon Sep 17 00:00:00 2001 From: "bastien.ollier@etu.uca.fr" Date: Wed, 8 Feb 2023 16:29:43 +0100 Subject: [PATCH 05/12] finish in memory --- Sources/TestUnitaire/TestEfLol.cs | 94 +++++++++++++++++-------------- 1 file changed, 52 insertions(+), 42 deletions(-) diff --git a/Sources/TestUnitaire/TestEfLol.cs b/Sources/TestUnitaire/TestEfLol.cs index ddaf938..e7ec753 100644 --- a/Sources/TestUnitaire/TestEfLol.cs +++ b/Sources/TestUnitaire/TestEfLol.cs @@ -45,48 +45,58 @@ namespace TestUnitaire } [Fact] - public void ModifyTestInMemory() - { - var options = new DbContextOptionsBuilder() - .UseInMemoryDatabase(databaseName: "Modify_Test_database") - .Options; - - //prepares the database with one instance of the context - - using (var context = new ChampionContext(options)) - { - ChampionEntity chewie = new ChampionEntity { Name = "Chewbacca", Bio = "Zeus is the king of the gods." }; - ChampionEntity yoda = new ChampionEntity { Name = "Yoda", Bio = "Zeus is the king of the gods." }; - ChampionEntity ewok = new ChampionEntity { Name = "Ewok", Bio = "Zeus is the king of the gods." }; - - context.Champions.Add(chewie); - context.Champions.Add(yoda); - context.Champions.Add(ewok); - context.SaveChanges(); - } - - //prepares the database with one instance of the context - using (var context = new ChampionContext(options)) - { - string nameToFind = "ew"; - Assert.Equal(2, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count()); - nameToFind = "ewo"; - Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count()); - var ewok = context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).First(); - ewok.Name = "Wicket"; - context.SaveChanges(); - } - - //prepares the database with one instance of the context - using (var context = new ChampionContext(options)) - { - string nameToFind = "ew"; - Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count()); - nameToFind = "wick"; - Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count()); - } - } - } + public void ModifyTestInMemory() + {//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 ChampionContext(options)) + { + //context.Database.OpenConnection(); + context.Database.EnsureCreated(); + + ChampionEntity chewie = new ChampionEntity { Name = "Chewbacca", Bio = "Zeus is the king of the gods." }; + ChampionEntity yoda = new ChampionEntity { Name = "Yoda", Bio = "Zeus is the king of the gods." }; + ChampionEntity ewok = new ChampionEntity { Name = "Ewok", Bio = "Zeus is the king of the gods." }; + + + context.Champions.Add(chewie); + context.Champions.Add(yoda); + context.Champions.Add(ewok); + context.SaveChanges(); + } + + //uses another instance of the context to do the tests + using (var context = new ChampionContext(options)) + { + context.Database.EnsureCreated(); + + string nameToFind = "ew"; + Assert.Equal(2, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count()); + nameToFind = "wo"; + Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count()); + var ewok = context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).First(); + ewok.Name = "Wicket"; + context.SaveChanges(); + } + + //uses another instance of the context to do the tests + using (var context = new ChampionContext(options)) + { + context.Database.EnsureCreated(); + + string nameToFind = "ew"; + Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count()); + nameToFind = "wick"; + Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count()); + } + } + } } -- 2.36.3 From 2190ca83c9f8613063d26073799e81f9344a1f5d Mon Sep 17 00:00:00 2001 From: "bastien.ollier@etu.uca.fr" Date: Wed, 8 Feb 2023 16:53:19 +0100 Subject: [PATCH 06/12] modify ChampionEntyty --- Sources/EFLol/ChampionEntity.cs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/Sources/EFLol/ChampionEntity.cs b/Sources/EFLol/ChampionEntity.cs index 8c8ac7f..8e6a25d 100644 --- a/Sources/EFLol/ChampionEntity.cs +++ b/Sources/EFLol/ChampionEntity.cs @@ -1,15 +1,16 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace EFLol +namespace EFLol { public class ChampionEntity { public int Id { get; set; } public string Name { get; set; } - public string Bio { get; set; } - } + public string Bio { get; set; } + + //public ChampionClass Class { get; set; } + //public string Icon { get; set; } + //public LargeImage Image { get; set; } + //public ReadOnlyCollection Skins { get; private set; } + //public ReadOnlyDictionary Characteristics { get; private set; } + //private HashSet skills = new HashSet(); + } } -- 2.36.3 From 548a3f57cce1b66b4e4ca6938ee141e14d0eb166 Mon Sep 17 00:00:00 2001 From: "bastien.ollier@etu.uca.fr" Date: Wed, 8 Feb 2023 17:07:06 +0100 Subject: [PATCH 07/12] add SkiinEntity --- Sources/EFLol/SkinEntity.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Sources/EFLol/SkinEntity.cs diff --git a/Sources/EFLol/SkinEntity.cs b/Sources/EFLol/SkinEntity.cs new file mode 100644 index 0000000..f186268 --- /dev/null +++ b/Sources/EFLol/SkinEntity.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EFLol +{ + public class SkinEntity + { + public String Name { get; set; } + public String Description { get; set; } + public String Icon { get; set; } + public float Price { get; set; } + } +} -- 2.36.3 From 2f1aff3c025a607779c301a24b42de53efd69d85 Mon Sep 17 00:00:00 2001 From: nathan boileau Date: Wed, 8 Feb 2023 17:25:11 +0100 Subject: [PATCH 08/12] Test unit : Limite de ajout --- Sources/EFLol/ChampionEntity.cs | 5 ++ Sources/TestUnitaire/TestEfLol.cs | 117 ++++++++++++++++-------------- 2 files changed, 69 insertions(+), 53 deletions(-) diff --git a/Sources/EFLol/ChampionEntity.cs b/Sources/EFLol/ChampionEntity.cs index 8c8ac7f..5638ce5 100644 --- a/Sources/EFLol/ChampionEntity.cs +++ b/Sources/EFLol/ChampionEntity.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -9,7 +10,11 @@ namespace EFLol public class ChampionEntity { public int Id { get; set; } + + [MaxLength(30, ErrorMessage = "Name cannot be longer than 30 characters.")] public string Name { get; set; } + + [MaxLength(256, ErrorMessage = "Bio cannot be longer than 256 characters.")] public string Bio { get; set; } } } diff --git a/Sources/TestUnitaire/TestEfLol.cs b/Sources/TestUnitaire/TestEfLol.cs index e7ec753..4d07f9a 100644 --- a/Sources/TestUnitaire/TestEfLol.cs +++ b/Sources/TestUnitaire/TestEfLol.cs @@ -1,52 +1,67 @@ -using EFLol; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; - -namespace TestUnitaire -{ - public class TestEfLol - { +using EFLol; +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; + +namespace TestUnitaire +{ + public class TestEfLol + { [Theory] - [InlineData("zeus","dieu")] - [InlineData("zeus", "dieu")] - [InlineData("zeus", "dieu")] - public async Task TestAddInMemory(String name, String bio) - { - // Arrange - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - // Act - using (var context = new ChampionContext(options)) - { - await context.Database.EnsureCreatedAsync(); - var Dieu = new ChampionEntity - { - Name = name, - Bio = bio - }; - - ChampionEntity found = await context.Champions.SingleOrDefaultAsync(c => c.Name == "Zeus"); - Assert.Null(found); - - await context.Champions.AddAsync(Dieu); - await context.SaveChangesAsync(); - - found = await context.Champions.SingleOrDefaultAsync(c => c.Name == name); - Assert.NotNull(found); - - Assert.Equal(1, await context.Champions.CountAsync()); - Assert.Equal(name, found.Name); - } - } - - [Fact] + [InlineData("Zeus", "Dieu de la foudre", true)] + [InlineData("Hades", "Dieu des enfers", true)] + [InlineData("Aphrodite", "Déesse de l'amour", true)] + [InlineData("AresAresAresAresAresAresAresAresAresAres", + "Dieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerre" + + "Dieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerre" + + "Dieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerre", false)] + public async Task TestAddInMemory(String name, String bio, bool expected) + { + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + + var options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + + using (var context = new ChampionContext(options)) + { + await context.Database.EnsureCreatedAsync(); + var Dieu = new ChampionEntity + { + Name = name, + Bio = bio + }; + + ChampionEntity found = await context.Champions.SingleOrDefaultAsync(c => c.Name == "Zeus"); + Assert.Null(found); + + await context.Champions.AddAsync(Dieu); + await context.SaveChangesAsync(); + + found = await context.Champions.SingleOrDefaultAsync(c => c.Name == name); + Assert.NotNull(found); + + Assert.Equal(1, await context.Champions.CountAsync()); + Assert.Equal(name, found.Name); + + // Test if the max length of the name is respected (30) and the max length of the bio is respected (256) + if (expected) + { + Assert.True(found.Name.Length <= 30); + Assert.True(found.Bio.Length <= 256); + } + else + { + Assert.False(found.Bio.Length <= 256); + Assert.False(found.Name.Length <= 30); + } + } + + } + + [Fact] public void ModifyTestInMemory() - {//connection must be opened to use In-memory database + { var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); @@ -96,9 +111,5 @@ namespace TestUnitaire Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count()); } } - } -} - - - - + } +} \ No newline at end of file -- 2.36.3 From d7dab3599880b9208f8f33aa3ef8c2f78276f1d0 Mon Sep 17 00:00:00 2001 From: "bastien.ollier@etu.uca.fr" Date: Wed, 8 Feb 2023 17:28:11 +0100 Subject: [PATCH 09/12] save --- Sources/EFLol/{ChampionContext.cs => MyDbContext.cs} | 6 +++--- Sources/EFLol/Program.cs | 4 ++-- Sources/TestUnitaire/TestEfLol.cs | 12 ++++++------ 3 files changed, 11 insertions(+), 11 deletions(-) rename Sources/EFLol/{ChampionContext.cs => MyDbContext.cs} (79%) diff --git a/Sources/EFLol/ChampionContext.cs b/Sources/EFLol/MyDbContext.cs similarity index 79% rename from Sources/EFLol/ChampionContext.cs rename to Sources/EFLol/MyDbContext.cs index 4a2654d..25c79ae 100644 --- a/Sources/EFLol/ChampionContext.cs +++ b/Sources/EFLol/MyDbContext.cs @@ -7,14 +7,14 @@ using Microsoft.EntityFrameworkCore; namespace EFLol { - public class ChampionContext : DbContext + public class MyDbContext : DbContext { public DbSet Champions { get; set; } - public ChampionContext() + public MyDbContext() { } - public ChampionContext(DbContextOptions options) + public MyDbContext(DbContextOptions options) : base(options) { } diff --git a/Sources/EFLol/Program.cs b/Sources/EFLol/Program.cs index 47f659d..4fd61f2 100644 --- a/Sources/EFLol/Program.cs +++ b/Sources/EFLol/Program.cs @@ -17,7 +17,7 @@ ChampionEntity chewie = new ChampionEntity { Name = "Chewbacca", Bio = "Zeus is ChampionEntity yoda = new ChampionEntity { Name = "Yoda", Bio = "Zeus is the king of the gods." }; ChampionEntity ewok = new ChampionEntity { Name = "Ewok" , Bio = "Zeus is the king of the gods." }; -using (var context = new ChampionContext()) +using (var context = new MyDbContext()) { // Crée des nounours et les insère dans la base Console.WriteLine("Creates and inserts new Nounours"); @@ -27,7 +27,7 @@ using (var context = new ChampionContext()) context.SaveChanges(); } -using (var context = new ChampionContext()) +using (var context = new MyDbContext()) { foreach (var n in context.Champions) { diff --git a/Sources/TestUnitaire/TestEfLol.cs b/Sources/TestUnitaire/TestEfLol.cs index e7ec753..7e506cb 100644 --- a/Sources/TestUnitaire/TestEfLol.cs +++ b/Sources/TestUnitaire/TestEfLol.cs @@ -16,12 +16,12 @@ namespace TestUnitaire var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); - var options = new DbContextOptionsBuilder() + var options = new DbContextOptionsBuilder() .UseSqlite(connection) .Options; // Act - using (var context = new ChampionContext(options)) + using (var context = new MyDbContext(options)) { await context.Database.EnsureCreatedAsync(); var Dieu = new ChampionEntity @@ -50,12 +50,12 @@ namespace TestUnitaire var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); - var options = new DbContextOptionsBuilder() + var options = new DbContextOptionsBuilder() .UseSqlite(connection) .Options; //prepares the database with one instance of the context - using (var context = new ChampionContext(options)) + using (var context = new MyDbContext(options)) { //context.Database.OpenConnection(); context.Database.EnsureCreated(); @@ -72,7 +72,7 @@ namespace TestUnitaire } //uses another instance of the context to do the tests - using (var context = new ChampionContext(options)) + using (var context = new MyDbContext(options)) { context.Database.EnsureCreated(); @@ -86,7 +86,7 @@ namespace TestUnitaire } //uses another instance of the context to do the tests - using (var context = new ChampionContext(options)) + using (var context = new MyDbContext(options)) { context.Database.EnsureCreated(); -- 2.36.3 From f249d70e648e0ba9eadd4690ce8ba8ce82ddff17 Mon Sep 17 00:00:00 2001 From: "bastien.ollier@etu.uca.fr" Date: Wed, 8 Feb 2023 17:30:38 +0100 Subject: [PATCH 10/12] save --- Sources/TestUnitaire/TestEfLol.cs | 85 ++++++++++++++++--------------- 1 file changed, 43 insertions(+), 42 deletions(-) diff --git a/Sources/TestUnitaire/TestEfLol.cs b/Sources/TestUnitaire/TestEfLol.cs index b8c0440..d9bae90 100644 --- a/Sources/TestUnitaire/TestEfLol.cs +++ b/Sources/TestUnitaire/TestEfLol.cs @@ -1,12 +1,14 @@ -using EFLol; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; - -namespace TestUnitaire -{ - public class TestEfLol - { +using EFLol; +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; + +namespace TestUnitaire +{ + public class TestEfLol + { + [Theory] + [InlineData("Zeus", "Dieu de la foudre", true)] [InlineData("Hades", "Dieu des enfers", true)] [InlineData("Aphrodite", "Déesse de l'amour", true)] @@ -15,35 +17,34 @@ namespace TestUnitaire "Dieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerre" + "Dieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerre", false)] public async Task TestAddInMemory(String name, String bio, bool expected) - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - // Act - using (var context = new MyDbContext(options)) - { - await context.Database.EnsureCreatedAsync(); - var Dieu = new ChampionEntity - { - Name = name, - Bio = bio - }; - - ChampionEntity found = await context.Champions.SingleOrDefaultAsync(c => c.Name == "Zeus"); - Assert.Null(found); - - await context.Champions.AddAsync(Dieu); - await context.SaveChangesAsync(); - - found = await context.Champions.SingleOrDefaultAsync(c => c.Name == name); - Assert.NotNull(found); - - Assert.Equal(1, await context.Champions.CountAsync()); - Assert.Equal(name, found.Name); + { + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + + var options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + + using (var context = new MyDbContext(options)) + { + await context.Database.EnsureCreatedAsync(); + var Dieu = new ChampionEntity + { + Name = name, + Bio = bio + }; + + ChampionEntity found = await context.Champions.SingleOrDefaultAsync(c => c.Name == "Zeus"); + Assert.Null(found); + + await context.Champions.AddAsync(Dieu); + await context.SaveChangesAsync(); + + found = await context.Champions.SingleOrDefaultAsync(c => c.Name == name); + Assert.NotNull(found); + + Assert.Equal(1, await context.Champions.CountAsync()); + Assert.Equal(name, found.Name); // Test if the max length of the name is respected (30) and the max length of the bio is respected (256) if (expected) @@ -56,11 +57,11 @@ namespace TestUnitaire Assert.False(found.Bio.Length <= 256); Assert.False(found.Name.Length <= 30); } - } + } + + } - } - - [Fact] + [Fact] public void ModifyTestInMemory() { var connection = new SqliteConnection("DataSource=:memory:"); @@ -112,5 +113,5 @@ namespace TestUnitaire Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count()); } } - } + } } \ No newline at end of file -- 2.36.3 From 20cf92b63606458719b5448d2ef8f2127bbc1198 Mon Sep 17 00:00:00 2001 From: nathan boileau Date: Wed, 22 Feb 2023 15:52:02 +0100 Subject: [PATCH 11/12] test --- Sources/EFLol/MyDbContext.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Sources/EFLol/MyDbContext.cs b/Sources/EFLol/MyDbContext.cs index 25c79ae..547fb9c 100644 --- a/Sources/EFLol/MyDbContext.cs +++ b/Sources/EFLol/MyDbContext.cs @@ -22,7 +22,6 @@ namespace EFLol { if (!optionsBuilder.IsConfigured) { - //optionsBuilder.UseSqlServer("Data Source=loldb.db"); optionsBuilder.UseSqlite("Data Source=loldb.db"); } } -- 2.36.3 From 25e375f1a9ca08165c3b87315cc1f4d20c2e877e Mon Sep 17 00:00:00 2001 From: nathan boileau Date: Wed, 22 Feb 2023 17:30:13 +0100 Subject: [PATCH 12/12] Add skin entity for the champions --- .gitignore | 2 + Sources/EFLol/ChampionEntity.cs | 17 ++++---- Sources/EFLol/MyDbContext.cs | 8 ++++ Sources/EFLol/Program.cs | 72 +++++++++++++++++++++++---------- Sources/EFLol/SkinEntity.cs | 7 ++-- 5 files changed, 74 insertions(+), 32 deletions(-) diff --git a/.gitignore b/.gitignore index 5913f3d..1132ce9 100644 --- a/.gitignore +++ b/.gitignore @@ -428,5 +428,7 @@ FodyWeavers.xsd *.sln.iml *.db +*.db-shm +*.db-wal *Migrations *.sonarqube \ No newline at end of file diff --git a/Sources/EFLol/ChampionEntity.cs b/Sources/EFLol/ChampionEntity.cs index 1e09902..b23a534 100644 --- a/Sources/EFLol/ChampionEntity.cs +++ b/Sources/EFLol/ChampionEntity.cs @@ -1,4 +1,5 @@ -using System.ComponentModel.DataAnnotations; +using System.Collections.ObjectModel; +using System.ComponentModel.DataAnnotations; namespace EFLol { @@ -12,11 +13,11 @@ namespace EFLol [MaxLength(256, ErrorMessage = "Bio cannot be longer than 256 characters.")] public string Bio { get; set; } - //public ChampionClass Class { get; set; } - //public string Icon { get; set; } - //public LargeImage Image { get; set; } - //public ReadOnlyCollection Skins { get; private set; } - //public ReadOnlyDictionary Characteristics { get; private set; } - //private HashSet skills = new HashSet(); - } + //public ChampionClass Class { get; set; } + //public string Icon { get; set; } + //public LargeImage Image { get; set; } + //public ReadOnlyDictionary Characteristics { get; private set; } + //private HashSet skills = new HashSet(); + public ReadOnlyCollection Skins { get; set; } + } } diff --git a/Sources/EFLol/MyDbContext.cs b/Sources/EFLol/MyDbContext.cs index 547fb9c..17ad759 100644 --- a/Sources/EFLol/MyDbContext.cs +++ b/Sources/EFLol/MyDbContext.cs @@ -10,6 +10,7 @@ namespace EFLol public class MyDbContext : DbContext { public DbSet Champions { get; set; } + public DbSet Skins { get; set; } public MyDbContext() { } @@ -25,5 +26,12 @@ namespace EFLol optionsBuilder.UseSqlite("Data Source=loldb.db"); } } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + modelBuilder.Entity().Property(c => c.Id).ValueGeneratedOnAdd(); + modelBuilder.Entity().Property(s => s.Id).ValueGeneratedOnAdd(); + } } } diff --git a/Sources/EFLol/Program.cs b/Sources/EFLol/Program.cs index 4fd61f2..fbac836 100644 --- a/Sources/EFLol/Program.cs +++ b/Sources/EFLol/Program.cs @@ -1,37 +1,67 @@ -using EFLol; +using System.Collections.ObjectModel; +using EFLol; -/*ChampionEntity Zeus = new ChampionEntity + +using (var context = new MyDbContext()) +{ + // Clean the DB before starting + Console.WriteLine("Clean the DB before starting"); + context.Database.EnsureDeleted(); + context.Database.EnsureCreated(); +} + +SkinEntity black = new SkinEntity { Name = "Black", Description = "Black skin", Icon = "black.png", Price = 0.99f }; +SkinEntity white = new SkinEntity { Name = "White", Description = "White skin", Icon = "white.png", Price = 150.99f }; +SkinEntity green = new SkinEntity { Name = "Green", Description = "Green skin", Icon = "green.png", Price = 4.99f }; + +ChampionEntity Zeus = new ChampionEntity { Name = "Zeus", - Bio = "Zeus is the king of the gods." + Bio = "Zeus is the god of the sky", + Skins = new ReadOnlyCollection(new List { black, white }) + }; +ChampionEntity Hera = new ChampionEntity +{ + Name = "Hera", + Bio = "Hera is the goddess of marriage", + Skins = new ReadOnlyCollection(new List { green }) -using (var context = new ChampionContext()) +}; +ChampionEntity Poseidon = new ChampionEntity { Name = "Poseidon", Bio = "Poseidon is the god of the sea" }; + + + +using (var context = new MyDbContext()) { - Console.WriteLine("Adding Zeus to the database..."); - context.Champions.Add(Zeus); + // Crée des champions et les insère dans la base + Console.WriteLine("Creates and inserts new Champions"); + context.Add(Zeus); + context.Add(Hera); + context.Add(Poseidon); context.SaveChanges(); -}*/ +} -ChampionEntity chewie = new ChampionEntity { Name = "Chewbacca", Bio = "Zeus is the king of the gods." }; -ChampionEntity yoda = new ChampionEntity { Name = "Yoda", Bio = "Zeus is the king of the gods." }; -ChampionEntity ewok = new ChampionEntity { Name = "Ewok" , Bio = "Zeus is the king of the gods." }; using (var context = new MyDbContext()) { - // Crée des nounours et les insère dans la base - Console.WriteLine("Creates and inserts new Nounours"); - context.Add(chewie); - context.Add(yoda); - context.Add(ewok); - context.SaveChanges(); + foreach (var n in context.Champions) + { + // Use LINQ to display the skins for each champion + var skins = from s in context.Skins + where n.Id == s.Id + select s; + + Console.WriteLine($"Champion n°{n.Id} - {n.Name}"); + } + context.SaveChanges(); } using (var context = new MyDbContext()) { - foreach (var n in context.Champions) - { - Console.WriteLine($"{n.Id} - {n.Name}"); - } - context.SaveChanges(); + foreach (var n in context.Skins) + { + Console.WriteLine($"Skin n°{n.Id} - {n.Name}" + (n.Price != null ? $" - {n.Price}" : "")); + } + context.SaveChanges(); } \ No newline at end of file diff --git a/Sources/EFLol/SkinEntity.cs b/Sources/EFLol/SkinEntity.cs index f186268..125d962 100644 --- a/Sources/EFLol/SkinEntity.cs +++ b/Sources/EFLol/SkinEntity.cs @@ -8,9 +8,10 @@ namespace EFLol { public class SkinEntity { - public String Name { get; set; } - public String Description { get; set; } - public String Icon { get; set; } + public int Id { get; set; } + public string Name { get; set; } + public string Description { get; set; } + public string Icon { get; set; } public float Price { get; set; } } } -- 2.36.3