|
| 1 | +using System.Data.Common; |
| 2 | +using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert; |
| 3 | +using static SqlServerSimulator.TestHelpers; |
| 4 | + |
| 5 | +namespace SqlServerSimulator; |
| 6 | + |
| 7 | +[TestClass] |
| 8 | +public sealed class VariableTests |
| 9 | +{ |
| 10 | + [TestMethod] |
| 11 | + public void Declare_NoInit_VariableIsNull() |
| 12 | + => AreEqual(DBNull.Value, ExecuteScalar("declare @v int; select @v")); |
| 13 | + |
| 14 | + [TestMethod] |
| 15 | + public void Declare_WithInit_HoldsValue() |
| 16 | + => AreEqual(7, ExecuteScalar<int>("declare @v int = 7; select @v")); |
| 17 | + |
| 18 | + [TestMethod] |
| 19 | + public void Declare_AsKeywordOptional() |
| 20 | + => AreEqual(1, ExecuteScalar<int>("declare @v as int = 1; select @v")); |
| 21 | + |
| 22 | + [TestMethod] |
| 23 | + public void Declare_MultiVariable_AllReadable() |
| 24 | + { |
| 25 | + using var conn = new Simulation().CreateOpenConnection(); |
| 26 | + using var cmd = conn.CreateCommand("declare @v int = 7, @w varchar(5) = 'abc'; select @v, @w"); |
| 27 | + using var reader = cmd.ExecuteReader(); |
| 28 | + IsTrue(reader.Read()); |
| 29 | + AreEqual(7, reader.GetInt32(0)); |
| 30 | + AreEqual("abc", reader.GetString(1)); |
| 31 | + } |
| 32 | + |
| 33 | + [TestMethod] |
| 34 | + public void Set_Assigns() |
| 35 | + => AreEqual(42, ExecuteScalar<int>("declare @v int; set @v = 42; select @v")); |
| 36 | + |
| 37 | + [TestMethod] |
| 38 | + public void Set_StringToInt_Coerces() |
| 39 | + => AreEqual(42, ExecuteScalar<int>("declare @v int; set @v = '42'; select @v")); |
| 40 | + |
| 41 | + [TestMethod] |
| 42 | + public void Set_BadStringToInt_RaisesMsg245() |
| 43 | + => AssertSqlError("declare @v int; set @v = 'abc'; select @v", 245, |
| 44 | + "Conversion failed when converting the varchar value 'abc' to data type int."); |
| 45 | + |
| 46 | + [TestMethod] |
| 47 | + public void Set_StringTruncatesToVarcharLength() |
| 48 | + => AreEqual("hel", ExecuteScalar("declare @v varchar(3); set @v = 'hello'; select @v")); |
| 49 | + |
| 50 | + [TestMethod] |
| 51 | + public void Set_DecimalToInt_Truncates() |
| 52 | + => AreEqual(3, ExecuteScalar<int>("declare @v int; set @v = 3.7; select @v")); |
| 53 | + |
| 54 | + [TestMethod] |
| 55 | + public void Set_ScalarSubquery_Assigns() |
| 56 | + => AreEqual(42, ExecuteScalar<int>("declare @v int; set @v = (select 42); select @v")); |
| 57 | + |
| 58 | + [TestMethod] |
| 59 | + public void Set_MultiRowSubquery_RaisesMsg512() |
| 60 | + { |
| 61 | + var sim = new Simulation(); |
| 62 | + _ = sim.ExecuteNonQuery("create table t (id int); insert t values (1),(2)"); |
| 63 | + _ = sim.AssertSqlError("declare @v int; set @v = (select id from t); select @v", 512); |
| 64 | + } |
| 65 | + |
| 66 | + [TestMethod] |
| 67 | + public void Set_EmptySubquery_AssignsNull() |
| 68 | + => AreEqual(DBNull.Value, ExecuteScalar( |
| 69 | + "declare @v int = 99; set @v = (select 1 where 1 = 0); select @v")); |
| 70 | + |
| 71 | + [TestMethod] |
| 72 | + public void SelectAssign_Single() |
| 73 | + => AreEqual(42, ExecuteScalar<int>("declare @v int; select @v = 42; select @v")); |
| 74 | + |
| 75 | + [TestMethod] |
| 76 | + public void SelectAssign_LastRowWins() |
| 77 | + { |
| 78 | + using var conn = new Simulation().CreateOpenConnection(); |
| 79 | + _ = conn.CreateCommand("create table t (id int); insert t values (1),(2),(3)").ExecuteNonQuery(); |
| 80 | + AreEqual(3, conn.CreateCommand( |
| 81 | + "declare @v int; select @v = id from t order by id; select @v").ExecuteScalar()); |
| 82 | + } |
| 83 | + |
| 84 | + [TestMethod] |
| 85 | + public void SelectAssign_EmptyResult_KeepsPriorValue() |
| 86 | + { |
| 87 | + using var conn = new Simulation().CreateOpenConnection(); |
| 88 | + _ = conn.CreateCommand("create table t (id int); insert t values (1)").ExecuteNonQuery(); |
| 89 | + AreEqual(99, conn.CreateCommand( |
| 90 | + "declare @v int = 99; select @v = id from t where id = 0; select @v").ExecuteScalar()); |
| 91 | + } |
| 92 | + |
| 93 | + [TestMethod] |
| 94 | + public void SelectAssign_NullResult_AssignsNull() |
| 95 | + => AreEqual(DBNull.Value, ExecuteScalar( |
| 96 | + "declare @v int = 99; select @v = case when 1=0 then 1 else null end; select @v")); |
| 97 | + |
| 98 | + [TestMethod] |
| 99 | + public void SelectAssign_MultiVariable() |
| 100 | + { |
| 101 | + using var conn = new Simulation().CreateOpenConnection(); |
| 102 | + using var cmd = conn.CreateCommand( |
| 103 | + "declare @v int, @w varchar(10); select @v = 1, @w = 'x'; select @v, @w"); |
| 104 | + using var reader = cmd.ExecuteReader(); |
| 105 | + IsTrue(reader.Read()); |
| 106 | + AreEqual(1, reader.GetInt32(0)); |
| 107 | + AreEqual("x", reader.GetString(1)); |
| 108 | + } |
| 109 | + |
| 110 | + [TestMethod] |
| 111 | + public void SelectAssign_MultiVariableLastRowWins() |
| 112 | + { |
| 113 | + using var conn = new Simulation().CreateOpenConnection(); |
| 114 | + _ = conn.CreateCommand("create table t (a int, b int); insert t values (1, 2), (3, 4)").ExecuteNonQuery(); |
| 115 | + using var cmd = conn.CreateCommand( |
| 116 | + "declare @v int, @w int; select @v = a, @w = b from t order by a; select @v, @w"); |
| 117 | + using var reader = cmd.ExecuteReader(); |
| 118 | + IsTrue(reader.Read()); |
| 119 | + AreEqual(3, reader.GetInt32(0)); |
| 120 | + AreEqual(4, reader.GetInt32(1)); |
| 121 | + } |
| 122 | + |
| 123 | + [TestMethod] |
| 124 | + public void SelectAssign_MixedWithProjection_RaisesMsg141() |
| 125 | + => AssertSqlError("declare @v int; select @v = 1, 2", 141, |
| 126 | + "A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations."); |
| 127 | + |
| 128 | + [TestMethod] |
| 129 | + public void Reference_BeforeDeclare_RaisesMsg137() |
| 130 | + => AssertSqlError("select @x", 137, "Must declare the scalar variable \"@x\"."); |
| 131 | + |
| 132 | + [TestMethod] |
| 133 | + public void Declare_Duplicate_RaisesMsg134() |
| 134 | + => AssertSqlError("declare @v int; declare @v int; select @v", 134, |
| 135 | + "The variable name '@v' has already been declared. Variable names must be unique within a query batch or stored procedure."); |
| 136 | + |
| 137 | + [TestMethod] |
| 138 | + public void Declare_NameCollidesWithParameter_RaisesMsg134() |
| 139 | + { |
| 140 | + using var conn = new Simulation().CreateOpenConnection(); |
| 141 | + using var cmd = conn.CreateCommand("declare @x int = 99; select @x", ("@x", 1)); |
| 142 | + var ex = Throws<DbException>(cmd.ExecuteScalar); |
| 143 | + AreEqual("134", ex.Data["HelpLink.EvtID"]); |
| 144 | + } |
| 145 | + |
| 146 | + [TestMethod] |
| 147 | + public void ReservedKeywordAsVariableName_Works() |
| 148 | + => AreEqual(1, ExecuteScalar<int>("declare @select int = 1; select @select")); |
| 149 | + |
| 150 | + [TestMethod] |
| 151 | + public void Declare_SelfReference_RaisesMsg137() |
| 152 | + => AssertSqlError("declare @v int = @v + 1; select @v", 137); |
| 153 | + |
| 154 | + [TestMethod] |
| 155 | + public void Declare_MultipleStatementsViaSemicolon() |
| 156 | + => AreEqual(3, ExecuteScalar<int>("declare @x int = 1; declare @y int = 2; select @x + @y")); |
| 157 | + |
| 158 | + [TestMethod] |
| 159 | + public void Variable_InWhereClause_FiltersRows() |
| 160 | + => IsNull(ExecuteScalar("declare @x int = 5; select 1 where 1 = @x")); |
| 161 | + |
| 162 | + [TestMethod] |
| 163 | + public void RowCount_AfterSelect() |
| 164 | + { |
| 165 | + using var conn = new Simulation().CreateOpenConnection(); |
| 166 | + using var cmd = conn.CreateCommand( |
| 167 | + "select 1 union select 2 union select 3; select @@rowcount as rc"); |
| 168 | + using var reader = cmd.ExecuteReader(); |
| 169 | + IsTrue(reader.NextResult()); |
| 170 | + IsTrue(reader.Read()); |
| 171 | + AreEqual(3, reader.GetInt32(0)); |
| 172 | + } |
| 173 | + |
| 174 | + [TestMethod] |
| 175 | + public void RowCount_AfterDeclareWithInit_IsOne() |
| 176 | + => AreEqual(1, ExecuteScalar<int>( |
| 177 | + "declare @v int = 99; select @@rowcount")); |
| 178 | + |
| 179 | + [TestMethod] |
| 180 | + public void RowCount_AfterBareDeclare_PreservedFromPriorStatement() |
| 181 | + { |
| 182 | + // INSERT sets @@ROWCOUNT to 3; bare DECLARE without init does NOT reset. |
| 183 | + using var conn = new Simulation().CreateOpenConnection(); |
| 184 | + _ = conn.CreateCommand("create table t (id int)").ExecuteNonQuery(); |
| 185 | + using var cmd = conn.CreateCommand( |
| 186 | + "insert t values (1),(2),(3); declare @v int; select @@rowcount"); |
| 187 | + AreEqual(3, cmd.ExecuteScalar()); |
| 188 | + } |
| 189 | + |
| 190 | + [TestMethod] |
| 191 | + public void RowCount_AfterSet_IsOne() |
| 192 | + => AreEqual(1, ExecuteScalar<int>("declare @v int; set @v = 42; select @@rowcount")); |
| 193 | + |
| 194 | + [TestMethod] |
| 195 | + public void OutputParameter_WrittenBackAfterBatch() |
| 196 | + { |
| 197 | + using var conn = new Simulation().CreateOpenConnection(); |
| 198 | + using var cmd = conn.CreateCommand("set @x = 999"); |
| 199 | + var p = cmd.CreateParameter(); |
| 200 | + p.ParameterName = "@x"; |
| 201 | + p.DbType = System.Data.DbType.Int32; |
| 202 | + p.Direction = System.Data.ParameterDirection.InputOutput; |
| 203 | + p.Value = 5; |
| 204 | + _ = cmd.Parameters.Add(p); |
| 205 | + _ = cmd.ExecuteNonQuery(); |
| 206 | + AreEqual(999, p.Value); |
| 207 | + } |
| 208 | + |
| 209 | + [TestMethod] |
| 210 | + public void InputParameter_NotWrittenBack() |
| 211 | + { |
| 212 | + using var conn = new Simulation().CreateOpenConnection(); |
| 213 | + using var cmd = conn.CreateCommand("set @x = 999"); |
| 214 | + var p = cmd.CreateParameter(); |
| 215 | + p.ParameterName = "@x"; |
| 216 | + p.DbType = System.Data.DbType.Int32; |
| 217 | + p.Direction = System.Data.ParameterDirection.Input; |
| 218 | + p.Value = 5; |
| 219 | + _ = cmd.Parameters.Add(p); |
| 220 | + _ = cmd.ExecuteNonQuery(); |
| 221 | + AreEqual(5, p.Value); |
| 222 | + } |
| 223 | +} |
0 commit comments