Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
vplauzon committed Oct 19, 2023
1 parent 614d45f commit c97b795
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 18 deletions.
2 changes: 2 additions & 0 deletions code/DeltaKustoLib/CommandModel/CodeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace DeltaKustoLib.CommandModel
{
internal static class CodeHelper
{
#region Descendants
public static IEnumerable<TElement> GetAtLeastOneDescendant<TElement>(
this SyntaxElement parent,
string descendantNameForExceptionMessage,
Expand Down Expand Up @@ -95,6 +96,7 @@ public static IReadOnlyList<TElement> GetImmediateDescendants<TElement>(

return descendants;
}
#endregion

#region Extract Children
public static (C1, C2) ExtractChildren<C1, C2>(
Expand Down
2 changes: 2 additions & 0 deletions code/DeltaKustoLib/CommandModel/CommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ public override int GetHashCode()
case "DeleteTablePolicyStreamingIngestion":
case "DeleteDatabasePolicyStreamingIngestion":
return DeleteStreamingIngestionPolicyCommand.FromCode(commandBlock);
case "AlterTablePolicyRowLevelSecurity":
return AlterRowLevelSecurityPolicyCommand.FromCode(commandBlock);
#endregion

default:
Expand Down
2 changes: 1 addition & 1 deletion code/DeltaKustoLib/CommandModel/EntityName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public EntityName(string name)
}
else
{
throw new DeltaException($"Unsuppored character for an entity: '{c}'");
throw new DeltaException($"Unsupported character for an entity: '{c}'");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class AlterRowLevelSecurityPolicyCommand : TableOnlyPolicyCommandBase
{
public bool IsEnabled { get; }

public string Query { get; }
public QuotedText Query { get; }

public override string CommandFriendlyName => ".alter <entity> policy row_level_security";

Expand All @@ -26,7 +26,7 @@ public class AlterRowLevelSecurityPolicyCommand : TableOnlyPolicyCommandBase
public AlterRowLevelSecurityPolicyCommand(
EntityName tableName,
bool isEnabled,
string query)
QuotedText query)
: base(tableName)
{
IsEnabled = isEnabled;
Expand All @@ -36,33 +36,40 @@ public AlterRowLevelSecurityPolicyCommand(
internal static CommandBase FromCode(SyntaxElement rootElement)
{
var tableName = rootElement.GetDescendants<NameReference>().Last();
var policyText = QuotedText.FromLiteral(
rootElement.GetUniqueDescendant<LiteralExpression>(
"AutoDeletePolicy",
e => e.NameInParent == "AutoDeletePolicy"));
var policy = Deserialize<JsonDocument>(policyText.Text);
var isEnabled = rootElement
.GetDescendants<SyntaxElement>(e => e.Kind == SyntaxKind.IdentifierToken)
.Select(e => e.ToString().Trim())
.Where(t => t == "enable" || t == "disable")
.Select(t => new bool?(t == "enable"))
.LastOrDefault();

if (policy == null)
if (isEnabled == null)
{
throw new DeltaException(
$"Can't extract policy objects from {policyText.ToScript()}");
"No 'enable' or 'disable' token found in row level security command");
}

return new AlterAutoDeletePolicyCommand(EntityName.FromCode(tableName.Name), policy);
var query = QuotedText.FromLiteral(
rootElement.GetUniqueDescendant<LiteralExpression>(
"Row Level Security",
e => e.NameInParent == "Query"));

return new AlterRowLevelSecurityPolicyCommand(
EntityName.FromCode(tableName.Name),
isEnabled.Value,
query);
}

public override string ToScript(ScriptingContext? context)
{
var builder = new StringBuilder();

builder.Append(".alter table ");
builder.Append(TableName);
builder.Append(" policy auto_delete");
builder.AppendLine();
builder.Append("```");
builder.Append(SerializePolicy());
builder.AppendLine();
builder.Append("```");
builder.Append(TableName.ToScript());
builder.Append(" policy row_level_security ");
builder.Append(IsEnabled ? "enable" : "disable");
builder.Append(" ");
builder.AppendLine(Query.ToScript());

return builder.ToString();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using DeltaKustoLib.CommandModel;
using DeltaKustoLib.CommandModel.Policies;
using System;
using System.Linq;
using System.Text.Json;
using Xunit;

namespace DeltaKustoUnitTest.CommandParsing.Policies
{
public class AlterRowLevelSecurityPolicyTest : ParsingTestBase
{
[Fact]
public void SimpleTable()
{
TestRowLevelPolicy("A", "MyFunction");
}

[Fact]
public void FunkyTable()
{
TestRowLevelPolicy("['A- 1']", "MyTable | where TenantId == 42");
}

// Currently not supported by the parser
//[Fact]
//public void DbComposedTableName()
//{
// TestRowLevelPolicy("mydb.mytable", "MyFunction");
//}

// Currently not supported by the parser
//[Fact]
//public void ClusterComposedTableName()
//{
// TestRowLevelPolicy("mycluster.['my db'].mytable", "MyFunction");
//}

private void TestRowLevelPolicy(string tableName, string query)
{
TestRowLevelPolicy(tableName, true, query);
TestRowLevelPolicy(tableName, false, query);
}

private void TestRowLevelPolicy(string tableName, bool isEnabled, string query)
{
var realTableName = tableName.Split('.').Last();
var enableToken = isEnabled ? "enable" : "disable";
var commandText = $@"
.alter table {tableName} policy row_level_security {enableToken} ""{query}""
";
var command = ParseOneCommand(commandText);

Assert.IsType<AlterRowLevelSecurityPolicyCommand>(command);

var realCommand = (AlterRowLevelSecurityPolicyCommand)command;

Assert.Equal(isEnabled, realCommand.IsEnabled);
Assert.Equal(query, realCommand.Query.Text);
}
}
}

0 comments on commit c97b795

Please sign in to comment.