|
| 1 | +using System.Data.Common; |
| 2 | +using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert; |
| 3 | + |
| 4 | +namespace SqlServerSimulator; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Behavioral tests for <c>DELETE FROM table [WHERE pred]</c> (the |
| 8 | +/// <c>FROM</c> keyword is optional in T-SQL). Covers row-count return, |
| 9 | +/// no-WHERE bulk delete, no-match zero-affected, post-delete enumeration |
| 10 | +/// (tombstoned slots invisible), and post-delete INSERT (slot directory |
| 11 | +/// continues from current count). LOB chain reclamation isn't part of |
| 12 | +/// this bundle — see CLAUDE.md for the leak quirk. |
| 13 | +/// </summary> |
| 14 | +[TestClass] |
| 15 | +public sealed class DeleteTests |
| 16 | +{ |
| 17 | + private static List<int> ReadInts(DbCommand command) |
| 18 | + { |
| 19 | + using var reader = command.ExecuteReader(); |
| 20 | + var values = new List<int>(); |
| 21 | + while (reader.Read()) |
| 22 | + values.Add(reader.GetInt32(0)); |
| 23 | + return values; |
| 24 | + } |
| 25 | + |
| 26 | + [TestMethod] |
| 27 | + public void Delete_BasicWhere_RemovesOneRow() |
| 28 | + { |
| 29 | + var simulation = new Simulation(); |
| 30 | + _ = simulation.ExecuteNonQuery("create table t (id int, v int)"); |
| 31 | + _ = simulation.ExecuteNonQuery("insert into t values (1, 10), (2, 20), (3, 30)"); |
| 32 | + |
| 33 | + var affected = simulation.ExecuteNonQuery("delete from t where id = 2"); |
| 34 | + AreEqual(1, affected); |
| 35 | + |
| 36 | + var values = ReadInts(simulation.CreateCommand("select id from t order by id")); |
| 37 | + CollectionAssert.AreEqual(new[] { 1, 3 }, values); |
| 38 | + } |
| 39 | + |
| 40 | + [TestMethod] |
| 41 | + public void Delete_NoWhere_RemovesAllRows() |
| 42 | + { |
| 43 | + var simulation = new Simulation(); |
| 44 | + _ = simulation.ExecuteNonQuery("create table t (id int)"); |
| 45 | + _ = simulation.ExecuteNonQuery("insert into t values (1), (2), (3), (4)"); |
| 46 | + |
| 47 | + var affected = simulation.ExecuteNonQuery("delete from t"); |
| 48 | + AreEqual(4, affected); |
| 49 | + |
| 50 | + var values = ReadInts(simulation.CreateCommand("select id from t")); |
| 51 | + IsEmpty(values); |
| 52 | + } |
| 53 | + |
| 54 | + [TestMethod] |
| 55 | + public void Delete_WhereNoMatch_ZeroAffected() |
| 56 | + { |
| 57 | + var simulation = new Simulation(); |
| 58 | + _ = simulation.ExecuteNonQuery("create table t (id int)"); |
| 59 | + _ = simulation.ExecuteNonQuery("insert into t values (1), (2)"); |
| 60 | + |
| 61 | + var affected = simulation.ExecuteNonQuery("delete from t where id = 999"); |
| 62 | + AreEqual(0, affected); |
| 63 | + |
| 64 | + var values = ReadInts(simulation.CreateCommand("select id from t order by id")); |
| 65 | + CollectionAssert.AreEqual(new[] { 1, 2 }, values); |
| 66 | + } |
| 67 | + |
| 68 | + [TestMethod] |
| 69 | + public void Delete_OptionalFromKeyword_StillWorks() |
| 70 | + { |
| 71 | + // T-SQL allows DELETE without FROM (FROM is optional in single-table form). |
| 72 | + var simulation = new Simulation(); |
| 73 | + _ = simulation.ExecuteNonQuery("create table t (id int)"); |
| 74 | + _ = simulation.ExecuteNonQuery("insert into t values (1), (2)"); |
| 75 | + |
| 76 | + var affected = simulation.ExecuteNonQuery("delete t where id = 1"); |
| 77 | + AreEqual(1, affected); |
| 78 | + } |
| 79 | + |
| 80 | + [TestMethod] |
| 81 | + public void Delete_NonexistentTable_RaisesMsg208() |
| 82 | + { |
| 83 | + var ex = Throws<DbException>(() => _ = new Simulation().ExecuteNonQuery("delete from no_such")); |
| 84 | + AreEqual("208", ex.Data["HelpLink.EvtID"]); |
| 85 | + } |
| 86 | + |
| 87 | + [TestMethod] |
| 88 | + public void Delete_ThenInsert_NewRowVisibleAfterTombstone() |
| 89 | + { |
| 90 | + // Verifies tombstoned slots don't block subsequent INSERTs from |
| 91 | + // creating new visible rows in the heap. |
| 92 | + var simulation = new Simulation(); |
| 93 | + _ = simulation.ExecuteNonQuery("create table t (id int, v varchar(20))"); |
| 94 | + _ = simulation.ExecuteNonQuery("insert into t values (1, 'a'), (2, 'b')"); |
| 95 | + _ = simulation.ExecuteNonQuery("delete from t where id = 1"); |
| 96 | + _ = simulation.ExecuteNonQuery("insert into t values (3, 'c')"); |
| 97 | + |
| 98 | + var values = ReadInts(simulation.CreateCommand("select id from t order by id")); |
| 99 | + CollectionAssert.AreEqual(new[] { 2, 3 }, values); |
| 100 | + } |
| 101 | + |
| 102 | + [TestMethod] |
| 103 | + public void Delete_AllThenInsert_HeapStillFunctional() |
| 104 | + { |
| 105 | + var simulation = new Simulation(); |
| 106 | + _ = simulation.ExecuteNonQuery("create table t (id int)"); |
| 107 | + _ = simulation.ExecuteNonQuery("insert into t values (1), (2), (3)"); |
| 108 | + _ = simulation.ExecuteNonQuery("delete from t"); |
| 109 | + _ = simulation.ExecuteNonQuery("insert into t values (4), (5)"); |
| 110 | + |
| 111 | + var values = ReadInts(simulation.CreateCommand("select id from t order by id")); |
| 112 | + CollectionAssert.AreEqual(new[] { 4, 5 }, values); |
| 113 | + } |
| 114 | + |
| 115 | + // === OUTPUT clause (literal-only) === |
| 116 | + |
| 117 | + [TestMethod] |
| 118 | + public void Delete_OutputLiteralOne_YieldsOneRowPerAffected() |
| 119 | + { |
| 120 | + // EF Core emits `DELETE FROM ... OUTPUT 1 WHERE ...` on |
| 121 | + // SaveChanges-Remove. Verifies the OUTPUT projection runs once per |
| 122 | + // deleted row. |
| 123 | + var simulation = new Simulation(); |
| 124 | + _ = simulation.ExecuteNonQuery("create table t (id int)"); |
| 125 | + _ = simulation.ExecuteNonQuery("insert into t values (1), (2), (3)"); |
| 126 | + |
| 127 | + using var connection = simulation.CreateOpenConnection(); |
| 128 | + using var reader = connection.CreateCommand("delete from t output 1 where id <= 2").ExecuteReader(); |
| 129 | + var ones = new List<int>(); |
| 130 | + while (reader.Read()) |
| 131 | + ones.Add(reader.GetInt32(0)); |
| 132 | + CollectionAssert.AreEqual(new[] { 1, 1 }, ones); |
| 133 | + } |
| 134 | + |
| 135 | + [TestMethod] |
| 136 | + public void Delete_OutputDeletedColumn_RaisesNotSupported() |
| 137 | + { |
| 138 | + var simulation = new Simulation(); |
| 139 | + _ = simulation.ExecuteNonQuery("create table t (id int)"); |
| 140 | + _ = simulation.ExecuteNonQuery("insert into t values (1)"); |
| 141 | + |
| 142 | + _ = Throws<NotSupportedException>(() => |
| 143 | + _ = simulation.ExecuteScalar("delete from t output deleted.id where id = 1")); |
| 144 | + } |
| 145 | + |
| 146 | + [TestMethod] |
| 147 | + public void Delete_PrimaryKeyTable_AllowsReinsertAfterDelete() |
| 148 | + { |
| 149 | + // After DELETE removes the row with k=1, a new INSERT of k=1 |
| 150 | + // must succeed (no PK collision against tombstoned data). |
| 151 | + var simulation = new Simulation(); |
| 152 | + _ = simulation.ExecuteNonQuery("create table t (k int primary key, v int)"); |
| 153 | + _ = simulation.ExecuteNonQuery("insert into t values (1, 10)"); |
| 154 | + _ = simulation.ExecuteNonQuery("delete from t where k = 1"); |
| 155 | + _ = simulation.ExecuteNonQuery("insert into t values (1, 20)"); |
| 156 | + |
| 157 | + var values = ReadInts(simulation.CreateCommand("select v from t")); |
| 158 | + CollectionAssert.AreEqual(new[] { 20 }, values); |
| 159 | + } |
| 160 | +} |
0 commit comments