public void ShouldCreateParameterTest() { Mockery mockery; AdoNetDataSource dataSource; IConnectionFactory mockConnectionFactory; IDbConnection mockDbConnection; IDbCommand mockDbCommand; IDataParameter p; IDbDataParameter mockDbParameter; mockery = new Mockery(); mockConnectionFactory = mockery.NewMock<IConnectionFactory>(); mockDbConnection = mockery.NewMock<IDbConnection>(); mockDbCommand = mockery.NewMock<IDbCommand>(); mockDbParameter = mockery.NewMock<IDbDataParameter>(); Expect.Once.On(mockConnectionFactory).Method("GetConnection").Will(Return.Value(mockDbConnection)); Expect.Once.On(mockDbConnection).Method("CreateCommand").Will(Return.Value(mockDbCommand)); Expect.Once.On(mockDbConnection).Method("Dispose"); Expect.Once.On(mockDbCommand).Method("CreateParameter").Will(Return.Value(mockDbParameter)); Expect.Once.On(mockDbCommand).Method("Dispose"); Expect.Once.On(mockDbParameter).SetProperty("ParameterName").To("@bob"); Expect.Once.On(mockDbParameter).SetProperty("Size").To(1); Expect.Once.On(mockDbParameter).SetProperty("Value").To("test"); Expect.Once.On(mockDbParameter).SetProperty("Direction").To(ParameterDirection.Input); Expect.Once.On(mockDbParameter).SetProperty("DbType").To(DbType.String); Expect.Once.On(mockDbParameter).SetProperty("Precision").To((byte)2); Expect.Once.On(mockDbParameter).SetProperty("Scale").To((byte)3); //Expect.Once.On(mockDbParameter).SetProperty("IsNullable").To(true); Expect.Once.On(mockDbParameter).GetProperty("ParameterName").Will(Return.Value("@bob")); Expect.Once.On(mockDbParameter).GetProperty("Size").Will(Return.Value(1)); Expect.Once.On(mockDbParameter).GetProperty("Value").Will(Return.Value("test")); Expect.Once.On(mockDbParameter).GetProperty("Direction").Will(Return.Value(ParameterDirection.Input)); Expect.Once.On(mockDbParameter).GetProperty("DbType").Will(Return.Value(DbType.String)); Expect.Once.On(mockDbParameter).GetProperty("Precision").Will(Return.Value((byte)2)); Expect.Once.On(mockDbParameter).GetProperty("Scale").Will(Return.Value((byte)3)); //Expect.Once.On(mockDbParameter).GetProperty("IsNullable").Will(Return.Value(true)); dataSource = new AdoNetDataSource(MOCK_CONNECTION_STRING, mockConnectionFactory); p = dataSource.CreateParameter(null, ParameterDirection.Input, DbType.String, 1, 2, 3, true, "@bob", "test"); Assert.IsNotNull(p); Assert.AreEqual(ParameterDirection.Input, p.Direction); Assert.AreEqual("@bob", p.ParameterName); Assert.AreEqual("test", p.Value); Assert.AreEqual(1, ((IDbDataParameter)p).Size); Assert.AreEqual(DbType.String, p.DbType); Assert.AreEqual((byte)2, ((IDbDataParameter)p).Precision); Assert.AreEqual((byte)3, ((IDbDataParameter)p).Scale); //Assert.AreEqual(true, ((IDbDataParameter)p).IsNullable); mockery.VerifyAllExpectationsHaveBeenMet(); }
public void ShouldFailOnNullConnectionStringCreateTest() { Mockery mockery; AdoNetDataSource dataSource; IConnectionFactory mockConnectionFactory; mockery = new Mockery(); mockConnectionFactory = mockery.NewMock<IConnectionFactory>(); dataSource = new AdoNetDataSource(null, mockConnectionFactory); }
public void ShouldFailOnNullConnectionGetConnectionFromFactoryTest() { Mockery mockery; AdoNetDataSource dataSource; IConnectionFactory mockConnectionFactory; mockery = new Mockery(); mockConnectionFactory = mockery.NewMock<IConnectionFactory>(); Expect.Once.On(mockConnectionFactory).Method("GetConnection").Will(Return.Value(null)); dataSource = new AdoNetDataSource(MOCK_CONNECTION_STRING, mockConnectionFactory); dataSource.CreateParameter(null, ParameterDirection.Input, DbType.String, 1, 2, 3, true, "@bob", "test"); }
public void ShouldFailOnNullConnectionFromFactoryGetOpenConnectionTest() { Mockery mockery; AdoNetDataSource dataSource; IConnectionFactory mockConnectionFactory; mockery = new Mockery(); mockConnectionFactory = mockery.NewMock<IConnectionFactory>(); Expect.Once.On(mockConnectionFactory).Method("GetConnection").Will(Return.Value(null)); dataSource = new AdoNetDataSource(MOCK_CONNECTION_STRING, mockConnectionFactory); dataSource.ExecuteNonQuery(CommandType.Text, "blah blah blah", null, null, false); }
public void ShouldFailOnNullConnectionFactoryCreateTest() { Mockery mockery; AdoNetDataSource dataSource; IConnectionFactory mockConnectionFactory; mockery = new Mockery(); mockConnectionFactory = null; dataSource = new AdoNetDataSource(MOCK_CONNECTION_STRING, mockConnectionFactory); }
public void ShouldCreateTest() { Mockery mockery; AdoNetDataSource dataSource; IConnectionFactory mockConnectionFactory; mockery = new Mockery(); mockConnectionFactory = mockery.NewMock<IConnectionFactory>(); dataSource = new AdoNetDataSource(MOCK_CONNECTION_STRING, mockConnectionFactory); mockery.VerifyAllExpectationsHaveBeenMet(); }
public void ShouldFailOnConnectionTypeMismatchUnderAmbientDataSourceTransactionTest() { Mockery mockery; AdoNetDataSource dataSource; IConnectionFactory mockConnectionFactory; IDbConnection mockDbConnection; IDbCommand mockDbCommand; IDataParameterCollection mockDataParameterCollection; IDataParameter[] mockDataParameters; IDataParameter mockDataParameter0; IDataParameter mockDataParameter1; IDbTransaction mockDbTransaction; DataSourceTransaction transaction; int recordsAffected; mockery = new Mockery(); mockConnectionFactory = mockery.NewMock<IConnectionFactory>(); mockDbConnection = mockery.NewMock<IDbConnection>(); mockDbCommand = mockery.NewMock<IDbCommand>(); mockDataParameterCollection = mockery.NewMock<IDataParameterCollection>(); mockDataParameter0 = mockery.NewMock<IDataParameter>(); mockDataParameter1 = mockery.NewMock<IDataParameter>(); mockDbTransaction = mockery.NewMock<IDbTransaction>(); mockDataParameters = new IDataParameter[] { mockDataParameter0, mockDataParameter1 }; Expect.Once.On(mockConnectionFactory).Method("GetConnection").Will(Return.Value(mockDbConnection)); Expect.Once.On(mockConnectionFactory).GetProperty("ConnectionType").Will(Return.Value(mockDbConnection.GetType())); //Expect.AtLeastOnce.On(mockDbConnection).GetProperty("State").Will(Return.Value(ConnectionState.Open)); Expect.AtLeastOnce.On(mockDbConnection).Method("Dispose").WithNoArguments(); Expect.AtLeastOnce.On(mockDbConnection).SetProperty("ConnectionString").To("myConnectionString"); Expect.AtLeastOnce.On(mockDbConnection).Method("Open").WithNoArguments(); Expect.AtLeastOnce.On(mockDbConnection).Method("CreateCommand").WithNoArguments().Will(Return.Value(mockDbCommand)); Expect.AtLeastOnce.On(mockDbConnection).Method("BeginTransaction").WithNoArguments().Will(Return.Value(mockDbTransaction)); Expect.AtLeastOnce.On(mockDbCommand).GetProperty("Parameters").Will(Return.Value(mockDataParameterCollection)); Expect.AtLeastOnce.On(mockDataParameterCollection).Method("Clear").WithNoArguments(); Expect.AtLeastOnce.On(mockDbCommand).Method("Dispose").WithNoArguments(); Expect.AtLeastOnce.On(mockDbCommand).SetProperty("Connection").To(mockDbConnection); Expect.AtLeastOnce.On(mockDbCommand).SetProperty("CommandType").To(CommandType.StoredProcedure); Expect.AtLeastOnce.On(mockDbCommand).SetProperty("CommandText").To("blah blah blah"); Expect.AtLeastOnce.On(mockDbCommand).SetProperty("Transaction"); Expect.AtLeastOnce.On(mockDbCommand).Method("ExecuteNonQuery").WithNoArguments().Will(Return.Value(1)); Expect.AtLeastOnce.On(mockDbTransaction).Method("Commit").WithNoArguments(); Expect.AtLeastOnce.On(mockDbTransaction).Method("Dispose").WithNoArguments(); Expect.AtLeastOnce.On(mockDataParameter0).GetProperty("Value").Will(Return.Value(1)); Expect.AtLeastOnce.On(mockDataParameter1).GetProperty("Value").Will(Return.Value(null)); Expect.AtLeastOnce.On(mockDataParameter1).SetProperty("Value").To(DBNull.Value); Expect.AtLeastOnce.On(mockDataParameterCollection).Method("Add").With(mockDataParameter0).Will(Return.Value(0)); Expect.AtLeastOnce.On(mockDataParameterCollection).Method("Add").With(mockDataParameter1).Will(Return.Value(0)); transaction = new DataSourceTransaction(); Assert.IsFalse(transaction.Bound); Assert.IsNull(DataSourceTransaction.Current); DataSourceTransaction.FrameTransaction(transaction); Assert.IsNotNull(DataSourceTransaction.Current); transaction.Bind(MOCK_CONNECTION_STRING, new MockConnection(), mockDbTransaction, null); dataSource = new AdoNetDataSource(MOCK_CONNECTION_STRING, mockConnectionFactory); recordsAffected = dataSource.ExecuteNonQuery(CommandType.StoredProcedure, "blah blah blah", mockDataParameters, null, false); }
public void ShouldExecuteUnderAmbientDistributedTransactionAndDataSourceTransactionCompleteTest() { Mockery mockery; AdoNetDataSource dataSource; IConnectionFactory mockConnectionFactory; IDbConnection mockDbConnection; IDbCommand mockDbCommand; IDataParameterCollection mockDataParameterCollection; IDataParameter[] mockDataParameters; IDataParameter mockDataParameter0; IDataParameter mockDataParameter1; int recordsAffected; mockery = new Mockery(); mockConnectionFactory = mockery.NewMock<IConnectionFactory>(); mockDbConnection = mockery.NewMock<IDbConnection>(); mockDbCommand = mockery.NewMock<IDbCommand>(); mockDataParameterCollection = mockery.NewMock<IDataParameterCollection>(); mockDataParameter0 = mockery.NewMock<IDataParameter>(); mockDataParameter1 = mockery.NewMock<IDataParameter>(); mockDataParameters = new IDataParameter[] { mockDataParameter0, mockDataParameter1 }; Expect.Once.On(mockConnectionFactory).Method("GetConnection").Will(Return.Value(mockDbConnection)); Expect.Once.On(mockConnectionFactory).GetProperty("ConnectionType").Will(Return.Value(mockDbConnection.GetType())); //Expect.AtLeastOnce.On(mockDbConnection).GetProperty("State").Will(Return.Value(ConnectionState.Open)); Expect.AtLeastOnce.On(mockDbConnection).Method("Dispose").WithNoArguments(); Expect.AtLeastOnce.On(mockDbConnection).SetProperty("ConnectionString").To("myConnectionString"); Expect.AtLeastOnce.On(mockDbConnection).Method("Open").WithNoArguments(); Expect.AtLeastOnce.On(mockDbConnection).Method("CreateCommand").WithNoArguments().Will(Return.Value(mockDbCommand)); Expect.AtLeastOnce.On(mockDbCommand).GetProperty("Parameters").Will(Return.Value(mockDataParameterCollection)); Expect.AtLeastOnce.On(mockDataParameterCollection).Method("Clear").WithNoArguments(); Expect.AtLeastOnce.On(mockDbCommand).Method("Dispose").WithNoArguments(); Expect.AtLeastOnce.On(mockDbCommand).SetProperty("Connection").To(mockDbConnection); Expect.AtLeastOnce.On(mockDbCommand).SetProperty("CommandType").To(CommandType.StoredProcedure); Expect.AtLeastOnce.On(mockDbCommand).SetProperty("CommandText").To("blah blah blah"); Expect.AtLeastOnce.On(mockDbCommand).SetProperty("Transaction"); Expect.AtLeastOnce.On(mockDbCommand).Method("ExecuteNonQuery").WithNoArguments().Will(Return.Value(1)); Expect.AtLeastOnce.On(mockDataParameter0).GetProperty("Value").Will(Return.Value(1)); Expect.AtLeastOnce.On(mockDataParameter1).GetProperty("Value").Will(Return.Value(null)); Expect.AtLeastOnce.On(mockDataParameter1).SetProperty("Value").To(DBNull.Value); Expect.AtLeastOnce.On(mockDataParameterCollection).Method("Add").With(mockDataParameter0).Will(Return.Value(0)); Expect.AtLeastOnce.On(mockDataParameterCollection).Method("Add").With(mockDataParameter1).Will(Return.Value(0)); using (TransactionScope ts = new TransactionScope()) { using (DataSourceTransactionScope dsts = new DataSourceTransactionScope()) { dataSource = new AdoNetDataSource(MOCK_CONNECTION_STRING, mockConnectionFactory); recordsAffected = dataSource.ExecuteNonQuery(CommandType.StoredProcedure, "blah blah blah", mockDataParameters, null, false); dsts.Complete(); ts.Complete(); } } Assert.AreEqual(1, recordsAffected); mockery.VerifyAllExpectationsHaveBeenMet(); }
public void ShouldExecuteScalarTextNoParametersTest() { Mockery mockery; AdoNetDataSource dataSource; IConnectionFactory mockConnectionFactory; IDbConnection mockDbConnection; IDbCommand mockDbCommand; IDataParameterCollection mockDataParameterCollection; object returnValue; mockery = new Mockery(); mockConnectionFactory = mockery.NewMock<IConnectionFactory>(); mockDbConnection = mockery.NewMock<IDbConnection>(); mockDbCommand = mockery.NewMock<IDbCommand>(); mockDataParameterCollection = mockery.NewMock<IDataParameterCollection>(); Expect.Once.On(mockConnectionFactory).Method("GetConnection").Will(Return.Value(mockDbConnection)); //Expect.AtLeastOnce.On(mockDbConnection).GetProperty("State").Will(Return.Value(ConnectionState.Open)); Expect.AtLeastOnce.On(mockDbConnection).Method("Dispose").WithNoArguments(); Expect.AtLeastOnce.On(mockDbConnection).SetProperty("ConnectionString").To("myConnectionString"); Expect.AtLeastOnce.On(mockDbConnection).Method("Open").WithNoArguments(); Expect.AtLeastOnce.On(mockDbConnection).Method("CreateCommand").WithNoArguments().Will(Return.Value(mockDbCommand)); Expect.AtLeastOnce.On(mockDbCommand).GetProperty("Parameters").Will(Return.Value(mockDataParameterCollection)); Expect.AtLeastOnce.On(mockDataParameterCollection).Method("Clear").WithNoArguments(); Expect.AtLeastOnce.On(mockDbCommand).Method("Dispose").WithNoArguments(); Expect.AtLeastOnce.On(mockDbCommand).SetProperty("Connection").To(mockDbConnection); Expect.AtLeastOnce.On(mockDbCommand).SetProperty("CommandType").To(CommandType.Text); Expect.AtLeastOnce.On(mockDbCommand).SetProperty("CommandText").To("blah blah blah"); Expect.AtLeastOnce.On(mockDbCommand).SetProperty("Transaction"); Expect.AtLeastOnce.On(mockDbCommand).Method("ExecuteScalar").WithNoArguments().Will(Return.Value(1)); Expect.AtLeastOnce.On(mockDbCommand).SetProperty("CommandTimeout").To(15); Expect.AtLeastOnce.On(mockDbCommand).Method("Prepare").WithNoArguments(); dataSource = new AdoNetDataSource(MOCK_CONNECTION_STRING, mockConnectionFactory); returnValue = dataSource.ExecuteScalar(CommandType.Text, "blah blah blah", null, 15, true); Assert.AreEqual(1, returnValue); mockery.VerifyAllExpectationsHaveBeenMet(); }
public void ShouldExecuteScalarSprocWithParametersTest() { Mockery mockery; AdoNetDataSource dataSource; IConnectionFactory mockConnectionFactory; IDbConnection mockDbConnection; IDbCommand mockDbCommand; IDataParameterCollection mockDataParameterCollection; IDataParameter[] mockDataParameters; IDataParameter mockDataParameter0; IDataParameter mockDataParameter1; object returnValue; mockery = new Mockery(); mockConnectionFactory = mockery.NewMock<IConnectionFactory>(); mockDbConnection = mockery.NewMock<IDbConnection>(); mockDbCommand = mockery.NewMock<IDbCommand>(); mockDataParameterCollection = mockery.NewMock<IDataParameterCollection>(); mockDataParameter0 = mockery.NewMock<IDataParameter>(); mockDataParameter1 = mockery.NewMock<IDataParameter>(); mockDataParameters = new IDataParameter[] { mockDataParameter0, mockDataParameter1 }; Expect.Once.On(mockConnectionFactory).Method("GetConnection").Will(Return.Value(mockDbConnection)); //Expect.AtLeastOnce.On(mockDbConnection).GetProperty("State").Will(Return.Value(ConnectionState.Open)); Expect.AtLeastOnce.On(mockDbConnection).Method("Dispose").WithNoArguments(); Expect.AtLeastOnce.On(mockDbConnection).SetProperty("ConnectionString").To("myConnectionString"); Expect.AtLeastOnce.On(mockDbConnection).Method("Open").WithNoArguments(); Expect.AtLeastOnce.On(mockDbConnection).Method("CreateCommand").WithNoArguments().Will(Return.Value(mockDbCommand)); Expect.AtLeastOnce.On(mockDbCommand).GetProperty("Parameters").Will(Return.Value(mockDataParameterCollection)); Expect.AtLeastOnce.On(mockDataParameterCollection).Method("Clear").WithNoArguments(); Expect.AtLeastOnce.On(mockDbCommand).Method("Dispose").WithNoArguments(); Expect.AtLeastOnce.On(mockDbCommand).SetProperty("Connection").To(mockDbConnection); Expect.AtLeastOnce.On(mockDbCommand).SetProperty("CommandType").To(CommandType.StoredProcedure); Expect.AtLeastOnce.On(mockDbCommand).SetProperty("CommandText").To("blah blah blah"); Expect.AtLeastOnce.On(mockDbCommand).SetProperty("Transaction"); Expect.AtLeastOnce.On(mockDbCommand).Method("ExecuteScalar").WithNoArguments().Will(Return.Value(1)); Expect.AtLeastOnce.On(mockDataParameter0).GetProperty("Value").Will(Return.Value(1)); Expect.AtLeastOnce.On(mockDataParameter1).GetProperty("Value").Will(Return.Value(null)); Expect.AtLeastOnce.On(mockDataParameter1).SetProperty("Value").To(DBNull.Value); Expect.AtLeastOnce.On(mockDataParameterCollection).Method("Add").With(mockDataParameter0).Will(Return.Value(0)); Expect.AtLeastOnce.On(mockDataParameterCollection).Method("Add").With(mockDataParameter1).Will(Return.Value(0)); dataSource = new AdoNetDataSource(MOCK_CONNECTION_STRING, mockConnectionFactory); returnValue = dataSource.ExecuteScalar(CommandType.StoredProcedure, "blah blah blah", mockDataParameters, null, false); Assert.AreEqual(1, returnValue); mockery.VerifyAllExpectationsHaveBeenMet(); }
public void ShouldExecuteReaderNoCloseConnectionTextNoParametersTest() { Mockery mockery; AdoNetDataSource dataSource; IConnectionFactory mockConnectionFactory; IDbConnection mockDbConnection; IDbCommand mockDbCommand; IDataParameterCollection mockDataParameterCollection; IDataReader mockDataReader; IDataReader dataReader; mockery = new Mockery(); mockConnectionFactory = mockery.NewMock<IConnectionFactory>(); mockDbConnection = mockery.NewMock<IDbConnection>(); mockDbCommand = mockery.NewMock<IDbCommand>(); mockDataParameterCollection = mockery.NewMock<IDataParameterCollection>(); mockDataReader = mockery.NewMock<IDataReader>(); Expect.Once.On(mockConnectionFactory).Method("GetConnection").Will(Return.Value(mockDbConnection)); //Expect.AtLeastOnce.On(mockDbConnection).GetProperty("State").Will(Return.Value(ConnectionState.Open)); Expect.AtLeastOnce.On(mockDbConnection).Method("Dispose").WithNoArguments(); Expect.AtLeastOnce.On(mockDbConnection).SetProperty("ConnectionString").To("myConnectionString"); Expect.AtLeastOnce.On(mockDbConnection).Method("Open").WithNoArguments(); Expect.AtLeastOnce.On(mockDbConnection).Method("CreateCommand").WithNoArguments().Will(Return.Value(mockDbCommand)); Expect.AtLeastOnce.On(mockDbCommand).GetProperty("Parameters").Will(Return.Value(mockDataParameterCollection)); Expect.AtLeastOnce.On(mockDataParameterCollection).Method("Clear").WithNoArguments(); Expect.AtLeastOnce.On(mockDbCommand).Method("Dispose").WithNoArguments(); Expect.AtLeastOnce.On(mockDbCommand).SetProperty("Connection").To(mockDbConnection); Expect.AtLeastOnce.On(mockDbCommand).SetProperty("CommandType").To(CommandType.Text); Expect.AtLeastOnce.On(mockDbCommand).SetProperty("CommandText").To("blah blah blah"); Expect.AtLeastOnce.On(mockDbCommand).SetProperty("Transaction"); Expect.AtLeastOnce.On(mockDbCommand).Method("ExecuteReader").With(CommandBehavior.SingleRow).Will(Return.Value(mockDataReader)); Expect.AtLeastOnce.On(mockDataReader).Method("Dispose").WithNoArguments(); dataSource = new AdoNetDataSource(MOCK_CONNECTION_STRING, mockConnectionFactory); dataReader = dataSource.ExecuteReader(CommandType.Text, "blah blah blah", null, CommandBehavior.SingleRow, null, false); Assert.IsNotNull(dataReader); dataReader.Dispose(); mockery.VerifyAllExpectationsHaveBeenMet(); }