-
Notifications
You must be signed in to change notification settings - Fork 261
Add ValueComparer for JSON DOM types using DeepEquals #3887
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
Draft
hostage2222
wants to merge
1
commit into
npgsql:main
Choose a base branch
from
hostage2222:json-dom-change-tracking
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+324
−6
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
266 changes: 266 additions & 0 deletions
266
test/EFCore.PG.FunctionalTests/Query/JsonDomChangeTrackingTest.cs
This file contains hidden or 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,266 @@ | ||
| using System.ComponentModel.DataAnnotations.Schema; | ||
| using System.Text.Json; | ||
|
|
||
| namespace Microsoft.EntityFrameworkCore.Query; | ||
|
|
||
| public class JsonDomChangeTrackingTest : IClassFixture<JsonDomChangeTrackingTest.JsonDomChangeTrackingFixture> | ||
| { | ||
| private JsonDomChangeTrackingFixture Fixture { get; } | ||
|
|
||
| public JsonDomChangeTrackingTest(JsonDomChangeTrackingFixture fixture, ITestOutputHelper testOutputHelper) | ||
| { | ||
| Fixture = fixture; | ||
| Fixture.TestSqlLoggerFactory.Clear(); | ||
| Fixture.TestSqlLoggerFactory.SetTestOutputHelper(testOutputHelper); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("""{"Name":"John","Age":25}""", true)] | ||
| [InlineData("""{"Age":25.00,"Name":"Joe"}""", false)] | ||
| public async Task SaveChanges_jsonb_document(string json, bool isModified) | ||
| { | ||
| await using var ctx = CreateContext(); | ||
| await ctx.Database.CreateExecutionStrategy().ExecuteAsync( | ||
| ctx, async context => | ||
| { | ||
| await using var transaction = await context.Database.BeginTransactionAsync(); | ||
|
|
||
| var entity = await context.JsonbEntities.SingleAsync(e => e.Id == 1); | ||
| entity.CustomerDocument = JsonDocument.Parse(json); | ||
| Fixture.TestSqlLoggerFactory.Clear(); | ||
| await context.SaveChangesAsync(); | ||
|
|
||
| if (isModified) | ||
| { | ||
| AssertSql( | ||
| """ | ||
| @p1='1' | ||
| @p0='System.Text.Json.JsonDocument' (DbType = Object) | ||
|
|
||
| UPDATE "JsonbEntities" SET "CustomerDocument" = @p0 | ||
| WHERE "Id" = @p1; | ||
| """); | ||
| } | ||
| else | ||
| { | ||
| AssertEmptySql(); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("""{"Name":"Joe","Age":26}""", true)] | ||
| [InlineData("""{"Age":25.00,"Name":"Joe"}""", false)] | ||
| public async Task SaveChanges_json_document(string json, bool isModified) | ||
| { | ||
| await using var ctx = CreateContext(); | ||
| await ctx.Database.CreateExecutionStrategy().ExecuteAsync( | ||
| ctx, async context => | ||
| { | ||
| await using var transaction = await context.Database.BeginTransactionAsync(); | ||
|
|
||
| var entity = await context.JsonEntities.SingleAsync(e => e.Id == 1); | ||
| entity.CustomerDocument = JsonDocument.Parse(json); | ||
| Fixture.TestSqlLoggerFactory.Clear(); | ||
| await context.SaveChangesAsync(); | ||
|
|
||
| if (isModified) | ||
| { | ||
| AssertSql( | ||
| """ | ||
| @p1='1' | ||
| @p0='System.Text.Json.JsonDocument' (DbType = Object) | ||
|
|
||
| UPDATE "JsonEntities" SET "CustomerDocument" = @p0 | ||
| WHERE "Id" = @p1; | ||
| """); | ||
| } | ||
| else | ||
| { | ||
| AssertEmptySql(); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("""{"Name":"John","Age":25}""", true)] | ||
| [InlineData("""{"Age":25.00,"Name":"Joe"}""", false)] | ||
| public async Task SaveChanges_jsonb_element(string json, bool isModified) | ||
| { | ||
| await using var ctx = CreateContext(); | ||
| await ctx.Database.CreateExecutionStrategy().ExecuteAsync( | ||
| ctx, async context => | ||
| { | ||
| await using var transaction = await context.Database.BeginTransactionAsync(); | ||
|
|
||
| var entity = await context.JsonbEntities.SingleAsync(e => e.Id == 1); | ||
| entity.CustomerElement = JsonElement.Parse(json); | ||
| Fixture.TestSqlLoggerFactory.Clear(); | ||
| await context.SaveChangesAsync(); | ||
|
|
||
| if (isModified) | ||
| { | ||
| AssertSql( | ||
| """ | ||
| @p1='1' | ||
| @p0='{"Name":"John","Age":25}' (DbType = Object) | ||
|
|
||
| UPDATE "JsonbEntities" SET "CustomerElement" = @p0 | ||
| WHERE "Id" = @p1; | ||
| """); | ||
| } | ||
| else | ||
| { | ||
| AssertEmptySql(); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("""{"Name":"Joe","Age":26}""", true)] | ||
| [InlineData("""{"Age":25.00,"Name":"Joe"}""", false)] | ||
| public async Task SaveChanges_json_element(string json, bool isModified) | ||
| { | ||
| await using var ctx = CreateContext(); | ||
| await ctx.Database.CreateExecutionStrategy().ExecuteAsync( | ||
| ctx, async context => | ||
| { | ||
| await using var transaction = await context.Database.BeginTransactionAsync(); | ||
|
|
||
| var entity = await context.JsonEntities.SingleAsync(e => e.Id == 1); | ||
| entity.CustomerElement = JsonElement.Parse(json); | ||
| Fixture.TestSqlLoggerFactory.Clear(); | ||
| await context.SaveChangesAsync(); | ||
|
|
||
| if (isModified) | ||
| { | ||
| AssertSql( | ||
| """ | ||
| @p1='1' | ||
| @p0='{"Name":"Joe","Age":26}' (DbType = Object) | ||
|
|
||
| UPDATE "JsonEntities" SET "CustomerElement" = @p0 | ||
| WHERE "Id" = @p1; | ||
| """); | ||
| } | ||
| else | ||
| { | ||
| AssertEmptySql(); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task DetectChanges_jsonb_undefined_element_no_throw() | ||
| { | ||
| await using var ctx = CreateContext(); | ||
|
|
||
| var entity = await ctx.JsonbEntities.SingleAsync(e => e.Id == 1); | ||
| entity.CustomerElement = new JsonElement(); | ||
| ctx.JsonbEntities.Add( | ||
| new JsonbEntity | ||
| { | ||
| Id = 2, | ||
| CustomerElement = new JsonElement(), | ||
| CustomerDocument = null | ||
| }); | ||
|
|
||
| ctx.ChangeTracker.DetectChanges(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task DetectChanges_json_undefined_element_no_throw() | ||
| { | ||
| await using var ctx = CreateContext(); | ||
|
|
||
| var entity = await ctx.JsonEntities.SingleAsync(e => e.Id == 1); | ||
| entity.CustomerElement = new JsonElement(); | ||
| ctx.JsonEntities.Add( | ||
| new JsonEntity | ||
| { | ||
| Id = 2, | ||
| CustomerElement = new JsonElement(), | ||
| CustomerDocument = null | ||
| }); | ||
|
|
||
| ctx.ChangeTracker.DetectChanges(); | ||
| } | ||
|
|
||
| #region Support | ||
|
|
||
| protected JsonDomChangeTrackingContext CreateContext() | ||
| => Fixture.CreateContext(); | ||
|
|
||
| private void AssertSql(params string[] expected) | ||
| => Fixture.TestSqlLoggerFactory.AssertBaseline(expected); | ||
|
|
||
| private void AssertEmptySql() | ||
| => Assert.Empty(Fixture.TestSqlLoggerFactory.SqlStatements); | ||
|
|
||
| public class JsonDomChangeTrackingContext(DbContextOptions options) : PoolableDbContext(options) | ||
| { | ||
| public DbSet<JsonbEntity> JsonbEntities { get; set; } | ||
| public DbSet<JsonEntity> JsonEntities { get; set; } | ||
|
|
||
| public static async Task SeedAsync(JsonDomChangeTrackingContext context) | ||
| { | ||
| var customer = CreateCustomer(); | ||
|
|
||
| context.JsonbEntities.Add( | ||
| new JsonbEntity | ||
| { | ||
| Id = 1, | ||
| CustomerDocument = customer, | ||
| CustomerElement = customer.RootElement | ||
| }); | ||
| context.JsonEntities.Add( | ||
| new JsonEntity | ||
| { | ||
| Id = 1, | ||
| CustomerDocument = customer, | ||
| CustomerElement = customer.RootElement | ||
| }); | ||
|
|
||
| await context.SaveChangesAsync(); | ||
|
|
||
| static JsonDocument CreateCustomer() | ||
| => JsonDocument.Parse("""{"Name":"Joe","Age":25}"""); | ||
| } | ||
| } | ||
|
|
||
| public class JsonbEntity | ||
| { | ||
| public required int Id { get; set; } | ||
|
|
||
| public required JsonDocument? CustomerDocument { get; set; } | ||
| public required JsonElement CustomerElement { get; set; } | ||
| } | ||
|
|
||
| public class JsonEntity | ||
| { | ||
| public required int Id { get; set; } | ||
|
|
||
| [Column(TypeName = "json")] | ||
| public required JsonDocument? CustomerDocument { get; set; } | ||
|
|
||
| [Column(TypeName = "json")] | ||
| public required JsonElement CustomerElement { get; set; } | ||
| } | ||
|
|
||
| public class JsonDomChangeTrackingFixture : SharedStoreFixtureBase<JsonDomChangeTrackingContext> | ||
| { | ||
| protected override string StoreName | ||
| => "JsonDomChangeTrackingTest"; | ||
|
|
||
| protected override ITestStoreFactory TestStoreFactory | ||
| => NpgsqlTestStoreFactory.Instance; | ||
|
|
||
| public TestSqlLoggerFactory TestSqlLoggerFactory | ||
| => (TestSqlLoggerFactory)ListLoggerFactory; | ||
|
|
||
| protected override Task SeedAsync(JsonDomChangeTrackingContext context) | ||
| => JsonDomChangeTrackingContext.SeedAsync(context); | ||
| } | ||
|
|
||
| #endregion | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't actually an issue.
ValueComparerhas the following constructor:public ValueComparer(
Expression<Func<T?, T?, bool>> equalsExpression,
Expression<Func<T, int>> hashCodeExpression,
Expression<Func<T, T>> snapshotExpression)
: base(equalsExpression, hashCodeExpression, snapshotExpression)
{
}
Note that unlike
equalsExpression,hashCodeExpressionis typed asFunc<T, int>rather thanFunc<T?, int>—Tisn't nullable here, so this is expected.