|
| 1 | +using System.Data.Common; |
| 2 | +using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert; |
| 3 | + |
| 4 | +namespace SqlServerSimulator; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Tests for <c>TRUNCATE TABLE</c>: row removal, identity-counter reset, |
| 8 | +/// transaction rollback semantics (rollback restores both rows AND the |
| 9 | +/// pre-truncate identity counter — distinct from the simulator's general |
| 10 | +/// "identity bypasses the log" rule that applies to INSERT). Errors and |
| 11 | +/// skip-mode interaction. Probed against SQL Server 2025 (2026-05-11). |
| 12 | +/// </summary> |
| 13 | +[TestClass] |
| 14 | +public sealed class TruncateTableTests |
| 15 | +{ |
| 16 | + [TestMethod] |
| 17 | + public void Truncate_RemovesAllRows() |
| 18 | + => AreEqual(0, new Simulation().ExecuteScalar(""" |
| 19 | + create table t (id int); |
| 20 | + insert t values (1), (2), (3); |
| 21 | + truncate table t; |
| 22 | + select count(*) from t |
| 23 | + """)); |
| 24 | + |
| 25 | + [TestMethod] |
| 26 | + public void Truncate_ResetsIdentityToSeed() |
| 27 | + => AreEqual(1, new Simulation().ExecuteScalar(""" |
| 28 | + create table t (id int identity(1,1), v int); |
| 29 | + insert t (v) values (10), (20), (30); |
| 30 | + truncate table t; |
| 31 | + insert t (v) values (99); |
| 32 | + select id from t |
| 33 | + """)); |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Custom seed (8, 3) — first insert after TRUNCATE gets the seed (8), |
| 37 | + /// not the next-after-prior-max. Probe-confirmed against SQL Server 2025. |
| 38 | + /// </summary> |
| 39 | + [TestMethod] |
| 40 | + public void Truncate_ResetsIdentityToCustomSeed() |
| 41 | + => AreEqual(8, new Simulation().ExecuteScalar(""" |
| 42 | + create table t (id int identity(8, 3), v int); |
| 43 | + insert t (v) values (10), (20); |
| 44 | + truncate table t; |
| 45 | + insert t (v) values (99); |
| 46 | + select id from t |
| 47 | + """)); |
| 48 | + |
| 49 | + [TestMethod] |
| 50 | + public void Truncate_OnEmptyTable_Succeeds() |
| 51 | + => AreEqual(0, new Simulation().ExecuteScalar(""" |
| 52 | + create table t (id int); |
| 53 | + truncate table t; |
| 54 | + select count(*) from t |
| 55 | + """)); |
| 56 | + |
| 57 | + [TestMethod] |
| 58 | + public void Truncate_OnTempTable_Works() |
| 59 | + => AreEqual(0, new Simulation().ExecuteScalar(""" |
| 60 | + create table #foo (id int); |
| 61 | + insert #foo values (1), (2); |
| 62 | + truncate table #foo; |
| 63 | + select count(*) from #foo |
| 64 | + """)); |
| 65 | + |
| 66 | + [TestMethod] |
| 67 | + public void Truncate_ThreePartName_Resolves() |
| 68 | + => AreEqual(0, new Simulation().ExecuteScalar(""" |
| 69 | + create table t (id int); |
| 70 | + insert t values (1); |
| 71 | + truncate table simulated.dbo.t; |
| 72 | + select count(*) from t |
| 73 | + """)); |
| 74 | + |
| 75 | + [TestMethod] |
| 76 | + public void Truncate_SetsRowCountToZero() |
| 77 | + { |
| 78 | + using var reader = new Simulation().ExecuteReader(""" |
| 79 | + create table t (id int); |
| 80 | + insert t values (1), (2), (3); |
| 81 | + truncate table t; |
| 82 | + select @@rowcount as rc |
| 83 | + """); |
| 84 | + IsTrue(reader.Read()); |
| 85 | + AreEqual(0, reader.GetInt32(0)); |
| 86 | + } |
| 87 | + |
| 88 | + [TestMethod] |
| 89 | + public void Truncate_NonExistentTable_Msg4701() |
| 90 | + => new Simulation().AssertSqlError( |
| 91 | + "truncate table does_not_exist", |
| 92 | + 4701, |
| 93 | + "Cannot find the object \"does_not_exist\" because it does not exist or you do not have permissions."); |
| 94 | + |
| 95 | + [TestMethod] |
| 96 | + public void Truncate_NonExistentTempTable_Msg4701() |
| 97 | + => new Simulation().AssertSqlError( |
| 98 | + "truncate table #does_not_exist", |
| 99 | + 4701); |
| 100 | + |
| 101 | + [TestMethod] |
| 102 | + public void Truncate_WithWhere_SyntaxError() |
| 103 | + { |
| 104 | + var ex = Throws<DbException>(() => new Simulation().ExecuteNonQuery(""" |
| 105 | + create table t (id int); |
| 106 | + truncate table t where id = 1 |
| 107 | + """)); |
| 108 | + AreEqual("102", ex.Data["HelpLink.EvtID"]); |
| 109 | + } |
| 110 | + |
| 111 | + // ---- Transaction rollback ---- |
| 112 | + // Probe-confirmed: ROLLBACK after TRUNCATE inside BEGIN TRAN restores |
| 113 | + // both the row data AND the identity counter. |
| 114 | + |
| 115 | + [TestMethod] |
| 116 | + public void Truncate_InTransaction_RollbackRestoresRows() |
| 117 | + => AreEqual(3, new Simulation().ExecuteScalar(""" |
| 118 | + create table t (id int); |
| 119 | + insert t values (1), (2), (3); |
| 120 | + begin tran; |
| 121 | + truncate table t; |
| 122 | + rollback; |
| 123 | + select count(*) from t |
| 124 | + """)); |
| 125 | + |
| 126 | + [TestMethod] |
| 127 | + public void Truncate_InTransaction_RollbackRestoresIdentity() |
| 128 | + { |
| 129 | + // After ROLLBACK, identity counter is back to where it was — the |
| 130 | + // next INSERT after re-inserting still continues from the prior |
| 131 | + // high-water mark. |
| 132 | + using var reader = new Simulation().ExecuteReader(""" |
| 133 | + create table t (id int identity(1,1), v int); |
| 134 | + insert t (v) values (10), (20), (30); |
| 135 | + begin tran; |
| 136 | + truncate table t; |
| 137 | + rollback; |
| 138 | + insert t (v) values (99); |
| 139 | + select id from t order by id |
| 140 | + """); |
| 141 | + var ids = new List<int>(); |
| 142 | + while (reader.Read()) ids.Add(reader.GetInt32(0)); |
| 143 | + CollectionAssert.AreEqual(new[] { 1, 2, 3, 4 }, ids); |
| 144 | + } |
| 145 | + |
| 146 | + [TestMethod] |
| 147 | + public void Truncate_InTransaction_Commit_TruncationPersists() |
| 148 | + => AreEqual(0, new Simulation().ExecuteScalar(""" |
| 149 | + create table t (id int); |
| 150 | + insert t values (1), (2), (3); |
| 151 | + begin tran; |
| 152 | + truncate table t; |
| 153 | + commit; |
| 154 | + select count(*) from t |
| 155 | + """)); |
| 156 | + |
| 157 | + [TestMethod] |
| 158 | + public void Truncate_InTransaction_InsertThenRollback_FullRestore() |
| 159 | + { |
| 160 | + // ROLLBACK undoes both the post-TRUNCATE INSERT and the TRUNCATE |
| 161 | + // itself (LIFO: INSERT undo first, then TRUNCATE undo). Final state: |
| 162 | + // the original three rows. |
| 163 | + using var reader = new Simulation().ExecuteReader(""" |
| 164 | + create table t (id int); |
| 165 | + insert t values (1), (2), (3); |
| 166 | + begin tran; |
| 167 | + truncate table t; |
| 168 | + insert t values (99); |
| 169 | + rollback; |
| 170 | + select id from t order by id |
| 171 | + """); |
| 172 | + var ids = new List<int>(); |
| 173 | + while (reader.Read()) ids.Add(reader.GetInt32(0)); |
| 174 | + CollectionAssert.AreEqual(new[] { 1, 2, 3 }, ids); |
| 175 | + } |
| 176 | + |
| 177 | + // ---- Skip-mode ---- |
| 178 | + |
| 179 | + [TestMethod] |
| 180 | + public void Truncate_InUntakenIf_TableUntouched() |
| 181 | + => AreEqual(3, new Simulation().ExecuteScalar(""" |
| 182 | + create table t (id int); |
| 183 | + insert t values (1), (2), (3); |
| 184 | + if 1=0 truncate table t; |
| 185 | + select count(*) from t |
| 186 | + """)); |
| 187 | + |
| 188 | + [TestMethod] |
| 189 | + public void Truncate_InUntakenIf_MissingTableNotChecked() |
| 190 | + { |
| 191 | + // Skip-mode gates the name resolution too — a TRUNCATE in an |
| 192 | + // un-taken branch against a non-existent table doesn't surface |
| 193 | + // Msg 4701. |
| 194 | + _ = new Simulation().ExecuteNonQuery("if 1=0 truncate table does_not_exist"); |
| 195 | + } |
| 196 | +} |
0 commit comments