public void ExecuteShouldExecuteSuccessfullyWithFilter() { // Arrange const string expectedSql = "SELECT [Articles].[Id] FROM [Articles] WHERE (([Articles].[Id] = @Id_0))"; var filter = new ArticleFilter { ArticleId = 1 }; var dataReaderMock = CreateDataReaderMock(); var parameters = new List <IDbDataParameter>(); var parametersMock = new Mock <IDataParameterCollection>(MockBehavior.Strict); parametersMock.Setup(p => p.Contains("Id_0")).Returns(() => parameters.Count == 1); parametersMock.Setup(p => p.Add(It.IsAny <IDbDataParameter>())).Returns((object p) => { parameters.Add((IDbDataParameter)p); return(parameters.Count - 1); }); parametersMock.SetupGet(p => p.Count).Returns(() => parameters.Count); parametersMock.Setup(p => p.GetEnumerator()).Returns(() => parameters.GetEnumerator()); var commandMock = CreateCommandMock(dataReaderMock, expectedSql, parametersMock); var connectionMock = CreateConnectionMock(commandMock); var selectQuery = _queryBuilder.Select <Article>(a => a.Id); // Act selectQuery.Execute(filter, connectionMock.Object); // Assert commandMock.VerifySet(c => c.CommandText = expectedSql, Times.Once); commandMock.Verify(c => c.CreateParameter(), Times.Once); parametersMock.Verify(p => p.Add(It.IsAny <IDbDataParameter>()), Times.Once); }
public void GetQueryShouldGetQueryWithFilter() { // Arrange var filter = new ArticleFilter { ArticleId = 1 }; // Act var selectQuery = _queryBuilder.Select <Article>(a => a.Id); var query = selectQuery.GetQuery(filter); // Assert query.Text.Should().Be("SELECT [Articles].[Id] FROM [Articles] WHERE (([Articles].[Id] = @Id_0))"); query.Parameters.Should().HaveCount(1); }
public void GetQueryShouldGetQueryWithJoinAndFilter() { // Arrange var filter = new ArticleFilter { ArticleId = 1 }; // Act var selectQuery = _queryBuilder .Select <Article>(a => a.Id) .Join <ArticleType>(at => at.Name); var query = selectQuery.GetQuery(filter); // Assert query.Text.Should().Be("SELECT [Articles].[Id], [ArticleTypes].[Name] AS [ArticleTypeName] FROM [Articles] INNER JOIN [ArticleTypes] ON [Articles].[ArticleTypeId] = [ArticleTypes].[Id] WHERE (([Articles].[Id] = @Id_0))"); query.Parameters.Should().HaveCount(1); }