Skip to content

Commit

Permalink
Add test for SQL generation
Browse files Browse the repository at this point in the history
  • Loading branch information
elsand committed Dec 12, 2024
1 parent fb4e7f9 commit fe0040e
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ namespace Digdir.Domain.Dialogporten.Application.Common.Extensions;

public static class DbSetExtensions
{
public static IQueryable<DialogEntity> PrefilterAuthorizedDialogs(this DbSet<DialogEntity> dialogs, DialogSearchAuthorizationResult authorizedResources)
public static (string sql, object[] parameters) GeneratePrefilterAuthorizedDialogsSql(DialogSearchAuthorizationResult authorizedResources)
{
var parameters = new List<object>();
// lang=sql
var sb = new StringBuilder()
.AppendLine(CultureInfo.InvariantCulture, $"""
SELECT *
Expand Down Expand Up @@ -40,10 +39,17 @@ public static IQueryable<DialogEntity> PrefilterAuthorizedDialogs(this DbSet<Dia
parameters.Add(resources);
}

return dialogs.FromSqlRaw(sb.ToString(), parameters.ToArray());
return (sb.ToString(), parameters.ToArray());
}

public static IQueryable<DialogEntity> PrefilterAuthorizedDialogs(this DbSet<DialogEntity> dialogs, DialogSearchAuthorizationResult authorizedResources)
{
var (sql, parameters) = GeneratePrefilterAuthorizedDialogsSql(authorizedResources);
return dialogs.FromSqlRaw(sql, parameters);
}
}


public sealed class HashSetEqualityComparer<T> : IEqualityComparer<HashSet<T>>
{
public bool Equals(HashSet<T>? x, HashSet<T>? y)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using Digdir.Domain.Dialogporten.Application.Common.Extensions;
using Digdir.Domain.Dialogporten.Application.Externals.AltinnAuthorization;
using FluentAssertions;

namespace Digdir.Domain.Dialogporten.Application.Unit.Tests;

public class DbSetExtensionsTests
{
[Fact]
public void PrefilterAuthorizedDialogs_GeneratesExpectedSql_ForGroupedParties()
{
// Arrange
var authorizedResources = new DialogSearchAuthorizationResult
{
DialogIds = [Guid.CreateVersion7()],
ResourcesByParties = new Dictionary<string, HashSet<string>>
{
{ "Party1", ["Resource1", "Resource2"] },
{ "Party2", ["Resource1", "Resource2"] },
{ "Party3", ["Resource1", "Resource2", "Resource3"] },
{ "Party4", ["Resource3"] },
{ "Party5", ["Resource4"] }
}
};

var expectedSql = """
SELECT *
FROM "Dialog"
WHERE "Id" = ANY(@p0)
OR (
"Party" = ANY(@p1)
AND "ServiceResource" = ANY(@p2)
)
OR (
"Party" = ANY(@p3)
AND "ServiceResource" = ANY(@p4)
)
OR (
"Party" = ANY(@p5)
AND "ServiceResource" = ANY(@p6)
)
OR (
"Party" = ANY(@p7)
AND "ServiceResource" = ANY(@p8)
)
""";
var expectedParameters = new object[]
{
authorizedResources.DialogIds,
new HashSet<string> { "Party1", "Party2" },
new HashSet<string> { "Resource1", "Resource2" },
new HashSet<string> { "Party3" },
new HashSet<string> { "Resource1", "Resource2", "Resource3" },
new HashSet<string> { "Party4" },
new HashSet<string> { "Resource3" },
new HashSet<string> { "Party5" },
new HashSet<string> { "Resource4" }
};

// Act
var (actualSql, actualParameters) = DbSetExtensions.GeneratePrefilterAuthorizedDialogsSql(authorizedResources);

// Assert
RemoveWhitespace(actualSql).Should().Be(RemoveWhitespace(expectedSql));
actualParameters.Should().BeEquivalentTo(expectedParameters);
}

private static string RemoveWhitespace(string input)
{
return string.Concat(input.Where(c => !char.IsWhiteSpace(c)));
}
}

0 comments on commit fe0040e

Please sign in to comment.