Пример #1
0
        public void AppendMemberAssignmentThrowsInvalidOperationExceptionOnDuplicateMemberAssignement()
        {
            // Arrange
            IQueryable <TestClass> queryable = new List <TestClass>().AsQueryable();

            var statementBuilder = new UpdateStatementBuilder <TestClass>(queryable);

            MemberAssignment assignment
                = Expression.Bind(
                      typeof(TestClass).GetProperty(nameof(TestClass.Id)),
                      Expression.Constant(3)
                      );

            // Act and Assert
            statementBuilder.AppendMemberAssignment(assignment);
            Assert.Throws <InvalidOperationException>(
                () => statementBuilder.AppendMemberAssignment(assignment)
                );
        }
Пример #2
0
        public void AppendMemberAssignmentThrowsArgumentNullExceptionWhenExpressionIsNull()
        {
            // Arrange
            IQueryable <TestClass> queryable = new List <TestClass>().AsQueryable();

            var statementBuilder = new UpdateStatementBuilder <TestClass>(queryable);

            // Act and Assert
            Assert.Throws <ArgumentNullException>(
                () => statementBuilder.AppendMemberAssignment(null)
                );
        }
Пример #3
0
        public void AppendMemberAssignmentThrowsArgumentExceptionWhenMemberIsNotOfTheRightType()
        {
            // Arrange
            IQueryable <TestClass> queryable = new List <TestClass>().AsQueryable();

            var statementBuilder = new UpdateStatementBuilder <TestClass>(queryable);

            MemberAssignment assignment
                = Expression.Bind(
                      typeof(OtherClass).GetProperty(nameof(OtherClass.Id)),
                      Expression.Constant(3)
                      );

            // Act and Assert
            Assert.Throws <ArgumentException>(
                () => statementBuilder.AppendMemberAssignment(assignment)
                );
        }
Пример #4
0
        public static int Update <T>(this IQueryable <T> queryable, Expression <Func <T, T> > constructorExpression) where T : class
        {
            // Check & Cast parameters
            Check.NotNull(queryable, nameof(queryable));

            MemberInitExpression memberInit
                = constructorExpression.Body as MemberInitExpression
                  ?? throw new ArgumentException(nameof(constructorExpression), $"The body of the expression must be of type\"{typeof(MemberInitExpression).Name}\".");

            // Create statement
            var statementBuilder = new UpdateStatementBuilder <T>(queryable);

            foreach (MemberAssignment assignement in memberInit.Bindings)
            {
                statementBuilder.AppendMemberAssignment(assignement);
            }

            // Act
            return(Update <T>(statementBuilder));
        }
Пример #5
0
        public void AppendMemberAssignmentShouldAppendAssignment()
        {
            // Arrange
            IQueryable <TestClass> queryable = new List <TestClass>().AsQueryable();

            var statementBuilder = new UpdateStatementBuilder <TestClass>(queryable);

            MemberAssignment assignment
                = Expression.Bind(
                      typeof(TestClass).GetProperty(nameof(TestClass.Id)),
                      Expression.Constant(3)
                      );

            // Act
            statementBuilder.AppendMemberAssignment(assignment);

            // Assert
            Assert.Single(statementBuilder.Assignements);
            Assert.Same(assignment, statementBuilder.Assignements.First());
        }