protected NBiConstraint InstantiateConstraint() { RowCountConstraint ctr; var childConstraint = BuildChildConstraint(ConstraintXml.Comparer); IResultSetFilter filter = null; if (ConstraintXml.Filter != null) { var filterXml = ConstraintXml.Filter; var expressions = new List <IColumnExpression>(); if (filterXml.Expression != null) { expressions.Add(filterXml.Expression); } var value = EvaluatePotentialVariable(ConstraintXml.Comparer.Value.Replace(" ", "")); var factory = new PredicateFilterFactory(); if (filterXml.Predication != null) { filter = factory.Instantiate ( filterXml.Aliases , expressions , filterXml.Predication ); } else if (filterXml.Combination != null) { filter = factory.Instantiate ( filterXml.Aliases , expressions , filterXml.Combination.Operator , filterXml.Combination.Predicates ); } if ((value is string& (value as string).EndsWith("%"))) { ctr = new RowCountFilterPercentageConstraint(childConstraint, filter); } else { ctr = new RowCountFilterConstraint(childConstraint, filter); } } else { ctr = new RowCountConstraint(childConstraint); } return(ctr); }
protected BasePredicateFilter InstantiateFilter() { var expressions = new List <IColumnExpression>(); if (ConstraintXml.Expression != null) { expressions.Add(ConstraintXml.Expression); } var factory = new PredicateFilterFactory(); if (ConstraintXml.Predication != null) { if (ConstraintXml.Predication.Reference != null) { ConstraintXml.Predication.Reference = EvaluatePotentialVariable(ConstraintXml.Predication.Reference); } return(factory.Instantiate ( ConstraintXml.Aliases , expressions , ConstraintXml.Predication )); } else if (ConstraintXml.Combination != null) { var predicateInfos = new List <IPredicateInfo>(); foreach (var predicateXml in ConstraintXml.Combination.Predicates) { if (predicateXml.Reference != null) { predicateXml.Reference = EvaluatePotentialVariable(predicateXml.Reference); } predicateInfos.Add(predicateXml); } return(factory.Instantiate ( ConstraintXml.Aliases , expressions , ConstraintXml.Combination.Operator , predicateInfos )); } else { throw new ArgumentException("You must specify a predicate or a combination of predicates. None of them is specified"); } }
public void Apply_ColumnIndex_CorrectResult() { var service = new ObjectsResultSetResolver( new ObjectsResultSetResolverArgs( new object[] { new List <object>() { "(null)", 10, 100 }, new List <object>() { "(empty)", 2, 75 }, new List <object>() { "C", 5, 50 } })); var rs = service.Execute(); var predicate = new Mock <IPredicateInfo>(); predicate.SetupGet(p => p.ColumnType).Returns(ColumnType.Text); predicate.SetupGet(p => p.ComparerType).Returns(ComparerType.NullOrEmpty); predicate.SetupGet(p => p.Operand).Returns("#0"); var factory = new PredicateFilterFactory(); var filter = factory.Instantiate(new IColumnAlias[0], new IColumnExpression[0], predicate.Object); var result = filter.Apply(rs); Assert.That(result.Rows, Has.Count.EqualTo(2)); Assert.That(filter.Describe(), Is.StringContaining("null").And.StringContaining("or empty")); }
public void Matches_AllValidatePredicate_False() { var rs = new ResultSet(); rs.Load(new[] { new object[] { "a", -1 }, new object[] { "b", -2 }, new object[] { "c", -3 } }); var predicate = new Mock <IPredicateInfo>(); predicate.SetupGet(p => p.ColumnType).Returns(ColumnType.Numeric); predicate.SetupGet(p => p.ComparerType).Returns(ComparerType.LessThan); predicate.SetupGet(p => p.Operand).Returns("#1"); predicate.As <IReferencePredicateInfo>().SetupGet(p => p.Reference).Returns((object)0); var factory = new PredicateFilterFactory(); var filter = factory.Instantiate ( new List <IColumnAlias>() , new List <IColumnExpression>() , predicate.Object ); var singleRowCtr = new SingleRowConstraint(filter); Assert.That(singleRowCtr.Matches(rs), Is.False); }
protected NBiConstraint InstantiateConstraint() { AllRowsConstraint ctr; var expressions = new List <IColumnExpression>(); if (ConstraintXml.Expression != null) { expressions.Add(ConstraintXml.Expression); } if (ConstraintXml.Predicate.Reference != null) { ConstraintXml.Predicate.Reference = EvaluatePotentialVariable(ConstraintXml.Predicate.Reference); } var factory = new PredicateFilterFactory(); var filter = factory.Instantiate ( ConstraintXml.Aliases , expressions , ConstraintXml.Predicate ); ctr = new AllRowsConstraint(filter); return(ctr); }
public void Apply_ColumnName_CorrectResult() { var builder = new ResultSetBuilder(); var rs = builder.Build(new object[] { new List <object>() { "(null)", 10, 100 }, new List <object>() { "(empty)", 2, 75 }, new List <object>() { "C", 5, 50 } }); rs.Table.Columns[0].ColumnName = "first"; var info = Mock.Of <IPredicateInfo> ( p => p.ComparerType == ComparerType.NullOrEmpty && p.ColumnType == ColumnType.Text && p.Name == "first" ); var factory = new PredicateFilterFactory(); var filter = factory.Instantiate(new IColumnAlias[0], new IColumnExpression[0], info); var result = filter.Apply(rs); Assert.That(result.Rows, Has.Count.EqualTo(2)); }
public void Apply_ColumnIndex_CorrectResult() { var builder = new ResultSetBuilder(); var rs = builder.Build(new object[] { new List <object>() { "(null)", 10, 100 }, new List <object>() { "(empty)", 2, 75 }, new List <object>() { "C", 5, 50 } }); var info = Mock.Of <IPredicateInfo> ( p => p.ComparerType == ComparerType.NullOrEmpty && p.ColumnType == ColumnType.Text && p.Name == "#0" ); var factory = new PredicateFilterFactory(); var filter = factory.Instantiate(new IColumnAlias[0], new IColumnExpression[0], info); var result = filter.Apply(rs); Assert.That(result.Rows, Has.Count.EqualTo(2)); Assert.That(filter.Describe(), Is.StringContaining("null").And.StringContaining("or empty")); }
public void Matches_AllValidatePredicate_False() { var rs = new ResultSet(); rs.Load(new[] { new object[] { "a", -1 }, new object[] { "b", -2 }, new object[] { "c", -3 } }); var predicate = Mock.Of <IPredicateInfo> ( p => p.ColumnType == ColumnType.Numeric && p.ComparerType == ComparerType.LessThan && p.Operand == "#1" && p.Reference == (object)0 ); var factory = new PredicateFilterFactory(); var filter = factory.Instantiate ( new List <IColumnAlias>() , new List <IColumnExpression>() , predicate ); var singleRowCtr = new SingleRowConstraint(filter); Assert.That(singleRowCtr.Matches(rs), Is.False); }
public void Apply_ColumnName_CorrectResult() { var service = new ObjectsResultSetResolver( new ObjectsResultSetResolverArgs( new object[] { new List <object>() { "(null)", 10, 100 }, new List <object>() { "(empty)", 2, 75 }, new List <object>() { "C", 5, 50 } })); var rs = service.Execute(); rs.Table.Columns[0].ColumnName = "first"; var predicate = new Mock <IPredicateInfo>(); predicate.SetupGet(p => p.ColumnType).Returns(ColumnType.Text); predicate.SetupGet(p => p.ComparerType).Returns(ComparerType.NullOrEmpty); predicate.SetupGet(p => p.Operand).Returns("first"); var factory = new PredicateFilterFactory(); var filter = factory.Instantiate(new IColumnAlias[0], new IColumnExpression[0], predicate.Object); var result = filter.Apply(rs); Assert.That(result.Rows, Has.Count.EqualTo(2)); }
private IEnumerable <Alter> InstantiateAlterations(ResultSetSystemXml resultSetXml) { if (resultSetXml.Alteration == null) { yield break; } if (resultSetXml.Alteration.Filters != null) { foreach (var filterXml in resultSetXml.Alteration.Filters) { var expressions = new List <IColumnExpression>(); if (filterXml.Expression != null) { expressions.Add(filterXml.Expression); } var factory = new PredicateFilterFactory(); if (filterXml.Predication != null) { yield return(factory.Instantiate ( filterXml.Aliases , expressions , filterXml.Predication ).Apply); } if (filterXml.Combination != null) { yield return(factory.Instantiate ( filterXml.Aliases , expressions , filterXml.Combination.Operator , filterXml.Combination.Predicates ).Apply); } } } }
public void Apply_AndWillNotEvaluateAll_CorrectResult() { var service = new ObjectsResultSetResolver( new ObjectsResultSetResolverArgs( new object[] { new List <object>() { null }, new List <object>() { 5 }, new List <object>() { 10 }, new List <object>() { null }, new List <object>() { 20 }, })); var rs = service.Execute(); var aliases = new[] { Mock.Of <IColumnAlias>(v => v.Column == 0 && v.Name == "a") }; var predicate1 = new Mock <IPredicateInfo>(); predicate1.SetupGet(p => p.ColumnType).Returns(ColumnType.Numeric); predicate1.SetupGet(p => p.ComparerType).Returns(ComparerType.Null); predicate1.SetupGet(p => p.Not).Returns(true); predicate1.SetupGet(p => p.Operand).Returns("#0"); var predicate2 = new Mock <IPredicateInfo>(); predicate2.SetupGet(p => p.ColumnType).Returns(ColumnType.Numeric); predicate2.SetupGet(p => p.ComparerType).Returns(ComparerType.LessThan); predicate2.SetupGet(p => p.Operand).Returns("#0"); predicate2.As <IReferencePredicateInfo>().SetupGet(p => p.Reference).Returns(10); var factory = new PredicateFilterFactory(); var filter = factory.Instantiate(aliases, new IColumnExpression[0], CombinationOperator.And, new[] { predicate1.Object, predicate2.Object }); var result = filter.Apply(rs); Assert.That(result.Rows, Has.Count.EqualTo(1)); }
public void Apply_XOr_CorrectResult() { var service = new ObjectsResultSetResolver( new ObjectsResultSetResolverArgs( new object[] { new List <object>() { "(null)", 10, 100 }, new List <object>() { "(empty)", 2, 75 }, new List <object>() { "(empty)", 20, 75 }, new List <object>() { "C", 5, 50 }, new List <object>() { "C", 15, 50 } })); var rs = service.Execute(); var aliases = new[] { Mock.Of <IColumnAlias>(v => v.Column == 0 && v.Name == "a") }; var predicate1 = new Mock <IPredicateInfo>(); predicate1.SetupGet(p => p.ColumnType).Returns(ColumnType.Text); predicate1.SetupGet(p => p.ComparerType).Returns(ComparerType.NullOrEmpty); predicate1.SetupGet(p => p.Operand).Returns("a"); var predicate2 = new Mock <IPredicateInfo>(); predicate2.SetupGet(p => p.ColumnType).Returns(ColumnType.Numeric); predicate2.SetupGet(p => p.ComparerType).Returns(ComparerType.LessThan); predicate2.SetupGet(p => p.Operand).Returns("#1"); predicate2.As <IReferencePredicateInfo>().SetupGet(p => p.Reference).Returns((object)10); var factory = new PredicateFilterFactory(); var filter = factory.Instantiate(aliases, new IColumnExpression[0], CombinationOperator.XOr, new[] { predicate1.Object, predicate2.Object }); var result = filter.Apply(rs); Assert.That(result.Rows, Has.Count.EqualTo(3)); }
public void Apply_Or_CorrectResult() { var service = new ObjectsResultSetResolver( new ObjectsResultSetResolverArgs( new object[] { new List <object>() { "(null)", 10, 100 }, new List <object>() { "(empty)", 2, 75 }, new List <object>() { "(empty)", 20, 75 }, new List <object>() { "C", 5, 50 }, new List <object>() { "C", 15, 50 } })); var rs = service.Execute(); var aliases = new[] { Mock.Of <IColumnAlias>(v => v.Column == 0 && v.Name == "a") }; var info1 = Mock.Of <IPredicateInfo> ( p => p.ComparerType == ComparerType.NullOrEmpty && p.ColumnType == ColumnType.Text && p.Operand == "a" ); var info2 = Mock.Of <IPredicateInfo> ( p => p.ComparerType == ComparerType.LessThan && p.ColumnType == ColumnType.Numeric && p.Operand == "#1" && p.Reference == (object)10 ); var factory = new PredicateFilterFactory(); var filter = factory.Instantiate(aliases, new IColumnExpression[0], CombinationOperator.Or, new[] { info1, info2 }); var result = filter.Apply(rs); Assert.That(result.Rows, Has.Count.EqualTo(4)); }
public void Apply_MixedExpression_CorrectResult() { var service = new ObjectsResultSetResolver( new ObjectsResultSetResolverArgs( new object[] { new List <object>() { 1, 10, 100 }, new List <object>() { 2, 2, 75 }, new List <object>() { 3, 5, 50 } })); var rs = service.Execute(); rs.Table.Columns[2].ColumnName = "c1"; var aliases = new List <IColumnAlias>() { Mock.Of <IColumnAlias>(v => v.Column == 0 && v.Name == "a"), }; var expressions = new List <IColumnExpression>() { Mock.Of <IColumnExpression>(e => e.Value == "Abs([a])+[e]" && e.Name == "d"), Mock.Of <IColumnExpression>(e => e.Value == "[#1]*[c1]" && e.Name == "e") }; var info = Mock.Of <IPredicateInfo> ( p => p.ComparerType == ComparerType.MoreThanOrEqual && p.ColumnType == ColumnType.Numeric && p.Operand == "d" && p.Reference == (object)200 ); var factory = new PredicateFilterFactory(); var filter = factory.Instantiate(aliases, expressions, info); var result = filter.Apply(rs); Assert.That(result.Rows, Has.Count.EqualTo(2)); }
public void Matches_SqlCommand_CallToResultSetBuilderOnce() { var resultSet = new ResultSet(); resultSet.Load("a;b;1"); var cmd = new SqlCommand(); var rsbMock = new Mock <ResultSetBuilder>(); rsbMock.Setup(engine => engine.Build(It.IsAny <object>())) .Returns(resultSet); var rsb = rsbMock.Object; var alias = Mock.Of <IColumnAlias>(v => v.Column == 2 && v.Name == "Value"); var predicate = Mock.Of <IPredicateInfo> ( p => p.ColumnType == ColumnType.Numeric && p.ComparerType == ComparerType.Equal && p.Name == "Value" && p.Reference == (object)1 ); var factory = new PredicateFilterFactory(); var filter = factory.Instantiate ( new List <IColumnAlias>() { alias } , new List <IColumnExpression>() { } , predicate ); var rowCount = new AllRowsConstraint(filter) { ResultSetBuilder = rsb }; rowCount.ResultSetBuilder = rsb; //Method under test rowCount.Matches(cmd); //Test conclusion rsbMock.Verify(engine => engine.Build(It.IsAny <object>()), Times.Once()); }
public void Apply_NestedExpression_CorrectResult() { var service = new ObjectsResultSetResolver( new ObjectsResultSetResolverArgs( new object[] { new List <object>() { 1, 10, 100 }, new List <object>() { 2, 2, 75 }, new List <object>() { 3, 5, 50 } })); var rs = service.Execute(); var aliases = new List <IColumnAlias>() { Mock.Of <IColumnAlias>(v => v.Column == 0 && v.Name == "a"), Mock.Of <IColumnAlias>(v => v.Column == 1 && v.Name == "b"), Mock.Of <IColumnAlias>(v => v.Column == 2 && v.Name == "c") }; var expressions = new List <IColumnExpression>() { Mock.Of <IColumnExpression>(e => e.Value == "Abs([a])+[e]" && e.Name == "d"), Mock.Of <IColumnExpression>(e => e.Value == "[b]*[c]" && e.Name == "e") }; var predicate = new Mock <IPredicateInfo>(); predicate.SetupGet(p => p.ColumnType).Returns(ColumnType.Numeric); predicate.SetupGet(p => p.ComparerType).Returns(ComparerType.MoreThanOrEqual); predicate.SetupGet(p => p.Operand).Returns("d"); predicate.As <IReferencePredicateInfo>().SetupGet(p => p.Reference).Returns((object)200); var factory = new PredicateFilterFactory(); var filter = factory.Instantiate(aliases, expressions, predicate.Object); var result = filter.Apply(rs); Assert.That(result.Rows, Has.Count.EqualTo(2)); }
public void Apply_NestedExpression_CorrectResult() { var builder = new ResultSetBuilder(); var rs = builder.Build(new object[] { new List <object>() { 1, 10, 100 }, new List <object>() { 2, 2, 75 }, new List <object>() { 3, 5, 50 } }); var aliases = new List <IColumnAlias>() { Mock.Of <IColumnAlias>(v => v.Column == 0 && v.Name == "a"), Mock.Of <IColumnAlias>(v => v.Column == 1 && v.Name == "b"), Mock.Of <IColumnAlias>(v => v.Column == 2 && v.Name == "c") }; var expressions = new List <IColumnExpression>() { Mock.Of <IColumnExpression>(e => e.Value == "Abs([a])+[e]" && e.Name == "d"), Mock.Of <IColumnExpression>(e => e.Value == "[b]*[c]" && e.Name == "e") }; var info = Mock.Of <IPredicateInfo> ( p => p.ComparerType == ComparerType.MoreThanOrEqual && p.ColumnType == ColumnType.Numeric && p.Name == "d" && p.Reference == (object)200 ); var factory = new PredicateFilterFactory(); var filter = factory.Instantiate(aliases, expressions, info); var result = filter.Apply(rs); Assert.That(result.Rows, Has.Count.EqualTo(2)); }
public void Matches_ResultSetService_CallToExecuteOnce() { var resultSet = new ResultSet(); resultSet.Load("a;b;1"); var serviceMock = new Mock <IResultSetService>(); serviceMock.Setup(s => s.Execute()) .Returns(resultSet); var service = serviceMock.Object; var alias = Mock.Of <IColumnAlias>(v => v.Column == 2 && v.Name == "Value"); var predicate = Mock.Of <IPredicateInfo> ( p => p.ColumnType == ColumnType.Numeric && p.ComparerType == ComparerType.Equal && p.Operand == "Value" && p.Reference == (object)1 ); var factory = new PredicateFilterFactory(); var filter = factory.Instantiate ( new List <IColumnAlias>() { alias } , new List <IColumnExpression>() { } , predicate ); var singleRowCtr = new SingleRowConstraint(filter); //Method under test singleRowCtr.Matches(service); //Test conclusion serviceMock.Verify(s => s.Execute(), Times.Once()); }
protected NBiConstraint InstantiateConstraint() { var expressions = new List <IColumnExpression>(); if (ConstraintXml.Expression != null) { expressions.Add(ConstraintXml.Expression); } var factory = new PredicateFilterFactory(); var filter = factory.Instantiate ( ConstraintXml.Aliases , expressions , ConstraintXml.Predicate ); var ctr = new NoRowsConstraint(filter); return(ctr); }
protected NBiConstraint InstantiateConstraint() { RowCountConstraint ctr; var childConstraint = BuildChildConstraint(ConstraintXml.Comparer); IResultSetFilter filter = null; if (ConstraintXml.Filter != null) { var filterXml = ConstraintXml.Filter; var expressions = new List <IColumnExpression>(); if (filterXml.Expression != null) { expressions.Add(filterXml.Expression); } var factory = new PredicateFilterFactory(); filter = factory.Instantiate ( filterXml.Aliases , expressions , filterXml.Predicate ); if (ConstraintXml.Comparer.Value.Replace(" ", "").EndsWith("%")) { ctr = new RowCountFilterPercentageConstraint(childConstraint, filter); } else { ctr = new RowCountFilterConstraint(childConstraint, filter); } } else { ctr = new RowCountConstraint(childConstraint); } return(ctr); }
public void Apply_Variable_CorrectResult() { var service = new ObjectsResultSetResolver( new ObjectsResultSetResolverArgs( new object[] { new List <object>() { "(null)", 10, 100 }, new List <object>() { "(empty)", 2, 75 }, new List <object>() { "C", 5, 50 } })); var rs = service.Execute(); var aliases = new[] { Mock.Of <IColumnAlias>(v => v.Column == 0 && v.Name == "a") }; var info = Mock.Of <IPredicateInfo> ( p => p.ComparerType == ComparerType.NullOrEmpty && p.ColumnType == ColumnType.Text && p.Operand == "a" ); var factory = new PredicateFilterFactory(); var filter = factory.Instantiate(aliases, new IColumnExpression[0], info); var result = filter.Apply(rs); Assert.That(result.Rows, Has.Count.EqualTo(2)); }
public IEnumerable <Alter> InstantiateAlterations(ResultSetSystemXml resultSetXml) { if (resultSetXml.Alteration == null) { yield break; } if (resultSetXml.Alteration.Filters != null) { foreach (var filterXml in resultSetXml.Alteration.Filters) { var expressions = new List <IColumnExpression>(); if (filterXml.Expression != null) { expressions.Add(filterXml.Expression); } var factory = new PredicateFilterFactory(); if (filterXml.Predication != null) { yield return(factory.Instantiate ( filterXml.Aliases , expressions , filterXml.Predication ).Apply); } if (filterXml.Combination != null) { yield return(factory.Instantiate ( filterXml.Aliases , expressions , filterXml.Combination.Operator , filterXml.Combination.Predicates ).Apply); } } } if (resultSetXml.Alteration.Conversions != null) { foreach (var conversionXml in resultSetXml.Alteration.Conversions) { var factory = new ConverterFactory(); var converter = factory.Instantiate(conversionXml.Converter.From, conversionXml.Converter.To, conversionXml.Converter.DefaultValue, conversionXml.Converter.Culture); var engine = new ConverterEngine(conversionXml.Column, converter); yield return(engine.Execute); } } if (resultSetXml.Alteration.Transformations != null) { var provider = new TransformationProvider(); foreach (var transformationXml in resultSetXml.Alteration.Transformations) { provider.Add(transformationXml.ColumnIndex, transformationXml); } yield return(provider.Transform); } }