Exemplo n.º 1
0
        public void GetTByExpressionThrowsArgumentNullException()
        {
            try
            {
                var provider = new IProvider();
                var session = new DataSession(provider.Object);

                Expression expression = null;
                session.GetT<Customer>(expression);
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex is ArgumentNullException);
            }
        }
Exemplo n.º 2
0
        public void GetTByExpressionThrowsUnableToCreateCommandException()
        {
            Expression<Func<Customer, bool>> expression = e => e.ID == 1;
            var provider = new IProvider();
            provider.CommandBuilder.Setup(c => c.CreateGetTCommand<Customer>(expression)).Returns(default(Data.ICommand));

            try
            {
                var target = new DataSession(provider.Object);
                target.GetT(expression);
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex is UnableToCreateCommandException);
            }
        }
Exemplo n.º 3
0
        public void GetTByExpression()
        {

            Expression<Func<Customer, bool>> expression = e => e.ID == 1;

            var customer = new Customer
            {
                ID = 1,
                Balance = 0,
                BalanceRate = 0,
                DefaultContactID = 1,
                Name = "Customer1",
                ProfileID = 1
            };

            var reader = new DbDataReader();
            reader.Setup(r => r.Read()).Returns(true);

            var provider = new IProvider();

            var command = new ICommand();

            var schema = new TypeTable(typeof(Customer));

            provider.Setup(p => p.GetSchema(typeof(Customer))).Returns(schema);

            provider.CommandBuilder.Setup(c => c.CreateGetTCommand<Customer>(expression)).Returns(command.Object);
            provider.CommandExecutor.Setup(c => c.ExecuteReader(command.Object)).Returns(reader.Object);

            provider.Mapper.Setup(m => m.GetTMappingMethod<Customer>()).Returns(d => customer);

            var target = new DataSession(provider.Object);

            var actual = target.GetT(expression);

            Assert.AreEqual(actual, customer);
        }
Exemplo n.º 4
0
        public void GetTByProcedureThrowsUnableToCreateCommandException()
        {
            var provider = new IProvider();
            var procedure = new Procedure("TestProcedure");
            provider.CommandBuilder.Setup(c => c.CreateCommandFromProcedure(procedure)).Returns(default(Data.ICommand));

            try
            {
                var session = new DataSession(provider.Object);
                session.GetT<Customer>(procedure);
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex is UnableToCreateCommandException);
            }
        }
Exemplo n.º 5
0
        public void GetTByProcedure()
        {
            var procedure = new Mock<Procedure>("TestProcedure");

            var customer = new Customer
            {
                ID = 1,
                Balance = 0,
                BalanceRate = 0,
                DefaultContactID = 1,
                Name = "Customer1",
                ProfileID = 1
            };

            var reader = new DbDataReader();
            reader.Setup(r => r.Read()).Returns(true);

            var provider = new IProvider();

            var command = new ICommand();

            var schema = new TypeTable(typeof(Customer));

            provider.Setup(p => p.GetSchema(typeof(Customer))).Returns(schema);

            provider.CommandBuilder.Setup(c => c.CreateCommandFromProcedure(procedure.Object)).Returns(command.Object);

            provider.CommandExecutor.Setup(c => c.ExecuteReader(command.Object)).Returns(reader.Object);

            provider.Mapper.Setup(m => m.GetTMappingMethod<Customer>()).Returns(d => customer);

            var target = new DataSession(provider.Object);

            var actual = target.GetT<Customer>(procedure.Object);

            Assert.AreEqual(actual, customer);

        }
Exemplo n.º 6
0
        public void CountUsingExpressionThrowsArgumentNullException()
        {
            var provider = new IProvider();
            var command = new ICommand();
            var schema = new TypeTable(typeof(Customer));

            Expression expression = null;

            provider.Setup(p => p.GetSchema(typeof(Customer))).Returns(schema);
            provider.CommandBuilder.Setup(c => c.CreateCountCommand<Customer>(expression)).Returns(command.Object);
            provider.CommandExecutor.Setup(c => c.ExecuteCount(command.Object)).Returns(10);

            try
            {
                var target = new DataSession(provider.Object);
                target.Count<Customer>(expression);
            }
            catch (Exception ex)
            {
                
                Assert.IsTrue(ex is ArgumentNullException);
            }
            
            
        }
Exemplo n.º 7
0
        public void CountAll()
        {
            var provider = new IProvider();
            var command = new ICommand();
            var schema = new TypeTable(typeof(Customer));

            provider.Setup(p => p.GetSchema(typeof(Customer))).Returns(schema);
            provider.CommandBuilder.Setup(c => c.CreateCountCommand<Customer>()).Returns(command.Object);
            provider.CommandExecutor.Setup(c => c.ExecuteCount(command.Object)).Returns(10);

            var target = new DataSession(provider.Object);
            var result = target.Count<Customer>();
            Assert.IsTrue(result == 10);
        }
Exemplo n.º 8
0
        public void GetPagedListThrowsUnableToCreateCommandException()
        {
            var provider = new IProvider();
            provider.CommandBuilder.Setup(c => c.CreateGetListByPageCommand<Customer>()).Returns(default(Data.ICommand));

            try
            {
                var target = new DataSession(provider.Object);
                target.GetPagedList<List<Customer>, Customer>(10, 1);
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex is UnableToCreateCommandException);
            }
        }
Exemplo n.º 9
0
        public void GetListByProcedureThrowsUnableToCreateCommandException()
        {
            var procedure = new Mock<Procedure>("TestProcedure");
            var provider = new IProvider();
            provider.CommandBuilder.Setup(c => c.CreateCommandFromProcedure(procedure.Object)).Returns(default(Data.ICommand));

            try
            {
                var target = new DataSession(provider.Object);
                target.GetList<List<Customer>, Customer>(procedure.Object);
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex is UnableToCreateCommandException);
            }
        }
Exemplo n.º 10
0
        public void SaveUpdateThrowsUnableToCreateCommandException()
        {
            var customer = new Customer
            {
                ID = 1,
                Balance = 0,
                BalanceRate = 0,
                DefaultContactID = 1,
                Name = "Customer1",
                ProfileID = 1
            };

            var provider = new IProvider();
            var transaction = new Mock<ITransaction>();
            var schema = new TypeTable(typeof(Customer));

            var reader = new DbDataReader();
            reader.Setup(r => r.Read()).Returns(true);


            provider.Setup(p => p.GetSchema(typeof(Customer))).Returns(schema);
            provider.CommandExecutor.Setup(c => c.InitiateTransaction()).Returns(transaction.Object);
            provider.Setup(p => p.GetEntityStatus(customer)).Returns(EntityStatus.Update);
            provider.CommandBuilder.Setup(c => c.CreateUpdateCommand(customer)).Returns(default(Data.ICommand));

            try
            {
                var target = new DataSession(provider.Object);
                target.Save(customer);
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex is UnableToCreateCommandException);
            }

        }
Exemplo n.º 11
0
        public void SaveUpdate()
        {
            var customer = new Customer
            {
                ID = 1,
                Balance = 0,
                BalanceRate = 0,
                DefaultContactID = 1,
                Name = "Customer1",
                ProfileID = 1
            };

            var provider = new IProvider();
            var transaction = new Mock<ITransaction>();
            var command = new ICommand();
            var schema = new TypeTable(typeof(Customer));

            var reader = new DbDataReader();
            reader.Setup(r => r.Read()).Returns(true);


            provider.Setup(p => p.GetSchema(typeof(Customer))).Returns(schema);
            provider.CommandExecutor.Setup(c => c.InitiateTransaction()).Returns(transaction.Object);
            provider.Setup(p => p.GetEntityStatus(customer)).Returns(EntityStatus.Update);
            provider.CommandBuilder.Setup(c => c.CreateUpdateCommand(customer)).Returns(command.Object);
            provider.CommandExecutor.Setup(c => c.ExecuteReader(command.Object, transaction.Object)).Returns(
                reader.Object);

            provider.Mapper.Setup(m => m.GetObjectMappingMethod(typeof(Customer))).Returns(d => customer);

            var target = new DataSession(provider.Object);
            target.Save(customer);

            provider.CommandExecutor.Verify(c => c.InitiateTransaction());
            provider.CommandExecutor.Verify(c => c.CommitTransaction(transaction.Object));

            Assert.IsTrue(customer.ID == 1);
        }
Exemplo n.º 12
0
        public void SaveRollBacksTransactionWhenExceptionThrown()
        {
            var customer = new Customer
            {
                ID = 1,
                Balance = 0,
                BalanceRate = 0,
                DefaultContactID = 1,
                Name = "Customer1",
                ProfileID = 1
            };

            var provider = new IProvider();
            var transaction = new Mock<ITransaction>();

            provider.CommandExecutor.Setup(c => c.InitiateTransaction()).Returns(transaction.Object);
            provider.Setup(p => p.GetEntityStatus(customer)).Returns(EntityStatus.Insert);
            provider.CommandBuilder.Setup(c => c.CreateInsertCommand(customer)).Returns(default(Data.ICommand));

            var target = new DataSession(provider.Object);
            try
            {
                target.BeginTransaction();
                target.Save(customer);
            }
            catch (Exception)
            {
                Assert.IsTrue(!target.IsTransactionInitiated);
                
            }
        }
Exemplo n.º 13
0
 public void SaveThrowsArgumentNullException()
 {
     try
     {
         var provider = new IProvider();
         var session = new DataSession(provider.Object);
         session.Save(null);
     }
     catch (Exception ex)
     {
         Assert.IsTrue(ex is ArgumentNullException);
     }
 }
Exemplo n.º 14
0
        public void RollbackTransactionWithoutBeginTransactionThrowsTransactionNotInstantiatedException()
        {
            var provider = new IProvider();
            var transaction = new Mock<ITransaction>();
            provider.CommandExecutor.Setup(c => c.InitiateTransaction()).Returns(transaction.Object);

            var target = new DataSession(provider.Object);
            try
            {
                target.RollBackTransaction();
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex is TransactionNotInstantiatedException);
            } 
        }
Exemplo n.º 15
0
        public void RollbackTransactionCallsExecutorRollBackTransaction()
        {
            var provider = new IProvider();
            var transaction = new Mock<ITransaction>();
            provider.CommandExecutor.Setup(c => c.InitiateTransaction()).Returns(transaction.Object);

            var target = new DataSession(provider.Object);
            target.BeginTransaction();
            target.RollBackTransaction();
            provider.CommandExecutor.Verify(c => c.InitiateTransaction());
            provider.CommandExecutor.Verify(c => c.RollbackTransaction(transaction.Object));
        }
Exemplo n.º 16
0
        public void InitiateTransactionMoreThanOnceThrowsTransactionInstantiatedAlreadyException()
        {
            var provider = new IProvider();
            var transaction = new Mock<ITransaction>();
            provider.CommandExecutor.Setup(c => c.InitiateTransaction()).Returns(transaction.Object);

            var target = new DataSession(provider.Object);
            try
            {
                target.BeginTransaction();
                target.BeginTransaction();
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex is TransactionInstantiatedAlreadyException);
            }
        }
Exemplo n.º 17
0
        public void GetListByProcedureThrowsArgumentNullException()
        {
            try
            {
                var provider = new IProvider();
                var session = new DataSession(provider.Object);

                Procedure procedure = null;
                session.GetList<List<Customer>, Customer>(procedure);
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex is ArgumentNullException);
            }
        }
Exemplo n.º 18
0
        public void ExecuteScalarByExpressionThrowsArgumentNullException()
        {
            var provider = new IProvider();
            var target = new DataSession(provider.Object);
            Expression<Func<Customer, bool>> expression = null;
            Expression<Func<Customer, object>> column = null;

            var result = false;
            try
            {
                target.ExecuteScalar(x => x.ID, expression);
            }
            catch (Exception ex)
            {
                result = ex is ArgumentNullException;
            }

            try
            {
                target.ExecuteScalar(column, x => x.ID == 1);
            }
            catch (Exception ex)
            {
                result = ex is ArgumentNullException;
            }

            Assert.IsTrue(result);
        }
Exemplo n.º 19
0
        public void GetTByKeyThrowsUnableToCreateCommandException()
        {
            var provider = new IProvider();
            provider.CommandBuilder.Setup(c => c.CreateGetObjectByKeyCommand<Customer>()).Returns(default(Data.ICommand));

            try
            {
                var target = new DataSession(provider.Object);
                target.GetT<Customer>("CUS00001");
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex is UnableToCreateCommandException);
            }
        }
Exemplo n.º 20
0
 public void ExecuteScalarByExpressionThrowsUnableToCreateCommandException()
 {
     try
     {
         Expression<Func<Customer, bool>> expression = x => x.ID == 1;
         Expression<Func<Customer, object>> column = c => c.ID;
         var provider = new IProvider();
         provider.CommandBuilder.Setup(c => c.CreateExecuteScalarCommand<Customer>(column, expression)).Returns(
             default(Data.ICommand));
         
         var target = new DataSession(provider.Object);
         target.ExecuteScalar(column, expression);
     }
     catch (Exception ex)
     {
         Assert.IsTrue(ex is UnableToCreateCommandException);
     }
 }
Exemplo n.º 21
0
        public void GetPagedListByExpression()
        {
            Expression<Func<Customer, bool>> expression = e => e.ID < 4;

            var customer1 = new Customer
            {
                ID = 1,
                Balance = 0,
                BalanceRate = 0,
                DefaultContactID = 1,
                Name = "Customer1",
                ProfileID = 1
            };
            var customer2 = new Customer
            {
                ID = 2,
                Balance = 0,
                BalanceRate = 0,
                DefaultContactID = 2,
                Name = "Customer2",
                ProfileID = 2
            };
            var customer3 = new Customer
            {
                ID = 3,
                Balance = 0,
                BalanceRate = 0,
                DefaultContactID = 3,
                Name = "Customer3",
                ProfileID = 3
            };

            var customerQueue = new Queue<Customer>(new[] { customer1, customer2, customer3 });
            var readerResults = new Queue<bool>(new[] { true, true, true, false });

            var reader = new DbDataReader();
            reader.Setup(r => r.Read()).Returns(readerResults.Dequeue);

            var provider = new IProvider();

            var command = new ICommand();

            var schema = new TypeTable(typeof(Customer));

            provider.Setup(p => p.GetSchema(typeof(Customer))).Returns(schema);

            provider.CommandBuilder.Setup(c => c.CreateGetListByPageCommand<Customer>(expression)).Returns(command.Object);
            provider.CommandExecutor.Setup(c => c.ExecuteReader(command.Object)).Returns(reader.Object);

            provider.Mapper.Setup(m => m.GetTMappingMethod<Customer>()).Returns(d => customerQueue.Dequeue());

            var target = new DataSession(provider.Object);

            var actual = target.GetPagedList<List<Customer>, Customer>(10, 1, expression);

            Assert.IsTrue(actual[0].ID == 1 &&
                actual[1].ID == 2 && actual[2].ID == 3);

        }
Exemplo n.º 22
0
        public void ExecuteScalarByExpressionThrowsInvalidCastException()
        {
            Expression<Func<Customer, bool>> expression = x => x.ID == 1;
            Expression<Func<Customer, int>> column = c => c.ID;
            var provider = new IProvider();
            var command = new ICommand();

            provider.CommandBuilder.Setup(c => c.CreateExecuteScalarCommand<Customer>(column, expression)).Returns(command.Object);
            provider.CommandExecutor.Setup(c => c.ExecuteScalar(command.Object)).Returns("HelloWorld");

            try
            {
                var target = new DataSession(provider.Object);
                var result = target.ExecuteScalar(column, expression);

                provider.CommandExecutor.Verify(c => c.FinalizeCommand(command.Object));
             
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex is InvalidCastException);
            }
            
        }
Exemplo n.º 23
0
        private static void GetPagedListByPageEnumExpression(int pageSize, Data.Page page, Expression<Func<Customer, bool>> expression)
        {
            var customer1 = new Customer
            {
                ID = 1,
                Balance = 0,
                BalanceRate = 0,
                DefaultContactID = 1,
                Name = "Customer1",
                ProfileID = 1
            };
            var customer2 = new Customer
            {
                ID = 2,
                Balance = 0,
                BalanceRate = 0,
                DefaultContactID = 2,
                Name = "Customer2",
                ProfileID = 2
            };
            var customer3 = new Customer
            {
                ID = 3,
                Balance = 0,
                BalanceRate = 0,
                DefaultContactID = 3,
                Name = "Customer3",
                ProfileID = 3
            };

            var customerQueue = new Queue<Customer>(new[] { customer1, customer2, customer3 });
            var readerResults = new Queue<bool>(new[] { true, true, true, false });

            var reader = new DbDataReader();
            reader.Setup(r => r.Read()).Returns(readerResults.Dequeue);

            var provider = new IProvider();

            var command = new ICommand();

            var schema = new TypeTable(typeof(Customer));

            provider.Setup(p => p.GetSchema(typeof(Customer))).Returns(schema);

            provider.CommandBuilder.Setup(c => c.CreateGetListByPageCommand<Customer>(expression)).Returns(command.Object);
            provider.CommandBuilder.Setup(c => c.CreateCountCommand<Customer>(expression)).Returns(command.Object);
            provider.CommandExecutor.Setup(c => c.ExecuteCount(command.Object)).Returns(3);
            provider.CommandExecutor.Setup(c => c.ExecuteReader(command.Object)).Returns(reader.Object);


            provider.Mapper.Setup(m => m.GetTMappingMethod<Customer>()).Returns(d => customerQueue.Dequeue());

            var target = new DataSession(provider.Object);

            try
            {
                var actual = target.GetPagedList<List<Customer>, Customer>(pageSize, page, expression);

                Assert.IsTrue(actual[0].ID == 1 &&
                    actual[1].ID == 2 && actual[2].ID == 3);
            }
            catch (Exception ex)
            {

                Assert.IsTrue(ex is ArgumentNullException);
            }

        }
Exemplo n.º 24
0
        public void ExecuteScalarByExpression()
        {
            Expression<Func<Customer, bool>> expression = x => x.ID == 1;
            Expression<Func<Customer, int>> column = c => c.ID;
            var provider = new IProvider();
            var command = new ICommand();

            provider.CommandBuilder.Setup(c => c.CreateExecuteScalarCommand<Customer>(column, expression)).Returns(command.Object);
            provider.CommandExecutor.Setup(c => c.ExecuteScalar(command.Object)).Returns(2);

            var target = new DataSession(provider.Object);
            var result = target.ExecuteScalar(column, expression);

            provider.CommandExecutor.Verify(c => c.FinalizeCommand(command.Object));
            Assert.AreEqual(result, 2);
        }
Exemplo n.º 25
0
        public void CountAllWithTransactionInitiated()
        {
            var provider = new IProvider();
            var command = new ICommand();
            var schema = new TypeTable(typeof(Customer));

            var transaction = new Mock<ITransaction>();

            provider.Setup(p => p.GetSchema(typeof(Customer))).Returns(schema);
            provider.CommandBuilder.Setup(c => c.CreateCountCommand<Customer>()).Returns(command.Object);
            provider.CommandExecutor.Setup(c => c.InitiateTransaction()).Returns(transaction.Object);
            provider.CommandExecutor.Setup(c => c.ExecuteCount(command.Object, transaction.Object)).Returns(10);

            var target = new DataSession(provider.Object);
            target.BeginTransaction();
            var result = target.Count<Customer>();
            target.CommitTransaction();
            Assert.IsTrue(result == 10);
        }
Exemplo n.º 26
0
        public void CountUsingLambda()
        {
            var provider = new IProvider();
            var command = new ICommand();
            var schema = new TypeTable(typeof(Customer));

            Expression<Func<Customer, bool>> expression = x => x.ID < 10;

            provider.Setup(p => p.GetSchema(typeof(Customer))).Returns(schema);
            provider.CommandBuilder.Setup(c => c.CreateCountCommand<Customer>(expression.Body)).Returns(command.Object);
            provider.CommandExecutor.Setup(c => c.ExecuteCount(command.Object)).Returns(10);

            var target = new DataSession(provider.Object);
            var result = target.Count(expression);
            Assert.IsTrue(result == 10);
        }