public void With_valid_arguments_doesnt_throw() { var internalContextMock = new Mock <InternalContextForMock>(); internalContextMock.Setup( m => m.ExecuteSqlCommandAsync(It.IsAny <TransactionalBehavior>(), It.IsAny <string>(), It.IsAny <CancellationToken>(), It.IsAny <object[]>())) .Returns(Task.FromResult(1)); var database = new Database(internalContextMock.Object); var cancellationToken = new CancellationTokenSource().Token; var parameters = new object[1]; Assert.NotNull(database.ExecuteSqlCommandAsync("query", parameters).Result); internalContextMock.Verify( m => m.ExecuteSqlCommandAsync(TransactionalBehavior.EnsureTransaction, "query", CancellationToken.None, parameters), Times.Once()); Assert.NotNull(database.ExecuteSqlCommandAsync("query", cancellationToken, parameters).Result); internalContextMock.Verify( m => m.ExecuteSqlCommandAsync(TransactionalBehavior.EnsureTransaction, "query", cancellationToken, parameters), Times.Once()); Assert.NotNull(database.ExecuteSqlCommandAsync(TransactionalBehavior.DoNotEnsureTransaction, "query", parameters).Result); internalContextMock.Verify( m => m.ExecuteSqlCommandAsync(TransactionalBehavior.EnsureTransaction, "query", CancellationToken.None, parameters), Times.Once()); Assert.NotNull(database.ExecuteSqlCommandAsync(TransactionalBehavior.DoNotEnsureTransaction, "query", cancellationToken, parameters).Result); internalContextMock.Verify( m => m.ExecuteSqlCommandAsync(TransactionalBehavior.EnsureTransaction, "query", cancellationToken, parameters), Times.Once()); }
public void With_whitespace_SQL_throws() { var database = new Database(new Mock <InternalContextForMock>().Object); Assert.Equal( Strings.ArgumentIsNullOrWhitespace("sql"), Assert.Throws <ArgumentException>(() => database.ExecuteSqlCommandAsync(" ").Result).Message); }
public void With_null_parameters_throws() { var database = new Database(new Mock <InternalContextForMock>().Object); Assert.Equal( "parameters", Assert.Throws <ArgumentNullException>(() => database.ExecuteSqlCommandAsync("query", null).Result).ParamName); }
// // Summary: // Asynchronously executes the given DDL/DML command against the database. As with // any API that accepts SQL it is important to parameterize any user input to protect // against a SQL injection attack. You can include parameter place holders in the // SQL query string and then supply parameter values as additional arguments. Any // parameter values you supply will automatically be converted to a DbParameter. // context.Database.ExecuteSqlCommandAsync("UPDATE dbo.Posts SET Rating = 5 WHERE // Author = @p0", userSuppliedAuthor){throw new NotImplementedException();} Alternatively, you can also construct a DbParameter // and supply it to SqlQuery. This allows you to use named parameters in the SQL // query string. context.Database.ExecuteSqlCommandAsync("UPDATE dbo.Posts SET Rating // = 5 WHERE Author = @author", new SqlParameter("@author", userSuppliedAuthor)){throw new NotImplementedException();} // // Parameters: // transactionalBehavior: // Controls the creation of a transaction for this command. // // sql: // The command string. // // parameters: // The parameters to apply to the command string. // // Returns: // A task that represents the asynchronous operation. The task result contains the // result returned by the database after executing the command. // // Remarks: // Multiple active operations on the same context instance are not supported. Use // 'await' to ensure that any asynchronous operations have completed before calling // another method on this context. public Task <int> ExecuteSqlCommandAsync(Core.Cmn.TransactionalBehavior transactionalBehavior, string sql, params object[] parameters) { var behavior = GetTransactionalBehavior(transactionalBehavior); return(_database.ExecuteSqlCommandAsync(behavior, sql, parameters)); }