-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathAdHocJsonQueryNpgsqlTest.cs
More file actions
319 lines (257 loc) · 14.2 KB
/
AdHocJsonQueryNpgsqlTest.cs
File metadata and controls
319 lines (257 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
using Xunit.Sdk;
#pragma warning disable EF8001 // ToJson on owned entities is obsolete
namespace Microsoft.EntityFrameworkCore.Query;
#nullable disable
public class AdHocJsonQueryNpgsqlTest(NonSharedFixture fixture) : AdHocJsonQueryRelationalTestBase(fixture)
{
protected override ITestStoreFactory NonSharedTestStoreFactory
=> NpgsqlTestStoreFactory.Instance;
protected override async Task Seed29219(DbContext ctx)
{
var entity1 = new Context29219.MyEntity
{
Id = 1,
Reference = new Context29219.MyJsonEntity { NonNullableScalar = 10, NullableScalar = 11 },
Collection =
[
new() { NonNullableScalar = 100, NullableScalar = 101 },
new() { NonNullableScalar = 200, NullableScalar = 201 },
new() { NonNullableScalar = 300, NullableScalar = null }
]
};
var entity2 = new Context29219.MyEntity
{
Id = 2,
Reference = new Context29219.MyJsonEntity { NonNullableScalar = 20, NullableScalar = null },
Collection = [new() { NonNullableScalar = 1001, NullableScalar = null }]
};
ctx.AddRange(entity1, entity2);
await ctx.SaveChangesAsync();
await ctx.Database.ExecuteSqlAsync(
$$"""
INSERT INTO "Entities" ("Id", "Reference", "Collection")
VALUES(3, '{ "NonNullableScalar" : 30 }', '[{ "NonNullableScalar" : 10001 }]')
""");
}
protected override async Task Seed30028(DbContext ctx)
{
// complete
await ctx.Database.ExecuteSqlAsync(
$$$$"""
INSERT INTO "Entities" ("Id", "Json")
VALUES(
1,
'{"RootName":"e1","Collection":[{"BranchName":"e1 c1","Nested":{"LeafName":"e1 c1 l"}},{"BranchName":"e1 c2","Nested":{"LeafName":"e1 c2 l"}}],"OptionalReference":{"BranchName":"e1 or","Nested":{"LeafName":"e1 or l"}},"RequiredReference":{"BranchName":"e1 rr","Nested":{"LeafName":"e1 rr l"}}}')
""");
// missing collection
await ctx.Database.ExecuteSqlAsync(
$$$$"""
INSERT INTO "Entities" ("Id", "Json")
VALUES(
2,
'{"RootName":"e2","OptionalReference":{"BranchName":"e2 or","Nested":{"LeafName":"e2 or l"}},"RequiredReference":{"BranchName":"e2 rr","Nested":{"LeafName":"e2 rr l"}}}')
""");
// missing optional reference
await ctx.Database.ExecuteSqlAsync(
$$$$"""
INSERT INTO "Entities" ("Id", "Json")
VALUES(
3,
'{"RootName":"e3","Collection":[{"BranchName":"e3 c1","Nested":{"LeafName":"e3 c1 l"}},{"BranchName":"e3 c2","Nested":{"LeafName":"e3 c2 l"}}],"RequiredReference":{"BranchName":"e3 rr","Nested":{"LeafName":"e3 rr l"}}}')
""");
// missing required reference
await ctx.Database.ExecuteSqlAsync(
$$$$"""
INSERT INTO "Entities" ("Id", "Json")
VALUES(
4,
'{"RootName":"e4","Collection":[{"BranchName":"e4 c1","Nested":{"LeafName":"e4 c1 l"}},{"BranchName":"e4 c2","Nested":{"LeafName":"e4 c2 l"}}],"OptionalReference":{"BranchName":"e4 or","Nested":{"LeafName":"e4 or l"}}}')
""");
}
protected override async Task Seed33046(DbContext ctx)
=> await ctx.Database.ExecuteSqlAsync(
$$"""
INSERT INTO "Reviews" ("Rounds", "Id")
VALUES('[{"RoundNumber":11,"SubRounds":[{"SubRoundNumber":111},{"SubRoundNumber":112}]}]', 1)
""");
protected override async Task SeedJunkInJson(DbContext ctx)
=> await ctx.Database.ExecuteSqlAsync(
$$$"""
INSERT INTO "Entities" ("Collection", "CollectionWithCtor", "Reference", "ReferenceWithCtor", "Id")
VALUES(
'[{"JunkReference":{"Something":"SomeValue" },"Name":"c11","JunkProperty1":50,"Number":11.5,"JunkCollection1":[],"JunkCollection2":[{"Foo":"junk value"}],"NestedCollection":[{"DoB":"2002-04-01T00:00:00","DummyProp":"Dummy value"},{"DoB":"2002-04-02T00:00:00","DummyReference":{"Foo":5}}],"NestedReference":{"DoB":"2002-03-01T00:00:00"}},{"Name":"c12","Number":12.5,"NestedCollection":[{"DoB":"2002-06-01T00:00:00"},{"DoB":"2002-06-02T00:00:00"}],"NestedDummy":59,"NestedReference":{"DoB":"2002-05-01T00:00:00"}}]',
'[{"MyBool":true,"Name":"c11 ctor","JunkReference":{"Something":"SomeValue","JunkCollection":[{"Foo":"junk value"}]},"NestedCollection":[{"DoB":"2002-08-01T00:00:00"},{"DoB":"2002-08-02T00:00:00"}],"NestedReference":{"DoB":"2002-07-01T00:00:00"}},{"MyBool":false,"Name":"c12 ctor","NestedCollection":[{"DoB":"2002-10-01T00:00:00"},{"DoB":"2002-10-02T00:00:00"}],"JunkCollection":[{"Foo":"junk value"}],"NestedReference":{"DoB":"2002-09-01T00:00:00"}}]',
'{"Name":"r1","JunkCollection":[{"Foo":"junk value"}],"JunkReference":{"Something":"SomeValue" },"Number":1.5,"NestedCollection":[{"DoB":"2000-02-01T00:00:00","JunkReference":{"Something":"SomeValue"}},{"DoB":"2000-02-02T00:00:00"}],"NestedReference":{"DoB":"2000-01-01T00:00:00"}}',
'{"MyBool":true,"JunkCollection":[{"Foo":"junk value"}],"Name":"r1 ctor","JunkReference":{"Something":"SomeValue" },"NestedCollection":[{"DoB":"2001-02-01T00:00:00"},{"DoB":"2001-02-02T00:00:00"}],"NestedReference":{"JunkCollection":[{"Foo":"junk value"}],"DoB":"2001-01-01T00:00:00"}}',
1)
""");
protected override async Task SeedTrickyBuffering(DbContext ctx)
=> await ctx.Database.ExecuteSqlAsync(
$$$"""
INSERT INTO "Entities" ("Reference", "Id")
VALUES(
'{"Name": "r1", "Number": 7, "JunkReference":{"Something": "SomeValue" }, "JunkCollection": [{"Foo": "junk value"}], "NestedReference": {"DoB": "2000-01-01T00:00:00Z"}, "NestedCollection": [{"DoB": "2000-02-01T00:00:00Z", "JunkReference": {"Something": "SomeValue"}}, {"DoB": "2000-02-02T00:00:00Z"}]}',1)
""");
protected override async Task SeedShadowProperties(DbContext ctx)
=> await ctx.Database.ExecuteSqlAsync(
$$"""
INSERT INTO "Entities" ("Collection", "CollectionWithCtor", "Reference", "ReferenceWithCtor", "Id", "Name")
VALUES(
'[{"Name":"e1_c1","ShadowDouble":5.5},{"ShadowDouble":20.5,"Name":"e1_c2"}]',
'[{"Name":"e1_c1 ctor","ShadowNullableByte":6},{"ShadowNullableByte":null,"Name":"e1_c2 ctor"}]',
'{"Name":"e1_r", "ShadowString":"Foo"}',
'{"ShadowInt":143,"Name":"e1_r ctor"}',
1,
'e1')
""");
protected override async Task SeedNotICollection(DbContext ctx)
{
await ctx.Database.ExecuteSqlAsync(
$$"""
INSERT INTO "Entities" ("Json", "Id")
VALUES(
'{"Collection":[{"Bar":11,"Foo":"c11"},{"Bar":12,"Foo":"c12"},{"Bar":13,"Foo":"c13"}]}',
1)
""");
await ctx.Database.ExecuteSqlAsync(
$$"""
INSERT INTO "Entities" ("Json", "Id")
VALUES(
'{"Collection":[{"Bar":21,"Foo":"c21"},{"Bar":22,"Foo":"c22"}]}',
2)
""");
}
#region BadJsonProperties
// PostgreSQL stores JSON as jsonb, which doesn't allow badly-formed JSON; so the following tests are irrelevant.
public override async Task Bad_json_properties_duplicated_navigations(bool noTracking)
{
if (noTracking)
{
await Assert.ThrowsAsync<NotSupportedException>(() => base.Bad_json_properties_duplicated_navigations(noTracking: true));
}
else
{
await base.Bad_json_properties_duplicated_navigations(noTracking: false);
}
}
public override Task Bad_json_properties_duplicated_scalars(bool noTracking)
=> Assert.ThrowsAsync<NotSupportedException>(() => base.Bad_json_properties_duplicated_scalars(noTracking));
public override Task Bad_json_properties_empty_navigations(bool noTracking)
=> Assert.ThrowsAsync<NotSupportedException>(() => base.Bad_json_properties_empty_navigations(noTracking));
public override Task Bad_json_properties_empty_scalars(bool noTracking)
=> Assert.ThrowsAsync<NotSupportedException>(() => base.Bad_json_properties_empty_scalars(noTracking));
public override Task Bad_json_properties_null_navigations(bool noTracking)
=> Assert.ThrowsAsync<ThrowsAnyException>(() => base.Bad_json_properties_null_navigations(noTracking));
public override Task Bad_json_properties_null_scalars(bool noTracking)
=> Assert.ThrowsAsync<ThrowsAnyException>(() => base.Bad_json_properties_null_scalars(noTracking));
protected override Task SeedBadJsonProperties(ContextBadJsonProperties ctx)
=> throw new NotSupportedException("PostgreSQL stores JSON as jsonb, which doesn't allow badly-formed JSON");
#endregion
[ConditionalTheory, MemberData(nameof(IsAsyncData))]
public virtual async Task Json_predicate_on_bytea(bool async)
{
var contextFactory = await InitializeNonSharedTest<TypesDbContext>(
seed: async context =>
{
context.Entities.AddRange(
new TypesContainerEntity { JsonEntity = new TypesJsonEntity { Bytea = [1, 2, 3] } },
new TypesContainerEntity { JsonEntity = new TypesJsonEntity { Bytea = [1, 2, 4] } });
await context.SaveChangesAsync();
});
using (var context = contextFactory.CreateDbContext())
{
var query = context.Entities.Where(x => x.JsonEntity.Bytea == new byte[] { 1, 2, 4 });
var result = async
? await query.SingleAsync()
: query.Single();
Assert.Equal(2, result.Id);
AssertSql(
"""
SELECT e."Id", e."JsonEntity"
FROM "Entities" AS e
WHERE (decode(e."JsonEntity" ->> 'Bytea', 'base64')) = BYTEA E'\\x010204'
LIMIT 2
""");
}
}
[ConditionalTheory, MemberData(nameof(IsAsyncData))]
public virtual async Task Json_predicate_on_interval(bool async)
{
var contextFactory = await InitializeNonSharedTest<TypesDbContext>(
seed: async context =>
{
context.Entities.AddRange(
new TypesContainerEntity { JsonEntity = new TypesJsonEntity { Interval = new TimeSpan(1, 2, 3, 4, 123, 456) } },
new TypesContainerEntity { JsonEntity = new TypesJsonEntity { Interval = new TimeSpan(2, 2, 3, 4, 123, 456) } });
await context.SaveChangesAsync();
});
using (var context = contextFactory.CreateDbContext())
{
var query = context.Entities.Where(x => x.JsonEntity.Interval == new TimeSpan(2, 2, 3, 4, 123, 456));
var result = async
? await query.SingleAsync()
: query.Single();
Assert.Equal(2, result.Id);
AssertSql(
"""
SELECT e."Id", e."JsonEntity"
FROM "Entities" AS e
WHERE (CAST(e."JsonEntity" ->> 'Interval' AS interval)) = INTERVAL '2 02:03:04.123456'
LIMIT 2
""");
}
}
protected class TypesDbContext(DbContextOptions options) : DbContext(options)
{
public DbSet<TypesContainerEntity> Entities { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
=> modelBuilder.Entity<TypesContainerEntity>().OwnsOne(b => b.JsonEntity).ToJson();
}
public class TypesContainerEntity
{
public int Id { get; set; }
public TypesJsonEntity JsonEntity { get; set; }
}
public class TypesJsonEntity
{
public byte[] Bytea { get; set; }
public TimeSpan Interval { get; set; }
}
#region Problematc tests (Unspecified DateTime)
// These tests use a model with a non-UTC DateTime, which isn't supported in PG's timestamp with time zone
public override Task Project_entity_with_json_null_values()
=> Assert.ThrowsAsync<ArgumentException>(() => base.Project_entity_with_json_null_values());
public override Task Try_project_collection_but_JSON_is_entity()
=> Assert.ThrowsAsync<ThrowsException>(() => base.Try_project_collection_but_JSON_is_entity());
public override Task Try_project_reference_but_JSON_is_collection()
=> Assert.ThrowsAsync<ThrowsException>(() => base.Try_project_reference_but_JSON_is_collection());
public override Task Project_entity_with_optional_json_entity_owned_by_required_json()
=> Assert.ThrowsAsync<ArgumentException>(() => base.Project_entity_with_optional_json_entity_owned_by_required_json());
public override Task Project_required_json_entity()
=> Assert.ThrowsAsync<ArgumentException>(() => base.Project_required_json_entity());
public override Task Project_optional_json_entity_owned_by_required_json_entity()
=> Assert.ThrowsAsync<ArgumentException>(() => base.Project_optional_json_entity_owned_by_required_json_entity());
public override Task Project_missing_required_scalar(bool async)
=> Assert.ThrowsAsync<ArgumentException>(() => base.Project_missing_required_scalar(async));
public override Task Project_nested_json_entity_with_missing_scalars(bool async)
=> Assert.ThrowsAsync<ArgumentException>(() => base.Project_nested_json_entity_with_missing_scalars(async));
public override Task Project_null_required_scalar(bool async)
=> Assert.ThrowsAsync<ArgumentException>(() => base.Project_null_required_scalar(async));
public override Task Project_root_entity_with_missing_required_navigation(bool async)
=> Assert.ThrowsAsync<ArgumentException>(() => base.Project_root_entity_with_missing_required_navigation(async));
public override Task Project_root_entity_with_null_required_navigation(bool async)
=> Assert.ThrowsAsync<ArgumentException>(() => base.Project_root_entity_with_null_required_navigation(async));
public override Task Project_root_with_missing_scalars(bool async)
=> Assert.ThrowsAsync<ArgumentException>(() => base.Project_root_with_missing_scalars(async));
public override Task Project_top_level_json_entity_with_missing_scalars(bool async)
=> Assert.ThrowsAsync<ArgumentException>(() => base.Project_top_level_json_entity_with_missing_scalars(async));
public override Task Project_missing_required_navigation(bool async)
=> Task.CompletedTask; // Different exception expected in the base implementation
public override Task Project_null_required_navigation(bool async)
=> Task.CompletedTask; // Different exception expected in the base implementation
public override Task Project_top_level_entity_with_null_value_required_scalars(bool async)
=> Task.CompletedTask; // Different exception expected in the base implementation
#endregion Problematc tests (Unspecified DateTime)
protected void AssertSql(params string[] expected)
=> TestSqlLoggerFactory.AssertBaseline(expected);
}