public void Command_trees_for_initialization_and_simple_query_and_update_commands_can_be_logged() { // Make sure that HistoryContext has been used to ensure test runs consistently regardless of // whether or not other tests have run before it. using (var initContext = new BlogContextNoInit()) { ExtendedSqlAzureExecutionStrategy.ExecuteNew( () => { initContext.Database.Initialize(force: false); }); } var logger = new CommandTreeLogger(); DbInterception.Add(logger); BlogContextLogAll context; try { using (context = new BlogContextLogAll()) { BlogContext.DoStuff(context); } } finally { DbInterception.Remove(logger); } // Sanity check that we got tree creations logged Assert.True(logger.Log.OfType <DbQueryCommandTree>().Any(t => t.DataSpace == DataSpace.CSpace)); Assert.True(logger.Log.OfType <DbQueryCommandTree>().Any(t => t.DataSpace == DataSpace.SSpace)); Assert.True(logger.Log.OfType <DbInsertCommandTree>().Any(t => t.DataSpace == DataSpace.SSpace)); #if !NET40 Assert.True(logger.Log.OfType <DbUpdateCommandTree>().Any(t => t.DataSpace == DataSpace.SSpace)); Assert.True( logger.Log.OfType <DbUpdateCommandTree>() .Any( t => t.SetClauses.OfType <DbSetClause>() .Any( c => ((DbPropertyExpression)c.Property).Property.Name == "Title" && (string)((DbConstantExpression)c.Value).Value == "I'm a logger and I'm okay..."))); #endif foreach (var interceptionContext in logger.LogWithContext.Select(t => t.Item2)) { Assert.Contains(context, interceptionContext.DbContexts); } }
public void Command_trees_for_initialization_and_simple_query_and_update_commands_can_be_logged() { // Make sure that HistoryContext has been used to ensure test runs consistently regardless of // whether or not other tests have run before it. using (var initContext = new BlogContextNoInit()) { ExtendedSqlAzureExecutionStrategy.ExecuteNew( () => { initContext.Database.Initialize(force: false); }); } var logger = new CommandTreeLogger(); DbInterception.Add(logger); BlogContextLogAll context; try { using (context = new BlogContextLogAll()) { BlogContext.DoStuff(context); } } finally { DbInterception.Remove(logger); } // Sanity check that we got tree creations logged Assert.True(logger.Log.OfType<DbQueryCommandTree>().Any(t => t.DataSpace == DataSpace.CSpace)); Assert.True(logger.Log.OfType<DbQueryCommandTree>().Any(t => t.DataSpace == DataSpace.SSpace)); Assert.True(logger.Log.OfType<DbInsertCommandTree>().Any(t => t.DataSpace == DataSpace.SSpace)); #if !NET40 Assert.True(logger.Log.OfType<DbUpdateCommandTree>().Any(t => t.DataSpace == DataSpace.SSpace)); Assert.True( logger.Log.OfType<DbUpdateCommandTree>() .Any( t => t.SetClauses.OfType<DbSetClause>() .Any( c => ((DbPropertyExpression)c.Property).Property.Name == "Title" && (string)((DbConstantExpression)c.Value).Value == "I'm a logger and I'm okay..."))); #endif foreach (var interceptionContext in logger.LogWithContext.Select(t => t.Item2)) { Assert.Contains(context, interceptionContext.DbContexts); } }
public void Initialization_and_simple_query_and_update_commands_can_be_logged() { var logger = new CommandLogger(); DbInterception.Add(logger); try { using (var context = new BlogContextLogAll()) { BlogContext.DoStuff(context); } } finally { DbInterception.Remove(logger); } var commandsUsed = new bool[Enum.GetValues(typeof(CommandMethod)).Length]; for (var i = 0; i < logger.Log.Count; i++) { var method = logger.Log[i].Method; commandsUsed[(int)method] = true; if (method.ToString().EndsWith("Executing")) { Assert.Equal(method + 1, logger.Log[i + 1].Method); Assert.Same(logger.Log[i].Command, logger.Log[i + 1].Command); } } // Check that every type of command used has log entries Assert.True(commandsUsed[(int)CommandMethod.NonQueryExecuting]); Assert.True(commandsUsed[(int)CommandMethod.NonQueryExecuted]); Assert.True(commandsUsed[(int)CommandMethod.ReaderExecuting]); Assert.True(commandsUsed[(int)CommandMethod.ReaderExecuted]); Assert.True(commandsUsed[(int)CommandMethod.ScalarExecuting]); Assert.True(commandsUsed[(int)CommandMethod.ScalarExecuted]); // Sanity check on command text var commandTexts = logger.Log.Select(l => l.CommandText.ToLowerInvariant()); Assert.True(commandTexts.Any(c => c.StartsWith("select"))); Assert.True(commandTexts.Any(c => c.StartsWith("create"))); Assert.True(commandTexts.Any(c => c.StartsWith("alter"))); Assert.True(commandTexts.Any(c => c.StartsWith("insert"))); }
public void Initialization_and_simple_query_and_update_commands_can_be_logged() { var logger = new CommandLogger(); Interception.AddInterceptor(logger); try { using (var context = new BlogContextLogAll()) { BlogContext.DoStuff(context); } } finally { Interception.RemoveInterceptor(logger); } var commandsUsed = new bool[Enum.GetValues(typeof(CommandMethod)).Length]; // Check that every "executed" call is preceded by an "executing" call. // (The reverse is not true since "executed" is not called for operations that throw.) for (var i = 0; i < logger.Log.Count; i++) { var method = logger.Log[i].Method; commandsUsed[(int)method] = true; if (method.ToString().EndsWith("Executed")) { Assert.Equal(method - 1, logger.Log[i - 1].Method); } } // Check that every type of command used has log entries Assert.True(commandsUsed[(int)CommandMethod.NonQueryExecuting]); Assert.True(commandsUsed[(int)CommandMethod.NonQueryExecuted]); Assert.True(commandsUsed[(int)CommandMethod.ReaderExecuting]); Assert.True(commandsUsed[(int)CommandMethod.ReaderExecuted]); Assert.True(commandsUsed[(int)CommandMethod.ScalarExecuting]); Assert.True(commandsUsed[(int)CommandMethod.ScalarExecuted]); #if NET40 Assert.False(commandsUsed[(int)CommandMethod.AsyncNonQueryExecuting]); Assert.False(commandsUsed[(int)CommandMethod.AsyncNonQueryExecuted]); Assert.False(commandsUsed[(int)CommandMethod.AsyncReaderExecuting]); Assert.False(commandsUsed[(int)CommandMethod.AsyncReaderExecuted]); #else Assert.True(commandsUsed[(int)CommandMethod.AsyncNonQueryExecuting]); Assert.True(commandsUsed[(int)CommandMethod.AsyncNonQueryExecuted]); Assert.True(commandsUsed[(int)CommandMethod.AsyncReaderExecuting]); Assert.True(commandsUsed[(int)CommandMethod.AsyncReaderExecuted]); #endif // EF and SQL provider never send ExecuteScalarAsync Assert.False(commandsUsed[(int)CommandMethod.AsyncScalarExecuting]); Assert.False(commandsUsed[(int)CommandMethod.AsyncScalarExecuted]); // Sanity check on command text var commandTexts = logger.Log.Select(l => l.CommandText.ToLowerInvariant()); Assert.True(commandTexts.Any(c => c.StartsWith("select"))); Assert.True(commandTexts.Any(c => c.StartsWith("create"))); Assert.True(commandTexts.Any(c => c.StartsWith("alter"))); Assert.True(commandTexts.Any(c => c.StartsWith("insert"))); #if !NET40 Assert.True(commandTexts.Any(c => c.StartsWith("update"))); #endif // Sanity check on results Assert.True(logger.Log.Where(l => l.Method == CommandMethod.NonQueryExecuted).All(l => l.Result != null)); Assert.True(logger.Log.Where(l => l.Method == CommandMethod.ReaderExecuted).All(l => l.Result != null)); #if !NET40 Assert.True( logger.Log.Where( l => l.Method.ToString().StartsWith("Async") && l.Method.ToString().EndsWith("Executed")).All(l => l.Result != null)); #endif }