|
| 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 the fixed-length string and binary types |
| 9 | +/// (<c>char(N)</c>, <c>nchar(N)</c>, <c>binary(N)</c>): right-padding semantics, |
| 10 | +/// silent CAST truncation, INSERT-time truncation errors (Msg 2628 / 8152), |
| 11 | +/// trailing-space-aware comparison, and width-validation error variants. |
| 12 | +/// </summary> |
| 13 | +[TestClass] |
| 14 | +public sealed class FixedLengthStringTests |
| 15 | +{ |
| 16 | + [TestMethod] |
| 17 | + public void Cast_String_PadsToDeclaredLength() |
| 18 | + { |
| 19 | + AreEqual("abc ", ExecuteScalar("select cast('abc' as char(5))")); |
| 20 | + } |
| 21 | + |
| 22 | + [TestMethod] |
| 23 | + public void Cast_NString_PadsNcharToDeclaredCodeUnits() |
| 24 | + { |
| 25 | + AreEqual("abc ", ExecuteScalar("select cast(N'abc' as nchar(5))")); |
| 26 | + } |
| 27 | + |
| 28 | + [TestMethod] |
| 29 | + public void Cast_Varbinary_PadsBinaryWithZeros() |
| 30 | + { |
| 31 | + var value = ExecuteScalar("select cast(0xCAFE as binary(5))") as byte[]; |
| 32 | + IsNotNull(value); |
| 33 | + CollectionAssert.AreEqual(new byte[] { 0xCA, 0xFE, 0, 0, 0 }, value); |
| 34 | + } |
| 35 | + |
| 36 | + [TestMethod] |
| 37 | + public void Cast_OversizeString_SilentlyTruncatesInCast() |
| 38 | + { |
| 39 | + // Verified against SQL Server 2025: CAST silently truncates without |
| 40 | + // raising — the truncation error is reserved for INSERT/UPDATE. |
| 41 | + AreEqual("hello", ExecuteScalar("select cast('hello world' as char(5))")); |
| 42 | + } |
| 43 | + |
| 44 | + [TestMethod] |
| 45 | + public void Cast_OversizeBinary_SilentlyTruncatesInCast() |
| 46 | + { |
| 47 | + var value = ExecuteScalar("select cast(0x0102030405060708 as binary(5))") as byte[]; |
| 48 | + IsNotNull(value); |
| 49 | + CollectionAssert.AreEqual(new byte[] { 1, 2, 3, 4, 5 }, value); |
| 50 | + } |
| 51 | + |
| 52 | + [TestMethod] |
| 53 | + public void Cast_EmptyString_PadsToFullWidth() |
| 54 | + { |
| 55 | + AreEqual(" ", ExecuteScalar("select cast('' as char(3))")); |
| 56 | + } |
| 57 | + |
| 58 | + [TestMethod] |
| 59 | + public void Comparison_CharWithVarchar_StripsTrailingSpaces() |
| 60 | + { |
| 61 | + // ANSI trailing-space padding is part of the equality semantics shared |
| 62 | + // by every string-family type, so char(5) 'abc ' equals varchar 'abc'. |
| 63 | + AreEqual(1, ExecuteScalar("select 1 where cast('abc' as char(5)) = 'abc'")); |
| 64 | + AreEqual(1, ExecuteScalar("select 1 where cast('abc' as char(5)) = 'abc '")); |
| 65 | + } |
| 66 | + |
| 67 | + [TestMethod] |
| 68 | + public void Comparison_DifferentDeclaredLengths_StillEqualByContent() |
| 69 | + { |
| 70 | + AreEqual(1, ExecuteScalar("select 1 where cast('abc' as char(5)) = cast('abc' as char(10))")); |
| 71 | + } |
| 72 | + |
| 73 | + [TestMethod] |
| 74 | + public void CreateTable_InsertSelect_RoundTripsPaddedValue() |
| 75 | + { |
| 76 | + var simulation = new Simulation(); |
| 77 | + _ = simulation.ExecuteNonQuery("create table t (c char(5))"); |
| 78 | + _ = simulation.ExecuteNonQuery("insert into t values ('hi')"); |
| 79 | + AreEqual("hi ", simulation.ExecuteScalar("select c from t")); |
| 80 | + } |
| 81 | + |
| 82 | + [TestMethod] |
| 83 | + public void CreateTable_NCharRoundTrip_PadsToDeclaredCodeUnits() |
| 84 | + { |
| 85 | + var simulation = new Simulation(); |
| 86 | + _ = simulation.ExecuteNonQuery("create table t (c nchar(5))"); |
| 87 | + _ = simulation.ExecuteNonQuery("insert into t values (N'hi')"); |
| 88 | + AreEqual("hi ", simulation.ExecuteScalar("select c from t")); |
| 89 | + } |
| 90 | + |
| 91 | + [TestMethod] |
| 92 | + public void CreateTable_BinaryRoundTrip_PadsWithZeros() |
| 93 | + { |
| 94 | + var simulation = new Simulation(); |
| 95 | + _ = simulation.ExecuteNonQuery("create table t (b binary(5))"); |
| 96 | + _ = simulation.ExecuteNonQuery("insert into t values (0xCAFE)"); |
| 97 | + var value = simulation.ExecuteScalar("select b from t") as byte[]; |
| 98 | + IsNotNull(value); |
| 99 | + CollectionAssert.AreEqual(new byte[] { 0xCA, 0xFE, 0, 0, 0 }, value); |
| 100 | + } |
| 101 | + |
| 102 | + [TestMethod] |
| 103 | + public void Insert_OversizeString_RaisesTruncationError() |
| 104 | + { |
| 105 | + // Default compatibility level is 170 (SQL Server 2025), so the verbose |
| 106 | + // Msg 2628 fires with the column-name and truncated-prefix detail. |
| 107 | + var simulation = new Simulation(); |
| 108 | + _ = simulation.ExecuteNonQuery("create table t (c char(5))"); |
| 109 | + var ex = Throws<DbException>(() => simulation.ExecuteNonQuery("insert into t values ('toolong')")); |
| 110 | + AreEqual("String or binary data would be truncated in table 't', column 'c'. Truncated value: 'toolo'.", ex.Message); |
| 111 | + } |
| 112 | + |
| 113 | + [TestMethod] |
| 114 | + public void Insert_OversizeNChar_RaisesTruncationError() |
| 115 | + { |
| 116 | + var simulation = new Simulation(); |
| 117 | + _ = simulation.ExecuteNonQuery("create table t (c nchar(5))"); |
| 118 | + var ex = Throws<DbException>(() => simulation.ExecuteNonQuery("insert into t values (N'toolong')")); |
| 119 | + AreEqual("String or binary data would be truncated in table 't', column 'c'. Truncated value: 'toolo'.", ex.Message); |
| 120 | + } |
| 121 | + |
| 122 | + [TestMethod] |
| 123 | + public void Insert_OversizeBinary_RaisesTruncationError() |
| 124 | + { |
| 125 | + var simulation = new Simulation(); |
| 126 | + _ = simulation.ExecuteNonQuery("create table t (b binary(5))"); |
| 127 | + var ex = Throws<DbException>(() => simulation.ExecuteNonQuery("insert into t values (0x010203040506)")); |
| 128 | + StringAssert.StartsWith(ex.Message, "String or binary data would be truncated in table 't', column 'b'."); |
| 129 | + } |
| 130 | + |
| 131 | + [TestMethod] |
| 132 | + public void Cast_CharZero_RaisesMsg1001() |
| 133 | + { |
| 134 | + var ex = Throws<DbException>(() => ExecuteScalar("select cast('a' as char(0))")); |
| 135 | + AreEqual("Line 1: Length or precision specification 0 is invalid.", ex.Message); |
| 136 | + } |
| 137 | + |
| 138 | + [TestMethod] |
| 139 | + public void Cast_CharOversize_RaisesMsg131WithTypeWording() |
| 140 | + { |
| 141 | + var ex = Throws<DbException>(() => ExecuteScalar("select cast('a' as char(8001))")); |
| 142 | + AreEqual("The size (8001) given to the type 'char' exceeds the maximum allowed for any data type (8000).", ex.Message); |
| 143 | + } |
| 144 | + |
| 145 | + [TestMethod] |
| 146 | + public void Cast_NCharOversize_RaisesMsg131WithConvertSpecificationWording() |
| 147 | + { |
| 148 | + var ex = Throws<DbException>(() => ExecuteScalar("select cast(N'a' as nchar(4001))")); |
| 149 | + AreEqual("The size (4001) given to the convert specification 'nchar' exceeds the maximum allowed for any data type (4000).", ex.Message); |
| 150 | + } |
| 151 | + |
| 152 | + [TestMethod] |
| 153 | + public void Cast_BinaryOversize_RaisesMsg131WithBinaryWording() |
| 154 | + { |
| 155 | + var ex = Throws<DbException>(() => ExecuteScalar("select cast(0xab as binary(8001))")); |
| 156 | + AreEqual("The size (8001) given to the type 'binary' exceeds the maximum allowed for any data type (8000).", ex.Message); |
| 157 | + } |
| 158 | + |
| 159 | + [TestMethod] |
| 160 | + public void CreateTable_CharOversize_RaisesMsg131WithColumnWording() |
| 161 | + { |
| 162 | + var simulation = new Simulation(); |
| 163 | + var ex = Throws<DbException>(() => simulation.ExecuteNonQuery("create table t (c char(8001))")); |
| 164 | + AreEqual("The size (8001) given to the column 'c' exceeds the maximum allowed for any data type (8000).", ex.Message); |
| 165 | + } |
| 166 | + |
| 167 | + [TestMethod] |
| 168 | + public void CreateTable_NCharOversize_RaisesMsg2717ParameterWording() |
| 169 | + { |
| 170 | + var simulation = new Simulation(); |
| 171 | + var ex = Throws<DbException>(() => simulation.ExecuteNonQuery("create table t (c nchar(4001))")); |
| 172 | + AreEqual("The size (4001) given to the parameter 'c' exceeds the maximum allowed (4000).", ex.Message); |
| 173 | + } |
| 174 | + |
| 175 | + [TestMethod] |
| 176 | + public void Cast_Default_NoParensInCastIs30() |
| 177 | + { |
| 178 | + // Verified against SQL Server 2025: CAST without parens defaults to 30, |
| 179 | + // matching SQL Server's documented "30 in CAST, 1 in column declaration" |
| 180 | + // split. Confirm via DATALENGTH. |
| 181 | + AreEqual(30, ExecuteScalar("select datalength(cast('abc' as char))")); |
| 182 | + } |
| 183 | + |
| 184 | + [TestMethod] |
| 185 | + public void CreateTable_DefaultIsOne() |
| 186 | + { |
| 187 | + // Column declaration default is 1 — SQL Server's documented split from |
| 188 | + // CAST's default of 30. INSERT a 1-char value to verify it round trips |
| 189 | + // without truncation; 'hello' would truncate. |
| 190 | + var simulation = new Simulation(); |
| 191 | + _ = simulation.ExecuteNonQuery("create table t (c char)"); |
| 192 | + _ = simulation.ExecuteNonQuery("insert into t values ('a')"); |
| 193 | + AreEqual("a", simulation.ExecuteScalar("select c from t")); |
| 194 | + // 'hello' wouldn't fit char(1). |
| 195 | + _ = Throws<DbException>(() => simulation.ExecuteNonQuery("insert into t values ('hello')")); |
| 196 | + } |
| 197 | + |
| 198 | + [TestMethod] |
| 199 | + public void Cast_UidToCharBelow36_RaisesMsg8170() |
| 200 | + { |
| 201 | + var ex = Throws<DbException>(() => ExecuteScalar("select cast(NEWID() as char(35))")); |
| 202 | + AreEqual("Insufficient result space to convert uniqueidentifier value to char.", ex.Message); |
| 203 | + } |
| 204 | + |
| 205 | + [TestMethod] |
| 206 | + public void Cast_UidToNCharBelow36_RaisesMsg8115WithNvarcharText() |
| 207 | + { |
| 208 | + // Verified against SQL Server 2025: nchar's overflow message names |
| 209 | + // "nvarchar" rather than "nchar" — same shared text path as nvarchar. |
| 210 | + var ex = Throws<DbException>(() => ExecuteScalar("select cast(NEWID() as nchar(35))")); |
| 211 | + AreEqual("Arithmetic overflow error converting expression to data type nvarchar.", ex.Message); |
| 212 | + } |
| 213 | + |
| 214 | + [TestMethod] |
| 215 | + public void Cast_VarcharToNvarchar_PreservesValue() |
| 216 | + { |
| 217 | + // String → string crossings now work in CAST (varchar ↔ nvarchar ↔ |
| 218 | + // char(N) ↔ nchar(N)). This was a known gap before fixed-length types |
| 219 | + // were added; fixing it fell out of the same string→string code path. |
| 220 | + AreEqual("abc", ExecuteScalar("select cast('abc' as nvarchar(10))")); |
| 221 | + } |
| 222 | + |
| 223 | + [TestMethod] |
| 224 | + public void Insert_WithLegacyCompatibility_RaisesMsg8152NoColumnDetail() |
| 225 | + { |
| 226 | + var simulation = new Simulation(); |
| 227 | + _ = simulation.ExecuteNonQuery("alter database current set compatibility_level = 150"); |
| 228 | + _ = simulation.ExecuteNonQuery("create table t (c char(5))"); |
| 229 | + var ex = Throws<DbException>(() => simulation.ExecuteNonQuery("insert into t values ('toolong')")); |
| 230 | + AreEqual("String or binary data would be truncated.", ex.Message); |
| 231 | + } |
| 232 | + |
| 233 | + [TestMethod] |
| 234 | + public void Cast_CharToVarchar_TrailingSpacesPreservedInString() |
| 235 | + { |
| 236 | + // The padded form is part of the value, so casting to varchar carries |
| 237 | + // the trailing spaces through (verified: varchar comparison still |
| 238 | + // strips them via ANSI padding). |
| 239 | + AreEqual(1, ExecuteScalar("select 1 where cast(cast('abc' as char(5)) as varchar(10)) = 'abc'")); |
| 240 | + } |
| 241 | +} |
0 commit comments