using EntityFrameworkLib; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using Model; namespace UnitTestsSQLiteInMemory { public class ChampionsDbTests { [Fact] public void Add_Champions_Test() { //connection must be opened to use In-memory database var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); var options = new DbContextOptionsBuilder() .UseInMemoryDatabase(databaseName: "TestChampions_database") .Options; using(var context = new LolContext(options)) { ChampionEntity akali = new ChampionEntity { Name = "Akali", Bio = "Bio Akali", Icon = "Icon Akali" }; ChampionEntity alistar = new ChampionEntity { Name = "Alistar", Bio = "Bio Alistar", Icon = "Icon Alistar" }; ChampionEntity bard = new ChampionEntity { Name = "Bard", Bio = "Bio Bard", Icon = "Icon Bard" }; context.Champions.Add(akali); context.Champions.Add(alistar); context.Champions.Add(bard); context.SaveChanges(); } using(var context = new LolContext(options)) { Assert.Equal(3, context.Champions.Count()); Assert.Equal("Akali", context.Champions.First().Name); } } [Fact] public void Modify_Champions_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 LolContext(options)) { //context.Database.OpenConnection(); context.Database.EnsureCreated(); ChampionEntity akali = new ChampionEntity { Name = "Akali", Bio = "Bio Akali", Icon = "Icon Akali" }; ChampionEntity akshan = new ChampionEntity { Name = "Akshan", Bio = "Bio Akshan", Icon = "Icon Akshan" }; ChampionEntity twitch = new ChampionEntity { Name = "Twitch", Bio = "Bio Twitch", Icon = "icon Twitch" }; context.Champions.Add(akali); context.Champions.Add(akshan); context.Champions.Add(twitch); context.SaveChanges(); } //uses another instance of the context to do the tests using (var context = new LolContext(options)) { context.Database.EnsureCreated(); string nameToFind = "ak"; Assert.Equal(2, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count()); nameToFind = "wi"; Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count()); var akshan = context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).First(); akshan.Name = "Volibear"; context.SaveChanges(); } //uses another instance of the context to do the tests using (var context = new LolContext(options)) { context.Database.EnsureCreated(); string nameToFind = "i"; Assert.Equal(2, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count()); nameToFind = "voli"; Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count()); } } } }