|
| 1 | +using Microsoft.EntityFrameworkCore; |
| 2 | + |
| 3 | +namespace SqlServerSimulator; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Exercises the simulator's <c>varchar</c> / <c>nvarchar</c> column support |
| 7 | +/// through EF Core's idiomatic surface: typed entities mapped via attributes, |
| 8 | +/// LINQ projections, and SaveChanges for the write path. Truncation surfaces |
| 9 | +/// as DbUpdateException with the simulator's SimulatedSqlException as the |
| 10 | +/// inner exception, matching real SQL Server's failure shape under EF Core. |
| 11 | +/// </summary> |
| 12 | +[TestClass] |
| 13 | +public class EFCoreStrings |
| 14 | +{ |
| 15 | + public TestContext TestContext { get; set; } = null!; |
| 16 | + |
| 17 | + [TestMethod] |
| 18 | + public void Insert_NameRoundTripsViaProjection() |
| 19 | + { |
| 20 | + using var context = new TestDbContext(TestDbContext.CreatePeopleSimulation()); |
| 21 | + |
| 22 | + _ = context.People.Add(new Person { Id = 1, Name = "Alice" }); |
| 23 | + _ = context.SaveChanges(); |
| 24 | + |
| 25 | + Assert.AreEqual("Alice", context.People.Select(p => p.Name).FirstOrDefault()); |
| 26 | + } |
| 27 | + |
| 28 | + [TestMethod] |
| 29 | + public async Task InsertAsync_NameRoundTrips() |
| 30 | + { |
| 31 | + await using var context = new TestDbContext(TestDbContext.CreatePeopleSimulation()); |
| 32 | + |
| 33 | + _ = context.People.Add(new Person { Id = 1, Name = "Alice" }); |
| 34 | + _ = await context.SaveChangesAsync(this.TestContext.CancellationToken); |
| 35 | + |
| 36 | + Assert.AreEqual("Alice", await context.People.Select(p => p.Name).FirstOrDefaultAsync(this.TestContext.CancellationToken)); |
| 37 | + } |
| 38 | + |
| 39 | + [TestMethod] |
| 40 | + public void Insert_VarcharCodeRoundTrips() |
| 41 | + { |
| 42 | + // Code is varchar(10) — the UTF-8 storage path. ASCII fits 1:1 with bytes. |
| 43 | + using var context = new TestDbContext(TestDbContext.CreatePeopleSimulation()); |
| 44 | + |
| 45 | + _ = context.People.Add(new Person { Id = 1, Name = "Bob", Code = "ABC123" }); |
| 46 | + _ = context.SaveChanges(); |
| 47 | + |
| 48 | + Assert.AreEqual("ABC123", context.People.Select(p => p.Code).FirstOrDefault()); |
| 49 | + } |
| 50 | + |
| 51 | + [TestMethod] |
| 52 | + public void Insert_NullableCodeAcceptsNull() |
| 53 | + { |
| 54 | + using var context = new TestDbContext(TestDbContext.CreatePeopleSimulation()); |
| 55 | + |
| 56 | + _ = context.People.Add(new Person { Id = 1, Name = "Carol", Code = null }); |
| 57 | + _ = context.SaveChanges(); |
| 58 | + |
| 59 | + Assert.IsNull(context.People.Select(p => p.Code).FirstOrDefault()); |
| 60 | + } |
| 61 | + |
| 62 | + [TestMethod] |
| 63 | + public void Insert_NameAtMaxLengthSucceeds() |
| 64 | + { |
| 65 | + // nvarchar(50) — 50 UCS-2 code units exactly fits. |
| 66 | + using var context = new TestDbContext(TestDbContext.CreatePeopleSimulation()); |
| 67 | + |
| 68 | + var atLimit = new string('x', 50); |
| 69 | + _ = context.People.Add(new Person { Id = 1, Name = atLimit }); |
| 70 | + _ = context.SaveChanges(); |
| 71 | + |
| 72 | + Assert.AreEqual(atLimit, context.People.Select(p => p.Name).FirstOrDefault()); |
| 73 | + } |
| 74 | + |
| 75 | + [TestMethod] |
| 76 | + public void Insert_NameOverMaxLengthRaisesUpdateException() |
| 77 | + { |
| 78 | + // EF Core wraps the simulator's SimulatedSqlException in DbUpdateException, |
| 79 | + // matching real SQL Server's failure shape — the Msg 2628 truncation error |
| 80 | + // surfaces on InnerException. |
| 81 | + using var context = new TestDbContext(TestDbContext.CreatePeopleSimulation()); |
| 82 | + |
| 83 | + _ = context.People.Add(new Person { Id = 1, Name = new string('x', 51) }); |
| 84 | + |
| 85 | + var ex = Assert.Throws<DbUpdateException>(() => context.SaveChanges()); |
| 86 | + Assert.IsNotNull(ex.InnerException); |
| 87 | + StringAssert.Contains(ex.InnerException.Message, "would be truncated"); |
| 88 | + StringAssert.Contains(ex.InnerException.Message, "People"); |
| 89 | + StringAssert.Contains(ex.InnerException.Message, "Name"); |
| 90 | + } |
| 91 | + |
| 92 | + [TestMethod] |
| 93 | + public void Insert_VarcharCodeOutOfCp1252_RoundTripsAsReplacement() |
| 94 | + { |
| 95 | + // varchar uses Windows-1252; characters outside CP1252 (CJK, emoji, |
| 96 | + // non-Latin scripts) silently round-trip as '?', matching SQL Server's |
| 97 | + // default collation. EF Core surfaces this without intervention. |
| 98 | + using var context = new TestDbContext(TestDbContext.CreatePeopleSimulation()); |
| 99 | + |
| 100 | + _ = context.People.Add(new Person { Id = 1, Name = "Eve", Code = "日本" }); |
| 101 | + _ = context.SaveChanges(); |
| 102 | + |
| 103 | + Assert.AreEqual("??", context.People.Select(p => p.Code).FirstOrDefault()); |
| 104 | + } |
| 105 | + |
| 106 | + [TestMethod] |
| 107 | + public void Insert_NVarcharAcceptsSupplementaryCharacter() |
| 108 | + { |
| 109 | + // 🎉 is one Unicode code point but two UTF-16 code units (surrogate pair). |
| 110 | + // It fits in nvarchar(50) — the simulator's check is on UTF-16 code units, |
| 111 | + // matching SQL Server's nvarchar semantics. |
| 112 | + using var context = new TestDbContext(TestDbContext.CreatePeopleSimulation()); |
| 113 | + |
| 114 | + _ = context.People.Add(new Person { Id = 1, Name = "🎉 party 🎉" }); |
| 115 | + _ = context.SaveChanges(); |
| 116 | + |
| 117 | + Assert.AreEqual("🎉 party 🎉", context.People.Select(p => p.Name).FirstOrDefault()); |
| 118 | + } |
| 119 | + |
| 120 | + [TestMethod] |
| 121 | + public void Insert_VarbinaryAvatarRoundTrips() |
| 122 | + { |
| 123 | + using var context = new TestDbContext(TestDbContext.CreatePeopleSimulation()); |
| 124 | + |
| 125 | + var bytes = new byte[] { 0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0xFF }; |
| 126 | + _ = context.People.Add(new Person { Id = 1, Name = "Frank", Avatar = bytes }); |
| 127 | + _ = context.SaveChanges(); |
| 128 | + |
| 129 | + var read = context.People.Select(p => p.Avatar).FirstOrDefault(); |
| 130 | + CollectionAssert.AreEqual(bytes, read); |
| 131 | + } |
| 132 | + |
| 133 | + [TestMethod] |
| 134 | + public void Insert_VarbinaryAvatarOverMax_RaisesTruncationWithHexValue() |
| 135 | + { |
| 136 | + using var context = new TestDbContext(TestDbContext.CreatePeopleSimulation()); |
| 137 | + |
| 138 | + var oversize = new byte[65]; // Avatar is varbinary(64) |
| 139 | + for (var i = 0; i < oversize.Length; i++) |
| 140 | + oversize[i] = (byte)i; |
| 141 | + |
| 142 | + _ = context.People.Add(new Person { Id = 1, Name = "Greta", Avatar = oversize }); |
| 143 | + |
| 144 | + var ex = Assert.Throws<DbUpdateException>(() => context.SaveChanges()); |
| 145 | + Assert.IsNotNull(ex.InnerException); |
| 146 | + StringAssert.Contains(ex.InnerException.Message, "would be truncated"); |
| 147 | + StringAssert.Contains(ex.InnerException.Message, "Avatar"); |
| 148 | + StringAssert.Contains(ex.InnerException.Message, "0x"); // hex prefix, not string |
| 149 | + } |
| 150 | + |
| 151 | + [TestMethod] |
| 152 | + public void Insert_NullableAvatarAcceptsNull() |
| 153 | + { |
| 154 | + using var context = new TestDbContext(TestDbContext.CreatePeopleSimulation()); |
| 155 | + |
| 156 | + _ = context.People.Add(new Person { Id = 1, Name = "Hank", Avatar = null }); |
| 157 | + _ = context.SaveChanges(); |
| 158 | + |
| 159 | + Assert.IsNull(context.People.Select(p => p.Avatar).FirstOrDefault()); |
| 160 | + } |
| 161 | + |
| 162 | + [TestMethod] |
| 163 | + public void Insert_MultipleRows_RoundTripsBothColumns() |
| 164 | + { |
| 165 | + using var context = new TestDbContext(TestDbContext.CreatePeopleSimulation()); |
| 166 | + |
| 167 | + context.People.AddRange( |
| 168 | + new Person { Id = 1, Name = "Alice", Code = "A" }, |
| 169 | + new Person { Id = 2, Name = "Bob", Code = "B" }, |
| 170 | + new Person { Id = 3, Name = "Carol", Code = null }); |
| 171 | + _ = context.SaveChanges(); |
| 172 | + |
| 173 | + var names = context.People.Select(p => p.Name).ToArray(); |
| 174 | + var codes = context.People.Select(p => p.Code).ToArray(); |
| 175 | + |
| 176 | + CollectionAssert.AreEquivalent(new[] { "Alice", "Bob", "Carol" }, names); |
| 177 | + CollectionAssert.AreEquivalent(new[] { "A", "B", null }, codes); |
| 178 | + } |
| 179 | +} |
0 commit comments