forked from npgsql/efcore.pg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNonSharedModelBulkUpdatesNpgsqlTest.cs
More file actions
303 lines (250 loc) · 8.18 KB
/
NonSharedModelBulkUpdatesNpgsqlTest.cs
File metadata and controls
303 lines (250 loc) · 8.18 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
namespace Microsoft.EntityFrameworkCore.BulkUpdates;
public class NonSharedModelBulkUpdatesNpgsqlTest(NonSharedFixture fixture) : NonSharedModelBulkUpdatesRelationalTestBase(fixture)
{
protected override ITestStoreFactory NonSharedTestStoreFactory
=> NpgsqlTestStoreFactory.Instance;
public override async Task Update_complex_type_property_with_view_mapping(bool async)
{
await base.Update_complex_type_property_with_view_mapping(async);
AssertSql(
"""
@p='6'
UPDATE "Blogs" AS b
SET "ComplexThing_Prop1" = @p
""");
}
public override async Task Delete_aggregate_root_when_eager_loaded_owned_collection(bool async)
{
await base.Delete_aggregate_root_when_eager_loaded_owned_collection(async);
AssertSql(
"""
DELETE FROM "Owner" AS o
""");
}
public override async Task Delete_with_owned_collection_and_non_natively_translatable_query(bool async)
{
await base.Delete_with_owned_collection_and_non_natively_translatable_query(async);
AssertSql(
"""
@p='1'
DELETE FROM "Owner" AS o
WHERE o."Id" IN (
SELECT o0."Id"
FROM "Owner" AS o0
ORDER BY o0."Title" NULLS FIRST
OFFSET @p
)
""");
}
public override async Task Delete_aggregate_root_when_table_sharing_with_owned(bool async)
{
await base.Delete_aggregate_root_when_table_sharing_with_owned(async);
AssertSql(
"""
DELETE FROM "Owner" AS o
""");
}
public override async Task Replace_ColumnExpression_in_column_setter(bool async)
{
await base.Replace_ColumnExpression_in_column_setter(async);
AssertSql(
"""
@p='SomeValue'
UPDATE "OwnedCollection" AS o0
SET "Value" = @p
FROM "Owner" AS o
WHERE o."Id" = o0."OwnerId"
""");
}
public override async Task Delete_aggregate_root_when_table_sharing_with_non_owned_throws(bool async)
{
await base.Delete_aggregate_root_when_table_sharing_with_non_owned_throws(async);
AssertSql();
}
public override async Task Update_non_owned_property_on_entity_with_owned(bool async)
{
await base.Update_non_owned_property_on_entity_with_owned(async);
AssertSql(
"""
@p='SomeValue'
UPDATE "Owner" AS o
SET "Title" = @p
""");
}
public override async Task Delete_predicate_based_on_optional_navigation(bool async)
{
await base.Delete_predicate_based_on_optional_navigation(async);
AssertSql(
"""
DELETE FROM "Posts" AS p
WHERE p."Id" IN (
SELECT p0."Id"
FROM "Posts" AS p0
LEFT JOIN "Blogs" AS b ON p0."BlogId" = b."Id"
WHERE b."Title" LIKE 'Arthur%'
)
""");
}
public override async Task Update_non_owned_property_on_entity_with_owned2(bool async)
{
await base.Update_non_owned_property_on_entity_with_owned2(async);
AssertSql(
"""
UPDATE "Owner" AS o
SET "Title" = COALESCE(o."Title", '') || '_Suffix'
""");
}
public override async Task Update_non_owned_property_on_entity_with_owned_in_join(bool async)
{
await base.Update_non_owned_property_on_entity_with_owned_in_join(async);
AssertSql(
"""
@p='NewValue'
UPDATE "Owner" AS o
SET "Title" = @p
FROM "Owner" AS o0
WHERE o."Id" = o0."Id"
""");
}
public override async Task Update_owned_and_non_owned_properties_with_table_sharing(bool async)
{
await base.Update_owned_and_non_owned_properties_with_table_sharing(async);
AssertSql(
"""
UPDATE "Owner" AS o
SET "Title" = COALESCE(o."OwnedReference_Number"::text, ''),
"OwnedReference_Number" = length(o."Title")::int
""");
}
public override async Task Update_main_table_in_entity_with_entity_splitting(bool async)
{
// Overridden/duplicated because we update DateTime, which Npgsql requires to be a UTC timestamp
var contextFactory = await InitializeNonSharedTest<DbContext>(
onModelCreating: mb => mb.Entity<Blog>()
.ToTable("Blogs")
.SplitToTable(
"BlogsPart1", tb =>
{
tb.Property(b => b.Title);
tb.Property(b => b.Rating);
}),
seed: async context =>
{
context.Set<Blog>().Add(new Blog { Title = "SomeBlog" });
await context.SaveChangesAsync();
});
await AssertUpdate(
async,
contextFactory.CreateDbContext,
ss => ss.Set<Blog>(),
s => s.SetProperty(b => b.CreationTimestamp, b => new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Utc)),
rowsAffectedCount: 1);
AssertSql(
"""
UPDATE "Blogs" AS b
SET "CreationTimestamp" = TIMESTAMPTZ '2020-01-01T00:00:00Z'
""");
}
public override async Task Update_non_main_table_in_entity_with_entity_splitting(bool async)
{
await base.Update_non_main_table_in_entity_with_entity_splitting(async);
AssertSql(
"""
UPDATE "BlogsPart1" AS b0
SET "Title" = b0."Rating"::text,
"Rating" = length(b0."Title")::int
FROM "Blogs" AS b
WHERE b."Id" = b0."Id"
""");
}
public override async Task Delete_entity_with_auto_include(bool async)
{
await base.Delete_entity_with_auto_include(async);
AssertSql(
"""
DELETE FROM "Context30572_Principal" AS c
WHERE c."Id" IN (
SELECT c0."Id"
FROM "Context30572_Principal" AS c0
LEFT JOIN "Context30572_Dependent" AS c1 ON c0."DependentId" = c1."Id"
)
""");
}
public override async Task Update_with_alias_uniquification_in_setter_subquery(bool async)
{
await base.Update_with_alias_uniquification_in_setter_subquery(async);
AssertSql(
"""
UPDATE "Orders" AS o
SET "Total" = (
SELECT COALESCE(sum(o0."Amount"), 0)::int
FROM "OrderProduct" AS o0
WHERE o."Id" = o0."OrderId")
WHERE o."Id" = 1
""");
}
[ConditionalTheory] // #3622
[MemberData(nameof(IsAsyncData))]
public virtual async Task Update_with_primitive_collection_in_value_selector(bool async)
{
var contextFactory = await InitializeNonSharedTest<Context3001>(
seed: async ctx =>
{
ctx.AddRange(new EntityWithPrimitiveCollection { Tags = ["tag1", "tag2"] });
await ctx.SaveChangesAsync();
});
await Assert.ThrowsAsync<InvalidOperationException>(() => AssertUpdate(
async,
contextFactory.CreateDbContext,
ss => ss.EntitiesWithPrimitiveCollection,
s => s.SetProperty(x => x.Tags, x => x.Tags.Append("another_tag")),
rowsAffectedCount: 1));
}
protected class Context3001(DbContextOptions options) : DbContext(options)
{
public DbSet<EntityWithPrimitiveCollection> EntitiesWithPrimitiveCollection { get; set; }
}
protected class EntityWithPrimitiveCollection
{
public int Id { get; set; }
public List<string> Tags { get; set; } = null!;
}
public override async Task Delete_with_view_mapping(bool async)
{
await base.Delete_with_view_mapping(async);
AssertSql(
"""
DELETE FROM "Blogs" AS b
""");
}
public override async Task Update_with_view_mapping(bool async)
{
await base.Update_with_view_mapping(async);
AssertSql(
"""
@p='Updated'
UPDATE "Blogs" AS b
SET "Data" = @p
""");
}
public override async Task Update_complex_type_with_view_mapping(bool async)
{
await base.Update_complex_type_with_view_mapping(async);
// #34706
AssertSql(
"""
@complex_type_p_Prop1='3' (Nullable = true)
@complex_type_p_Prop2='4' (Nullable = true)
UPDATE "Blogs" AS b
SET "ComplexThing_Prop1" = @complex_type_p_Prop1,
"ComplexThing_Prop2" = @complex_type_p_Prop2
""");
}
private void AssertSql(params string[] expected)
=> TestSqlLoggerFactory.AssertBaseline(expected);
private void AssertExecuteUpdateSql(params string[] expected)
=> TestSqlLoggerFactory.AssertBaseline(expected, forUpdate: true);
[ConditionalFact]
public virtual void Check_all_tests_overridden()
=> TestHelpers.AssertAllMethodsOverridden(GetType());
}