using DTO; using EntityFramwork; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; namespace TestUnitaireBDD { public class TestChampions { [Fact] public void Add_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 BDDContext(options)) { //context.Database.OpenConnection(); context.Database.EnsureCreated(); EntityChampions chewie = new EntityChampions { Name = "Chewbacca",Bio ="Inconnu",Icon="Inconnu" }; EntityChampions yoda = new EntityChampions { Name = "Yoda",Bio = "Inconnu", Icon = "Inconnu" }; EntityChampions ewok = new EntityChampions { Name = "Ewok",Bio = "Inconnu", Icon = "Inconnu" }; context.Add(chewie); context.Add(yoda); context.Add(ewok); context.SaveChanges(); } //uses another instance of the context to do the tests using (var context = new BDDContext(options)) { context.Database.EnsureCreated(); Assert.Equal(3, context.Champions.Count()); Assert.Equal("Chewbacca", context.Champions.First().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 BDDContext(options)) { //context.Database.OpenConnection(); context.Database.EnsureCreated(); EntityChampions chewie = new EntityChampions { Name = "Chewbacca", Bio = "Inconnu", Icon = "Inconnu" }; EntityChampions yoda = new EntityChampions { Name = "Yoda", Bio = "Inconnu", Icon = "Inconnu" }; EntityChampions ewok = new EntityChampions { Name = "Ewok", Bio = "Inconnu", Icon = "Inconnu" }; context.Add(chewie); context.Add(yoda); context.Add(ewok); context.SaveChanges(); } //uses another instance of the context to do the tests using (var context = new BDDContext(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 BDDContext(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()); } } } }