|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.ComponentModel.DataAnnotations.Schema; |
| 5 | +using System.Text.Json.Nodes; |
| 6 | + |
| 7 | +namespace Microsoft.EntityFrameworkCore.Query.Translations; |
| 8 | + |
| 9 | +// This test suite covers translations of JSON functions on EF.Functions (e.g. EF.Functions.JsonExists). |
| 10 | +// It does not cover general, built-in JSON support via complex type mapping, etc. |
| 11 | +public abstract class JsonTranslationsRelatinalTestBase<TFixture>(TFixture fixture) : QueryTestBase<TFixture>(fixture) |
| 12 | + where TFixture : JsonTranslationsRelatinalTestBase<TFixture>.JsonTranslationsQueryFixtureBase, new() |
| 13 | +{ |
| 14 | + [ConditionalFact] |
| 15 | + public virtual Task JsonExists_on_scalar_string_column() |
| 16 | + => AssertQuery( |
| 17 | + ss => ss.Set<JsonTranslationsEntity>() |
| 18 | + .Where(b => EF.Functions.JsonExists(b.JsonString, "$.OptionalInt")), |
| 19 | + ss => ss.Set<JsonTranslationsEntity>() |
| 20 | + .Where(b => ((IDictionary<string, JsonNode>)JsonNode.Parse(b.JsonString)!).ContainsKey("OptionalInt"))); |
| 21 | + |
| 22 | + [ConditionalFact] |
| 23 | + public virtual Task JsonExists_on_complex_property() |
| 24 | + => AssertQuery( |
| 25 | + ss => ss.Set<JsonTranslationsEntity>() |
| 26 | + .Where(b => EF.Functions.JsonExists(b.JsonComplexType, "$.OptionalInt")), |
| 27 | + ss => ss.Set<JsonTranslationsEntity>() |
| 28 | + .Where(b => ((IDictionary<string, JsonNode>)JsonNode.Parse(b.JsonString)!).ContainsKey("OptionalInt"))); |
| 29 | + |
| 30 | + public class JsonTranslationsEntity |
| 31 | + { |
| 32 | + [DatabaseGenerated(DatabaseGeneratedOption.None)] |
| 33 | + public int Id { get; set; } |
| 34 | + |
| 35 | + public required string JsonString { get; set; } |
| 36 | + |
| 37 | + public required JsonComplexType JsonComplexType { get; set; } |
| 38 | + } |
| 39 | + |
| 40 | + public class JsonComplexType |
| 41 | + { |
| 42 | + public required int RequiredInt { get; set; } |
| 43 | + public int? OptionalInt { get; set; } |
| 44 | + } |
| 45 | + |
| 46 | + public class JsonTranslationsQueryContext(DbContextOptions options) : PoolableDbContext(options) |
| 47 | + { |
| 48 | + public DbSet<JsonTranslationsEntity> JsonEntities { get; set; } = null!; |
| 49 | + |
| 50 | + protected override void OnModelCreating(ModelBuilder modelBuilder) |
| 51 | + => modelBuilder.Entity<JsonTranslationsEntity>().ComplexProperty(j => j.JsonComplexType, j => j.ToJson()); |
| 52 | + } |
| 53 | + |
| 54 | + // The translation tests usually use BasicTypesQueryFixtureBase, which manages a single database with all the data needed for the tests. |
| 55 | + // However, here in the JSON translation tests we use a separate fixture and database, since not all providers necessary implement full |
| 56 | + // JSON support, and we don't want to make life difficult for them with the basic translation tests. |
| 57 | + public abstract class JsonTranslationsQueryFixtureBase : SharedStoreFixtureBase<JsonTranslationsQueryContext>, IQueryFixtureBase, ITestSqlLoggerFactory |
| 58 | + { |
| 59 | + private JsonTranslationsData? _expectedData; |
| 60 | + |
| 61 | + protected override string StoreName |
| 62 | + => "JsonTranslationsQueryTest"; |
| 63 | + |
| 64 | + protected override async Task SeedAsync(JsonTranslationsQueryContext context) |
| 65 | + { |
| 66 | + var data = new JsonTranslationsData(); |
| 67 | + context.AddRange(data.JsonTranslationsEntities); |
| 68 | + await context.SaveChangesAsync(); |
| 69 | + |
| 70 | + var entityType = context.Model.FindEntityType(typeof(JsonTranslationsEntity))!; |
| 71 | + var sqlGenerationHelper = context.GetService<ISqlGenerationHelper>(); |
| 72 | + var table = sqlGenerationHelper.DelimitIdentifier(entityType.GetTableName()!); |
| 73 | + var idColumn = sqlGenerationHelper.DelimitIdentifier( |
| 74 | + entityType.FindProperty(nameof(JsonTranslationsEntity.Id))!.GetColumnName()); |
| 75 | + var complexTypeColumn = sqlGenerationHelper.DelimitIdentifier( |
| 76 | + entityType.FindComplexProperty(nameof(JsonTranslationsEntity.JsonComplexType))!.ComplexType.GetContainerColumnName()!); |
| 77 | + |
| 78 | + await context.Database.ExecuteSqlRawAsync( |
| 79 | + $$"""UPDATE {{table}} SET {{complexTypeColumn}} = {{RemoveJsonProperty(complexTypeColumn, "$.OptionalInt")}} WHERE {{idColumn}} = 4"""); |
| 80 | + } |
| 81 | + |
| 82 | + protected abstract string RemoveJsonProperty(string column, string jsonPath); |
| 83 | + |
| 84 | + public virtual ISetSource GetExpectedData() |
| 85 | + => _expectedData ??= new JsonTranslationsData(); |
| 86 | + |
| 87 | + public IReadOnlyDictionary<Type, object> EntitySorters { get; } = new Dictionary<Type, Func<object?, object?>> |
| 88 | + { |
| 89 | + { typeof(JsonTranslationsEntity), e => ((JsonTranslationsEntity?)e)?.Id }, |
| 90 | + }.ToDictionary(e => e.Key, e => (object)e.Value); |
| 91 | + |
| 92 | + public IReadOnlyDictionary<Type, object> EntityAsserters { get; } = new Dictionary<Type, Action<object?, object?>> |
| 93 | + { |
| 94 | + { |
| 95 | + typeof(JsonTranslationsEntity), (e, a) => |
| 96 | + { |
| 97 | + Assert.Equal(e == null, a == null); |
| 98 | + |
| 99 | + if (a != null) |
| 100 | + { |
| 101 | + var ee = (JsonTranslationsEntity)e!; |
| 102 | + var aa = (JsonTranslationsEntity)a; |
| 103 | + |
| 104 | + Assert.Equal(ee.Id, aa.Id); |
| 105 | + |
| 106 | + Assert.Equal(ee.JsonString, aa.JsonString); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + }.ToDictionary(e => e.Key, e => (object)e.Value); |
| 111 | + |
| 112 | + public Func<DbContext> GetContextCreator() |
| 113 | + => CreateContext; |
| 114 | + |
| 115 | + public TestSqlLoggerFactory TestSqlLoggerFactory |
| 116 | + => (TestSqlLoggerFactory)ListLoggerFactory; |
| 117 | + } |
| 118 | + |
| 119 | + public class JsonTranslationsData : ISetSource |
| 120 | + { |
| 121 | + public IReadOnlyList<JsonTranslationsEntity> JsonTranslationsEntities { get; } = CreateJsonTranslationsEntities(); |
| 122 | + |
| 123 | + public IQueryable<TEntity> Set<TEntity>() |
| 124 | + where TEntity : class |
| 125 | + => typeof(TEntity) == typeof(JsonTranslationsEntity) |
| 126 | + ? (IQueryable<TEntity>)JsonTranslationsEntities.AsQueryable() |
| 127 | + : throw new InvalidOperationException("Invalid entity type: " + typeof(TEntity)); |
| 128 | + |
| 129 | + public static IReadOnlyList<JsonTranslationsEntity> CreateJsonTranslationsEntities() => |
| 130 | + [ |
| 131 | + // In the following, JsonString should correspond exactly to JsonComplexType; we don't currently support mapping both |
| 132 | + // a string scalar property and a complex JSON property to the same column in the database. |
| 133 | + |
| 134 | + new() |
| 135 | + { |
| 136 | + Id = 1, |
| 137 | + JsonString = """{ "RequiredInt": 8, "OptionalInt": 8 }""", |
| 138 | + JsonComplexType = new() |
| 139 | + { |
| 140 | + RequiredInt = 8, |
| 141 | + OptionalInt = 8 |
| 142 | + } |
| 143 | + }, |
| 144 | + // Different values |
| 145 | + new() |
| 146 | + { |
| 147 | + Id = 2, |
| 148 | + JsonString = """{ "RequiredInt": 9, "OptionalInt": 9 }""", |
| 149 | + JsonComplexType = new() |
| 150 | + { |
| 151 | + RequiredInt = 9, |
| 152 | + OptionalInt = 9 |
| 153 | + } |
| 154 | + }, |
| 155 | + // OptionalInt is null. |
| 156 | + new() |
| 157 | + { |
| 158 | + Id = 3, |
| 159 | + JsonString = """{ "RequiredInt": 10, "OptionalInt": null }""", |
| 160 | + JsonComplexType = new() |
| 161 | + { |
| 162 | + RequiredInt = 10, |
| 163 | + OptionalInt = null |
| 164 | + } |
| 165 | + }, |
| 166 | + // OptionalInt is missing (not null). |
| 167 | + // Note that this requires a manual SQL update since EF's complex type support always writes out the property (with null); |
| 168 | + // any change here requires updating JsonTranslationsQueryContext.SeedAsync as well. |
| 169 | + new() |
| 170 | + { |
| 171 | + Id = 4, |
| 172 | + JsonString = """{ "RequiredInt": 10 }""", |
| 173 | + JsonComplexType = new() |
| 174 | + { |
| 175 | + RequiredInt = 10, |
| 176 | + OptionalInt = null // This will be replaced by a missing property |
| 177 | + } |
| 178 | + } |
| 179 | + ]; |
| 180 | + } |
| 181 | + |
| 182 | + protected JsonTranslationsQueryContext CreateContext() |
| 183 | + => Fixture.CreateContext(); |
| 184 | +} |
0 commit comments