|
| 1 | +using System.Data.Common; |
| 2 | +using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert; |
| 3 | + |
| 4 | +namespace SqlServerSimulator; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Behavioral tests for the <c>rowversion</c> / <c>timestamp</c> type: |
| 8 | +/// auto-generation on INSERT, auto-bump on UPDATE, rejection of explicit |
| 9 | +/// values (Msg 273 / Msg 272), one-per-table (Msg 2738), implicit |
| 10 | +/// NOT NULL, comparison with <c>varbinary</c> for optimistic-concurrency |
| 11 | +/// WHERE clauses, and <c>CAST</c> outbound to <c>bigint</c> / |
| 12 | +/// <c>varbinary</c>. Sourced from probes against SQL Server 2025. |
| 13 | +/// </summary> |
| 14 | +[TestClass] |
| 15 | +public sealed class RowVersionTests |
| 16 | +{ |
| 17 | + private static byte[] ReadRowVersion(DbCommand command, int ordinal = 0) |
| 18 | + { |
| 19 | + using var reader = command.ExecuteReader(); |
| 20 | + IsTrue(reader.Read()); |
| 21 | + return (byte[])reader.GetValue(ordinal); |
| 22 | + } |
| 23 | + |
| 24 | + private static List<byte[]> ReadAllRowVersions(DbCommand command) |
| 25 | + { |
| 26 | + using var reader = command.ExecuteReader(); |
| 27 | + var values = new List<byte[]>(); |
| 28 | + while (reader.Read()) |
| 29 | + values.Add((byte[])reader.GetValue(0)); |
| 30 | + return values; |
| 31 | + } |
| 32 | + |
| 33 | + // === Type declaration === |
| 34 | + |
| 35 | + [TestMethod] |
| 36 | + public void RowVersion_KeywordAccepted() |
| 37 | + { |
| 38 | + var simulation = new Simulation(); |
| 39 | + _ = simulation.ExecuteNonQuery("create table t (id int, rv rowversion)"); |
| 40 | + _ = simulation.ExecuteNonQuery("insert into t (id) values (1)"); |
| 41 | + |
| 42 | + var rvs = ReadAllRowVersions(simulation.CreateCommand("select rv from t")); |
| 43 | + HasCount(1, rvs); |
| 44 | + HasCount(8, rvs[0]); |
| 45 | + } |
| 46 | + |
| 47 | + [TestMethod] |
| 48 | + public void Timestamp_KeywordAccepted_LegacySynonym() |
| 49 | + { |
| 50 | + var simulation = new Simulation(); |
| 51 | + _ = simulation.ExecuteNonQuery("create table t (id int, ts timestamp)"); |
| 52 | + _ = simulation.ExecuteNonQuery("insert into t (id) values (1)"); |
| 53 | + |
| 54 | + var rvs = ReadAllRowVersions(simulation.CreateCommand("select ts from t")); |
| 55 | + HasCount(1, rvs); |
| 56 | + HasCount(8, rvs[0]); |
| 57 | + } |
| 58 | + |
| 59 | + [TestMethod] |
| 60 | + public void TwoRowVersionColumns_RaisesMsg2738() |
| 61 | + { |
| 62 | + var ex = Throws<DbException>(() => |
| 63 | + _ = new Simulation().ExecuteNonQuery("create table t (rv1 rowversion, rv2 rowversion)")); |
| 64 | + AreEqual("2738", ex.Data["HelpLink.EvtID"]); |
| 65 | + } |
| 66 | + |
| 67 | + // === Auto-generation on INSERT === |
| 68 | + |
| 69 | + [TestMethod] |
| 70 | + public void Insert_AutoGeneratesRowVersionWhenColumnOmitted() |
| 71 | + { |
| 72 | + var simulation = new Simulation(); |
| 73 | + _ = simulation.ExecuteNonQuery("create table t (id int, rv rowversion)"); |
| 74 | + _ = simulation.ExecuteNonQuery("insert into t (id) values (1), (2), (3)"); |
| 75 | + |
| 76 | + var rvs = ReadAllRowVersions(simulation.CreateCommand("select rv from t")); |
| 77 | + HasCount(3, rvs); |
| 78 | + // Each is 8 bytes, monotonically increasing. |
| 79 | + var rv0 = BitConverter.ToUInt64(rvs[0].Reverse().ToArray()); |
| 80 | + var rv1 = BitConverter.ToUInt64(rvs[1].Reverse().ToArray()); |
| 81 | + var rv2 = BitConverter.ToUInt64(rvs[2].Reverse().ToArray()); |
| 82 | + IsLessThan(rv1, rv0); |
| 83 | + IsLessThan(rv2, rv1); |
| 84 | + } |
| 85 | + |
| 86 | + [TestMethod] |
| 87 | + public void Insert_RowVersionListedExplicitly_RaisesMsg273() |
| 88 | + { |
| 89 | + var simulation = new Simulation(); |
| 90 | + _ = simulation.ExecuteNonQuery("create table t (id int, rv rowversion)"); |
| 91 | + |
| 92 | + var ex = Throws<DbException>(() => |
| 93 | + _ = simulation.ExecuteNonQuery("insert into t (id, rv) values (1, 0x00000000000000FF)")); |
| 94 | + AreEqual("273", ex.Data["HelpLink.EvtID"]); |
| 95 | + } |
| 96 | + |
| 97 | + [TestMethod] |
| 98 | + public void Insert_NoColumnList_AutoGeneratesAndDoesntFail() |
| 99 | + { |
| 100 | + // Insert without column list: the simulator must skip rowversion when |
| 101 | + // synthesizing the destination list (rowversion is auto-only). |
| 102 | + var simulation = new Simulation(); |
| 103 | + _ = simulation.ExecuteNonQuery("create table t (id int, rv rowversion)"); |
| 104 | + _ = simulation.ExecuteNonQuery("insert into t values (1)"); |
| 105 | + |
| 106 | + HasCount(1, ReadAllRowVersions(simulation.CreateCommand("select rv from t"))); |
| 107 | + } |
| 108 | + |
| 109 | + // === Auto-bump on UPDATE === |
| 110 | + |
| 111 | + [TestMethod] |
| 112 | + public void Update_AutoBumpsRowVersionEvenWhenSetIsUnrelated() |
| 113 | + { |
| 114 | + var simulation = new Simulation(); |
| 115 | + _ = simulation.ExecuteNonQuery("create table t (id int, name varchar(20), rv rowversion)"); |
| 116 | + _ = simulation.ExecuteNonQuery("insert into t (id, name) values (1, 'a')"); |
| 117 | + |
| 118 | + var initialRv = ReadRowVersion(simulation.CreateCommand("select rv from t")); |
| 119 | + _ = simulation.ExecuteNonQuery("update t set name = 'A' where id = 1"); |
| 120 | + var afterRv = ReadRowVersion(simulation.CreateCommand("select rv from t")); |
| 121 | + |
| 122 | + IsFalse(initialRv.SequenceEqual(afterRv), "rowversion must change on UPDATE"); |
| 123 | + } |
| 124 | + |
| 125 | + [TestMethod] |
| 126 | + public void Update_SetRowVersion_RaisesMsg272() |
| 127 | + { |
| 128 | + var simulation = new Simulation(); |
| 129 | + _ = simulation.ExecuteNonQuery("create table t (id int, rv rowversion)"); |
| 130 | + _ = simulation.ExecuteNonQuery("insert into t (id) values (1)"); |
| 131 | + |
| 132 | + var ex = Throws<DbException>(() => |
| 133 | + _ = simulation.ExecuteNonQuery("update t set rv = 0x00 where id = 1")); |
| 134 | + AreEqual("272", ex.Data["HelpLink.EvtID"]); |
| 135 | + } |
| 136 | + |
| 137 | + // === CAST outbound === |
| 138 | + |
| 139 | + [TestMethod] |
| 140 | + public void Cast_RowVersion_To_Varbinary8() |
| 141 | + { |
| 142 | + var simulation = new Simulation(); |
| 143 | + _ = simulation.ExecuteNonQuery("create table t (id int, rv rowversion)"); |
| 144 | + _ = simulation.ExecuteNonQuery("insert into t (id) values (1)"); |
| 145 | + |
| 146 | + using var connection = simulation.CreateOpenConnection(); |
| 147 | + using var reader = connection.CreateCommand("select cast(rv as varbinary(8)) from t").ExecuteReader(); |
| 148 | + IsTrue(reader.Read()); |
| 149 | + var bytes = (byte[])reader.GetValue(0); |
| 150 | + HasCount(8, bytes); |
| 151 | + } |
| 152 | + |
| 153 | + [TestMethod] |
| 154 | + public void Cast_RowVersion_To_BigInt_BigEndian() |
| 155 | + { |
| 156 | + var simulation = new Simulation(); |
| 157 | + _ = simulation.ExecuteNonQuery("create table t (id int, rv rowversion)"); |
| 158 | + _ = simulation.ExecuteNonQuery("insert into t (id) values (1)"); |
| 159 | + |
| 160 | + var asBigInt = (long)simulation.ExecuteScalar("select cast(rv as bigint) from t")!; |
| 161 | + // The exact value depends on the simulation's counter, but it must |
| 162 | + // match the big-endian interpretation of the same bytes. |
| 163 | + var rvBytes = ReadRowVersion(simulation.CreateCommand("select rv from t")); |
| 164 | + var expected = System.Buffers.Binary.BinaryPrimitives.ReadInt64BigEndian(rvBytes); |
| 165 | + AreEqual(expected, asBigInt); |
| 166 | + } |
| 167 | + |
| 168 | + // === Comparison with varbinary (optimistic-concurrency WHERE) === |
| 169 | + |
| 170 | + [TestMethod] |
| 171 | + public void Where_RowVersion_Equals_VarbinaryParameter() |
| 172 | + { |
| 173 | + var simulation = new Simulation(); |
| 174 | + _ = simulation.ExecuteNonQuery("create table t (id int, rv rowversion)"); |
| 175 | + _ = simulation.ExecuteNonQuery("insert into t (id) values (1)"); |
| 176 | + |
| 177 | + // Capture the current rv, then use it as a varbinary parameter in WHERE. |
| 178 | + var rvBytes = ReadRowVersion(simulation.CreateCommand("select rv from t")); |
| 179 | + |
| 180 | + using var connection = simulation.CreateOpenConnection(); |
| 181 | + using var update = connection.CreateCommand("update t set id = 99 output 1 where id = 1 and rv = @originalRv"); |
| 182 | + var p = update.CreateParameter(); |
| 183 | + p.ParameterName = "@originalRv"; |
| 184 | + p.Value = rvBytes; |
| 185 | + _ = update.Parameters.Add(p); |
| 186 | + |
| 187 | + using var reader = update.ExecuteReader(); |
| 188 | + IsTrue(reader.Read(), "rowversion = varbinary parameter should match the row"); |
| 189 | + } |
| 190 | + |
| 191 | + [TestMethod] |
| 192 | + public void Where_RowVersion_Equals_StaleVarbinary_NoMatch() |
| 193 | + { |
| 194 | + // Concurrency-violation case: a stale rowversion doesn't match. |
| 195 | + var simulation = new Simulation(); |
| 196 | + _ = simulation.ExecuteNonQuery("create table t (id int, rv rowversion)"); |
| 197 | + _ = simulation.ExecuteNonQuery("insert into t (id) values (1)"); |
| 198 | + |
| 199 | + var staleRv = new byte[8]; // all zeros, never a real rowversion |
| 200 | + using var connection = simulation.CreateOpenConnection(); |
| 201 | + using var update = connection.CreateCommand("update t set id = 99 output 1 where rv = @stale"); |
| 202 | + var p = update.CreateParameter(); |
| 203 | + p.ParameterName = "@stale"; |
| 204 | + p.Value = staleRv; |
| 205 | + _ = update.Parameters.Add(p); |
| 206 | + |
| 207 | + using var reader = update.ExecuteReader(); |
| 208 | + IsFalse(reader.Read()); |
| 209 | + } |
| 210 | +} |
0 commit comments