public virtual async Task Can_ExecuteNonQueryAsync() { var executeNonQueryCount = 0; var disposeCount = -1; var fakeDbConnection = new FakeDbConnection( ConnectionString, new FakeCommandExecutor( executeNonQueryAsync: (c, ct) => { executeNonQueryCount++; disposeCount = c.DisposeCount; return(Task.FromResult(1)); })); var optionsExtension = new FakeRelationalOptionsExtension().WithConnection(fakeDbConnection); var options = CreateOptions(optionsExtension); var relationalCommand = CreateRelationalCommand(); var result = await relationalCommand.ExecuteNonQueryAsync( new FakeRelationalConnection(options), null, null); Assert.Equal(1, result); var expectedCount = 1; Assert.Equal(expectedCount, fakeDbConnection.OpenCount); Assert.Equal(expectedCount, fakeDbConnection.CloseCount); // During command execution Assert.Equal(1, executeNonQueryCount); Assert.Equal(0, disposeCount); // After command execution Assert.Equal(1, fakeDbConnection.DbCommands[0].DisposeCount); }
public void Can_ExecuteNonQuery() { var executeNonQueryCount = 0; var disposeCount = -1; var fakeDbConnection = new FakeDbConnection( ConnectionString, new FakeCommandExecutor( executeNonQuery: c => { executeNonQueryCount++; disposeCount = c.DisposeCount; return(1); })); var optionsExtension = new FakeRelationalOptionsExtension().WithConnection(fakeDbConnection); var options = CreateOptions(optionsExtension); var relationalCommand = CreateRelationalCommand(); var result = relationalCommand.ExecuteNonQuery( new FakeRelationalConnection(options)); Assert.Equal(1, result); var expectedCount = 1; Assert.Equal(expectedCount, fakeDbConnection.OpenCount); Assert.Equal(expectedCount, fakeDbConnection.CloseCount); // During command execution Assert.Equal(1, executeNonQueryCount); Assert.Equal(0, disposeCount); // After command execution Assert.Equal(1, fakeDbConnection.DbCommands[0].DisposeCount); }
public async Task Can_ExecuteScalarAsync() { var executeScalarCount = 0; var disposeCount = -1; var fakeDbConnection = new FakeDbConnection( ConnectionString, new FakeCommandExecutor( executeScalarAsync: (c, ct) => { executeScalarCount++; disposeCount = c.DisposeCount; return(Task.FromResult <object>("ExecuteScalar Result")); })); var optionsExtension = new FakeRelationalOptionsExtension().WithConnection(fakeDbConnection); var options = CreateOptions(optionsExtension); var relationalCommand = CreateRelationalCommand(); var result = (string)await relationalCommand.ExecuteScalarAsync( new FakeRelationalConnection(options)); Assert.Equal("ExecuteScalar Result", result); var expectedCount = 1; Assert.Equal(expectedCount, fakeDbConnection.OpenCount); Assert.Equal(expectedCount, fakeDbConnection.CloseCount); // Durring command execution Assert.Equal(1, executeScalarCount); Assert.Equal(0, disposeCount); // After command execution Assert.Equal(1, fakeDbConnection.DbCommands[0].DisposeCount); }
public async Task Reports_command_diagnostic_on_exception( Delegate commandDelegate, DbCommandMethod diagnosticName, bool async) { var exception = new InvalidOperationException(); var fakeDbConnection = new FakeDbConnection( ConnectionString, new FakeCommandExecutor( c => throw exception, c => throw exception, (c, cb) => throw exception, (c, ct) => throw exception, (c, ct) => throw exception, (c, cb, ct) => throw exception)); var optionsExtension = new FakeRelationalOptionsExtension().WithConnection(fakeDbConnection); var options = CreateOptions(optionsExtension); var diagnostic = new List <Tuple <string, object> >(); var fakeConnection = new FakeRelationalConnection(options); var relationalCommand = CreateRelationalCommand( new DiagnosticsLogger <DbLoggerCategory.Database.Command>( new ListLoggerFactory(new List <(LogLevel, EventId, string)>()), new FakeLoggingOptions(false), new ListDiagnosticSource(diagnostic)), parameters: new[] { new TypeMappedRelationalParameter("FirstInvariant", "FirstParameter", new IntTypeMapping("int", DbType.Int32), false) }); var parameterValues = new Dictionary <string, object> { { "FirstInvariant", 17 } }; if (async) { await Assert.ThrowsAsync <InvalidOperationException>( async() => await((CommandFunc)commandDelegate)(fakeConnection, relationalCommand, parameterValues)); } else { Assert.Throws <InvalidOperationException>( () => ((CommandAction)commandDelegate)(fakeConnection, relationalCommand, parameterValues)); } Assert.Equal(2, diagnostic.Count); Assert.Equal(RelationalEventId.CommandExecuting.Name, diagnostic[0].Item1); Assert.Equal(RelationalEventId.CommandError.Name, diagnostic[1].Item1); var beforeData = (CommandEventData)diagnostic[0].Item2; var afterData = (CommandErrorEventData)diagnostic[1].Item2; Assert.Equal(fakeDbConnection.DbCommands[0], beforeData.Command); Assert.Equal(fakeDbConnection.DbCommands[0], afterData.Command); Assert.Equal(diagnosticName, beforeData.ExecuteMethod); Assert.Equal(diagnosticName, afterData.ExecuteMethod); Assert.Equal(async, beforeData.IsAsync); Assert.Equal(async, afterData.IsAsync); Assert.Equal(exception, afterData.Exception); }
public void Every_eventId_has_a_logger_method_and_logs_when_level_enabled() { var constantExpression = Expression.Constant("A"); var model = new Model(); var entityType = new EntityType(typeof(object), model, ConfigurationSource.Convention); var property = entityType.AddProperty("A", typeof(int), ConfigurationSource.Convention, ConfigurationSource.Convention); var key = entityType.AddKey(property, ConfigurationSource.Convention); var foreignKey = new ForeignKey(new List <Property> { property }, key, entityType, entityType, ConfigurationSource.Convention); var index = new Index(new List <Property> { property }, "IndexName", entityType, ConfigurationSource.Convention); var contextServices = RelationalTestHelpers.Instance.CreateContextServices(model.FinalizeModel()); var fakeFactories = new Dictionary <Type, Func <object> > { { typeof(string), () => "Fake" }, { typeof(IList <string>), () => new List <string> { "Fake1", "Fake2" } }, { typeof(IEnumerable <IUpdateEntry>), () => new List <IUpdateEntry> { new InternalClrEntityEntry( contextServices.GetRequiredService <IStateManager>(), entityType, new object()) } }, { typeof(IRelationalConnection), () => new FakeRelationalConnection() }, { typeof(LoggingDefinitions), () => new TestRelationalLoggingDefinitions() }, { typeof(DbCommand), () => new FakeDbCommand() }, { typeof(DbTransaction), () => new FakeDbTransaction() }, { typeof(DbDataReader), () => new FakeDbDataReader() }, { typeof(Transaction), () => new CommittableTransaction() }, { typeof(IMigrator), () => new FakeMigrator() }, { typeof(Migration), () => new FakeMigration() }, { typeof(IMigrationsAssembly), () => new FakeMigrationsAssembly() }, { typeof(MethodCallExpression), () => Expression.Call(constantExpression, typeof(object).GetMethod("ToString")) }, { typeof(Expression), () => constantExpression }, { typeof(IEntityType), () => entityType }, { typeof(IProperty), () => property }, { typeof(IKey), () => key }, { typeof(IForeignKey), () => foreignKey }, { typeof(IIndex), () => index }, { typeof(TypeInfo), () => typeof(object).GetTypeInfo() }, { typeof(Type), () => typeof(object) }, { typeof(ValueConverter), () => new BoolToZeroOneConverter <int>() }, { typeof(DbContext), () => new FakeDbContext() }, { typeof(SqlExpression), () => new FakeSqlExpression() } }; TestEventLogging( typeof(RelationalEventId), typeof(RelationalLoggerExtensions), new[] { typeof(IRelationalConnectionDiagnosticsLogger), typeof(IRelationalCommandDiagnosticsLogger) }, new TestRelationalLoggingDefinitions(), fakeFactories, services => FakeRelationalOptionsExtension.AddEntityFrameworkRelationalDatabase(services), new Dictionary <string, IList <string> > { { nameof(RelationalEventId.CommandExecuting), new List <string> { nameof(IRelationalCommandDiagnosticsLogger.CommandReaderExecuting), nameof(IRelationalCommandDiagnosticsLogger.CommandScalarExecuting), nameof(IRelationalCommandDiagnosticsLogger.CommandNonQueryExecuting), nameof(IRelationalCommandDiagnosticsLogger.CommandReaderExecutingAsync), nameof(IRelationalCommandDiagnosticsLogger.CommandScalarExecutingAsync), nameof(IRelationalCommandDiagnosticsLogger.CommandNonQueryExecutingAsync) } }, { nameof(RelationalEventId.CommandExecuted), new List <string> { nameof(IRelationalCommandDiagnosticsLogger.CommandReaderExecutedAsync), nameof(IRelationalCommandDiagnosticsLogger.CommandScalarExecutedAsync), nameof(IRelationalCommandDiagnosticsLogger.CommandNonQueryExecutedAsync), nameof(IRelationalCommandDiagnosticsLogger.CommandReaderExecuted), nameof(IRelationalCommandDiagnosticsLogger.CommandScalarExecuted), nameof(IRelationalCommandDiagnosticsLogger.CommandNonQueryExecuted) } } }); }
public override IServiceCollection AddProviderServices(IServiceCollection services) => FakeRelationalOptionsExtension.AddEntityFrameworkRelationalDatabase(services);
public async Task Reports_command_diagnostic_on_exception( Delegate commandDelegate, string diagnosticName, bool async) { var exception = new InvalidOperationException(); var fakeDbConnection = new FakeDbConnection( ConnectionString, new FakeCommandExecutor( c => { throw exception; }, c => { throw exception; }, (c, cb) => { throw exception; }, (c, ct) => { throw exception; }, (c, ct) => { throw exception; }, (c, cb, ct) => { throw exception; })); var optionsExtension = new FakeRelationalOptionsExtension { Connection = fakeDbConnection }; var options = CreateOptions(optionsExtension); var diagnostic = new List <Tuple <string, object> >(); var fakeConnection = new FakeRelationalConnection(options); var relationalCommand = CreateRelationalCommand( diagnosticSource: new ListDiagnosticSource(diagnostic), parameters: new[] { new TypeMappedRelationalParameter("FirstInvariant", "FirstParameter", new RelationalTypeMapping("int", typeof(int), DbType.Int32), false) }); var parameterValues = new Dictionary <string, object> { { "FirstInvariant", 17 } }; if (async) { await Assert.ThrowsAsync <InvalidOperationException>( async() => await((CommandFunc)commandDelegate)(fakeConnection, relationalCommand, parameterValues)); } else { Assert.Throws <InvalidOperationException>(() => ((CommandAction)commandDelegate)(fakeConnection, relationalCommand, parameterValues)); } Assert.Equal(2, diagnostic.Count); Assert.Equal(RelationalDiagnostics.BeforeExecuteCommand, diagnostic[0].Item1); Assert.Equal(RelationalDiagnostics.CommandExecutionError, diagnostic[1].Item1); var beforeData = (RelationalDiagnosticSourceBeforeMessage)diagnostic[0].Item2; var afterData = (RelationalDiagnosticSourceAfterMessage)diagnostic[1].Item2; Assert.Equal(fakeDbConnection.DbCommands[0], beforeData.Command); Assert.Equal(fakeDbConnection.DbCommands[0], afterData.Command); Assert.Equal(diagnosticName, beforeData.ExecuteMethod); Assert.Equal(diagnosticName, afterData.ExecuteMethod); Assert.Equal(async, beforeData.IsAsync); Assert.Equal(async, afterData.IsAsync); Assert.Equal(exception, afterData.Exception); }
protected FakeRelationalOptionsExtension(FakeRelationalOptionsExtension copyFrom) : base(copyFrom) { }
public async Task Reports_command_diagnostic_on_exception( Delegate commandDelegate, string diagnosticName, bool async) { var exception = new InvalidOperationException(); var fakeDbConnection = new FakeDbConnection( ConnectionString, new FakeCommandExecutor( c => { throw exception; }, c => { throw exception; }, (c, cb) => { throw exception; }, (c, ct) => { throw exception; }, (c, ct) => { throw exception; }, (c, cb, ct) => { throw exception; })); var optionsExtension = new FakeRelationalOptionsExtension { Connection = fakeDbConnection }; var options = CreateOptions(optionsExtension); var fakeConnection = new FakeRelationalConnection(options); var diagnostic = new List <Tuple <string, object> >(); var relationalCommand = new RelationalCommand( new SensitiveDataLogger <RelationalCommand>( new FakeSensitiveDataLogger <RelationalCommand>(), options), new ListDiagnosticSource(diagnostic), "Command Text", new[] { new RelationalParameter("FirstParameter", 17, new RelationalTypeMapping("int", typeof(int), DbType.Int32), false, null) }); if (async) { await Assert.ThrowsAsync <InvalidOperationException>( async() => await((Func <RelationalCommand, bool, IRelationalConnection, Task>)commandDelegate)(relationalCommand, true, fakeConnection)); } else { Assert.Throws <InvalidOperationException>(() => ((Action <RelationalCommand, bool, IRelationalConnection>)commandDelegate)(relationalCommand, true, fakeConnection)); } Assert.Equal(2, diagnostic.Count); Assert.Equal(RelationalDiagnostics.BeforeExecuteCommand, diagnostic[0].Item1); Assert.Equal(RelationalDiagnostics.CommandExecutionError, diagnostic[1].Item1); var beforeData = (RelationalDiagnosticSourceMessage)diagnostic[0].Item2; var afterData = (RelationalDiagnosticSourceMessage)diagnostic[1].Item2; Assert.Equal(fakeDbConnection.DbCommands[0], beforeData.Command); Assert.Equal(fakeDbConnection.DbCommands[0], afterData.Command); Assert.Equal(diagnosticName, beforeData.ExecuteMethod); Assert.Equal(diagnosticName, afterData.ExecuteMethod); Assert.Equal(async, beforeData.IsAsync); Assert.Equal(async, afterData.IsAsync); Assert.Equal(exception, afterData.Exception); }
public override IServiceCollection AddProviderServices(IServiceCollection services) { ConfigureServices?.Invoke(services); return(FakeRelationalOptionsExtension.AddEntityFrameworkRelationalDatabase(services)); }