Skip to content

Commit ce59403

Browse files
committed
[Test] Update in-memory tests to use a different entity instance for concurrency checks
Due to #13098 Fixes #13053
1 parent 2b2b13c commit ce59403

File tree

3 files changed

+92
-15
lines changed

3 files changed

+92
-15
lines changed

src/EFCore.Specification.Tests/TestModels/UpdatesModel/UpdatesContext.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ public static void Seed(UpdatesContext context)
2424
context.Add(new Category { Id = 78, PrincipalId = 778 });
2525
context.Add(new Product { Id = productId1, Name = "Apple Cider", Price = 1.49M, DependentId = 778 });
2626
context.Add(new Product { Id = productId2, Name = "Apple Cobler", Price = 2.49M, DependentId = 778 });
27-
context.Add(new ProductWithBytes { Id = productId1, Name = "MegaChips", Bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 } });
2827

2928
context.SaveChanges();
3029
}

src/EFCore.Specification.Tests/UpdatesTestBase.cs

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,21 @@ public virtual void Save_partial_update_on_concurrency_token_original_value_mism
103103
[Fact]
104104
public virtual void Update_on_bytes_concurrency_token_original_value_mismatch_throws()
105105
{
106-
var productId = new Guid("984ade3c-2f7b-4651-a351-642e92ab7146");
106+
var productId = Guid.NewGuid();
107107

108108
ExecuteWithStrategyInTransaction(
109+
context =>
110+
{
111+
context.Add(
112+
new ProductWithBytes
113+
{
114+
Id = productId,
115+
Name = "MegaChips",
116+
Bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }
117+
});
118+
119+
context.SaveChanges();
120+
},
109121
context =>
110122
{
111123
var entry = context.ProductWithBytes.Attach(
@@ -130,9 +142,21 @@ public virtual void Update_on_bytes_concurrency_token_original_value_mismatch_th
130142
[Fact]
131143
public virtual void Update_on_bytes_concurrency_token_original_value_matches_does_not_throw()
132144
{
133-
var productId = new Guid("984ade3c-2f7b-4651-a351-642e92ab7146");
145+
var productId = Guid.NewGuid();
134146

135147
ExecuteWithStrategyInTransaction(
148+
context =>
149+
{
150+
context.Add(
151+
new ProductWithBytes
152+
{
153+
Id = productId,
154+
Name = "MegaChips",
155+
Bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }
156+
});
157+
158+
context.SaveChanges();
159+
},
136160
context =>
137161
{
138162
var entry = context.ProductWithBytes.Attach(
@@ -156,9 +180,21 @@ public virtual void Update_on_bytes_concurrency_token_original_value_matches_doe
156180
[Fact]
157181
public virtual void Remove_on_bytes_concurrency_token_original_value_mismatch_throws()
158182
{
159-
var productId = new Guid("984ade3c-2f7b-4651-a351-642e92ab7146");
183+
var productId = Guid.NewGuid();
160184

161185
ExecuteWithStrategyInTransaction(
186+
context =>
187+
{
188+
context.Add(
189+
new ProductWithBytes
190+
{
191+
Id = productId,
192+
Name = "MegaChips",
193+
Bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }
194+
});
195+
196+
context.SaveChanges();
197+
},
162198
context =>
163199
{
164200
var entry = context.ProductWithBytes.Attach(
@@ -183,9 +219,21 @@ public virtual void Remove_on_bytes_concurrency_token_original_value_mismatch_th
183219
[Fact]
184220
public virtual void Remove_on_bytes_concurrency_token_original_value_matches_does_not_throw()
185221
{
186-
var productId = new Guid("984ade3c-2f7b-4651-a351-642e92ab7146");
222+
var productId = Guid.NewGuid();
187223

188224
ExecuteWithStrategyInTransaction(
225+
context =>
226+
{
227+
context.Add(
228+
new ProductWithBytes
229+
{
230+
Id = productId,
231+
Name = "MegaChips",
232+
Bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }
233+
});
234+
235+
context.SaveChanges();
236+
},
189237
context =>
190238
{
191239
var entry = context.ProductWithBytes.Attach(
@@ -454,17 +502,19 @@ public Task SaveChangesAsync_false_processes_all_tracked_entities_without_callin
454502

455503
protected virtual void ExecuteWithStrategyInTransaction(
456504
Action<UpdatesContext> testOperation,
457-
Action<UpdatesContext> nestedTestOperation1 = null)
505+
Action<UpdatesContext> nestedTestOperation1 = null,
506+
Action<UpdatesContext> nestedTestOperation2 = null)
458507
=> TestHelpers.ExecuteWithStrategyInTransaction(
459508
CreateContext, UseTransaction,
460-
testOperation, nestedTestOperation1);
509+
testOperation, nestedTestOperation1, nestedTestOperation2);
461510

462511
protected virtual Task ExecuteWithStrategyInTransactionAsync(
463512
Func<UpdatesContext, Task> testOperation,
464-
Func<UpdatesContext, Task> nestedTestOperation1 = null)
513+
Func<UpdatesContext, Task> nestedTestOperation1 = null,
514+
Func<UpdatesContext, Task> nestedTestOperation2 = null)
465515
=> TestHelpers.ExecuteWithStrategyInTransactionAsync(
466516
CreateContext, UseTransaction,
467-
testOperation, nestedTestOperation1);
517+
testOperation, nestedTestOperation1, nestedTestOperation2);
468518

469519
protected virtual void UseTransaction(DatabaseFacade facade, IDbContextTransaction transaction)
470520
{

test/EFCore.InMemory.FunctionalTests/UpdatesInMemoryTestBase.cs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,25 @@ protected UpdatesInMemoryTestBase(TFixture fixture)
2525
[Fact]
2626
public virtual void Update_on_bytes_concurrency_token_original_value_matches_throws_with_quirk()
2727
{
28-
var productId = new Guid("984ade3c-2f7b-4651-a351-642e92ab7146");
28+
var productId = Guid.NewGuid();
2929

3030
try
3131
{
3232
AppContext.SetSwitch("Microsoft.EntityFrameworkCore.Issue12214", true);
3333

3434
ExecuteWithStrategyInTransaction(
35+
context =>
36+
{
37+
context.Add(
38+
new ProductWithBytes
39+
{
40+
Id = productId,
41+
Name = "MegaChips",
42+
Bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }
43+
});
44+
45+
context.SaveChanges();
46+
},
3547
context =>
3648
{
3749
var entry = context.ProductWithBytes.Attach(
@@ -62,13 +74,25 @@ public virtual void Update_on_bytes_concurrency_token_original_value_matches_thr
6274
[Fact]
6375
public virtual void Remove_on_bytes_concurrency_token_original_value_matches_throws_with_quirk()
6476
{
65-
var productId = new Guid("984ade3c-2f7b-4651-a351-642e92ab7146");
77+
var productId = Guid.NewGuid();
6678

6779
try
6880
{
6981
AppContext.SetSwitch("Microsoft.EntityFrameworkCore.Issue12214", true);
7082

7183
ExecuteWithStrategyInTransaction(
84+
context =>
85+
{
86+
context.Add(
87+
new ProductWithBytes
88+
{
89+
Id = productId,
90+
Name = "MegaChips",
91+
Bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }
92+
});
93+
94+
context.SaveChanges();
95+
},
7296
context =>
7397
{
7498
var entry = context.ProductWithBytes.Attach(
@@ -101,16 +125,20 @@ protected override string UpdateConcurrencyMessage
101125
=> InMemoryStrings.UpdateConcurrencyException;
102126

103127
protected override void ExecuteWithStrategyInTransaction(
104-
Action<UpdatesContext> testOperation, Action<UpdatesContext> nestedTestOperation1 = null)
128+
Action<UpdatesContext> testOperation,
129+
Action<UpdatesContext> nestedTestOperation1 = null,
130+
Action<UpdatesContext> nestedTestOperation2 = null)
105131
{
106-
base.ExecuteWithStrategyInTransaction(testOperation, nestedTestOperation1);
132+
base.ExecuteWithStrategyInTransaction(testOperation, nestedTestOperation1, nestedTestOperation2);
107133
Fixture.Reseed();
108134
}
109135

110136
protected override async Task ExecuteWithStrategyInTransactionAsync(
111-
Func<UpdatesContext, Task> testOperation, Func<UpdatesContext, Task> nestedTestOperation1 = null)
137+
Func<UpdatesContext, Task> testOperation,
138+
Func<UpdatesContext, Task> nestedTestOperation1 = null,
139+
Func<UpdatesContext, Task> nestedTestOperation2 = null)
112140
{
113-
await base.ExecuteWithStrategyInTransactionAsync(testOperation, nestedTestOperation1);
141+
await base.ExecuteWithStrategyInTransactionAsync(testOperation, nestedTestOperation1, nestedTestOperation2);
114142
Fixture.Reseed();
115143
}
116144
}

0 commit comments

Comments
 (0)