-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from danielearwicker/safeLiteralFilterValues
Safe literal filter values
- Loading branch information
Showing
2 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
server/dotnet/FlowerBI.Engine.Tests/FilterParametersTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
namespace FlowerBI.Engine.Tests; | ||
|
||
using FluentAssertions; | ||
using Xunit; | ||
|
||
public class FilterParametersTests | ||
{ | ||
private static Filter MakeFilter(object val) | ||
=> new(new LabelledColumn("x", new Column<string>("c1")), "=", val, null); | ||
|
||
[Fact] | ||
public void DapperFilterParameters_String_GeneratesActualParam() | ||
{ | ||
var p = new DapperFilterParameters(); | ||
var result = p[MakeFilter("hi")]; | ||
result.Should().Be("@filter0"); | ||
p.DapperParams.ParameterNames.Should().BeEquivalentTo(["filter0"]); | ||
} | ||
|
||
[Fact] | ||
public void DapperFilterParameters_ListWithString_GeneratesActualParam() | ||
{ | ||
var p = new DapperFilterParameters(); | ||
var result = p[MakeFilter("hi")]; | ||
result.Should().Be("@filter0"); | ||
p.DapperParams.ParameterNames.Should().BeEquivalentTo(["filter0"]); | ||
} | ||
|
||
[Theory] | ||
[InlineData(42, "42")] | ||
[InlineData((short)42, "42")] | ||
[InlineData((long)42, "42")] | ||
[InlineData(3.14, "3.14")] | ||
[InlineData((float)3.14, "3.14")] | ||
[InlineData(true, "1")] | ||
[InlineData(false, "0")] | ||
public void DapperFilterParameters_SimpleNumber_GeneratesLiteral(object val, string expected) | ||
{ | ||
var p = new DapperFilterParameters(); | ||
var result = p[MakeFilter(val)]; | ||
result.Should().Be(expected); | ||
p.DapperParams.ParameterNames.Should().BeEmpty(); | ||
} | ||
|
||
[Fact] | ||
public void DapperFilterParameters_Decimal_GeneratesLiteral() | ||
{ | ||
var p = new DapperFilterParameters(); | ||
var result = p[MakeFilter(3.14m)]; | ||
result.Should().Be("3.14"); | ||
p.DapperParams.ParameterNames.Should().BeEmpty(); | ||
} | ||
|
||
[Theory] | ||
[InlineData(42, "(42, 42)")] | ||
[InlineData((short)42, "(42, 42)")] | ||
[InlineData((long)42, "(42, 42)")] | ||
[InlineData(3.14, "(3.14, 3.14)")] | ||
[InlineData((float)3.14, "(3.14, 3.14)")] | ||
[InlineData(true, "(1, 1)")] | ||
[InlineData(false, "(0, 0)")] | ||
public void DapperFilterParameters_SimpleNumberList_GeneratesLiteral(object val, string expected) | ||
{ | ||
var p = new DapperFilterParameters(); | ||
var result = p[MakeFilter(new[] {val, val})]; | ||
result.Should().Be(expected); | ||
p.DapperParams.ParameterNames.Should().BeEmpty(); | ||
} | ||
|
||
[Fact] | ||
public void DapperFilterParameters_DecimalList_GeneratesLiteral() | ||
{ | ||
var p = new DapperFilterParameters(); | ||
var result = p[MakeFilter(new object[] {3.14, 8, (short)3})]; | ||
result.Should().Be("(3.14, 8, 3)"); | ||
p.DapperParams.ParameterNames.Should().BeEmpty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters