Skip to content

Commit

Permalink
Add test PBIX file, refactor unit tests (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
albertospelta authored Mar 1, 2024
1 parent ab23094 commit 1db8666
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 45 deletions.
Binary file added assets/ObfuscatorTest.pbix
Binary file not shown.
47 changes: 20 additions & 27 deletions tests/Dax.Vpax.Obfuscator.Tests/DaxModelDeobfuscatorTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq.Expressions;
using Dax.Metadata;
using Dax.Metadata;
using Dax.Vpax.Obfuscator.Common;
using Dax.Vpax.Obfuscator.Tests.TestUtils;
using Dax.Tokenizer;
using Xunit;

namespace Dax.Vpax.Obfuscator.Tests;
Expand Down Expand Up @@ -51,8 +47,8 @@ public void DeobfuscateExpression_CalendarDateColumn_IsNotDeobfuscatedBecauseItI
[Fact]
public void DeobfuscateExpression_TableConstructorColumnName_IsNotDeobfuscatedBecauseItIsNotObfuscated_SingleColumnTest()
{
var expression = """ SELECTCOLUMNS({ (0) }, "XXX", [Value]) """;
var expected = """ SELECTCOLUMNS({ (0) }, "@c1", [Value]) """;
var expression = """ SELECTCOLUMNS({0}, "XXX", [Value]) """;
var expected = """ SELECTCOLUMNS({0}, "@c1", [Value]) """;

var (_, _, deobfuscator) = CreateTest(
[
Expand All @@ -66,8 +62,8 @@ public void DeobfuscateExpression_TableConstructorColumnName_IsNotDeobfuscatedBe
[Fact]
public void DeobfuscateExpression_TableConstructorColumnName_IsNotDeobfuscatedBecauseItIsNotObfuscated_MultipleColumnsTest()
{
var expression = """ SELECTCOLUMNS({ (1,2,3) }, "XXX", [Value1], "YYY", [value2], "ZZZ", [VALUE3]) """;
var expected = """ SELECTCOLUMNS({ (1,2,3) }, "@c1", [Value1], "@c2", [value2], "@c3", [VALUE3]) """;
var expression = """ SELECTCOLUMNS({(1,2,3)}, "XXX", [Value1], "YYY", [value2], "ZZZ", [VALUE3]) """;
var expected = """ SELECTCOLUMNS({(1,2,3)}, "@c1", [Value1], "@c2", [value2], "@c3", [VALUE3]) """;

var (_, _, deobfuscator) = CreateTest(
[
Expand All @@ -81,14 +77,13 @@ public void DeobfuscateExpression_TableConstructorColumnName_IsNotDeobfuscatedBe
}

[Fact]
public void DeobfuscateExpression_ExtensionColumnNameWithDifferentCasings_ReturnsSameDeobfuscatedValue()
public void DeobfuscateExpression_ExtensionColumnNameMultipleReferencesWithDifferentCasings_ReturnsSameDeobfuscatedValue()
{
var expression = """ SUMX(ADDCOLUMNS(Date, "XXXXXXX", 1), [XXXXXXX]) """;
var expected = """ SUMX(ADDCOLUMNS(Date, "@column", 1), [@COLUMN]) """;
var expression = """ SUMX(ADDCOLUMNS({}, "XXXXXXX", 1), [XXXXXXX]) """;
var expected = """ SUMX(ADDCOLUMNS({}, "@column", 1), [@COLUMN]) """;

var (_, _, deobfuscator) = CreateTest(
[
new ObfuscationText("Date", "Date"),
new ObfuscationText("@column", "XXXXXXX"),
]);
var actual = deobfuscator.DeobfuscateExpression(expression);
Expand All @@ -97,15 +92,14 @@ public void DeobfuscateExpression_ExtensionColumnNameWithDifferentCasings_Return
}

[Fact]
public void DeobfuscateExpression_ExtensionColumnName_ReturnsDeobfuscatedTableNameAndColumnNameParts()
public void DeobfuscateExpression_ExtensionColumnNameFullyQualified_ReturnsDeobfuscatedColumnNameParts()
{
var expression = """ SUMX(ADDCOLUMNS(Date, "XXXXXX[Y]", 1), XXXXXX[Y]) """;
var expected = """ SUMX(ADDCOLUMNS(Date, "__rate[%]", 1), __rate[%]) """;
var expression = """ SUMX(ADDCOLUMNS({}, "XXXX[Y]", 1), XXXX[Y]) """;
var expected = """ SUMX(ADDCOLUMNS({}, "rate[%]", 1), rate[%]) """;

var (_, _, deobfuscator) = CreateTest(
[
new ObfuscationText("Date", "Date"),
new ObfuscationText("__rate", "XXXXXX"),
new ObfuscationText("rate", "XXXX"),
new ObfuscationText("%", "Y"),
]);
var actual = deobfuscator.DeobfuscateExpression(expression);
Expand All @@ -114,14 +108,13 @@ public void DeobfuscateExpression_ExtensionColumnName_ReturnsDeobfuscatedTableNa
}

[Fact]
public void DeobfuscateExpression_ExtensionColumnName_ReturnsDeobfuscatedValuePreservingEscapeChar()
public void DeobfuscateExpression_ExtensionColumnNameFullyQualified_ReturnsDeobfuscatedColumnNamePartsPreservingQuotationMarkEscapeChar()
{
var expression = """ SELECTCOLUMNS(ADDCOLUMNS(Date, "XXX[Y""Y]", 1), XXX[Y"Y]) """;
var expected = """ SELECTCOLUMNS(ADDCOLUMNS(Date, "aaa[b""c]", 1), aaa[b"c]) """;
var expression = """ SELECTCOLUMNS(ADDCOLUMNS({}, "XXX[Y""Y]", 1), XXX[Y"Y]) """;
var expected = """ SELECTCOLUMNS(ADDCOLUMNS({}, "aaa[b""c]", 1), aaa[b"c]) """;

var (_, _, deobfuscator) = CreateTest(
[
new ObfuscationText("Date", "Date"),
new ObfuscationText("aaa", "XXX"),
new ObfuscationText("b\"c", "Y\"Y"),
]);
Expand All @@ -131,7 +124,7 @@ public void DeobfuscateExpression_ExtensionColumnName_ReturnsDeobfuscatedValuePr
}

[Fact]
public void DeobfuscateExpression_TableNameWithDifferentCasings_ReturnsSameDeobfuscatedValue()
public void DeobfuscateExpression_TableNameMultipleReferencesWithDifferentCasings_ReturnsSameDeobfuscatedValue()
{
var expression = "COUNTROWS('XXXXX') + COUNTROWS(XXXXX) + COUNTROWS(XXXXX) + COUNTROWS(XXXXX)";
var expected = "COUNTROWS('Sales') + COUNTROWS(sales) + COUNTROWS(SALES) + COUNTROWS(SaLeS)";
Expand All @@ -146,7 +139,7 @@ public void DeobfuscateExpression_TableNameWithDifferentCasings_ReturnsSameDeobf
}

[Fact]
public void DeobfuscateExpression_ColumnNameWithEscapeSquareBracket_ReturnsDeobfuscatedValuePreservingEscapeChar()
public void DeobfuscateExpression_ColumnName_ReturnsDeobfuscatedValuePreservingSquareBracketEscapeChar()
{
var expression = "RELATED( XXXXX[YYYYYY]]] )";
var expected = "RELATED( Sales[Rate[%]]] )";
Expand All @@ -162,7 +155,7 @@ public void DeobfuscateExpression_ColumnNameWithEscapeSquareBracket_ReturnsDeobf
}

[Fact]
public void DeobfuscateExpression_VariableNameWithDifferentCasings_ReturnsSameDeobfuscatedValue()
public void DeobfuscateExpression_VariableNameMultipleReferencesWithDifferentCasings_ReturnsSameDeobfuscatedValue()
{
var expression = "VAR XXXXXX = 1 RETURN XXXXXX + XXXXXX + XXXXXX";
var expected = "VAR Amount = 1 RETURN AMOUNT + AmOuNt + amount";
Expand All @@ -179,8 +172,8 @@ public void DeobfuscateExpression_VariableNameWithDifferentCasings_ReturnsSameDe
[Fact]
public void ObfuscateExpression_ValueExtensionColumnName_IsNotObfuscated()
{
var expression = """ SELECTCOLUMNS ( { 1 }, "XXXXXXXXXX", ''[Value]) """;
var expected = """ SELECTCOLUMNS ( { 1 }, "__Measures", ''[Value]) """;
var expression = """ SELECTCOLUMNS({0}, "XXXXXXXXXX", ''[Value]) """;
var expected = """ SELECTCOLUMNS({0}, "__Measures", ''[Value]) """;

var (_, _, deobfuscator) = CreateTest(
[
Expand Down
36 changes: 18 additions & 18 deletions tests/Dax.Vpax.Obfuscator.Tests/DaxModelObfuscatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public void ObfuscateExpression_CalendarDateColumn_IsNotObfuscated()
[Fact]
public void ObfuscateExpression_TableConstructorColumnName_IsNotObfuscated_SingleColumnTest()
{
var expression = """ SELECTCOLUMNS({ (0) }, "@c1", [Value]) """;
var expected = """ SELECTCOLUMNS({ (0) }, "XXX", [Value]) """;
var expression = """ SELECTCOLUMNS({0}, "@c1", [Value]) """;
var expected = """ SELECTCOLUMNS({0}, "XXX", [Value]) """;

var obfuscator = new DaxModelObfuscator(new Model());
obfuscator.Texts.Add(new DaxText("@c1", "XXX"));
Expand All @@ -55,8 +55,8 @@ public void ObfuscateExpression_TableConstructorColumnName_IsNotObfuscated_Singl
[Fact]
public void ObfuscateExpression_TableConstructorColumnName_IsNotObfuscated_MultipleColumnsTest()
{
var expression = """ SELECTCOLUMNS({ (1,2,3) }, "@c1", [Value1], "@c2", [value2], "@c3", [VALUE3]) """;
var expected = """ SELECTCOLUMNS({ (1,2,3) }, "XXX", [Value1], "YYY", [value2], "ZZZ", [VALUE3]) """;
var expression = """ SELECTCOLUMNS({(1,2,3)}, "@c1", [Value1], "@c2", [value2], "@c3", [VALUE3]) """;
var expected = """ SELECTCOLUMNS({(1,2,3)}, "XXX", [Value1], "YYY", [value2], "ZZZ", [VALUE3]) """;

var obfuscator = new DaxModelObfuscator(new Model());
obfuscator.Texts.Add(new DaxText("@c1", "XXX"));
Expand All @@ -68,10 +68,10 @@ public void ObfuscateExpression_TableConstructorColumnName_IsNotObfuscated_Multi
}

[Fact]
public void ObfuscateExpression_ExtensionColumnNameWithDifferentCasings_ReturnsSameObfuscatedValue()
public void ObfuscateExpression_ExtensionColumnNameMultipleReferencesWithDifferentCasings_ReturnsSameObfuscatedValue()
{
var expression = """ SUMX(ADDCOLUMNS(Date, "@column", 1), [@COLUMN]) """;
var expected = """ SUMX(ADDCOLUMNS(Date, "XXXXXXX", 1), [XXXXXXX]) """;
var expression = """ SUMX(ADDCOLUMNS({}, "@column", 1), [@COLUMN]) """;
var expected = """ SUMX(ADDCOLUMNS({}, "XXXXXXX", 1), [XXXXXXX]) """;

var obfuscator = new DaxModelObfuscator(new Model());
obfuscator.Texts.Add(new DaxText("@column", "XXXXXXX"));
Expand All @@ -81,24 +81,24 @@ public void ObfuscateExpression_ExtensionColumnNameWithDifferentCasings_ReturnsS
}

[Fact]
public void ObfuscateExpression_ExtensionColumnName_ReturnsObfuscatedTableNameAndColumnNameParts()
public void ObfuscateExpression_ExtensionColumnNameFullyQualified_ReturnsObfuscatedColumnNameParts()
{
var expression = """ SUMX(ADDCOLUMNS(Date, "__rate[%]", 1), __rate[%]) """;
var expected = """ SUMX(ADDCOLUMNS(Date, "XXXXXX[Y]", 1), XXXXXX[Y]) """;
var expression = """ SUMX(ADDCOLUMNS({}, "rate[%]", 1), rate[%]) """;
var expected = """ SUMX(ADDCOLUMNS({}, "XXXX[Y]", 1), XXXX[Y]) """;

var obfuscator = new DaxModelObfuscator(new Model());
obfuscator.Texts.Add(new DaxText("__rate", "XXXXXX"));
obfuscator.Texts.Add(new DaxText("rate", "XXXX"));
obfuscator.Texts.Add(new DaxText("%", "Y"));
var actual = obfuscator.ObfuscateExpression(expression);

Assert.Equal(expected, actual);
}

[Fact]
public void ObfuscateExpression_ExtensionColumnName_ReturnsObfuscatedValuePreservingEscapeChar()
public void ObfuscateExpression_ExtensionColumnNameFullyQualified_ReturnsObfuscatedColumnNamePartsPreservingQuotationMarkEscapeChar()
{
var expression = """ SELECTCOLUMNS(ADDCOLUMNS(Date, "aaa[b""c]", 1), aaa[b"c]) """;
var expected = """ SELECTCOLUMNS(ADDCOLUMNS(Date, "XXX[Y""Y]", 1), XXX[Y"Y]) """;
var expression = """ SELECTCOLUMNS(ADDCOLUMNS({}, "aaa[b""c]", 1), aaa[b"c]) """;
var expected = """ SELECTCOLUMNS(ADDCOLUMNS({}, "XXX[Y""Y]", 1), XXX[Y"Y]) """;

var obfuscator = new DaxModelObfuscator(new Model());
obfuscator.Texts.Add(new DaxText("aaa", "XXX"));
Expand All @@ -122,7 +122,7 @@ public void ObfuscateExpression_TableNameWithDifferentCasings_ReturnsSameObfusca
}

[Fact]
public void ObfuscateExpression_ColumnNameWithEscapeSquareBracket_ReturnsObfuscatedValuePreservingEscapeChar()
public void ObfuscateExpression_ColumnName_ReturnsObfuscatedValuePreservingSquareBracketEscapeChar()
{
var expression = "RELATED( Sales[Rate[%]]] )";
var expected = "RELATED( XXXXX[YYYYYY]]] )";
Expand All @@ -136,7 +136,7 @@ public void ObfuscateExpression_ColumnNameWithEscapeSquareBracket_ReturnsObfusca
}

[Fact]
public void ObfuscateExpression_VariableNameWithDifferentCasings_ReturnsSameObfuscatedValue()
public void ObfuscateExpression_VariableNameMultipleReferencesWithDifferentCasings_ReturnsSameObfuscatedValue()
{
var expression = "VAR Amount = 1 RETURN AMOUNT + AmOuNt + amount";
var expected = "VAR XXXXXX = 1 RETURN XXXXXX + XXXXXX + XXXXXX";
Expand All @@ -151,8 +151,8 @@ public void ObfuscateExpression_VariableNameWithDifferentCasings_ReturnsSameObfu
[Fact]
public void ObfuscateExpression_ValueExtensionColumnName_IsNotObfuscated()
{
var expression = """ SELECTCOLUMNS ( { 1 }, "__Measures", ''[Value]) """;
var expected = """ SELECTCOLUMNS ( { 1 }, "XXXXXXXXXX", ''[Value]) """;
var expression = """ SELECTCOLUMNS({0}, "__Measures", ''[Value]) """;
var expected = """ SELECTCOLUMNS({0}, "XXXXXXXXXX", ''[Value]) """;

var obfuscator = new DaxModelObfuscator(new Model());
obfuscator.Texts.Add(new DaxText("__Measures", "XXXXXXXXXX"));
Expand Down

0 comments on commit 1db8666

Please sign in to comment.