forked from npgsql/efcore.pg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNonSharedModelBulkUpdatesNpgsqlTest.cs
194 lines (162 loc) · 5.83 KB
/
NonSharedModelBulkUpdatesNpgsqlTest.cs
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
using Microsoft.EntityFrameworkCore.BulkUpdates;
using Npgsql.EntityFrameworkCore.PostgreSQL.TestUtilities;
namespace Npgsql.EntityFrameworkCore.PostgreSQL.Update;
public class NonSharedModelBulkUpdatesNpgsqlTest : NonSharedModelBulkUpdatesTestBase
{
protected override ITestStoreFactory TestStoreFactory
=> NpgsqlTestStoreFactory.Instance;
[ConditionalFact]
public virtual void Check_all_tests_overridden()
=> TestHelpers.AssertAllMethodsOverridden(GetType());
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_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 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(
"""
UPDATE "Owner" AS o
SET "Title" = 'SomeValue'
""");
}
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_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 "OwnedReference_Number" = length(o."Title")::int,
"Title" = o."OwnedReference_Number"::text
""");
}
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 InitializeAsync<DbContext>(
onModelCreating: mb => mb.Entity<Blog>()
.ToTable("Blogs")
.SplitToTable(
"BlogsPart1", tb =>
{
tb.Property(b => b.Title);
tb.Property(b => b.Rating);
}),
seed: context =>
{
context.Set<Blog>().Add(new Blog { Title = "SomeBlog" });
context.SaveChanges();
});
await AssertUpdate(
async,
contextFactory.CreateContext,
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 "Rating" = length(b0."Title")::int,
"Title" = b0."Rating"::text
""");
}
public override Task Delete_entity_with_auto_include(bool async)
=> Assert.ThrowsAsync<ArgumentOutOfRangeException>(() => base.Delete_entity_with_auto_include(async)); // #30577
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] // #3001
[MemberData(nameof(IsAsyncData))]
public virtual async Task Update_with_primitive_collection_in_value_selector(bool async)
{
var contextFactory = await InitializeAsync<Context3001>(
seed: ctx =>
{
ctx.AddRange(new EntityWithPrimitiveCollection { Tags = new List<string> { "tag1", "tag2" }});
ctx.SaveChanges();
});
await AssertUpdate(
async,
contextFactory.CreateContext,
ss => ss.EntitiesWithPrimitiveCollection,
s => s.SetProperty(x => x.Tags, x => x.Tags.Append("another_tag")),
rowsAffectedCount: 1);
}
protected class Context3001 : DbContext
{
public Context3001(DbContextOptions options)
: base(options)
{
}
public DbSet<EntityWithPrimitiveCollection> EntitiesWithPrimitiveCollection { get; set; }
}
protected class EntityWithPrimitiveCollection
{
public int Id { get; set; }
public List<string> Tags { get; set; }
}
private void AssertSql(params string[] expected)
=> TestSqlLoggerFactory.AssertBaseline(expected);
private void AssertExecuteUpdateSql(params string[] expected)
=> TestSqlLoggerFactory.AssertBaseline(expected, forUpdate: true);
}