public void TestEfCoreLoggingCheckSqlOutputShowLog() { //SETUP var logs = new List <string>(); var logToOptions = new LogToOptions { ShowLog = false }; var options = SqliteInMemory.CreateOptionsWithLogTo <BookContext>(log => logs.Add(log), logToOptions); using var context = new BookContext(options); context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); //ATTEMPT logToOptions.ShowLog = true; var book = context.Books.Single(x => x.Reviews.Count() > 1); //VERIFY logs.Count.ShouldEqual(1); logs.Single().ShouldEqual("Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']\r\n" + "SELECT \"b\".\"BookId\", \"b\".\"Description\", \"b\".\"ImageUrl\", \"b\".\"Price\"," + " \"b\".\"PublishedOn\", \"b\".\"Publisher\", \"b\".\"SoftDeleted\", \"b\".\"Title\"\r\n" + "FROM \"Books\" AS \"b\"\r\nWHERE NOT (\"b\".\"SoftDeleted\") AND ((\r\n" + " SELECT COUNT(*)\r\n FROM \"Review\" AS \"r\"\r\n WHERE \"b\".\"BookId\" = \"r\".\"BookId\") > 1)\r\nLIMIT 2"); }
public void TestEfCoreLoggingCheckFilterFunction() { bool MyFilterFunction(EventId eventId, LogLevel logLevel) { return(eventId.Name == RelationalEventId.CommandExecuted.Name && logLevel == LogLevel.Information); } //SETUP var logs = new List <string>(); var logToOptions = new LogToOptions { FilterFunction = MyFilterFunction }; var options = SqliteInMemory.CreateOptionsWithLogTo <BookContext>(log => logs.Add(log), logToOptions); using var context = new BookContext(options); context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); //ATTEMPT var books = context.Books.Select(x => x.BookId).ToList(); //VERIFY logs.All(l => l.StartsWith("Executed DbCommand ")).ShouldBeTrue(); }
public void TestDbQueryChildReadOnlyOk() { //SETUP var options = SqliteInMemory.CreateOptionsWithLogTo <TestDbContext>(log => _output.WriteLine(log.ToString())); using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); context.ExecuteScriptFileInTransaction(TestData.GetFilePath("ReplaceTableWithView.sql")); context.Add(new Parent { Children = new List <Child> { new Child { MyString = "Hello" }, new Child { MyString = "Goodbye" } } }); context.SaveChanges(); context.ChangeTracker.Clear(); //ATTEMPT var children = context.Children.ToList(); //VERIFY children.Count.ShouldEqual(2); } }
public void TestDisconnectedCascadeSoftDeleteEmployeeSoftDelOk(bool readEveryTime) { //SETUP var logs = new List <string>(); var options = SqliteInMemory.CreateOptionsWithLogTo <CascadeSoftDelDbContext>(log => logs.Add(log)); using (var context = new CascadeSoftDelDbContext(options)) { context.Database.EnsureCreated(); Employee.SeedEmployeeSoftDel(context); context.ChangeTracker.Clear(); var config = new ConfigCascadeDeleteWithUserId(context) { ReadEveryTime = readEveryTime }; var service = new CascadeSoftDelService <ICascadeSoftDelete>(config); //ATTEMPT logs.Clear(); var status = service.SetCascadeSoftDelete(context.Employees.Single(x => x.Name == "CTO")); //VERIFY status.IsValid.ShouldBeTrue(status.GetAllErrors()); logs.Count(x => _selectMatchRegex.IsMatch(x)).ShouldEqual(7); status.Result.ShouldEqual(7 + 6); context.Employees.Count().ShouldEqual(4); context.Employees.IgnoreQueryFilters().Count().ShouldEqual(11); context.Contracts.Count().ShouldEqual(3); context.Contracts.IgnoreQueryFilters().Count().ShouldEqual(9); } }
public void TestSqlLiteDefaultColValueWorks() { //SETUP var options = SqliteInMemory.CreateOptionsWithLogTo <DiffConfigDbContext>(_output.WriteLine, null, builder => builder.ReplaceService <IModelCacheKeyFactory, DynamicModelCacheKeyFactory>()); using var context = new DiffConfigDbContext(options, DiffConfigs.SetDefaultCol); //ATTEMPT context.Database.EnsureCreated(); //VERIFY }
public void TestSqlLiteAddSequenceFAILS() { //SETUP var options = SqliteInMemory.CreateOptionsWithLogTo <DiffConfigDbContext>(_output.WriteLine, null, builder => builder.ReplaceService <IModelCacheKeyFactory, DynamicModelCacheKeyFactory>()); using var context = new DiffConfigDbContext(options, DiffConfigs.AddSequence); //ATTEMPT var ex = Assert.Throws <NotSupportedException>(() => context.Database.EnsureCreated()); //VERIFY }
public void TestEfCoreLoggingExampleOfOutputToConsole() { //SETUP var options = SqliteInMemory.CreateOptionsWithLogTo <BookContext>(_output.WriteLine); using var context = new BookContext(options); context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); //ATTEMPT var books = context.Books.ToList(); //VERIFY }
public void TestSqlLiteComputedColMightFAIL() { //SETUP var options = SqliteInMemory.CreateOptionsWithLogTo <DiffConfigDbContext>(_output.WriteLine, null, builder => builder.ReplaceService <IModelCacheKeyFactory, DynamicModelCacheKeyFactory>()); using var context = new DiffConfigDbContext(options, DiffConfigs.SetComputedCol); //ATTEMPT var ex = Assert.Throws <SqliteException>(() => context.Database.EnsureCreated()); //VERIFY ex.Message.ShouldEqual("SQLite Error 1: 'no such column: yyyy'."); }
public void TestSqlLiteDefaultUserDefinedFunctionsFAILsOnRun() { //SETUP var options = SqliteInMemory.CreateOptionsWithLogTo <DiffConfigDbContext>(_output.WriteLine, null, builder => builder.ReplaceService <IModelCacheKeyFactory, DynamicModelCacheKeyFactory>()); using var context = new DiffConfigDbContext(options, DiffConfigs.Nothing); context.Database.EnsureCreated(); //ATTEMPT var filepath = TestData.GetFilePath("AddUserDefinedFunctions.sql"); var ex = Assert.Throws <SqliteException>(() => context.ExecuteScriptFileInTransaction(filepath)); //VERIFY ex.Message.ShouldEqual("SQLite Error 1: 'near \"IF\": syntax error'."); }
public void TestEfCoreLoggingLogToOptionBad() { //SETUP var logs = new List <string>(); var logToOptions = new LogToOptions { OnlyShowTheseEvents = new[] { CoreEventId.ContextInitialized }, OnlyShowTheseCategories = new[] { DbLoggerCategory.Database.Command.Name } }; //ATTEMPT var ex = Assert.Throws <NotSupportedException>(() => SqliteInMemory.CreateOptionsWithLogTo <BookContext>(log => logs.Add(log), logToOptions)); //VERIFY ex.Message.ShouldEqual("You can't define OnlyShowTheseCategories and OnlyShowTheseEvents at the same time."); }
public void TestEfCoreLoggingCheckSqlOutput() { //SETUP var logs = new List <string>(); var options = SqliteInMemory.CreateOptionsWithLogTo <BookDbContext>(log => logs.Add(log)); using var context = new BookDbContext(options); context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); //ATTEMPT var book = context.Books.Count(); //VERIFY logs.Last().ShouldEqual("Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']\r\n" + "SELECT COUNT(*)\r\nFROM \"Books\" AS \"b\"\r\nWHERE NOT (\"b\".\"SoftDeleted\")"); }
public void TestEfCoreLoggingCheckLoggerOptionsDefaultWithUtcTime() { //SETUP var logs = new List <string>(); var logToOptions = new LogToOptions { LoggerOptions = DbContextLoggerOptions.DefaultWithUtcTime }; var options = SqliteInMemory.CreateOptionsWithLogTo <BookContext>(log => logs.Add(log), logToOptions); using var context = new BookContext(options); context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); //ATTEMPT var books = context.Books.Select(x => x.BookId).ToList(); //VERIFY logs.All(x => x.StartsWith("warn:") || x.StartsWith("info:")).ShouldBeTrue(); }
public void TestEfCoreLoggingCheckOnlyShowTheseCategories() { //SETUP var logs = new List <string>(); var logToOptions = new LogToOptions { OnlyShowTheseCategories = new[] { DbLoggerCategory.Database.Command.Name } }; var options = SqliteInMemory.CreateOptionsWithLogTo <BookContext>(log => logs.Add(log), logToOptions); using var context = new BookContext(options); context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); //ATTEMPT var books = context.Books.Select(x => x.BookId).ToList(); //VERIFY logs.All(x => x.StartsWith("Executed DbCommand")).ShouldBeTrue(); }
public void TestEfCoreLoggingCheckLoggerOptionsSingleLine() { //SETUP var logs = new List <string>(); var logToOptions = new LogToOptions { LoggerOptions = DbContextLoggerOptions.Id }; var options = SqliteInMemory.CreateOptionsWithLogTo <BookContext>(log => logs.Add(log), logToOptions); using var context = new BookContext(options); context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); //ATTEMPT var books = context.Books.Select(x => x.BookId).ToList(); //VERIFY logs.All(x => x.StartsWith("RelationalEventId.CommandExecuted") || x.StartsWith("CoreEventId.")).ShouldBeTrue(); }
public void TestEfCoreLoggingCheckOnlyShowTheseEvents() { //SETUP var logs = new List <string>(); var logToOptions = new LogToOptions { OnlyShowTheseEvents = new[] { CoreEventId.ContextInitialized } }; var options = SqliteInMemory.CreateOptionsWithLogTo <BookContext>(log => logs.Add(log), logToOptions); using var context = new BookContext(options); context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); //ATTEMPT var books = context.Books.Select(x => x.BookId).ToList(); //VERIFY logs.Count.ShouldEqual(1); logs.Single().ShouldStartWith("Entity Framework Core 5.0.1 initialized 'BookContext' using provider 'Microsoft.EntityFrameworkCore.Sqlite' with options: "); }
static async Task Main(string[] args) { var options = SqliteInMemory.CreateOptionsWithLogTo <SampleDbContext>(Console.WriteLine); await using var context = new SampleDbContext(options); await context.Database.EnsureCreatedAsync(); var type = new AircraftType("Airbus 321") { ["Vendor"] = "Airbus Co", ["MaxTOW"] = 40000, ["MaxFuel"] = 2000.0F }; var id = type.Id; await context.AircraftTypes.AddAsync(type); await context.SaveChangesAsync(); context.ChangeTracker.Clear(); var retType = await context.AircraftTypes.SingleAsync(t => ((string)t["Vendor"]).Contains("Airbus")); Console.WriteLine(retType); }
private static async Task Main() { var options = SqliteInMemory.CreateOptionsWithLogTo <SampleDbContext>(Console.WriteLine); await using var context = new SampleDbContext(options); await context.Database.EnsureCreatedAsync(); var craft = new Aircraft("Boeing 737"); var id = craft.Id; context.Aircrafts.Add(craft); await context.SaveChangesAsync(); var c1 = await context.FindAsync <Aircraft>(id); context.ChangeTracker.Clear(); var c2 = await context.FindAsync <Aircraft>(id); Console.WriteLine(ReferenceEquals(c1, c2)); Console.WriteLine(c1 == c2); }
private static async Task Main() { var options = SqliteInMemory.CreateOptionsWithLogTo <SampleDbContext>(Console.WriteLine); await using var context = new SampleDbContext(options); await context.Database.EnsureCreatedAsync(); var a1 = new Dictionary <string, object> { ["Id"] = Guid.NewGuid(), ["Name"] = "Vnukovo Andrei Tupolev International Airport", ["ICAO"] = "UUWW" }; context.Airports.Add(a1); await context.SaveChangesAsync(); context.ChangeTracker.Clear(); var a2 = await context.Airports.SingleAsync(e => (string)e["ICAO"] == "UUWW"); Console.WriteLine((string)a1["ICAO"] == (string)a2["ICAO"]); }
public async Task TestCascadeSoftDeleteEmployeeSoftDelWithLoggingOk(bool readEveryTime, int selectCount) { //SETUP var logs = new List <string>(); var options = SqliteInMemory.CreateOptionsWithLogTo <CascadeSoftDelDbContext>(log => logs.Add(log)); using (var context = new CascadeSoftDelDbContext(options)) { context.Database.EnsureCreated(); var ceo = Employee.SeedEmployeeSoftDel(context); var config = new ConfigCascadeDeleteWithUserId(context) { ReadEveryTime = readEveryTime }; var service = new CascadeSoftDelServiceAsync <ICascadeSoftDelete>(config); //ATTEMPT logs.Clear(); var status = await service.SetCascadeSoftDeleteAsync(ceo.WorksFromMe.First()); //VERIFY status.IsValid.ShouldBeTrue(status.GetAllErrors()); logs.Count(x => _selectMatchRegex.IsMatch(x)).ShouldEqual(selectCount); status.Result.ShouldEqual(7 + 6); context.ChangeTracker.Clear(); context.Employees.Count().ShouldEqual(4); context.Employees.IgnoreQueryFilters().Count().ShouldEqual(11); context.Employees.IgnoreQueryFilters().Select(x => x.SoftDeleteLevel).Where(x => x > 0).ToArray() .ShouldEqual(new byte[] { 1, 2, 2, 3, 3, 3, 3 }); context.Contracts.Count().ShouldEqual(3); context.Contracts.IgnoreQueryFilters().Count().ShouldEqual(9); context.Employees.IgnoreQueryFilters().Select(x => x.Contract).Where(x => x != null) .Select(x => x.SoftDeleteLevel).Where(x => x > 0).ToArray() .ShouldEqual(new byte[] { 2, 3, 3, 4, 4, 4 }); } }
public void TestEfCoreLoggingCheckSqlOutputShowLog() { //SETUP var logToOptions = new LogToOptions //#A { //#A ShowLog = false //#A }; //#A var options = SqliteInMemory //#B .CreateOptionsWithLogTo //#B <BookDbContext>( //#B _output.WriteLine, //#C logToOptions); //#D using var context = new BookDbContext(options); //#E context.Database.EnsureCreated(); //#E context.SeedDatabaseFourBooks(); //#E //ATTEMPT logToOptions.ShowLog = true; //#F var book = context.Books.Count(); //#G //VERIFY }