@@ -13,21 +13,29 @@ public static async Task HotPath(TestContext context)
1313
1414 // Triggers JIT compilation of the most common path among all tests, improving the accuracy of their timings.
1515 // Also functions as a sanity check against the simulator being completely broken.
16- using var dbContext = new TestDbContext ( ) ;
17-
18- Assert . IsEmpty ( dbContext . Rows . Select ( x => x . Id ) . AsEnumerable ( ) ) ;
19-
20- Assert . AreEqual ( 0 , await dbContext . SaveChangesAsync ( context . CancellationToken ) ) ;
16+ using var dbContext = new TestDbContext ( 1 ) ;
17+ _ = await dbContext . Rows . Select ( x => x . Id ) . FirstOrDefaultAsync ( context . CancellationToken ) ;
2118 }
2219
23- public static Simulation CreateDefaultSimulation ( )
20+ public static Simulation CreateDefaultSimulation ( params ReadOnlySpan < int > values )
2421 {
2522 var simulation = new Simulation ( ) ;
2623 _ = simulation
2724 . CreateOpenConnection ( )
2825 . CreateCommand ( "create table Rows ( Id int )" )
2926 . ExecuteNonQuery ( ) ;
3027
28+ if ( values . Length != 0 )
29+ {
30+ using var context = new TestDbContext ( simulation ) ;
31+ foreach ( var value in values )
32+ {
33+ var row = new TestRow { Id = value } ;
34+ _ = context . Rows . Add ( row ) ;
35+ _ = context . SaveChanges ( ) ;
36+ }
37+ }
38+
3139 return simulation ;
3240 }
3341
@@ -40,8 +48,8 @@ class TestDbContext(Simulation simulation) : DbContext
4048 {
4149 public Simulation Simulation { get ; set ; } = simulation ;
4250
43- public TestDbContext ( )
44- : this ( CreateDefaultSimulation ( ) )
51+ public TestDbContext ( params ReadOnlySpan < int > values )
52+ : this ( CreateDefaultSimulation ( values ) )
4553 {
4654 }
4755
@@ -56,13 +64,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
5664 [ TestMethod ]
5765 public void InsertRowSync ( )
5866 {
59- using var context = new TestDbContext ( ) ;
60-
61- var row = new TestRow { Id = 1 } ;
62-
63- _ = context . Rows . Add ( row ) ;
64-
65- _ = context . SaveChanges ( ) ;
67+ using var context = new TestDbContext ( 1 ) ;
6668 }
6769
6870 /// <summary>
@@ -84,69 +86,48 @@ public async Task InsertRowAsync()
8486 [ TestMethod ]
8587 public void RoundTrip ( )
8688 {
87- var simulation = CreateDefaultSimulation ( ) ;
8889 const int storedValue = 3 ;
90+ using var context = new TestDbContext ( storedValue ) ;
91+ var receivedValue = context . Rows . Select ( x => x . Id ) . AsEnumerable ( ) ;
8992
90- using ( var context = new TestDbContext ( simulation ) )
91- {
92- var row = new TestRow { Id = storedValue } ;
93-
94- _ = context . Rows . Add ( row ) ;
95-
96- _ = context . SaveChanges ( ) ;
97- }
98-
99- using ( var context = new TestDbContext ( simulation ) )
100- {
101- var receivedValue = context . Rows . Select ( x => x . Id ) . AsEnumerable ( ) ;
102-
103- Assert . AreEqual ( storedValue , receivedValue . FirstOrDefault ( ) ) ;
104- }
93+ Assert . AreEqual ( storedValue , receivedValue . FirstOrDefault ( ) ) ;
10594 }
10695
10796 [ TestMethod ]
10897 public void SeparateInserts ( )
10998 {
110- var simulation = CreateDefaultSimulation ( ) ;
11199 int [ ] storedValues = [ 2 , 3 ] ;
112-
113- using ( var context = new TestDbContext ( simulation ) )
114- {
115- foreach ( var value in storedValues )
116- {
117- var row = new TestRow { Id = value } ;
118- _ = context . Rows . Add ( row ) ;
119- _ = context . SaveChanges ( ) ;
120- }
121- }
122-
123- using ( var context = new TestDbContext ( simulation ) )
124- {
125- CollectionAssert . AreEquivalent ( storedValues , context . Rows . Select ( x => x . Id ) . ToArray ( ) ) ;
126- }
100+ using var context = new TestDbContext ( storedValues ) ;
101+ CollectionAssert . AreEquivalent ( storedValues , context . Rows . Select ( x => x . Id ) . ToArray ( ) ) ;
127102 }
128103
129104 [ TestMethod ]
130105 public void FirstOrDefault ( )
131106 {
132- var simulation = CreateDefaultSimulation ( ) ;
133107 int [ ] storedValues = [ 4 , 5 ] ;
108+ using var context = new TestDbContext ( storedValues ) ;
109+ var receivedValue = context . Rows . Select ( x => x . Id ) ;
110+ // Without an OrderBy, we can't guarantee which of the two possibilities is returned.
111+ CollectionAssert . Contains ( storedValues , receivedValue . FirstOrDefault ( ) ) ;
112+ }
134113
135- using ( var context = new TestDbContext ( simulation ) )
136- {
137- foreach ( var value in storedValues )
138- {
139- var row = new TestRow { Id = value } ;
140- _ = context . Rows . Add ( row ) ;
141- _ = context . SaveChanges ( ) ;
142- }
143- }
114+ [ TestMethod ]
115+ public void SingleOrDefault ( )
116+ {
117+ const int storedValue = 6 ;
118+ using var context = new TestDbContext ( storedValue ) ;
119+ var receivedValue = context . Rows . Select ( x => x . Id ) ;
120+ // Without an OrderBy, we can't guarantee which of the two possibilities is returned.
121+ Assert . AreEqual ( storedValue , receivedValue . SingleOrDefault ( ) ) ;
122+ }
144123
145- using ( var context = new TestDbContext ( simulation ) )
146- {
147- var receivedValue = context . Rows . Select ( x => x . Id ) ;
148- // Without an OrderBy, we can't guarantee which of the two possibilities is returned.
149- CollectionAssert . Contains ( storedValues , receivedValue . FirstOrDefault ( ) ) ;
150- }
124+ [ TestMethod ]
125+ public void Take ( )
126+ {
127+ int [ ] storedValues = [ 4 , 5 ] ;
128+ using var context = new TestDbContext ( storedValues ) ;
129+ var receivedValue = context . Rows . Select ( x => x . Id ) ;
130+ // Without an OrderBy, we can't guarantee which of the two possibilities is returned.
131+ CollectionAssert . Contains ( storedValues , receivedValue . Take ( 1 ) . AsEnumerable ( ) . First ( ) ) ;
151132 }
152133}
0 commit comments