Skip to content

Commit 586b526

Browse files
committed
Added initialization tests to warm up JIT compilation and ensure basic functionality.
1 parent 2b042d6 commit 586b526

3 files changed

Lines changed: 33 additions & 11 deletions

File tree

SqlServerSimulator.Tests.EFCore/EFCoreBasics.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@ namespace SqlServerSimulator;
55
[TestClass]
66
public class EFCoreBasics
77
{
8+
[AssemblyInitialize]
9+
public static async Task HotPath(TestContext context)
10+
{
11+
if (System.Diagnostics.Debugger.IsAttached)
12+
return;
13+
14+
// Triggers JIT compilation of the most common path among all tests, improving the accuracy of their timings.
15+
// 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));
21+
}
22+
823
public static Simulation CreateDefaultSimulation()
924
{
1025
var simulation = new Simulation();
@@ -21,11 +36,11 @@ class TestRow
2136
public int Id { get; set; }
2237
}
2338

24-
class TestContext(Simulation simulation) : DbContext
39+
class TestDbContext(Simulation simulation) : DbContext
2540
{
2641
public Simulation Simulation { get; set; } = simulation;
2742

28-
public TestContext()
43+
public TestDbContext()
2944
: this(CreateDefaultSimulation())
3045
{
3146
}
@@ -41,7 +56,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
4156
[TestMethod]
4257
public void InsertRowSync()
4358
{
44-
using var context = new TestContext();
59+
using var context = new TestDbContext();
4560

4661
var row = new TestRow { Id = 1 };
4762

@@ -57,7 +72,7 @@ public void InsertRowSync()
5772
[TestMethod]
5873
public async Task InsertRowAsync()
5974
{
60-
await using var context = new TestContext();
75+
await using var context = new TestDbContext();
6176

6277
var row = new TestRow { Id = 1 };
6378

@@ -72,7 +87,7 @@ public void RoundTrip()
7287
var simulation = CreateDefaultSimulation();
7388
const int storedValue = 3;
7489

75-
using (var context = new TestContext(simulation))
90+
using (var context = new TestDbContext(simulation))
7691
{
7792
var row = new TestRow { Id = storedValue };
7893

@@ -81,7 +96,7 @@ public void RoundTrip()
8196
_ = context.SaveChanges();
8297
}
8398

84-
using (var context = new TestContext(simulation))
99+
using (var context = new TestDbContext(simulation))
85100
{
86101
var receivedValue = context.Rows.Select(x => x.Id).AsEnumerable();
87102

SqlServerSimulator.Tests/QualityTests.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,18 @@
33
[TestClass]
44
public class QualityTests
55
{
6-
[TestMethod()]
6+
[AssemblyInitialize]
7+
public static void HotPath(TestContext context)
8+
{
9+
if (System.Diagnostics.Debugger.IsAttached)
10+
return;
11+
12+
// Triggers JIT compilation of the most common path among all tests, improving the accuracy of their timings.
13+
// Also functions as a sanity check against the simulator being completely broken.
14+
Assert.AreEqual(1, new Simulation().ExecuteScalar<int>("select 1"));
15+
}
16+
17+
[TestMethod]
718
[Description("Prevents unintentional expansion of the public API.")]
819
public void PublicApiWhitelist()
920
{

SqlServerSimulator.Tests/SelectTests.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ namespace SqlServerSimulator;
55
[TestClass]
66
public class SelectTests
77
{
8-
[TestMethod]
9-
public void Select1ViaExecuteScalar()
10-
=> Assert.AreEqual(1, new Simulation().ExecuteScalar("select 1"));
11-
128
private static DbDataReader ScalarViaReaderTest(string commandText)
139
{
1410
var reader = new Simulation().ExecuteReader(commandText);

0 commit comments

Comments
 (0)