Skip to content

Commit 8aae029

Browse files
committed
Densify test suite: drop optional INTO; consolidate setup batches
1 parent f0bd652 commit 8aae029

52 files changed

Lines changed: 1712 additions & 1697 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

SqlServerSimulator.Tests.EFCore/EFCoreCheckConstraint.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ public class EFCoreCheckConstraint
1818
[TestMethod]
1919
public void SaveChanges_StockItemPositiveQuantity_RoundTrips()
2020
{
21-
using var context = new TestDbContext(TestDbContext.CreateStockItemsSimulation());
22-
_ = context.StockItems.Add(new StockItem { Sku = "BOLT-7", Quantity = 12 });
23-
_ = context.SaveChanges();
21+
using var context = new TestDbContext(TestDbContext.CreateStockItemsSimulation()).WithSaved(new StockItem { Sku = "BOLT-7", Quantity = 12 });
2422

2523
var fetched = context.StockItems.AsNoTracking().Single();
2624
Assert.AreEqual("BOLT-7", fetched.Sku);

SqlServerSimulator.Tests.EFCore/EFCoreDateTime.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ public async Task InsertAsync_DateTime_RoundTrips()
3838
[TestMethod]
3939
public void Insert_NullableDateTime_AcceptsNull()
4040
{
41-
using var context = new TestDbContext(TestDbContext.CreateEventsSimulation());
42-
_ = context.Events.Add(new Event { Id = 1, CreatedAt = new DateTime(2026, 5, 4) });
43-
_ = context.SaveChanges();
41+
using var context = new TestDbContext(TestDbContext.CreateEventsSimulation()).WithSaved(new Event { Id = 1, CreatedAt = new DateTime(2026, 5, 4) });
4442

4543
Assert.IsNull(context.Events.Select(e => e.Updated).First());
4644
}
@@ -182,19 +180,15 @@ public void Insert_LegacyDateTime_RoundTripsAtTickGranularity()
182180
[TestMethod]
183181
public void Insert_LegacyDateTime_999msRollsToNextSecond()
184182
{
185-
using var context = new TestDbContext(TestDbContext.CreateEventsSimulation());
186-
_ = context.Events.Add(new Event { Id = 1, CreatedAt = new DateTime(2026, 5, 4), Started = new DateTime(2026, 5, 4, 13, 45, 30, 999) });
187-
_ = context.SaveChanges();
183+
using var context = new TestDbContext(TestDbContext.CreateEventsSimulation()).WithSaved(new Event { Id = 1, CreatedAt = new DateTime(2026, 5, 4), Started = new DateTime(2026, 5, 4, 13, 45, 30, 999) });
188184

189185
Assert.AreEqual(new DateTime(2026, 5, 4, 13, 45, 31), context.Events.Select(e => e.Started).First());
190186
}
191187

192188
[TestMethod]
193189
public void Insert_NullableLegacyDateTime_AcceptsNull()
194190
{
195-
using var context = new TestDbContext(TestDbContext.CreateEventsSimulation());
196-
_ = context.Events.Add(new Event { Id = 1, CreatedAt = new DateTime(2026, 5, 4) });
197-
_ = context.SaveChanges();
191+
using var context = new TestDbContext(TestDbContext.CreateEventsSimulation()).WithSaved(new Event { Id = 1, CreatedAt = new DateTime(2026, 5, 4) });
198192

199193
Assert.IsNull(context.Events.Select(e => e.Started).First());
200194
Assert.IsNull(context.Events.Select(e => e.Ended).First());

SqlServerSimulator.Tests.EFCore/EFCoreDecimal.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ public async Task InsertAsync_Decimal_RoundTrips()
3939
[TestMethod]
4040
public void Insert_NullableDecimal_AcceptsNull()
4141
{
42-
using var context = new TestDbContext(TestDbContext.CreateProductsSimulation());
43-
_ = context.Products.Add(new Product { Id = 1, Price = 9.99m });
44-
_ = context.SaveChanges();
42+
using var context = new TestDbContext(TestDbContext.CreateProductsSimulation()).WithSaved(new Product { Id = 1, Price = 9.99m });
4543

4644
Assert.IsNull(context.Products.Select(p => p.Discount).First());
4745
}
@@ -90,9 +88,7 @@ public void OrderBy_DecimalAscending()
9088
[TestMethod]
9189
public void Cast_DecimalToDouble_ProjectsAsFloat()
9290
{
93-
using var context = new TestDbContext(TestDbContext.CreateProductsSimulation());
94-
_ = context.Products.Add(new Product { Id = 1, Price = 19.99m });
95-
_ = context.SaveChanges();
91+
using var context = new TestDbContext(TestDbContext.CreateProductsSimulation()).WithSaved(new Product { Id = 1, Price = 19.99m });
9692

9793
var asDouble = context.Products.Select(p => (double)p.Price).Single();
9894
Assert.AreEqual(19.99, asDouble, 0.001);

SqlServerSimulator.Tests.EFCore/EFCoreIdentity.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ public void SaveChanges_AcrossBatches_ReseedsThroughHighWaterMark()
5252
{
5353
// Each SaveChanges produces a fresh INSERT/MERGE. Identity continues
5454
// counting from the table's high-water mark across calls.
55-
using var context = new TestDbContext(TestDbContext.CreateWidgetsSimulation());
56-
57-
_ = context.Widgets.Add(new Widget { Name = "first" });
58-
_ = context.SaveChanges();
55+
using var context = new TestDbContext(TestDbContext.CreateWidgetsSimulation()).WithSaved(new Widget { Name = "first" });
5956

6057
var pair = new[] { new Widget { Name = "second" }, new Widget { Name = "third" } };
6158
context.Widgets.AddRange(pair);
@@ -97,7 +94,7 @@ public void SqlQueryRaw_ScopeIdentity_ReturnsLastInsertedValue()
9794
var simulation = TestDbContext.CreateWidgetsSimulation();
9895
_ = simulation
9996
.CreateOpenConnection()
100-
.CreateCommand("insert into Widgets (Name) values ('First'),('Second')")
97+
.CreateCommand("insert Widgets (Name) values ('First'),('Second')")
10198
.ExecuteNonQuery();
10299

103100
using var context = new TestDbContext(simulation);

SqlServerSimulator.Tests.EFCore/EFCoreLike.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,9 @@ public void EFFunctionsLike_WithEscape()
9494
// The 3-arg EF.Functions.Like overload threads an ESCAPE clause through
9595
// to the simulator. Insert a row whose name contains a literal '%' and
9696
// verify the escaped pattern matches it without treating '%' as wild.
97-
using var context = new TestDbContext(TestDbContext.CreatePeopleSimulation());
98-
context.People.AddRange(
97+
using var context = new TestDbContext(TestDbContext.CreatePeopleSimulation()).WithSaved(
9998
new Person { Id = 1, Name = "100% Pure" },
10099
new Person { Id = 2, Name = "Mostly Pure" });
101-
_ = context.SaveChanges();
102100

103101
var ids = context.People.Where(p => EF.Functions.Like(p.Name, "%!%%", "!")).Select(p => p.Id).ToArray();
104102
CollectionAssert.AreEqual(new[] { 1 }, ids);
@@ -108,12 +106,10 @@ public void EFFunctionsLike_WithEscape()
108106
public void StartsWith_OnVarcharColumn_AlsoWorks()
109107
{
110108
// Code is varchar(10) — exercises the CP1252 path rather than nvarchar.
111-
using var context = new TestDbContext(TestDbContext.CreatePeopleSimulation());
112-
context.People.AddRange(
109+
using var context = new TestDbContext(TestDbContext.CreatePeopleSimulation()).WithSaved(
113110
new Person { Id = 1, Name = "x", Code = "ABC-1" },
114111
new Person { Id = 2, Name = "y", Code = "ABC-2" },
115112
new Person { Id = 3, Name = "z", Code = "XYZ-1" });
116-
_ = context.SaveChanges();
117113

118114
var ids = context.People.Where(p => p.Code!.StartsWith("ABC")).OrderBy(p => p.Id).Select(p => p.Id).ToArray();
119115
CollectionAssert.AreEqual(new[] { 1, 2 }, ids);

SqlServerSimulator.Tests.EFCore/EFCoreMaxTypes.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ public class EFCoreMaxTypes
1616
public void Insert_NVarcharMax_DefaultStringMapping_RoundTrips()
1717
{
1818
// Body has no [Column] annotation; EF picks nvarchar(max) by default.
19-
using var context = new TestDbContext(TestDbContext.CreateArticlesSimulation());
20-
_ = context.Articles.Add(new Article { Id = 1, Body = "hello world" });
21-
_ = context.SaveChanges();
19+
using var context = new TestDbContext(TestDbContext.CreateArticlesSimulation()).WithSaved(new Article { Id = 1, Body = "hello world" });
2220

2321
Assert.AreEqual("hello world", context.Articles.Select(a => a.Body).First());
2422
}
@@ -78,12 +76,10 @@ public async Task InsertAsync_NVarcharMax_RoundTrips()
7876
[TestMethod]
7977
public void Where_FiltersByNVarcharMaxEquality()
8078
{
81-
using var context = new TestDbContext(TestDbContext.CreateArticlesSimulation());
82-
context.Articles.AddRange(
79+
using var context = new TestDbContext(TestDbContext.CreateArticlesSimulation()).WithSaved(
8380
new Article { Id = 1, Body = "first" },
8481
new Article { Id = 2, Body = "second" },
8582
new Article { Id = 3, Body = "third" });
86-
_ = context.SaveChanges();
8783

8884
var match = context.Articles.Where(a => a.Body == "second").Select(a => a.Id).Single();
8985
Assert.AreEqual(2, match);
@@ -92,9 +88,7 @@ public void Where_FiltersByNVarcharMaxEquality()
9288
[TestMethod]
9389
public void Insert_NullableVarcharMax_AcceptsNull()
9490
{
95-
using var context = new TestDbContext(TestDbContext.CreateArticlesSimulation());
96-
_ = context.Articles.Add(new Article { Id = 1, Body = "body" });
97-
_ = context.SaveChanges();
91+
using var context = new TestDbContext(TestDbContext.CreateArticlesSimulation()).WithSaved(new Article { Id = 1, Body = "body" });
9892

9993
Assert.IsNull(context.Articles.Select(a => a.Summary).First());
10094
}

SqlServerSimulator.Tests.EFCore/EFCoreOrderingAndDistinct.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,10 @@ public void OrderBy_ThenBy_OrdersByPrimaryThenSecondary()
6262
[TestMethod]
6363
public void OrderBy_String_RespectsCollationCaseInsensitive()
6464
{
65-
using var context = new TestDbContext(TestDbContext.CreatePeopleSimulation());
66-
context.People.AddRange(
65+
using var context = new TestDbContext(TestDbContext.CreatePeopleSimulation()).WithSaved(
6766
new Person { Id = 1, Name = "B" },
6867
new Person { Id = 2, Name = "a" },
6968
new Person { Id = 3, Name = "C" });
70-
_ = context.SaveChanges();
7169

7270
// Default collation case-insensitive: 'a' sorts with 'A', so order is a, B, C.
7371
var names = context.People.OrderBy(p => p.Name).Select(p => p.Name).ToArray();
@@ -133,13 +131,11 @@ public void Distinct_OnSingleColumn_RemovesDuplicates()
133131
[TestMethod]
134132
public void Distinct_OnMultiColumn_DedupesByTuple()
135133
{
136-
using var context = new TestDbContext(TestDbContext.CreatePeopleSimulation());
137-
context.People.AddRange(
134+
using var context = new TestDbContext(TestDbContext.CreatePeopleSimulation()).WithSaved(
138135
new Person { Id = 1, Name = "Alice", Code = "A" },
139136
new Person { Id = 2, Name = "Alice", Code = "A" }, // duplicate (Name, Code)
140137
new Person { Id = 3, Name = "Alice", Code = "B" }, // different Code
141138
new Person { Id = 4, Name = "Bob", Code = "A" });
142-
_ = context.SaveChanges();
143139

144140
var pairs = context.People
145141
.Select(p => new { p.Name, p.Code })
@@ -165,12 +161,10 @@ public async Task OrderBy_FirstAsync_ReturnsLowestKey()
165161
[TestMethod]
166162
public void OrderBy_DateTime_ReturnsChronologicalOrder()
167163
{
168-
using var context = new TestDbContext(TestDbContext.CreateEventsSimulation());
169-
context.Events.AddRange(
164+
using var context = new TestDbContext(TestDbContext.CreateEventsSimulation()).WithSaved(
170165
new Event { Id = 1, CreatedAt = new DateTime(2026, 5, 4) },
171166
new Event { Id = 2, CreatedAt = new DateTime(2024, 1, 15) },
172167
new Event { Id = 3, CreatedAt = new DateTime(2025, 7, 22) });
173-
_ = context.SaveChanges();
174168

175169
var times = context.Events
176170
.OrderByDescending(e => e.CreatedAt)

SqlServerSimulator.Tests.EFCore/EFCorePrimaryKey.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ public class EFCorePrimaryKey
1616
[TestMethod]
1717
public void SaveChanges_FirstRow_RoundTripsThroughPrimaryKey()
1818
{
19-
using var context = new TestDbContext(TestDbContext.CreateInventorySimulation());
20-
21-
_ = context.Inventory.Add(new Inventory { Sku = "WIDGET-A", Quantity = 7 });
22-
_ = context.SaveChanges();
19+
using var context = new TestDbContext(TestDbContext.CreateInventorySimulation()).WithSaved(new Inventory { Sku = "WIDGET-A", Quantity = 7 });
2320

2421
var fetched = context.Inventory.AsNoTracking().Single();
2522
Assert.AreEqual("WIDGET-A", fetched.Sku);
@@ -29,10 +26,7 @@ public void SaveChanges_FirstRow_RoundTripsThroughPrimaryKey()
2926
[TestMethod]
3027
public void SaveChanges_DuplicateKey_RaisesDbUpdateException()
3128
{
32-
using var context = new TestDbContext(TestDbContext.CreateInventorySimulation());
33-
34-
_ = context.Inventory.Add(new Inventory { Sku = "WIDGET-A", Quantity = 1 });
35-
_ = context.SaveChanges();
29+
using var context = new TestDbContext(TestDbContext.CreateInventorySimulation()).WithSaved(new Inventory { Sku = "WIDGET-A", Quantity = 1 });
3630

3731
using var context2 = new TestDbContext(context.Simulation);
3832
_ = context2.Inventory.Add(new Inventory { Sku = "WIDGET-A", Quantity = 99 });
@@ -50,9 +44,7 @@ public void SaveChanges_BatchWithMidBatchPkViolation_RollsBackEntireBatch()
5044
// statement atomicity (Bundle 1) means a mid-batch PK collision rolls
5145
// back the entire INSERT — neither the valid rows before nor after
5246
// the collision land in the table.
53-
using var context = new TestDbContext(TestDbContext.CreateInventorySimulation());
54-
_ = context.Inventory.Add(new Inventory { Sku = "EXISTING", Quantity = 1 });
55-
_ = context.SaveChanges();
47+
using var context = new TestDbContext(TestDbContext.CreateInventorySimulation()).WithSaved(new Inventory { Sku = "EXISTING", Quantity = 1 });
5648

5749
using var context2 = new TestDbContext(context.Simulation);
5850
_ = context2.Inventory.Add(new Inventory { Sku = "NEW-1", Quantity = 10 });

0 commit comments

Comments
 (0)