|
| 1 | +using System.Data.Common; |
| 2 | +using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert; |
| 3 | +using static SqlServerSimulator.TestHelpers; |
| 4 | + |
| 5 | +namespace SqlServerSimulator; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Behavioral tests for compound assignment (<c>+=</c> / <c>-=</c> / <c>*=</c> |
| 9 | +/// / <c>/=</c> / <c>%=</c> / <c>&=</c> / <c>|=</c> / <c>^=</c>) at both |
| 10 | +/// surfaces it appears in T-SQL: <c>SET @v op= expr</c> for variables and |
| 11 | +/// <c>UPDATE t SET col op= expr</c> for table columns. Compound forms are |
| 12 | +/// implemented as a parse-time desugar — <c>@v += rhs</c> is rewritten as |
| 13 | +/// <c>FromCompoundOp('+', VariableReference(@v), rhs)</c> and the existing |
| 14 | +/// arithmetic / string-concat dispatch handles the runtime semantics |
| 15 | +/// (NULL propagation, string concat, decimal widening, divide-by-zero). |
| 16 | +/// All assertions probe-confirmed against SQL Server 2025. |
| 17 | +/// </summary> |
| 18 | +[TestClass] |
| 19 | +public sealed class CompoundAssignmentTests |
| 20 | +{ |
| 21 | + [TestMethod] |
| 22 | + public void Variable_PlusEquals_Int() |
| 23 | + => AreEqual(15, ExecuteScalar<int>("declare @v int = 10; set @v += 5; select @v")); |
| 24 | + |
| 25 | + [TestMethod] |
| 26 | + public void Variable_MinusEquals_Int() |
| 27 | + => AreEqual(7, ExecuteScalar<int>("declare @v int = 10; set @v -= 3; select @v")); |
| 28 | + |
| 29 | + [TestMethod] |
| 30 | + public void Variable_StarEquals_Int() |
| 31 | + => AreEqual(20, ExecuteScalar<int>("declare @v int = 10; set @v *= 2; select @v")); |
| 32 | + |
| 33 | + [TestMethod] |
| 34 | + public void Variable_SlashEquals_IntTruncates() |
| 35 | + => AreEqual(3, ExecuteScalar<int>("declare @v int = 10; set @v /= 3; select @v")); |
| 36 | + |
| 37 | + [TestMethod] |
| 38 | + public void Variable_PercentEquals_Int() |
| 39 | + => AreEqual(1, ExecuteScalar<int>("declare @v int = 10; set @v %= 3; select @v")); |
| 40 | + |
| 41 | + [TestMethod] |
| 42 | + public void Variable_AndEquals_Int() |
| 43 | + => AreEqual(2, ExecuteScalar<int>("declare @v int = 10; set @v &= 6; select @v")); |
| 44 | + |
| 45 | + [TestMethod] |
| 46 | + public void Variable_OrEquals_Int() |
| 47 | + => AreEqual(15, ExecuteScalar<int>("declare @v int = 10; set @v |= 5; select @v")); |
| 48 | + |
| 49 | + [TestMethod] |
| 50 | + public void Variable_XorEquals_Int() |
| 51 | + => AreEqual(12, ExecuteScalar<int>("declare @v int = 10; set @v ^= 6; select @v")); |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// NULL propagates through compound arithmetic — an uninitialized |
| 55 | + /// variable is NULL, and NULL <c>+</c> int → NULL. |
| 56 | + /// </summary> |
| 57 | + [TestMethod] |
| 58 | + public void Variable_NullPlusEquals_StaysNull() |
| 59 | + => AreEqual(DBNull.Value, ExecuteScalar("declare @v int; set @v += 5; select @v")); |
| 60 | + |
| 61 | + [TestMethod] |
| 62 | + public void Variable_StringPlusEquals_Concatenates() |
| 63 | + => AreEqual("hi there", ExecuteScalar("declare @s varchar(50) = 'hi'; set @s += ' there'; select @s")); |
| 64 | + |
| 65 | + [TestMethod] |
| 66 | + public void Variable_StringPlusEquals_NullRhs_Propagates() |
| 67 | + => AreEqual(DBNull.Value, ExecuteScalar("declare @s varchar(50) = 'hi'; set @s += cast(null as varchar(5)); select @s")); |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Decimal participation widens the same way it does for plain <c>*</c>: |
| 71 | + /// <c>decimal(10,2) * int</c> stays decimal with scale preserved. |
| 72 | + /// </summary> |
| 73 | + [TestMethod] |
| 74 | + public void Variable_DecimalStarEquals_PreservesScale() |
| 75 | + => AreEqual(7.00m, ExecuteScalar<decimal>("declare @v decimal(10,2) = 3.50; set @v *= 2; select @v")); |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Decimal compound divide-by-zero raises Msg 8134, matching the plain |
| 79 | + /// <c>cast(10 as decimal(10,2)) / 0</c> path. Integer compound |
| 80 | + /// divide-by-zero surfaces a raw <see cref="DivideByZeroException"/> |
| 81 | + /// instead — pre-existing simulator divergence (see CLAUDE.md and |
| 82 | + /// IfBlockTests's <c>Integer_DivideByZero_*</c>). |
| 83 | + /// </summary> |
| 84 | + [TestMethod] |
| 85 | + public void Variable_Decimal_DivideByZero_RaisesMsg8134() |
| 86 | + => AssertSqlError("declare @v decimal(10,2) = 10; set @v /= 0; select @v", 8134, |
| 87 | + "Divide by zero error encountered."); |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// Probe-confirmed: SQL Server tokenizes <c>+ =</c> with a space as two |
| 91 | + /// separate operators and raises Msg 102 near <c>'+'</c>. The simulator's |
| 92 | + /// adjacency check enforces no whitespace between the arith char and |
| 93 | + /// trailing <c>=</c>; the "near" token text may differ slightly from real |
| 94 | + /// SQL Server (pre-existing simulator pattern, see CLAUDE.md). |
| 95 | + /// </summary> |
| 96 | + [TestMethod] |
| 97 | + public void Variable_PlusSpaceEquals_RaisesMsg102() |
| 98 | + { |
| 99 | + var ex = Throws<DbException>(() => ExecuteScalar("declare @v int = 10; set @v + = 5; select @v")); |
| 100 | + AreEqual("102", ex.Data["HelpLink.EvtID"]); |
| 101 | + } |
| 102 | + |
| 103 | + [TestMethod] |
| 104 | + public void Variable_PlusEquals_ChainedAcrossStatements() |
| 105 | + { |
| 106 | + var simulation = new Simulation(); |
| 107 | + AreEqual(6, simulation.ExecuteScalar<int>("declare @v int = 0; set @v += 1; set @v += 2; set @v += 3; select @v")); |
| 108 | + } |
| 109 | + |
| 110 | + [TestMethod] |
| 111 | + public void Update_PlusEquals_AllRows() |
| 112 | + { |
| 113 | + var simulation = new Simulation(); |
| 114 | + _ = simulation.ExecuteNonQuery(""" |
| 115 | + create table t (id int, v int); |
| 116 | + insert t values (1, 10), (2, 20) |
| 117 | + """); |
| 118 | + AreEqual(2, simulation.ExecuteNonQuery("update t set v += 5")); |
| 119 | + using var reader = simulation.CreateCommand("select v from t order by id").ExecuteReader(); |
| 120 | + IsTrue(reader.Read()); AreEqual(15, reader.GetInt32(0)); |
| 121 | + IsTrue(reader.Read()); AreEqual(25, reader.GetInt32(0)); |
| 122 | + IsFalse(reader.Read()); |
| 123 | + } |
| 124 | + |
| 125 | + [TestMethod] |
| 126 | + public void Update_PlusEquals_WhereClause_OnlySelectedRows() |
| 127 | + { |
| 128 | + var simulation = new Simulation(); |
| 129 | + _ = simulation.ExecuteNonQuery(""" |
| 130 | + create table t (id int, v int); |
| 131 | + insert t values (1, 10), (2, 20), (3, 30) |
| 132 | + """); |
| 133 | + AreEqual(1, simulation.ExecuteNonQuery("update t set v += 100 where id = 2")); |
| 134 | + using var reader = simulation.CreateCommand("select v from t order by id").ExecuteReader(); |
| 135 | + IsTrue(reader.Read()); AreEqual(10, reader.GetInt32(0)); |
| 136 | + IsTrue(reader.Read()); AreEqual(120, reader.GetInt32(0)); |
| 137 | + IsTrue(reader.Read()); AreEqual(30, reader.GetInt32(0)); |
| 138 | + } |
| 139 | + |
| 140 | + /// <summary> |
| 141 | + /// Multi-column SET with mixed plain / compound. The pre-update snapshot |
| 142 | + /// is shared across all assignments in a single SET (matches existing |
| 143 | + /// multi-column UPDATE semantics), so <c>a *= 2, b = a</c> sees the |
| 144 | + /// original <c>a</c>, not the doubled one. |
| 145 | + /// </summary> |
| 146 | + [TestMethod] |
| 147 | + public void Update_MultiColumnSet_MixedPlainAndCompound() |
| 148 | + { |
| 149 | + var simulation = new Simulation(); |
| 150 | + _ = simulation.ExecuteNonQuery(""" |
| 151 | + create table t (id int, a int, b int); |
| 152 | + insert t values (1, 3, 0) |
| 153 | + """); |
| 154 | + AreEqual(1, simulation.ExecuteNonQuery("update t set a *= 2, b = a where id = 1")); |
| 155 | + using var reader = simulation.CreateCommand("select a, b from t").ExecuteReader(); |
| 156 | + IsTrue(reader.Read()); |
| 157 | + AreEqual(6, reader.GetInt32(0)); |
| 158 | + AreEqual(3, reader.GetInt32(1)); |
| 159 | + } |
| 160 | + |
| 161 | + [TestMethod] |
| 162 | + public void Update_QualifiedColumn_PlusEquals() |
| 163 | + { |
| 164 | + var simulation = new Simulation(); |
| 165 | + _ = simulation.ExecuteNonQuery(""" |
| 166 | + create table t (id int, v int); |
| 167 | + insert t values (1, 10) |
| 168 | + """); |
| 169 | + AreEqual(1, simulation.ExecuteNonQuery("update t set t.v += 5 where id = 1")); |
| 170 | + AreEqual(15, simulation.ExecuteScalar<int>("select v from t")); |
| 171 | + } |
| 172 | + |
| 173 | + [TestMethod] |
| 174 | + public void Update_StringPlusEquals_Concatenates() |
| 175 | + { |
| 176 | + var simulation = new Simulation(); |
| 177 | + _ = simulation.ExecuteNonQuery(""" |
| 178 | + create table t (id int, s varchar(20)); |
| 179 | + insert t values (1, 'hi'), (2, 'there') |
| 180 | + """); |
| 181 | + AreEqual(1, simulation.ExecuteNonQuery("update t set s += '!' where id = 1")); |
| 182 | + using var reader = simulation.CreateCommand("select s from t order by id").ExecuteReader(); |
| 183 | + IsTrue(reader.Read()); AreEqual("hi!", reader.GetString(0)); |
| 184 | + IsTrue(reader.Read()); AreEqual("there", reader.GetString(0)); |
| 185 | + } |
| 186 | + |
| 187 | + /// <summary> |
| 188 | + /// NULL column value + int → NULL: the compound operator participates in |
| 189 | + /// three-valued logic the same way plain <c>+</c> does. |
| 190 | + /// </summary> |
| 191 | + [TestMethod] |
| 192 | + public void Update_NullColumnPlusEquals_StaysNull() |
| 193 | + { |
| 194 | + var simulation = new Simulation(); |
| 195 | + _ = simulation.ExecuteNonQuery(""" |
| 196 | + create table t (id int, v int null); |
| 197 | + insert t values (1, null), (2, 5) |
| 198 | + """); |
| 199 | + AreEqual(2, simulation.ExecuteNonQuery("update t set v += 10")); |
| 200 | + using var reader = simulation.CreateCommand("select v from t order by id").ExecuteReader(); |
| 201 | + IsTrue(reader.Read()); IsTrue(reader.IsDBNull(0)); |
| 202 | + IsTrue(reader.Read()); AreEqual(15, reader.GetInt32(0)); |
| 203 | + } |
| 204 | + |
| 205 | + [TestMethod] |
| 206 | + public void Update_FromJoinSyntax_CompoundWithAlias() |
| 207 | + { |
| 208 | + var simulation = new Simulation(); |
| 209 | + _ = simulation.ExecuteNonQuery(""" |
| 210 | + create table t (id int, v int); |
| 211 | + create table u (id int, bonus int); |
| 212 | + insert t values (1, 10), (2, 20); |
| 213 | + insert u values (1, 5), (2, 50) |
| 214 | + """); |
| 215 | + AreEqual(2, simulation.ExecuteNonQuery("update t set v += u.bonus from t join u on t.id = u.id")); |
| 216 | + using var reader = simulation.CreateCommand("select v from t order by id").ExecuteReader(); |
| 217 | + IsTrue(reader.Read()); AreEqual(15, reader.GetInt32(0)); |
| 218 | + IsTrue(reader.Read()); AreEqual(70, reader.GetInt32(0)); |
| 219 | + } |
| 220 | +} |
0 commit comments