Example #1
0
        public void CompileShouldSelectWithColumnSpecificationAndJoins()
        {
            // Arrange
            Expression <Func <Article, object> >[] fields = { a => a.Id };
            const string expectedQuery = "SELECT [Articles].[Id], [ArticleTypes].[Name] AS [ArticleTypeName] FROM [Articles] INNER JOIN [ArticleTypes] ON [Articles].[ArticleTypeId] = [ArticleTypes].[Id]";

            var articleClassMapper     = new ArticleClassMapper();
            var articleTypeClassMapper = new ArticleTypeClassMapper();

            _classMapperFactoryMock.Setup(f => f.Get <Article>()).Returns(articleClassMapper);
            _classMapperFactoryMock.Setup(f => f.Get(typeof(Article))).Returns(articleClassMapper);
            _classMapperFactoryMock.Setup(f => f.Get(typeof(ArticleType))).Returns(articleTypeClassMapper);

            var queryBuilder = new SelectQueryBuilder <Article, Article>(_classMapperFactoryMock.Object, _predicateFactoryMock.Object, _predicateQueryBuilderFactoryMock.Object, fields);

            queryBuilder.Join <ArticleType>(at => at.Name);

            // Act
            var query = queryBuilder.Compile();

            // Assert
            GetPrivate <bool>(query, "_compiled").Should().BeTrue();
            GetPrivate <string>(query, "_query").Should().Be(expectedQuery);

            _classMapperFactoryMock.Verify(f => f.Get <Article>(), Times.Exactly(2));
            _classMapperFactoryMock.Verify(f => f.Get(typeof(Article)), Times.Once);
            _classMapperFactoryMock.Verify(f => f.Get(typeof(ArticleType)), Times.Exactly(2));
        }
Example #2
0
        public void CompileShouldCompileSelect()
        {
            // Arrange
            const string expectedQuery = "SELECT [Articles].[Id], [Articles].[ArticleTypeId], [Articles].[Name] FROM [Articles]";

            var articleClassMapper = new ArticleClassMapper();

            _classMapperFactoryMock.Setup(f => f.Get <Article>()).Returns(articleClassMapper);
            _classMapperFactoryMock.Setup(f => f.Get(typeof(Article))).Returns(articleClassMapper);

            var queryBuilder = new SelectQueryBuilder <Article, Article>(_classMapperFactoryMock.Object, _predicateFactoryMock.Object, _predicateQueryBuilderFactoryMock.Object);

            // Act
            var query = queryBuilder.Compile();

            // Assert
            GetPrivate <bool>(query, "_compiled").Should().BeTrue();
            GetPrivate <string>(query, "_query").Should().Be(expectedQuery);

            _classMapperFactoryMock.Verify(f => f.Get <Article>(), Times.Once);
            _classMapperFactoryMock.Verify(f => f.Get(typeof(Article)), Times.Once);
        }