Пример #1
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);
        }
Пример #2
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);
        }
Пример #3
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);
        }
Пример #4
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);
            }

        }
Пример #5
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);

        }
Пример #6
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);

        }
Пример #7
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);
        }
Пример #8
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);
            }
            
        }
Пример #9
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);
        }
Пример #10
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);
            }
            
            
        }
Пример #11
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);
        }