Skip to content

Commit

Permalink
Merge branch 'feature/Handling_errors_with_operands_(#423)' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Cédric L. Charlier committed Nov 13, 2018
2 parents 1557b18 + 5a486f7 commit 69e22ba
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 8 deletions.
20 changes: 12 additions & 8 deletions NBi.Core/Calculation/BasePredicateFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public ResultSet.ResultSet Apply(ResultSet.ResultSet rs)
return Apply(rs, (x => x));
}

protected ResultSet.ResultSet Apply(ResultSet.ResultSet rs, Func<bool,bool> onApply)
protected ResultSet.ResultSet Apply(ResultSet.ResultSet rs, Func<bool, bool> onApply)
{
var filteredRs = new ResultSet.ResultSet();
var table = rs.Table.Clone();
filteredRs.Load(table);
filteredRs.Table.Clear();

foreach (DataRow row in rs.Rows)
{
if (onApply(RowApply(row)))
Expand Down Expand Up @@ -74,11 +74,11 @@ protected object GetValueFromRow(DataRow row, IColumnIdentifier identifier)
}

var name = (identifier as ColumnNameIdentifier).Name;
var alias = aliases.SingleOrDefault(x => x.Name == name);
var alias = aliases.SingleOrDefault(x => string.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase));
if (alias != null)
return row.ItemArray[alias.Column];

var expression = expressions.SingleOrDefault(x => x.Name == name);
var expression = expressions.SingleOrDefault(x => string.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase));
if (expression != null)
{
var result = EvaluateExpression(expression, row);
Expand All @@ -88,16 +88,20 @@ protected object GetValueFromRow(DataRow row, IColumnIdentifier identifier)
var newColumn = new DataColumn(expColumnName, typeof(object));
row.Table.Columns.Add(newColumn);
}

row[expColumnName] = result;
return result;
}

var column = row.Table.Columns.Cast<DataColumn>().SingleOrDefault(x => x.ColumnName == name);
var column = row.Table.Columns.Cast<DataColumn>().SingleOrDefault(x => string.Equals(x.ColumnName, name, StringComparison.OrdinalIgnoreCase));
if (column != null)
return row[column.ColumnName];

throw new ArgumentException($"The value '{name}' is not recognized as a column name or a column position or a column alias or an expression.");
var existingNames = row.Table.Columns.Cast<DataColumn>().Select(x => x.ColumnName)
.Union(aliases.Select(x => x.Name)
.Union(expressions.Select(x => x.Name)));

throw new ArgumentException($"The value '{name}' is not recognized as a column position, a column name, a column alias or an expression. Possible arguments are: '{string.Join("', '", existingNames.ToArray())}'");
}

protected object EvaluateExpression(IColumnExpression expression, DataRow row)
Expand All @@ -107,7 +111,7 @@ protected object EvaluateExpression(IColumnExpression expression, DataRow row)

exp.EvaluateParameter += delegate (string name, NCalc.ParameterArgs args)
{
args.Result=GetValueFromRow(row, factory.Instantiate(name));
args.Result = GetValueFromRow(row, factory.Instantiate(name));
};

return exp.Evaluate();
Expand Down
56 changes: 56 additions & 0 deletions NBi.Testing/Unit/Core/Calculation/SinglePredicateFilterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,62 @@ public void Apply_ColumnName_CorrectResult()
Assert.That(result.Rows, Has.Count.EqualTo(2));
}

[Test]
public void Apply_ColumnNameCaseNotMatching_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(new ColumnNameIdentifier("FirST"));

var factory = new ResultSetFilterFactory(null);
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));
}


[Test]
public void Apply_UnexistingColumnName_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";
rs.Table.Columns[1].ColumnName = "second";
rs.Table.Columns[2].ColumnName = "third";

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(new ColumnNameIdentifier("Unexisting"));

var factory = new ResultSetFilterFactory(null);
var filter = factory.Instantiate(new IColumnAlias[0], new IColumnExpression[0], predicate.Object);
var ex = Assert.Throws<ArgumentException>(() => filter.Apply(rs));
Assert.That(ex.Message, Is.StringContaining("first"));
Assert.That(ex.Message, Is.StringContaining("second"));
Assert.That(ex.Message, Is.StringContaining("third"));
}

[Test]
public void Apply_NestedExpression_CorrectResult()
{
Expand Down

0 comments on commit 69e22ba

Please sign in to comment.