|
| 1 | +using System.Data.Common; |
| 2 | +using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert; |
| 3 | + |
| 4 | +namespace SqlServerSimulator; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Behavioral tests for SQL Server's <c>CASE</c> expression in both forms: |
| 8 | +/// searched (<c>CASE WHEN cond THEN ... [ELSE ...] END</c>) and simple |
| 9 | +/// (<c>CASE input WHEN val THEN ... [ELSE ...] END</c>). NULL handling, |
| 10 | +/// type promotion across branches, and composability with other expression |
| 11 | +/// contexts (WHERE, arithmetic, scalar subqueries) are sourced from probes |
| 12 | +/// against SQL Server 2025. |
| 13 | +/// </summary> |
| 14 | +[TestClass] |
| 15 | +public sealed class CaseExpressionTests |
| 16 | +{ |
| 17 | + // === Searched form === |
| 18 | + |
| 19 | + [TestMethod] |
| 20 | + public void Searched_FirstMatchingWhenWins() |
| 21 | + { |
| 22 | + AreEqual("a", new Simulation().ExecuteScalar("select case when 1=1 then 'a' when 1=1 then 'b' end")); |
| 23 | + } |
| 24 | + |
| 25 | + [TestMethod] |
| 26 | + public void Searched_NoMatchNoElse_ReturnsNull() |
| 27 | + { |
| 28 | + // ExecuteScalar returns the value of the first column of the first row. |
| 29 | + // For a single row with a NULL column, that's DBNull.Value. |
| 30 | + AreEqual(DBNull.Value, new Simulation().ExecuteScalar("select case when 1=0 then 'a' end")); |
| 31 | + } |
| 32 | + |
| 33 | + [TestMethod] |
| 34 | + public void Searched_NoMatch_FallsBackToElse() |
| 35 | + { |
| 36 | + AreEqual("z", new Simulation().ExecuteScalar("select case when 1=0 then 'a' else 'z' end")); |
| 37 | + } |
| 38 | + |
| 39 | + [TestMethod] |
| 40 | + public void Searched_UnknownPredicate_TreatedAsExclude() |
| 41 | + { |
| 42 | + // `null = 1` evaluates to UNKNOWN; UNKNOWN is not a match (same as WHERE). |
| 43 | + AreEqual("unmatched", new Simulation().ExecuteScalar("select case when null = 1 then 'matched' else 'unmatched' end")); |
| 44 | + } |
| 45 | + |
| 46 | + [TestMethod] |
| 47 | + public void Searched_MultipleWhens_FirstTrueWins() |
| 48 | + { |
| 49 | + AreEqual("second", new Simulation().ExecuteScalar( |
| 50 | + "select case when 1=0 then 'first' when 2=2 then 'second' when 3=3 then 'third' end")); |
| 51 | + } |
| 52 | + |
| 53 | + // === Simple form === |
| 54 | + |
| 55 | + [TestMethod] |
| 56 | + public void Simple_InputMatchesWhen_ReturnsThen() |
| 57 | + { |
| 58 | + AreEqual("two", new Simulation().ExecuteScalar("select case 2 when 1 then 'one' when 2 then 'two' else 'other' end")); |
| 59 | + } |
| 60 | + |
| 61 | + [TestMethod] |
| 62 | + public void Simple_NullInputVsNullWhen_NotAMatch() |
| 63 | + { |
| 64 | + // `case null when null` follows `=` semantics: NULL = NULL is UNKNOWN, not a match. |
| 65 | + AreEqual("unmatched", new Simulation().ExecuteScalar( |
| 66 | + "select case cast(null as int) when null then 'matched' else 'unmatched' end")); |
| 67 | + } |
| 68 | + |
| 69 | + [TestMethod] |
| 70 | + public void Simple_NoMatch_FallsBackToElse() |
| 71 | + { |
| 72 | + AreEqual("other", new Simulation().ExecuteScalar( |
| 73 | + "select case 99 when 1 then 'one' when 2 then 'two' else 'other' end")); |
| 74 | + } |
| 75 | + |
| 76 | + [TestMethod] |
| 77 | + public void Simple_NoMatchNoElse_ReturnsNull() |
| 78 | + { |
| 79 | + AreEqual(DBNull.Value, new Simulation().ExecuteScalar("select case 99 when 1 then 'one' when 2 then 'two' end")); |
| 80 | + } |
| 81 | + |
| 82 | + // === Composition === |
| 83 | + |
| 84 | + [TestMethod] |
| 85 | + public void Case_InArithmetic_FlowsThroughOperator() |
| 86 | + { |
| 87 | + AreEqual(15, new Simulation().ExecuteScalar("select case when 1=1 then 10 else 20 end + 5")); |
| 88 | + } |
| 89 | + |
| 90 | + [TestMethod] |
| 91 | + public void Case_InWhereClause_FiltersByDecision() |
| 92 | + { |
| 93 | + // Note: outer parens around the CASE are NOT used here — the |
| 94 | + // simulator's parser doesn't accept `(arith) cmp rhs` (a known |
| 95 | + // limitation). Bare `case ... end = 1` parses fine. |
| 96 | + var simulation = new Simulation(); |
| 97 | + _ = simulation.ExecuteNonQuery("create table t (id int, name nvarchar(20))"); |
| 98 | + _ = simulation.ExecuteNonQuery("insert into t values (1, 'one'), (2, 'two'), (3, 'three')"); |
| 99 | + |
| 100 | + using var connection = simulation.CreateOpenConnection(); |
| 101 | + using var reader = connection.CreateCommand( |
| 102 | + "select id from t where case when name='two' then 1 else 0 end = 1").ExecuteReader(); |
| 103 | + var ids = new List<int>(); |
| 104 | + while (reader.Read()) |
| 105 | + ids.Add(reader.GetInt32(0)); |
| 106 | + CollectionAssert.AreEqual(new[] { 2 }, ids); |
| 107 | + } |
| 108 | + |
| 109 | + [TestMethod] |
| 110 | + public void Case_WithColumnReference_ResolvesPerRow() |
| 111 | + { |
| 112 | + var simulation = new Simulation(); |
| 113 | + _ = simulation.ExecuteNonQuery("create table t (id int, name nvarchar(20))"); |
| 114 | + _ = simulation.ExecuteNonQuery("insert into t values (1, 'one'), (2, 'two'), (3, 'three')"); |
| 115 | + |
| 116 | + using var connection = simulation.CreateOpenConnection(); |
| 117 | + using var reader = connection.CreateCommand( |
| 118 | + "select case when id = 2 then name else 'other' end from t").ExecuteReader(); |
| 119 | + var labels = new List<string>(); |
| 120 | + while (reader.Read()) |
| 121 | + labels.Add(reader.GetString(0)); |
| 122 | + CollectionAssert.AreEqual(new[] { "other", "two", "other" }, labels); |
| 123 | + } |
| 124 | + |
| 125 | + [TestMethod] |
| 126 | + public void Case_Nested_OuterAndInnerEvaluateCorrectly() |
| 127 | + { |
| 128 | + AreEqual("inner-yes", new Simulation().ExecuteScalar( |
| 129 | + "select case when 1=1 then case when 2=2 then 'inner-yes' else 'inner-no' end else 'outer-no' end")); |
| 130 | + } |
| 131 | + |
| 132 | + [TestMethod] |
| 133 | + public void Case_WithNullElse_ReturnsThenWhenMatched() |
| 134 | + { |
| 135 | + AreEqual(5, new Simulation().ExecuteScalar("select case when 1=1 then 5 else null end")); |
| 136 | + } |
| 137 | + |
| 138 | + [TestMethod] |
| 139 | + public void Case_WithNullThen_ReturnsNullWhenMatched() |
| 140 | + { |
| 141 | + AreEqual(DBNull.Value, new Simulation().ExecuteScalar("select case when 1=1 then null else 5 end")); |
| 142 | + } |
| 143 | + |
| 144 | + [TestMethod] |
| 145 | + public void Case_InScalarSubquery_ProjectsValue() |
| 146 | + { |
| 147 | + var simulation = new Simulation(); |
| 148 | + _ = simulation.ExecuteNonQuery("create table t (id int)"); |
| 149 | + _ = simulation.ExecuteNonQuery("insert into t values (1)"); |
| 150 | + |
| 151 | + AreEqual("hit", simulation.ExecuteScalar( |
| 152 | + "select (select case when t.id = 1 then 'hit' else 'miss' end from t)")); |
| 153 | + } |
| 154 | + |
| 155 | + // === Syntax errors === |
| 156 | + |
| 157 | + [TestMethod] |
| 158 | + public void Case_EmptyNoWhen_RaisesSyntaxError() |
| 159 | + { |
| 160 | + // `case end` has no WHEN clauses; SQL Server raises Msg 156 near `end`. |
| 161 | + var ex = Throws<DbException>(() => _ = new Simulation().ExecuteScalar("select case end")); |
| 162 | + AreEqual("102", ex.Data["HelpLink.EvtID"]); |
| 163 | + } |
| 164 | + |
| 165 | + [TestMethod] |
| 166 | + public void Case_MissingEnd_RaisesSyntaxError() |
| 167 | + { |
| 168 | + var ex = Throws<DbException>(() => _ = new Simulation().ExecuteScalar("select case when 1=1 then 'a'")); |
| 169 | + AreEqual("102", ex.Data["HelpLink.EvtID"]); |
| 170 | + } |
| 171 | +} |
0 commit comments