Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix graphql where clause when database schema is configured #16364

Merged
merged 8 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ public string GetColumnName(string propertyPath)
{
// Switch the given alias in the path with the mapped alias.
// aliasPart.alias -> AliasPartIndex.Alias
return Dialect.QuoteForTableName($"{tableAlias}", _configuration.Schema) + "." + Dialect.QuoteForColumnName(columnName);
return $"{Dialect.QuoteForAliasName(tableAlias)}.{Dialect.QuoteForColumnName(columnName)}";
}
}
else
{
// no property provider exists; hope sql is case-insensitive (will break postgres; property providers must be supplied for postgres)
// Switch the given alias in the path with the mapped alias.
// aliasPart.Alias -> AliasPartIndex.alias
return Dialect.QuoteForTableName($"{tableAlias}", _configuration.Schema) + "." + Dialect.QuoteForColumnName(values[0]);
return $"{Dialect.QuoteForAliasName(tableAlias)}.{Dialect.QuoteForColumnName(values[0])}";
}
}

Expand Down
81 changes: 81 additions & 0 deletions test/OrchardCore.Tests/Apis/GraphQL/Queries/PredicateQueryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using OrchardCore.ContentManagement.GraphQL.Queries;
using OrchardCore.ContentManagement.GraphQL.Queries.Predicates;
using YesSql.Indexes;

namespace OrchardCore.Tests.Apis.GraphQL;

public class PredicateQueryTests
{
private YesSql.IConfiguration _configuration;

public PredicateQueryTests()
{
_configuration = new Configuration()
.UseSqlServer("Fake database connection string for testing;", "TenantSchema")
.SetTablePrefix("Tenant1");
}

[Fact]
public void ShouldReturnQuotedColumnNameWhenAliasNotExists()
{
// Arrange
var predicateQuery = new PredicateQuery(_configuration, []);

// Act
var columnName = predicateQuery.GetColumnName("ListItemIndex.Value");

// Assert
Assert.Equal("[ListItemIndex.Value]", columnName);
}

[Fact]
public void ShouldReturnQuotedAliasColumnNameWhenAliasExists()
{
// Arrange
var predicateQuery = new PredicateQuery(_configuration, []);
predicateQuery.CreateAlias("ListItemIndexPath.ValuePath", "ListItemIndexAlias.ValueAlias");

// Act
var columnName = predicateQuery.GetColumnName("ListItemIndexPath.ValuePath");

// Assert
Assert.Equal("[ListItemIndexAlias.ValueAlias]", columnName);
}

[Fact]
public void ShouldReturnQuotedTableAliasAndColumnNameWhenProviderExists()
{
// Arrange
var predicateQuery = new PredicateQuery(_configuration, [new IndexPropertyProvider<ListItemIndex>()]);

predicateQuery.CreateAlias("ListItemIndexPath", nameof(ListItemIndex));
predicateQuery.CreateTableAlias(nameof(ListItemIndex), "ListItemIndexAlias");

// Act
var columnName = predicateQuery.GetColumnName("ListItemIndexPath.Value");

// Assert
Assert.Equal("[ListItemIndexAlias].[Value]", columnName);
}

[Fact]
public void ShouldReturnQuotedTableAliasAndPathWhenProviderNotExists()
{
// Arrange
var predicateQuery = new PredicateQuery(_configuration, []);

predicateQuery.CreateAlias("ListItemIndexPath", nameof(ListItemIndex));
predicateQuery.CreateTableAlias(nameof(ListItemIndex), "ListItemIndexAlias");

// Act
var columnName = predicateQuery.GetColumnName("ListItemIndexPath.Value");

// Assert
Assert.Equal("[ListItemIndexAlias].[ListItemIndexPath]", columnName);
}

public class ListItemIndex : MapIndex
{
public string Value { get; set; }
}
}