-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: anonymous object support in CustomOperators #76
- Loading branch information
1 parent
7375b53
commit 039840e
Showing
6 changed files
with
297 additions
and
2 deletions.
There are no files selected for viewing
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
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
22 changes: 22 additions & 0 deletions
22
...ntityFrameworkPostgreSqlIntegrationTests/EntityFrameworkPostgreSqlIntegrationTests.csproj
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,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.3" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.3" /> | ||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.3" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" /> | ||
<PackageReference Include="xunit" Version="2.4.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.2" /> | ||
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Gridify.EntityFramework\Gridify.EntityFramework.csproj" /> | ||
<ProjectReference Include="..\..\src\Gridify\Gridify.csproj" /> | ||
</ItemGroup> | ||
</Project> |
153 changes: 153 additions & 0 deletions
153
test/EntityFrameworkPostgreSqlIntegrationTests/Interceptors.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,153 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Data.Common; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.EntityFrameworkCore.Diagnostics; | ||
|
||
namespace EntityFrameworkIntegrationTests.cs; | ||
|
||
public class SuppressConnectionInterceptor : DbConnectionInterceptor | ||
{ | ||
public override ValueTask<InterceptionResult> ConnectionOpeningAsync(DbConnection connection, ConnectionEventData eventData, | ||
InterceptionResult result, | ||
CancellationToken cancellationToken = new()) | ||
{ | ||
result = InterceptionResult.Suppress(); | ||
return base.ConnectionOpeningAsync(connection, eventData, result, cancellationToken); | ||
} | ||
|
||
public override InterceptionResult ConnectionOpening(DbConnection connection, ConnectionEventData eventData, InterceptionResult result) | ||
{ | ||
result = InterceptionResult.Suppress(); | ||
return base.ConnectionOpening(connection, eventData, result); | ||
} | ||
} | ||
|
||
public class EmptyMessageDataReader : DbDataReader | ||
{ | ||
|
||
private readonly List<User> _users = new List<User>(); | ||
|
||
public EmptyMessageDataReader() | ||
{ | ||
} | ||
|
||
public override int FieldCount | ||
=> 0; | ||
|
||
public override int RecordsAffected | ||
=> 0; | ||
|
||
public override bool HasRows | ||
=> false; | ||
|
||
public override bool IsClosed | ||
=> true; | ||
|
||
public override int Depth | ||
=> 0; | ||
|
||
public override bool Read() | ||
=> false; | ||
|
||
public override int GetInt32(int ordinal) | ||
=> 0; | ||
|
||
public override bool IsDBNull(int ordinal) | ||
=> false; | ||
|
||
public override string GetString(int ordinal) | ||
=> "suppressed message"; | ||
|
||
public override bool GetBoolean(int ordinal) | ||
=> true; | ||
|
||
public override byte GetByte(int ordinal) | ||
=> 0; | ||
|
||
public override long GetBytes(int ordinal, long dataOffset, byte[] buffer, int bufferOffset, int length) | ||
=> 0; | ||
|
||
public override char GetChar(int ordinal) | ||
=> '\0'; | ||
|
||
public override long GetChars(int ordinal, long dataOffset, char[] buffer, int bufferOffset, int length) | ||
=> 0; | ||
|
||
public override string GetDataTypeName(int ordinal) | ||
=> string.Empty; | ||
|
||
public override DateTime GetDateTime(int ordinal) | ||
=> DateTime.Now; | ||
|
||
public override decimal GetDecimal(int ordinal) | ||
=> 0; | ||
|
||
public override double GetDouble(int ordinal) | ||
=> 0; | ||
|
||
public override Type GetFieldType(int ordinal) | ||
=> typeof(User); | ||
|
||
public override float GetFloat(int ordinal) | ||
=> 0; | ||
|
||
public override Guid GetGuid(int ordinal) | ||
=> Guid.Empty; | ||
|
||
public override short GetInt16(int ordinal) | ||
=> 0; | ||
|
||
public override long GetInt64(int ordinal) | ||
=> 0; | ||
|
||
public override string GetName(int ordinal) | ||
=> ""; | ||
|
||
public override int GetOrdinal(string name) | ||
=> 0; | ||
|
||
public override object GetValue(int ordinal) | ||
=> new object(); | ||
|
||
public override int GetValues(object[] values) | ||
=> 0; | ||
|
||
public override object this[int ordinal] | ||
=> new object(); | ||
|
||
public override object this[string name] | ||
=> new object(); | ||
|
||
public override bool NextResult() | ||
=> false; | ||
|
||
public override IEnumerator GetEnumerator() | ||
=> _users.GetEnumerator(); | ||
} | ||
|
||
public class SuppressCommandResultInterceptor : DbCommandInterceptor | ||
{ | ||
public override InterceptionResult<DbDataReader> ReaderExecuting( | ||
DbCommand command, | ||
CommandEventData eventData, | ||
InterceptionResult<DbDataReader> result) | ||
{ | ||
result = InterceptionResult<DbDataReader>.SuppressWithResult(new EmptyMessageDataReader()); | ||
|
||
return result; | ||
} | ||
|
||
public override ValueTask<InterceptionResult<DbDataReader>> ReaderExecutingAsync( | ||
DbCommand command, | ||
CommandEventData eventData, | ||
InterceptionResult<DbDataReader> result, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
result = InterceptionResult<DbDataReader>.SuppressWithResult(new EmptyMessageDataReader()); | ||
|
||
return new ValueTask<InterceptionResult<DbDataReader>>(result); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
test/EntityFrameworkPostgreSqlIntegrationTests/Issue76Tests.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,46 @@ | ||
using System.Linq.Expressions; | ||
using System.Reflection.Metadata; | ||
using EntityFrameworkIntegrationTests.cs; | ||
using Gridify.Syntax; | ||
using Microsoft.EntityFrameworkCore; | ||
using Xunit; | ||
|
||
namespace Gridify.Tests; | ||
|
||
public class Issue76Tests | ||
{ | ||
|
||
private readonly MyDbContext _dbContext; | ||
|
||
public Issue76Tests() | ||
{ | ||
_dbContext = new MyDbContext(); | ||
} | ||
|
||
[Fact] | ||
public void CustomOperator_EFJsonContains_ShouldGenerateCorrectExpression() | ||
{ | ||
// Arrange | ||
GridifyGlobalConfiguration.CustomOperators.Register(new JsonContainsOperator()); | ||
|
||
var gm = new GridifyMapper<Products>() | ||
.AddMap("u", q => q.Users); | ||
var expected = _dbContext.ProductsViews.Where(q => EF.Functions.JsonContains(q.Users, new[] { new { Id = 1 } })).ToQueryString(); | ||
|
||
// Act | ||
var actual = _dbContext.ProductsViews.ApplyFiltering("u #= 1", gm).ToQueryString(); | ||
|
||
// Assert | ||
Assert.Equal(expected, actual); | ||
} | ||
|
||
} | ||
|
||
public class JsonContainsOperator : IGridifyOperator | ||
{ | ||
public string GetOperator() => "#="; | ||
public Expression<OperatorParameter> OperatorHandler() | ||
{ | ||
return (prop, value) => EF.Functions.JsonContains(prop, new[] { new { Id = int.Parse(value.ToString()!) } }); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
test/EntityFrameworkPostgreSqlIntegrationTests/MyDbContext.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,50 @@ | ||
using System; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Diagnostics; | ||
|
||
namespace EntityFrameworkIntegrationTests.cs; | ||
|
||
public class MyDbContext : DbContext | ||
{ | ||
public DbSet<User> Users { get; set; } | ||
public DbSet<Products> ProductsViews { get; set; } | ||
|
||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
modelBuilder.Entity<User>().Property<string>("shadow1"); | ||
base.OnModelCreating(modelBuilder); | ||
} | ||
|
||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
{ | ||
optionsBuilder.UseNpgsql("Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;"); | ||
optionsBuilder.AddInterceptors(new SuppressCommandResultInterceptor()); | ||
optionsBuilder.AddInterceptors(new SuppressConnectionInterceptor()); | ||
optionsBuilder.EnableServiceProviderCaching(); | ||
|
||
base.OnConfiguring(optionsBuilder); | ||
} | ||
} | ||
|
||
public class User | ||
{ | ||
public int Id { get; set; } | ||
public string Name { get; set; } | ||
public DateTime? CreateDate { get; set; } | ||
public Guid FkGuid { get; set; } | ||
} | ||
public class Products | ||
{ | ||
public int Id { get; set; } | ||
public string Name { get; set; } | ||
|
||
[Column(TypeName = "jsonb")] | ||
public IEnumerable<ProductUser> Users { get; set; } | ||
} | ||
|
||
public class ProductUser | ||
{ | ||
public int Id { get; set; } | ||
public string Name { get; set; } | ||
} |