-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathUpdatesRelationalTestBase.cs
More file actions
358 lines (305 loc) · 13.8 KB
/
UpdatesRelationalTestBase.cs
File metadata and controls
358 lines (305 loc) · 13.8 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.EntityFrameworkCore.TestModels.UpdatesModel;
// ReSharper disable InconsistentNaming
namespace Microsoft.EntityFrameworkCore.Update;
#nullable disable
public abstract class UpdatesRelationalTestBase<TFixture>(TFixture fixture) : UpdatesTestBase<TFixture>(fixture)
where TFixture : UpdatesRelationalTestBase<TFixture>.UpdatesRelationalFixture
{
[ConditionalFact]
public virtual Task SaveChanges_works_for_entities_also_mapped_to_view()
=> ExecuteWithStrategyInTransactionAsync(
async context =>
{
var category = await context.Categories.SingleAsync();
context.Add(
new ProductTableWithView
{
Id = Guid.NewGuid(),
Name = "Pear Cider",
Price = 1.39M,
DependentId = category.Id
});
context.Add(
new ProductViewTable
{
Id = Guid.NewGuid(),
Name = "Pear Cobler",
Price = 2.39M,
DependentId = category.Id
});
await context.SaveChangesAsync();
}, async context =>
{
var viewProduct = await context.Set<ProductTableWithView>().SingleAsync();
var tableProduct = await context.Set<ProductTableView>().SingleAsync();
Assert.Equal("Pear Cider", tableProduct.Name);
Assert.Equal("Pear Cobler", viewProduct.Name);
});
[ConditionalFact]
public virtual Task SaveChanges_throws_for_entities_only_mapped_to_view()
=> ExecuteWithStrategyInTransactionAsync(async context =>
{
var category = await context.Categories.SingleAsync();
context.Add(
new ProductTableView
{
Id = Guid.NewGuid(),
Name = "Pear Cider",
Price = 1.39M,
DependentId = category.Id
});
Assert.Equal(
RelationalStrings.ReadonlyEntitySaved(nameof(ProductTableView)),
(await Assert.ThrowsAsync<InvalidOperationException>(() => context.SaveChangesAsync())).Message);
});
[ConditionalFact]
public virtual Task Save_with_shared_foreign_key()
{
Guid productId = default;
return ExecuteWithStrategyInTransactionAsync(
async context =>
{
var product = new ProductWithBytes();
context.Add(product);
await context.SaveChangesAsync();
productId = product.Id;
}, async context =>
{
var product = (await context.ProductWithBytes.FindAsync(productId))!;
var category = new SpecialCategory { PrincipalId = 777 };
var productCategory = new ProductCategory { Category = category };
product.ProductCategories = new List<ProductCategory> { productCategory };
await context.SaveChangesAsync();
Assert.True(category.Id > 0);
Assert.Equal(category.Id, productCategory.CategoryId);
}, async context =>
{
var product = await context.Set<ProductBase>()
.Include(p => ((ProductWithBytes)p).ProductCategories)
.Include(p => ((Product)p).ProductCategories)
.OfType<ProductWithBytes>()
.SingleAsync();
var productCategory = product.ProductCategories.Single();
Assert.Equal(productCategory.CategoryId, (await context.Set<ProductCategory>().SingleAsync()).CategoryId);
Assert.Equal(productCategory.CategoryId, (await context.Set<SpecialCategory>().SingleAsync(c => c.PrincipalId == 777)).Id);
});
}
[ConditionalFact]
public virtual Task Can_use_shared_columns_with_conversion()
=> ExecuteWithStrategyInTransactionAsync(
context =>
{
var person = new Person("1", null)
{
Address = new Address { Country = Country.Eswatini, City = "Bulembu" }, Country = "Eswatini"
};
context.Add(person);
return context.SaveChangesAsync();
}, async context =>
{
var person = await context.Set<Person>().SingleAsync();
person.Address = new Address
{
Country = Country.Türkiye,
City = "Konya",
ZipCode = 42100
};
await context.SaveChangesAsync();
},
async context =>
{
var person = await context.Set<Person>().SingleAsync();
Assert.Equal(Country.Türkiye, person.Address!.Country);
Assert.Equal("Konya", person.Address.City);
Assert.Equal(42100, person.Address.ZipCode);
Assert.Equal("Türkiye", person.Country);
Assert.Equal("42100", person.ZipCode);
});
[ConditionalFact]
public virtual Task Swap_filtered_unique_index_values()
{
var productId1 = new Guid("984ade3c-2f7b-4651-a351-642e92ab7146");
var productId2 = new Guid("0edc9136-7eed-463b-9b97-bdb9648ab877");
return ExecuteWithStrategyInTransactionAsync(
async context =>
{
var product1 = (await context.Products.FindAsync(productId1))!;
var product2 = (await context.Products.FindAsync(productId2))!;
product2.Name = null;
product2.Price = product1.Price;
await context.SaveChangesAsync();
}, async context =>
{
var product1 = (await context.Products.FindAsync(productId1))!;
var product2 = (await context.Products.FindAsync(productId2))!;
product2.Name = product1.Name;
product1.Name = null;
await context.SaveChangesAsync();
}, async context =>
{
var product1 = (await context.Products.FindAsync(productId1))!;
var product2 = (await context.Products.FindAsync(productId2))!;
Assert.Equal(1.49M, product1.Price);
Assert.Null(product1.Name);
Assert.Equal(1.49M, product2.Price);
Assert.Equal("Apple Cider", product2.Name);
});
}
[ConditionalFact] // Issue #33023
public virtual Task Swap_computed_unique_index_values()
{
var productId1 = new Guid("984ade3c-2f7b-4651-a351-642e92ab7146");
var productId2 = new Guid("0edc9136-7eed-463b-9b97-bdb9648ab877");
return ExecuteWithStrategyInTransactionAsync(
async context =>
{
var product1 = (await context.Products.FindAsync(productId1))!;
var product2 = (await context.Products.FindAsync(productId2))!;
product1.IsPrimary = false;
product2.Name = product1.Name;
product2.IsPrimary = true;
await context.SaveChangesAsync();
}, async context =>
{
var product1 = (await context.Products.FindAsync(productId1))!;
var product2 = (await context.Products.FindAsync(productId2))!;
product1.Name = "Apple Cobler";
product1.IsPrimary = true;
product2.IsPrimary = false;
await context.SaveChangesAsync();
}, async context =>
{
var product1 = (await context.Products.FindAsync(productId1))!;
var product2 = (await context.Products.FindAsync(productId2))!;
Assert.True(product1.IsPrimary);
Assert.False(product2.IsPrimary);
});
}
[ConditionalFact]
public virtual Task Update_non_indexed_values()
{
var productId1 = new Guid("984ade3c-2f7b-4651-a351-642e92ab7146");
var productId2 = new Guid("0edc9136-7eed-463b-9b97-bdb9648ab877");
return ExecuteWithStrategyInTransactionAsync(
async context =>
{
var product1 = (await context.Products.FindAsync(productId1))!;
var product2 = (await context.Products.FindAsync(productId2))!;
product2.Price = product1.Price;
await context.SaveChangesAsync();
},
context =>
{
var product1 = new Product
{
Id = productId1,
Name = "",
Price = 1.49M,
IsPrimary = true
};
var product2 = new Product
{
Id = productId2,
Name = "",
Price = 1.49M,
IsPrimary = false
};
context.Attach(product1).Property(p => p.DependentId).IsModified = true;
context.Attach(product2).Property(p => p.DependentId).IsModified = true;
return context.SaveChangesAsync();
}, async context =>
{
var product1 = (await context.Products.FindAsync(productId1))!;
var product2 = (await context.Products.FindAsync(productId2))!;
Assert.Equal(1.49M, product1.Price);
Assert.Null(product1.DependentId);
Assert.Equal(1.49M, product2.Price);
Assert.Null(product2.DependentId);
});
}
[ConditionalTheory, InlineData(false), InlineData(true)] // Issue #37525
public virtual async Task Can_save_owned_entity_with_default_values_in_TPH_with_shared_columns(bool async)
=> await ExecuteWithStrategyInTransactionAsync(
async context =>
{
var entity = new CrunchyNougat { Name = "Test" };
context.Add(entity);
_ = async ? await context.SaveChangesAsync() : context.SaveChanges();
},
async context =>
{
var entity = await context.Set<CrunchyNougat>().SingleAsync();
Assert.Null(entity.Filling);
entity.Filling = new NougatFilling();
_ = async ? await context.SaveChangesAsync() : context.SaveChanges();
},
async context =>
{
var entity = await context.Set<CrunchyNougat>().SingleAsync();
Assert.NotNull(entity.Filling);
Assert.Equal(NougatFillingKind.Unknown, entity.Filling.Kind);
Assert.False(entity.Filling.IsFresh);
});
[ConditionalFact]
public abstract void Identifiers_are_generated_correctly();
protected override void UseTransaction(DatabaseFacade facade, IDbContextTransaction transaction)
=> facade.UseTransaction(transaction.GetDbTransaction());
protected override string UpdateConcurrencyMessage
=> RelationalStrings.UpdateConcurrencyException(1, 0);
protected override string UpdateConcurrencyTokenMessage
=> RelationalStrings.UpdateConcurrencyException(1, 0);
public abstract class UpdatesRelationalFixture : UpdatesFixtureBase
{
public TestSqlLoggerFactory TestSqlLoggerFactory
=> (TestSqlLoggerFactory)ListLoggerFactory;
protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext context)
{
base.OnModelCreating(modelBuilder, context);
modelBuilder.Entity<ProductViewTable>().HasBaseType((string)null).ToTable("ProductView");
modelBuilder.Entity<ProductTableWithView>().HasBaseType((string)null).ToView("ProductView").ToTable("ProductTable");
modelBuilder.Entity<ProductTableView>().HasBaseType((string)null).ToView("ProductTable");
modelBuilder.Entity<Product>().HasIndex(p => new { p.Name, p.Price }).IsUnique();
modelBuilder.Entity<Person>(pb =>
{
pb.Property(p => p.Country)
.HasColumnName("Country");
pb.Property(p => p.ZipCode)
.HasColumnName("ZipCode");
pb.OwnsOne(p => p.Address)
.Property(p => p.Country)
.HasColumnName("Country");
pb.OwnsOne(p => p.Address)
.Property(p => p.ZipCode)
.HasColumnName("ZipCode");
});
modelBuilder.Entity<CrunchyNougat>(b =>
{
b.OwnsOne(e => e.Filling, ob =>
{
ob.Property(o => o.Kind).HasColumnName("FillingKind");
ob.Property(o => o.IsFresh).HasColumnName("FillingIsFresh");
});
});
modelBuilder.Entity<SoftNougat>(b =>
{
b.OwnsOne(e => e.Filling, ob =>
{
ob.Property(o => o.Kind).HasColumnName("FillingKind");
ob.Property(o => o.IsFresh).HasColumnName("FillingIsFresh");
});
});
modelBuilder
.Entity<
LoginEntityTypeWithAnExtremelyLongAndOverlyConvolutedNameThatIsUsedToVerifyThatTheStoreIdentifierGenerationLengthLimitIsWorkingCorrectlyDetails
>(eb =>
{
eb.HasKey(l => new { l.ProfileId })
.HasName("PK_LoginDetails");
eb.HasOne(d => d.Login).WithOne()
.HasConstraintName("FK_LoginDetails_Login");
});
}
}
}