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.Name == "Akali"); Assert.NotNull(champs); Assert.Equal("Akali", champs.Name); } } [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 StubEFChampions(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 StubEFChampions(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 StubEFChampions(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()); } } /* [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); } */ } }