Exemplo n.º 1
0
        public void ShouldGetConnectionWithOpenTest()
        {
            Mockery mockery;
            MockAdoNetAmbientAware ambientAware;
            IConnectionFactory mockConnectionFactory;
            IDbConnection mockDbConnection;

            mockery = new Mockery();
            mockConnectionFactory = mockery.NewMock<IConnectionFactory>();
            mockDbConnection = mockery.NewMock<IDbConnection>();

            Expect.Once.On(mockConnectionFactory).Method("GetConnection").Will(Return.Value(mockDbConnection));
            Expect.AtLeastOnce.On(mockDbConnection).SetProperty("ConnectionString").To("myConnectionString");
            Expect.AtLeastOnce.On(mockDbConnection).Method("Open").WithNoArguments();

            ambientAware = new MockAdoNetAmbientAware(MOCK_CONNECTION_STRING, mockConnectionFactory);

            ambientAware.BypassGetConnection(true);

            mockery.VerifyAllExpectationsHaveBeenMet();
        }
Exemplo n.º 2
0
        public void ShouldGetConnectionAndTransactionUnderAmbientDistributedTransactionAndDataSourceTransactionTest()
        {
            Mockery mockery;
            MockAdoNetAmbientAware ambientAware;
            IConnectionFactory mockConnectionFactory;
            IDbConnection mockDbConnection;

            IDbConnection dbConnection;
            IDbTransaction dbTransaction;

            mockery = new Mockery();
            mockConnectionFactory = mockery.NewMock<IConnectionFactory>();
            mockDbConnection = mockery.NewMock<IDbConnection>();

            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();

            ambientAware = new MockAdoNetAmbientAware(MOCK_CONNECTION_STRING, mockConnectionFactory);

            Assert.True(ambientAware.IsShouldDisposeResources);
            using (TransactionScope ts = new TransactionScope())
            {
                Assert.IsTrue(ambientAware.IsShouldDisposeResources);
                using (DataSourceTransactionScope dsts = new DataSourceTransactionScope())
                {
                    Assert.IsFalse(ambientAware.IsShouldDisposeResources);
                    ambientAware.BypassGetConnectionAndTransaction(out dbConnection, out dbTransaction);

                    Assert.IsNotNull(dbConnection);
                    Assert.IsNull(dbTransaction);
                }
                Assert.True(ambientAware.IsShouldDisposeResources);
            }
            Assert.True(ambientAware.IsShouldDisposeResources);

            mockery.VerifyAllExpectationsHaveBeenMet();
        }
Exemplo n.º 3
0
        public void ShouldGetConnectionNoOpenTest()
        {
            Mockery mockery;
            MockAdoNetAmbientAware ambientAware;
            IConnectionFactory mockConnectionFactory;
            IDbConnection mockDbConnection;

            mockery = new Mockery();
            mockConnectionFactory = mockery.NewMock<IConnectionFactory>();
            mockDbConnection = mockery.NewMock<IDbConnection>();

            Expect.Once.On(mockConnectionFactory).Method("GetConnection").Will(Return.Value(mockDbConnection));

            ambientAware = new MockAdoNetAmbientAware(MOCK_CONNECTION_STRING, mockConnectionFactory);

            ambientAware.BypassGetConnection(false);

            mockery.VerifyAllExpectationsHaveBeenMet();
        }
Exemplo n.º 4
0
        public void ShouldFailOnNullConnectionStringCreateTest()
        {
            Mockery mockery;
            MockAdoNetAmbientAware ambientAware;
            IConnectionFactory mockConnectionFactory;

            mockery = new Mockery();
            mockConnectionFactory = mockery.NewMock<IConnectionFactory>();

            ambientAware = new MockAdoNetAmbientAware(null, mockConnectionFactory);
        }
Exemplo n.º 5
0
        public void ShouldGetConnectionAndTransactionNotAmbientTest()
        {
            Mockery mockery;
            MockAdoNetAmbientAware ambientAware;
            IConnectionFactory mockConnectionFactory;
            IDbConnection mockDbConnection;

            IDbConnection dbConnection;
            IDbTransaction dbTransaction;

            mockery = new Mockery();
            mockConnectionFactory = mockery.NewMock<IConnectionFactory>();
            mockDbConnection = mockery.NewMock<IDbConnection>();

            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).SetProperty("ConnectionString").To("myConnectionString");
            Expect.AtLeastOnce.On(mockDbConnection).Method("Open").WithNoArguments();

            ambientAware = new MockAdoNetAmbientAware(MOCK_CONNECTION_STRING, mockConnectionFactory);

            ambientAware.BypassGetConnectionAndTransaction(out dbConnection, out dbTransaction);

            Assert.IsNotNull(dbConnection);
            Assert.IsNull(dbTransaction);

            mockery.VerifyAllExpectationsHaveBeenMet();
        }
Exemplo n.º 6
0
        public void ShouldFailOnNullConnectionGetConnectionFromFactoryTest()
        {
            Mockery mockery;
            MockAdoNetAmbientAware ambientAware;
            IConnectionFactory mockConnectionFactory;

            mockery = new Mockery();
            mockConnectionFactory = mockery.NewMock<IConnectionFactory>();

            Expect.Once.On(mockConnectionFactory).Method("GetConnection").Will(Return.Value(null));

            ambientAware = new MockAdoNetAmbientAware(MOCK_CONNECTION_STRING, mockConnectionFactory);

            ambientAware.BypassGetConnection(false);
        }
Exemplo n.º 7
0
        public void ShouldFailOnNullConnectionFromFactoryGetOpenConnectionTest()
        {
            Mockery mockery;
            MockAdoNetAmbientAware ambientAware;
            IConnectionFactory mockConnectionFactory;
            IDbConnection dbConnection;
            IDbTransaction dbTransaction;

            mockery = new Mockery();
            mockConnectionFactory = mockery.NewMock<IConnectionFactory>();

            Expect.Once.On(mockConnectionFactory).Method("GetConnection").Will(Return.Value(null));

            ambientAware = new MockAdoNetAmbientAware(MOCK_CONNECTION_STRING, mockConnectionFactory);

            ambientAware.BypassGetConnectionAndTransaction(out dbConnection, out dbTransaction);
        }
Exemplo n.º 8
0
        public void ShouldFailOnNullConnectionFactoryCreateTest()
        {
            Mockery mockery;
            MockAdoNetAmbientAware ambientAware;
            IConnectionFactory mockConnectionFactory;

            mockery = new Mockery();
            mockConnectionFactory = null;

            ambientAware = new MockAdoNetAmbientAware(MOCK_CONNECTION_STRING, mockConnectionFactory);
        }
Exemplo n.º 9
0
        public void ShouldFailOnConnectionTypeMismatchUnderAmbientDataSourceTransactionTest()
        {
            Mockery mockery;
            MockAdoNetAmbientAware ambientAware;
            IConnectionFactory mockConnectionFactory;
            IDbConnection mockDbConnection;
            IDbTransaction mockDbTransaction;
            DataSourceTransaction transaction;

            IDbConnection dbConnection;
            IDbTransaction dbTransaction;

            mockery = new Mockery();
            mockConnectionFactory = mockery.NewMock<IConnectionFactory>();
            mockDbConnection = mockery.NewMock<IDbConnection>();
            mockDbTransaction = mockery.NewMock<IDbTransaction>();

            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("BeginTransaction").WithNoArguments().Will(Return.Value(mockDbTransaction));
            Expect.AtLeastOnce.On(mockDbTransaction).Method("Commit").WithNoArguments();
            Expect.AtLeastOnce.On(mockDbTransaction).Method("Dispose").WithNoArguments();

            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);

            ambientAware = new MockAdoNetAmbientAware(MOCK_CONNECTION_STRING, mockConnectionFactory);

            ambientAware.BypassGetConnectionAndTransaction(out dbConnection, out dbTransaction);
        }
Exemplo n.º 10
0
        public void ShouldCreateTest()
        {
            Mockery mockery;
            MockAdoNetAmbientAware ambientAware;
            IConnectionFactory mockConnectionFactory;

            mockery = new Mockery();
            mockConnectionFactory = mockery.NewMock<IConnectionFactory>();

            ambientAware = new MockAdoNetAmbientAware(MOCK_CONNECTION_STRING, mockConnectionFactory);

            Assert.AreEqual(MOCK_CONNECTION_STRING, ambientAware.BypassConnectionString);
            Assert.AreEqual(mockConnectionFactory, ambientAware.BypassConnectionFactory);

            mockery.VerifyAllExpectationsHaveBeenMet();
        }