public void WillReturnQueryInfoIfTypeHasNoJoinAttributes() { QueryInfo expected = null; expected = ObjectCreator.CreateNew <QueryInfo>(); var strategy = new Mock <IQueryBuilderStrategy>(); strategy .Setup(x => x.BuildQuery <NoJoinAttributeClass>(It.IsAny <object>())) .Returns(expected); Mocks.Get <IQueryBuilderStrategyFactory>() .Setup(x => x.GetBuilderStrategy(QueryKind.SelectSingleTable)) .Returns(strategy.Object); dynamic parameters = new ExpandoObject(); Expression <Func <NoJoinAttributeClass, bool> > predicate = p => p.IsNotNull(); parameters.Predicate = predicate; parameters.CanDirtyRead = true; parameters.IncludeParameters = true; parameters.DesiredFields = null; parameters.TableName = null; var actual = SystemUnderTest.BuildQuery <NoJoinAttributeClass>(parameters); Asserter.AssertEquality(expected, actual, new[] { "Parameters", "tableObjectMappings" }); }
public void WillReturnParameterBasedOnName() { var parameterName = "SomeProperty"; var expected = "@someProperty"; var actual = SystemUnderTest.GetParameterName(parameterName); Asserter.AssertEquality(expected, actual); }
private void CompareParameters(SqlParameter expected, SqlParameter actual) { Asserter.AssertEquality(expected, actual, new[] { "Value", "SqlDbType", "DbType", "SqlValue", "SourceVersion", "CompareInfo", "Direction" }); Asserter.AssertEquality(expected.SqlDbType, actual.SqlDbType); }
private void CompareFieldParameterInfos(KeyValuePair <string, FieldParameterMapping> expected, KeyValuePair <string, FieldParameterMapping> actual, Action <object> valueAsserter) { Asserter.AssertEquality(expected.Key, actual.Key); Asserter.AssertEquality(expected.Value, actual.Value, new[] { "DbType", "Value" }); Asserter.AssertEquality(expected.Value.DbType, actual.Value.DbType); valueAsserter(actual.Value.Value); }
public void WillSetAlias() { string alias = null; alias = DataGenerator.GenerateString(); var actual = SystemUnderTest.BuildFields <TestClass>(alias: alias); Asserter.AssertEquality(alias, actual.Alias); }
public void WillSetTableName() { string tableName = null; tableName = DataGenerator.GenerateString(); var actual = SystemUnderTest.BuildFields <TestClass>(tableName: tableName); Asserter.AssertEquality(tableName, actual.TableName); }
public void WillReturnHashCodeOfObject() { string value = null; string expected = null; value = DataGenerator.GenerateString(); expected = value.GetHashCode().ToString(); var actual = SystemUnderTest.GenerateHash(value); Asserter.AssertEquality(expected, actual); }
public void WillBuildItemWithoutValueFactoriesAndNoPrefix() { TestClass expected = null; MockDataReader reader = null; expected = ObjectCreator.CreateNew <TestClass>(); reader = new MockDataReaderHelper().BuildMockDataReader(new[] { expected }); var actual = SystemUnderTest.Build <TestClass>(reader); Asserter.AssertEquality(expected, actual); }
public void WillBuildContainerWithPrefix() { Expression <Func <TestClass, bool> > expression = null; QueryContainer expected = null; var id = DataGenerator.GenerateInteger(); var testClass = ObjectCreator.CreateNew <TestClass>(); expression = t => t.Id == id && t.Bar == testClass.Bar || t.Foo == "Margaritas"; expected = new QueryContainer( "t1.t1_TestClassId = @id AND t1.t1_PioneerSquareBar = @itsFridayLetsGoToTheBar OR t1.t1_SomeFoo = @fooParameter", new List <IDbDataParameter> { new SqlParameter { ParameterName = "@id", SqlDbType = SqlDbType.Int, Value = id }, new SqlParameter { ParameterName = "@itsFridayLetsGoToTheBar", SqlDbType = SqlDbType.NVarChar, Value = testClass.Bar }, new SqlParameter { ParameterName = "@fooParameter", SqlDbType = SqlDbType.NVarChar, Value = "Margaritas" } }); var actual = SystemUnderTest.BuildContainer(expression, typeof(TestClass), tableAlias: "t1", fieldPrefix: "t1_"); Expression <Action <SqlParameter, SqlParameter> > compareExpression = (e, a) => CompareParameters(e, a); Asserter.AssertEquality(expected, actual, new[] { "Parameters", "OrderByClause" }); Asserter.AssertEquality( expected.Parameters.Select(p => (SqlParameter)p), actual.Parameters.Select(p => (SqlParameter)p), additionalParameters: new Dictionary <string, object> { { Norml.Tests.Common.Constants.ParameterNames.ComparisonDelegate, compareExpression } }); }
public void WillHandleEqualsNullClause() { QueryContainer expected = null; Expression <Func <TestClass, bool> > expression = null; expression = t => t.Bar.Equals(null); expected = new QueryContainer("PioneerSquareBar IS NULL"); var actual = SystemUnderTest.BuildContainer(expression, typeof(TestClass)); Asserter.AssertEquality(expected, actual, new[] { "Parameters", "OrderByClause" }); }
private void VerifyParameters(object expected, object actual) { Asserter.AssertEquality(expected, actual, new[] { "SqlDbType", "Value" }); var type = expected.GetType(); var property = type.GetProperty("SqlDbType"); Asserter.AssertEquality((SqlDbType)property.GetValue(expected), (SqlDbType)property.GetValue(actual)); }
public void WillBuildItemWithoutValueFactoriesAndPrefix() { TestClass expected = null; MockDataReader reader = null; var prefix = String.Empty; prefix = DataGenerator.GenerateString(); expected = ObjectCreator.CreateNew <TestClass>(); reader = new MockDataReaderHelper().BuildMockDataReader(new[] { expected }, prefix); var actual = SystemUnderTest.Build <TestClass>(reader, prefix); Asserter.AssertEquality(expected, actual); }
public void WillBuildCountQuery() { QueryInfo expected = null; const string query = "SELECT COUNT(Id) AS IdCount FROM dbo.SomeTable;"; expected = new QueryInfo(query); var actual = SystemUnderTest.BuildQuery <DummyCountClassWithCountAttributes>(It.IsAny <dynamic>()); Assert.IsNotNull(actual); Asserter.AssertEquality(expected.Query, actual.Query); Assert.IsNull(actual.Parameters); }
public void WillHandleDateTimeWithParameter() { Expression <Func <DateClass, bool> > expression = null; string expected = null; DateClass dateClass = null; dateClass = ObjectCreator.CreateNew <DateClass>(); var dateValue = dateClass.SomeDateTime; expression = d => d.Name == dateClass.Name && d.SomeDateTime.Day.Equals(dateValue.Day) && d.SomeDateTime.Month.Equals(dateValue.Month) && d.SomeDateTime.Year.Equals(dateValue.Year); var mapper = new Mock <IDataMapper>(); mapper .Setup(x => x.GetMappingForType(typeof(DateClass))) .Returns(GetDateMapping); Mocks .Get <IObjectMapperFactory>() .Setup(x => x.GetMapper(It.IsAny <MappingKind>())) .Returns(mapper.Object); expected = "SomeName = @someName AND DAY(SomeDate) = {0} AND MONTH(SomeDate) = {1} AND YEAR(SomeDate) = {2}" .FormatString(dateValue.Day.ToString(), dateValue.Month.ToString(), dateValue.Year.ToString()); var actual = SystemUnderTest.BuildContainer(expression, typeof(DateClass)); Expression <Action <SqlParameter, SqlParameter> > compareExpression = (e, a) => CompareParameters(e, a); Asserter.AssertEquality(new QueryContainer(expected), actual, propertiesToIgnore: new[] { "Parameters", "OrderByClause" }); Asserter.AssertEquality(new List <SqlParameter> { new SqlParameter("@someName", SqlDbType.NVarChar) { Value = dateClass.Name } }, actual.Parameters .Select(p => (SqlParameter)p), additionalParameters: new Dictionary <string, object> { { Norml.Tests.Common.Constants.ParameterNames.ComparisonDelegate, compareExpression } }); }
public void WillHandleIntEqualsClauses() { Expression <Func <TestClass, bool> > expression = null; QueryContainer expected = null; expression = t => t.Id.Equals(5); expected = new QueryContainer( "TestClassId = 5"); var actual = SystemUnderTest.BuildContainer(expression, typeof(TestClass)); Asserter.AssertEquality(expected, actual, new[] { "Parameters", "OrderByClause" }); }
public void WillHandleStringEqualsClauses() { Expression <Func <TestClass, bool> > expression = null; QueryContainer expected = null; expression = t => t.Bar.Equals("yikes"); expected = new QueryContainer( "PioneerSquareBar = 'yikes'"); var actual = SystemUnderTest.BuildContainer(expression, typeof(TestClass)); Asserter.AssertEquality(expected, actual, new[] { "Parameters", "OrderByClause" }); }
public void WillHandleStartsWithClauses() { Expression <Func <TestClass, bool> > expression = null; QueryContainer expected = null; expression = t => t.Bar.StartsWith("Hink"); expected = new QueryContainer( "PioneerSquareBar LIKE ('Hink%')"); var actual = SystemUnderTest.BuildContainer(expression, typeof(TestClass)); Asserter.AssertEquality(expected, actual, new[] { "Parameters", "OrderByClause" }); }
public void WillReturnExpressionIfParameterIsNull() { Expression <Func <object, object> > expected = null; var key = String.Empty; expected = e => null; key = DataGenerator.GenerateString(); SystemUnderTest.Delegates .Add(key, expected); var actual = SystemUnderTest.GetValueFactory(key); Asserter.AssertEquality("() => e => null", actual.ToString()); }
public void WillBuildQuery() { string expectedQuery = null; expectedQuery = "SET @sortColumn = LOWER(@sortColumn); SET @sortOrder = LOWER(@sortOrder); WITH SortedItems AS ( SELECT ClientId, FirstName, MiddleName, LastName, ROW_NUMBER() OVER ( ORDER BY CASE WHEN @sortOrder <> 'desc' THEN NULL WHEN @sortColumn = 'firstname' THEN FirstName END DESC, CASE WHEN @sortOrder <> 'asc' THEN NULL WHEN @sortColumn = 'firstname' THEN FirstName END ASC, CASE WHEN @sortOrder <> 'desc' THEN NULL WHEN @sortColumn = 'lastname' THEN LastName END DESC, CASE WHEN @sortOrder <> 'asc' THEN NULL WHEN @sortColumn = 'lastname' THEN LastName END ASC ) AS RowNumber, COUNT(ClientId) OVER() AS TotalRecords FROM dbo.Clients (NOLOCK) WHERE [FirstName] LIKE ('Jo%') ) SELECT ClientId, FirstName, MiddleName, LastName, [TotalRecords], ([TotalRecords] + @rowsPerPage - 1) / @rowsPerPage AS NumberOfPages FROM SortedItems WHERE SortedItems.RowNumber BETWEEN ((@pageNumber - 1) * @rowsPerPage) + 1 AND @rowsPerPage * @pageNumber AND [FirstName] LIKE ('Jo%') ORDER BY CASE WHEN @sortOrder <> 'desc' THEN NULL WHEN @sortColumn = 'firstname' THEN FirstName END DESC, CASE WHEN @sortOrder <> 'asc' THEN NULL WHEN @sortColumn = 'firstname' THEN FirstName END ASC, CASE WHEN @sortOrder <> 'desc' THEN NULL WHEN @sortColumn = 'lastname' THEN LastName END DESC, CASE WHEN @sortOrder <> 'asc' THEN NULL WHEN @sortColumn = 'lastname' THEN LastName END ASC ;"; Mocks.Get <IFieldHelper>() .Setup(x => x.BuildFields(It.IsAny <IEnumerable <string> >(), It.IsAny <string>(), It.IsAny <ClientDataModel>(), It.IsAny <bool>(), It.IsAny <string>(), It.IsAny <string>())) .Returns(new TableObjectMapping { TableName = "dbo.Clients", FieldMappings = new Dictionary <string, FieldParameterMapping> { { "Id", new FieldParameterMapping("ClientId", "@clientId", SqlDbType.UniqueIdentifier, isIdentity: true) }, { "FirstName", new FieldParameterMapping("FirstName", "@firstName", SqlDbType.NVarChar) }, { "MiddleName", new FieldParameterMapping("MiddleName", "@middleName", SqlDbType.NVarChar) }, { "LastName", new FieldParameterMapping("LastName", "@lastName", SqlDbType.NVarChar) } } }); Mocks.Get <IPredicateBuilder>() .Setup(x => x.BuildContainer(It.IsAny <Expression>(), It.IsAny <Type>(), It.IsAny <bool>(), It.IsAny <string>(), It.IsAny <string>())) .Returns(new QueryContainer("[FirstName] LIKE ('Jo%')")); dynamic parameters = new ExpandoObject(); Expression <Func <ClientDataModel, bool> > predicate = m => m.FirstName.StartsWith("Jo%"); parameters.PagingInfo = new PagingInfo { PageNumber = 2, RowsPerPage = 50, SortColumn = "firstname", SortOrder = "desc" }; parameters.Predicate = predicate; parameters.IncludeParameters = true; parameters.CanDirtyRead = true; parameters.DesiredFields = null; QueryInfo query = SystemUnderTest.BuildQuery <ClientDataModel>(parameters); Asserter.AssertEquality(expectedQuery, query.Query); Assert.AreEqual(4, query.Parameters.Count()); }
public void WillHandleEndsWithClausesWithPrefix() { Expression <Func <TestClass, bool> > expression = null; QueryContainer expected = null; expression = t => t.Bar.EndsWith("yikes"); expected = new QueryContainer( "t1.t1_PioneerSquareBar LIKE ('%yikes')"); var actual = SystemUnderTest.BuildContainer(expression, typeof(TestClass), tableAlias: "t1", fieldPrefix: "t1_"); Asserter.AssertEquality(expected, actual, new[] { "Parameters", "OrderByClause" }); }
public void WillConvertModelsToDataTable() { DatatableObjectMapping expected = null; IEnumerable <TestData> models = null; models = CreateEnumerableOfItems(() => ObjectCreator.CreateNew <TestData>(new Dictionary <string, object> { { "Baz", null } })); expected = new DatatableObjectMapping(BuildDataTable(models), new Dictionary <string, string> { { "Id", "Id" }, { "DateCreated", "DateCreated" }, { "DateLastUpdated", "DateLastUpdated" }, { "Foo", "Foo" }, { "Bar", "Bar" }, { "Baz", "Baz" } }); var actual = SystemUnderTest.ConvertToDataTable(models); Expression <Action <KeyValuePair <string, string>, KeyValuePair <string, string> > > expression = (e, a) => CompareKeyValuePairs(e, a); Assert.AreEqual(expected.DataTable.Columns.Count, actual.DataTable.Columns.Count); for (var index = 0; index < expected.DataTable.Columns.Count; index++) { Asserter.AssertEquality(expected.DataTable.Columns[index].ColumnName, actual.DataTable.Columns[index].ColumnName); Assert.AreEqual(expected.DataTable.Columns[index].DataType, actual.DataTable.Columns[index].DataType); } for (var index = 0; index < expected.DataTable.Rows.Count; index++) { Assert.AreEqual(expected.DataTable.Rows[index].ItemArray.Length, actual.DataTable.Rows[index].ItemArray.Length); for (var field = 0; field < expected.DataTable.Rows[index].ItemArray.Length; field++) { Assert.AreEqual(expected.DataTable.Rows[index].ItemArray[field], actual.DataTable.Rows[index].ItemArray[field]); } } Asserter.AssertEquality(expected.ColumnMappings, actual.ColumnMappings, additionalParameters: new Dictionary <string, object> { { Norml.Tests.Common.Constants.ParameterNames.ComparisonDelegate, expression } }); }
public void WillPopulateProperties() { var expectedJoinType = JoinType.None; Type expectedType = null; var expectedLeftKey = String.Empty; var expectedRightKey = String.Empty; var expectedJoinTable = String.Empty; var expectedJoinTableLeftKey = String.Empty; var expectedJoinTableRightKey = String.Empty; var expectedJoinTableJoinType = JoinType.None; var expectedParentProperty = String.Empty; var expectedChildProperty = String.Empty; TestRunner .DoCustomSetup(() => { expectedJoinType = JoinType.Left; expectedType = typeof(Object); expectedLeftKey = DataGenerator.GenerateString(); expectedRightKey = DataGenerator.GenerateString(); expectedJoinTable = DataGenerator.GenerateString(); expectedJoinTableLeftKey = DataGenerator.GenerateString(); expectedJoinTableRightKey = DataGenerator.GenerateString(); expectedJoinTableJoinType = JoinType.Right; expectedParentProperty = DataGenerator.GenerateString(); expectedChildProperty = DataGenerator.GenerateString(); }) .ExecuteTest(() => { var join = new JoinAttribute(expectedJoinType, expectedType, expectedLeftKey, expectedRightKey, expectedJoinTable, expectedJoinTableLeftKey, expectedJoinTableRightKey, expectedJoinTableJoinType, expectedParentProperty, expectedChildProperty); Asserter.AssertEquality(expectedJoinType, join.JoinType); Asserter.AssertEquality(expectedType.ToString(), join.JoinedType.ToString()); Asserter.AssertEquality(expectedLeftKey, expectedLeftKey); Asserter.AssertEquality(expectedRightKey, join.RightKey); Asserter.AssertEquality(expectedJoinTable, join.JoinTable); Asserter.AssertEquality(expectedJoinTableLeftKey, join.JoinTableLeftKey); Asserter.AssertEquality(expectedJoinTableRightKey, join.JoinTableRightKey); Asserter.AssertEquality(expectedJoinTableJoinType, join.JoinTableJoinType); Asserter.AssertEquality(expectedParentProperty, join.ParentProperty); Asserter.AssertEquality(expectedChildProperty, join.ChildProperty); }); }
public void WillReturnMappingBasedOnType() { var helper = Mocks.Get <IDataBuilderHelper>(); helper .Setup(x => x.InferDatabaseType(typeof(string))) .Returns(SqlDbType.NVarChar); helper .Setup(x => x.GetParameterName("NotDecorated")) .Returns("@notDecorated"); var expected = GetExpected(); var actual = SystemUnderTest.GetMappingForType(typeof(TestClass)); Asserter.AssertEquality(expected, actual, new[] { "PropertyMappings" }); Asserter.AssertEquality(expected.PropertyMappings, actual.PropertyMappings); }
public void WillReturnRightKeyIfChildPropertyPassedToConstructorIsEmpty() { var expected = String.Empty; TestRunner .DoCustomSetup(() => { expected = DataGenerator.GenerateString(); }) .ExecuteTest(() => { var attribute = new JoinAttribute(JoinType.Left, typeof(object), DataGenerator.GenerateString(), expected, childProperty: String.Empty); Asserter.AssertEquality(expected, attribute.RightKey); Asserter.AssertEquality(expected, attribute.ChildProperty); }); }
public void WillReturnParentPropertyIfParentPropertyPassedToConstructorHasValue() { var expected = String.Empty; TestRunner .DoCustomSetup(() => { expected = DataGenerator.GenerateString(); }) .ExecuteTest(() => { var attribute = new JoinAttribute(JoinType.Left, typeof(object), DataGenerator.GenerateString(), DataGenerator.GenerateString(), parentProperty: expected); Assert.AreNotEqual(expected, attribute.LeftKey); Asserter.AssertEquality(expected, attribute.ParentProperty); }); }
public void WillReturnExpressionWithParameter() { string expected = null; var key = String.Empty; var random = String.Empty; key = DataGenerator.GenerateString(); expected = DataGenerator.GenerateString(); random = DataGenerator.GenerateString(); Expression <Func <object, string> > expression = e => e.ToString() + random; SystemUnderTest.Delegates .Add(key, expression); var returnExpression = SystemUnderTest.GetValueFactory(key, new ParameterInfo(typeof(string), expected)); var actual = returnExpression.Compile()(); Asserter.AssertEquality(expected + random, actual.ToString()); }
public void WillHandleDateTimeWithPrefix() { Expression expression = null; string expected = null; var dateClass = ObjectCreator.CreateNew <DateClass>(); var parameterExpression = Expression.Parameter(typeof(DateClass), "d"); var propertyInfo = typeof(DateClass).GetProperty("SomeDateTime"); var memberExpression = Expression.MakeMemberAccess(parameterExpression, propertyInfo); var dateValue = dateClass.SomeDateTime; var dayExpression = Build(memberExpression, Expression.Constant(dateValue.Day), "Day", "Equals", typeof(Int32)); var monthExpression = Build(memberExpression, Expression.Constant(dateValue.Month), "Month", "Equals", typeof(Int32)); var yearExpression = Build(memberExpression, Expression.Constant(dateValue.Year), "Year", "Equals", typeof(Int32)); var andExpression = Expression.AndAlso(dayExpression, monthExpression); expression = Expression.AndAlso(andExpression, yearExpression); var mapper = new Mock <IDataMapper>(); mapper .Setup(x => x.GetMappingForType(typeof(DateClass))) .Returns(GetDateMapping); Mocks .Get <IObjectMapperFactory>() .Setup(x => x.GetMapper(It.IsAny <MappingKind>())) .Returns(mapper.Object); expected = "DAY(t1.t1_SomeDate) = {0} AND MONTH(t1.t1_SomeDate) = {1} AND YEAR(t1.t1_SomeDate) = {2}" .FormatString(dateValue.Day.ToString(), dateValue.Month.ToString(), dateValue.Year.ToString()); var actual = SystemUnderTest.BuildContainer(expression, typeof(DateClass), tableAlias: "t1", fieldPrefix: "t1_"); Asserter.AssertEquality(new QueryContainer(expected), actual, new[] { "Parameters", "OrderByClause" }); }
public void WillBuildItemWithValueFactories() { TestClass expected = null; MockDataReader reader = null; Mock <IValueFactory> valueFactory = null; var valueFactoryModel = ObjectCreator.CreateNew <TestClassWithValueFactories>(); expected = ObjectCreator.CreateNew <TestClass>(); reader = new MockDataReaderHelper().BuildMockDataReader(new [] { valueFactoryModel }); valueFactory = Mocks.Get <IValueFactory>(); Expression <Func <object> > fakeExpression = () => expected; valueFactory .Setup(x => x.GetValueFactory(It.IsAny <string>(), It.IsAny <ParameterInfo>())) .Returns(fakeExpression); var actual = SystemUnderTest.Build <TestClassWithValueFactories>(reader); valueFactory .Verify(x => x.GetValueFactory("TestClass1", It.IsAny <ParameterInfo>()), Times.Once); valueFactory .Verify(x => x.GetValueFactory("TestClass2", It.IsAny <ParameterInfo>()), Times.Once); Assert.IsFalse(actual.ValueFactories.IsNullOrEmpty()); Assert.IsTrue(actual.ValueFactories.ContainsKey("TestClass1")); Assert.IsNotNull(actual.ValueFactories["TestClass1"]); Asserter.AssertEquality(expected, actual.TestClass1); Assert.IsTrue(actual.ValueFactories.ContainsKey("TestClass2")); Assert.IsNotNull(actual.ValueFactories["TestClass2"]); Asserter.AssertEquality(expected, actual.TestClass2); }
public void WillBuildSelectQueryWithoutPredicate() { QueryInfo expected = null; var expectedQuery = "SELECT [TestClassId], [SomeFoo], [PioneerSquareBar] FROM dbo.TestTable (NOLOCK);"; expected = new QueryInfo(expectedQuery); Mocks.Get <IFieldHelper>() .Setup(x => x.BuildFields(It.IsAny <IEnumerable <string> >(), It.IsAny <string>(), It.IsAny <TestClass>(), It.IsAny <bool>(), It.IsAny <string>(), It.IsAny <string>())) .Returns(new TableObjectMapping { TableName = "dbo.TestTable", FieldMappings = new Dictionary <string, FieldParameterMapping> { { "Id", new FieldParameterMapping("TestClassId", "@id", SqlDbType.Int, DBNull.Value) }, { "Foo", new FieldParameterMapping("SomeFoo", "@fooParameter", SqlDbType.NVarChar, DBNull.Value) }, { "Bar", new FieldParameterMapping("PioneerSquareBar", "@itsFridayLetsGoToTheBar", SqlDbType.NVarChar, DBNull.Value) } } }); Expression <Func <TestClass, bool> > predicate = null; dynamic parameters = new ExpandoObject(); parameters.CanDirtyRead = true; parameters.IncludeParameters = true; parameters.DesiredFields = null; parameters.TableName = null; parameters.Predicate = predicate; QueryInfo actual = SystemUnderTest.BuildQuery <TestClass>(parameters); Asserter.AssertEquality(expected, actual, new[] { "Parameters", "tableObjectMappings" }); Assert.IsTrue(actual.Parameters.IsNullOrEmpty()); }
public void WillMixSimpleAndComplexJoins() { QueryInfo expected = null; const string expectedQuery = @"SELECT t1.ParentProperty1 AS t1_ParentProperty1, t2.ChildProperty1 AS t2_ChildProperty1, t2.ChildProperty2 AS t2_ChildProperty2, t2.ChildProperty3 AS t2_ChildProperty3, t3.ChildProperty1 AS t3_ChildProperty1, t3.ChildProperty2 AS t3_ChildProperty2, t3.ChildProperty3 AS t3_ChildProperty3, t4.ChildProperty1 AS t4_ChildProperty1, t4.ChildProperty2 AS t4_ChildProperty2, t4.ChildProperty3 AS t4_ChildProperty3 FROM dbo.Parent2 (NOLOCK) t1 INNER JOIN dbo.Child1 (NOLOCK) t2 ON t1.t1_ParentProperty1 = t2.t2_ChildProperty1 LEFT JOIN dbo.Child2 (NOLOCK) t3 ON t1.t1_ParentProperty1 = t3.t3_ChildProperty1 LEFT JOIN dbo.Parent2_Child1 (NOLOCK) ON t1.t1_ParentProperty1 = dbo.Parent2_Child1.ParentProperty1 LEFT JOIN dbo.Child3 (NOLOCK) t4 ON dbo.Parent2_Child1.ChildProperty1 = t4.t4_ChildProperty1;"; var fieldHelper = Mocks.Get <IFieldHelper>(); fieldHelper .Setup(x => x.BuildFields(It.IsAny <IEnumerable <string> >(), It.IsAny <string>(), It.IsAny <ParentClass2>(), It.IsAny <bool>(), "t1", It.IsAny <string>())) .Returns(new TableObjectMapping { TableName = "dbo.Parent2", FieldMappings = new Dictionary <string, FieldParameterMapping> { { "ParentProperty1", new FieldParameterMapping { FieldName = "ParentProperty1", Prefix = "t1", DbType = SqlDbType.Int } } }, Alias = "t1" }); fieldHelper .Setup(x => x.BuildFields(It.IsAny <IEnumerable <string> >(), It.IsAny <string>(), It.IsAny <Child1>(), It.IsAny <bool>(), "t2", It.IsAny <string>())) .Returns(new TableObjectMapping { TableName = "dbo.Child1", FieldMappings = new Dictionary <string, FieldParameterMapping> { { "ChildProperty1", new FieldParameterMapping { FieldName = "ChildProperty1", Prefix = "t2", DbType = SqlDbType.Int } }, { "ChildProperty2", new FieldParameterMapping { FieldName = "ChildProperty2", Prefix = "t2", DbType = SqlDbType.NVarChar } }, { "ChildProperty3", new FieldParameterMapping { FieldName = "ChildProperty3", Prefix = "t2", DbType = SqlDbType.NVarChar } } }, Alias = "t2" }); fieldHelper .Setup(x => x.BuildFields(It.IsAny <IEnumerable <string> >(), It.IsAny <string>(), It.IsAny <Child2>(), It.IsAny <bool>(), "t3", It.IsAny <string>())) .Returns(new TableObjectMapping { TableName = "dbo.Child2", FieldMappings = new Dictionary <string, FieldParameterMapping> { { "ChildProperty1", new FieldParameterMapping { FieldName = "ChildProperty1", Prefix = "t3", DbType = SqlDbType.Int } }, { "ChildProperty2", new FieldParameterMapping { FieldName = "ChildProperty2", Prefix = "t3", DbType = SqlDbType.NVarChar } }, { "ChildProperty3", new FieldParameterMapping { FieldName = "ChildProperty3", Prefix = "t3", DbType = SqlDbType.NVarChar } } }, Alias = "t3" }); fieldHelper .Setup(x => x.BuildFields(It.IsAny <IEnumerable <string> >(), It.IsAny <string>(), It.IsAny <Child3>(), It.IsAny <bool>(), "t4", It.IsAny <string>())) .Returns(new TableObjectMapping { TableName = "dbo.Child3", FieldMappings = new Dictionary <string, FieldParameterMapping> { { "ChildProperty1", new FieldParameterMapping { FieldName = "ChildProperty1", Prefix = "t4", DbType = SqlDbType.Int } }, { "ChildProperty2", new FieldParameterMapping { FieldName = "ChildProperty2", Prefix = "t4", DbType = SqlDbType.NVarChar } }, { "ChildProperty3", new FieldParameterMapping { FieldName = "ChildProperty3", Prefix = "t4", DbType = SqlDbType.NVarChar } } }, Alias = "t4" }); expected = new QueryInfo(expectedQuery); dynamic parameters = new ExpandoObject(); parameters.Predicate = null; parameters.CanDirtyRead = true; parameters.IncludeParameters = true; parameters.DesiredFields = null; parameters.TableName = null; QueryInfo actual = SystemUnderTest.BuildQuery <ParentClass2>(parameters); Asserter.AssertEquality(expected.Query, actual.Query); }