|
| 1 | +using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert; |
| 2 | +using static SqlServerSimulator.TestHelpers; |
| 3 | + |
| 4 | +namespace SqlServerSimulator; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// <c>CONCAT</c> and <c>CONCAT_WS</c>: variadic string-concatenation scalars |
| 8 | +/// that skip NULL inputs (rather than propagating). Probe-anchored against |
| 9 | +/// SQL Server 2025 (2026-05-09). |
| 10 | +/// </summary> |
| 11 | +[TestClass] |
| 12 | +public sealed class ConcatTests |
| 13 | +{ |
| 14 | + [TestMethod] |
| 15 | + [DataRow("concat('a', 'b')", "ab")] |
| 16 | + [DataRow("concat('a', 'b', 'c')", "abc")] |
| 17 | + [DataRow("concat('a', '', 'c')", "ac")] |
| 18 | + [DataRow("concat(1, 2)", "12")] |
| 19 | + [DataRow("concat('a', 1)", "a1")] |
| 20 | + [DataRow("concat(1, 2, 3, 4, 5)", "12345")] |
| 21 | + [DataRow("concat('x=', cast(3.14 as decimal(10,2)))", "x=3.14")] |
| 22 | + [DataRow("concat('today: ', cast('2026-05-09' as date))", "today: 2026-05-09")] |
| 23 | + [DataRow("concat('t=', cast('2026-05-09T12:34:56' as datetime2(7)))", "t=2026-05-09 12:34:56.0000000")] |
| 24 | + [DataRow("concat('z=', cast('2026-05-09T12:34:56+05:00' as datetimeoffset(7)))", "z=2026-05-09 12:34:56.0000000 +05:00")] |
| 25 | + [DataRow("concat('b=', cast(1 as bit))", "b=1")] |
| 26 | + [DataRow("concat('m=', cast(123.45 as money))", "m=123.45")] |
| 27 | + public void Concat_Basic(string expression, string expected) => |
| 28 | + AreEqual(expected, ExecuteScalar($"select {expression}")); |
| 29 | + |
| 30 | + [TestMethod] |
| 31 | + [DataRow("concat('a', null, 'b')", "ab")] |
| 32 | + [DataRow("concat(null, 'a', null, 'b', null)", "ab")] |
| 33 | + [DataRow("concat(null, null)", "")] |
| 34 | + [DataRow("concat(null, null, null)", "")] |
| 35 | + [DataRow("concat('a', null)", "a")] |
| 36 | + [DataRow("concat(null, 'a')", "a")] |
| 37 | + public void Concat_SkipsNulls(string expression, string expected) => |
| 38 | + AreEqual(expected, ExecuteScalar($"select {expression}")); |
| 39 | + |
| 40 | + [TestMethod] |
| 41 | + [DataRow("concat()")] |
| 42 | + [DataRow("concat('a')")] |
| 43 | + public void Concat_TooFewArguments_RaisesMsg189(string expression) => |
| 44 | + AssertSqlError($"select {expression}", 189, "The concat function requires 2 to 254 arguments."); |
| 45 | + |
| 46 | + [TestMethod] |
| 47 | + [DataRow("concat_ws(',', 'a', 'b', 'c')", "a,b,c")] |
| 48 | + [DataRow("concat_ws('-', 'a', 'b')", "a-b")] |
| 49 | + [DataRow("concat_ws('', 'a', 'b')", "ab")] |
| 50 | + [DataRow("concat_ws('-', 1, 2, 3)", "1-2-3")] |
| 51 | + [DataRow("concat_ws('-', 'a', 1, cast('2026-05-09' as date))", "a-1-2026-05-09")] |
| 52 | + public void ConcatWs_Basic(string expression, string expected) => |
| 53 | + AreEqual(expected, ExecuteScalar($"select {expression}")); |
| 54 | + |
| 55 | + [TestMethod] |
| 56 | + [DataRow("concat_ws(',', 'a', null, 'b')", "a,b")] |
| 57 | + [DataRow("concat_ws(',', null, 'a', 'b')", "a,b")] |
| 58 | + [DataRow("concat_ws(',', 'a', 'b', null)", "a,b")] |
| 59 | + [DataRow("concat_ws(',', null, null, null)", "")] |
| 60 | + [DataRow("concat_ws(',', 'a', null, null, 'b')", "a,b")] |
| 61 | + public void ConcatWs_SkipsNullValues(string expression, string expected) => |
| 62 | + AreEqual(expected, ExecuteScalar($"select {expression}")); |
| 63 | + |
| 64 | + [TestMethod] |
| 65 | + [DataRow("concat_ws(null, 'a', 'b')", "ab")] |
| 66 | + [DataRow("concat_ws(null, 'a', 'b', 'c')", "abc")] |
| 67 | + [DataRow("concat_ws(null, null, null)", "")] |
| 68 | + public void ConcatWs_NullSeparator_DegradesToEmpty(string expression, string expected) => |
| 69 | + AreEqual(expected, ExecuteScalar($"select {expression}")); |
| 70 | + |
| 71 | + [TestMethod] |
| 72 | + [DataRow("concat_ws()")] |
| 73 | + [DataRow("concat_ws(',')")] |
| 74 | + [DataRow("concat_ws(',', 'a')")] |
| 75 | + [DataRow("concat_ws(',', null)")] |
| 76 | + public void ConcatWs_TooFewArguments_RaisesMsg189(string expression) => |
| 77 | + AssertSqlError($"select {expression}", 189, "The concat_ws function requires 3 to 254 arguments."); |
| 78 | + |
| 79 | + [TestMethod] |
| 80 | + public void Concat_ResultIsVarcharByDefault() |
| 81 | + { |
| 82 | + // All-ASCII string args → varchar; round-trip through DataLength |
| 83 | + // (varchar = 1 byte/char) confirms no nvarchar promotion. |
| 84 | + AreEqual(3, ExecuteScalar("select datalength(concat('a', 'b', 'c'))")); |
| 85 | + } |
| 86 | + |
| 87 | + [TestMethod] |
| 88 | + public void Concat_AnyNVarcharArg_PromotesToNVarchar() |
| 89 | + { |
| 90 | + // nvarchar = 2 bytes/char in CP1252 / UTF-16; presence of N'...' |
| 91 | + // on any arg promotes the result. |
| 92 | + AreEqual(6, ExecuteScalar("select datalength(concat('a', 'b', N'c'))")); |
| 93 | + } |
| 94 | + |
| 95 | + [TestMethod] |
| 96 | + public void Concat_NullsOnly_ReturnsEmptyString_NotNull() |
| 97 | + { |
| 98 | + // Probe-confirmed SQL Server quirk: typed metadata says NOT NULL |
| 99 | + // even when every input is NULL; runtime returns ''. |
| 100 | + AreEqual("", ExecuteScalar("select concat(null, null)")); |
| 101 | + } |
| 102 | + |
| 103 | + [TestMethod] |
| 104 | + public void ConcatWs_NullsOnly_ReturnsEmptyString_NotNull() => |
| 105 | + AreEqual("", ExecuteScalar("select concat_ws(',', null, null, null)")); |
| 106 | + |
| 107 | + [TestMethod] |
| 108 | + public void Concat_OfColumns_FromTable() => |
| 109 | + AreEqual("foobar", new Simulation().ExecuteScalar(""" |
| 110 | + create table t (a varchar(10), b varchar(10), c varchar(10)); |
| 111 | + insert t values ('foo', null, 'bar'); |
| 112 | + select concat(a, b, c) from t |
| 113 | + """)); |
| 114 | + |
| 115 | + [TestMethod] |
| 116 | + public void ConcatWs_OfColumns_FromTable() => |
| 117 | + AreEqual("foo|bar", new Simulation().ExecuteScalar(""" |
| 118 | + create table t (a varchar(10), b varchar(10), c varchar(10)); |
| 119 | + insert t values ('foo', null, 'bar'); |
| 120 | + select concat_ws('|', a, b, c) from t |
| 121 | + """)); |
| 122 | +} |
| 123 | + |
| 124 | +/// <summary> |
| 125 | +/// String <c>+</c> operator: NULL-propagating concatenation distinct from |
| 126 | +/// CONCAT's NULL-skipping. EF Core 10 emits this for <c>string.Concat</c> and |
| 127 | +/// <c>+</c>-chains over server-evaluated string operands. Probe-anchored |
| 128 | +/// against SQL Server 2025 (2026-05-09). |
| 129 | +/// </summary> |
| 130 | +[TestClass] |
| 131 | +public sealed class StringPlusOperatorTests |
| 132 | +{ |
| 133 | + [TestMethod] |
| 134 | + [DataRow("'a' + 'b'", "ab")] |
| 135 | + [DataRow("'' + 'a'", "a")] |
| 136 | + [DataRow("'a' + ''", "a")] |
| 137 | + [DataRow("'a' + 'b' + 'c'", "abc")] |
| 138 | + [DataRow("'a' + 'b' + 'c' + 'd'", "abcd")] |
| 139 | + [DataRow("N'a' + N'b'", "ab")] |
| 140 | + [DataRow("'a' + N'b'", "ab")] |
| 141 | + [DataRow("N'a' + 'b'", "ab")] |
| 142 | + public void Plus_StringConcat(string expression, string expected) => |
| 143 | + AreEqual(expected, ExecuteScalar($"select {expression}")); |
| 144 | + |
| 145 | + [TestMethod] |
| 146 | + [DataRow("'a' + null")] |
| 147 | + [DataRow("null + 'a'")] |
| 148 | + [DataRow("'a' + null + 'b'")] |
| 149 | + [DataRow("cast(null as varchar(10)) + 'a'")] |
| 150 | + [DataRow("'a' + cast(null as nvarchar(10))")] |
| 151 | + public void Plus_PropagatesNull(string expression) => |
| 152 | + IsInstanceOfType<DBNull>(ExecuteScalar($"select {expression}")); |
| 153 | + |
| 154 | + [TestMethod] |
| 155 | + public void Plus_VarcharVarchar_StaysVarchar() => |
| 156 | + AreEqual(2, ExecuteScalar("select datalength('a' + 'b')")); |
| 157 | + |
| 158 | + [TestMethod] |
| 159 | + [DataRow("'a' + N'b'")] |
| 160 | + [DataRow("N'a' + 'b'")] |
| 161 | + public void Plus_NVarcharOnEitherSide_PromotesToNVarchar(string expression) => |
| 162 | + AreEqual(4, ExecuteScalar($"select datalength({expression})")); |
| 163 | + |
| 164 | + /// <summary> |
| 165 | + /// <c>char(5) + char(5)</c> → <c>char(10)</c> (probe-confirmed). Trailing-space |
| 166 | + /// padding on each operand survives the concat as a side-effect of the storage rep. |
| 167 | + /// </summary> |
| 168 | + [TestMethod] |
| 169 | + public void Plus_CharPair_PreservesFixedLengthAndCombinesLengths() => |
| 170 | + AreEqual("a b ", new Simulation().ExecuteScalar(""" |
| 171 | + create table t (a char(5), b char(5)); |
| 172 | + insert t values ('a', 'b'); |
| 173 | + select a + b from t |
| 174 | + """)); |
| 175 | + |
| 176 | + /// <summary><c>char(5)+char(3)</c> → <c>char(8)</c>, one byte per char.</summary> |
| 177 | + [TestMethod] |
| 178 | + public void Plus_CharPair_DataLength_MatchesCombinedLength() => |
| 179 | + AreEqual(8, new Simulation().ExecuteScalar(""" |
| 180 | + create table t (a char(5), b char(3)); |
| 181 | + insert t values ('x', 'y'); |
| 182 | + select datalength(a + b) from t |
| 183 | + """)); |
| 184 | + |
| 185 | + /// <summary><c>nchar(5)+nchar(5)</c> → <c>nchar(10)</c>, two bytes per char → datalength = 20.</summary> |
| 186 | + [TestMethod] |
| 187 | + public void Plus_NCharPair_PromotesToNCharWithCombinedLength() => |
| 188 | + AreEqual(20, new Simulation().ExecuteScalar(""" |
| 189 | + create table t (a nchar(5), b nchar(5)); |
| 190 | + insert t values (N'x', N'y'); |
| 191 | + select datalength(a + b) from t |
| 192 | + """)); |
| 193 | + |
| 194 | + /// <summary><c>char(5) + nchar(5)</c> → <c>nchar(10)</c>, datalength 20.</summary> |
| 195 | + [TestMethod] |
| 196 | + public void Plus_CharNCharMix_PromotesToNCharWithCombinedLength() => |
| 197 | + AreEqual(20, new Simulation().ExecuteScalar(""" |
| 198 | + create table t (a char(5), b nchar(5)); |
| 199 | + insert t values ('x', N'y'); |
| 200 | + select datalength(a + b) from t |
| 201 | + """)); |
| 202 | + |
| 203 | + [TestMethod] |
| 204 | + public void Plus_TextOperand_RaisesMsg402() => |
| 205 | + AssertSqlError("select cast('a' as text) + 'b'", 402, |
| 206 | + "The data types text and varchar are incompatible in the add operator."); |
| 207 | + |
| 208 | + [TestMethod] |
| 209 | + public void Plus_NTextOperand_RaisesMsg402() => |
| 210 | + AssertSqlError("select 'a' + cast(N'b' as ntext)", 402, |
| 211 | + "The data types varchar and ntext are incompatible in the add operator."); |
| 212 | + |
| 213 | + /// <summary> |
| 214 | + /// <c>'5'</c> parses to int via existing integer↔string promotion; result is int. |
| 215 | + /// </summary> |
| 216 | + [TestMethod] |
| 217 | + [DataRow("'5' + 1", 6)] |
| 218 | + [DataRow("1 + '5'", 6)] |
| 219 | + public void Plus_StringPlusInteger_RoutesThroughIntegerArithmetic(string expression, int expected) => |
| 220 | + AreEqual(expected, ExecuteScalar($"select {expression}")); |
| 221 | + |
| 222 | + [TestMethod] |
| 223 | + public void Plus_OfColumns_FromTable() => |
| 224 | + AreEqual("foobar", new Simulation().ExecuteScalar(""" |
| 225 | + create table t (a varchar(10), b varchar(10)); |
| 226 | + insert t values ('foo', 'bar'); |
| 227 | + select a + b from t |
| 228 | + """)); |
| 229 | + |
| 230 | + [TestMethod] |
| 231 | + public void Plus_ColumnAndNull_PropagatesNull() => |
| 232 | + IsInstanceOfType<DBNull>(new Simulation().ExecuteScalar(""" |
| 233 | + create table t (a varchar(10), b varchar(10)); |
| 234 | + insert t values ('foo', null); |
| 235 | + select a + b from t |
| 236 | + """)); |
| 237 | +} |
0 commit comments