|
| 1 | +using System.Data; |
| 2 | +using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert; |
| 3 | + |
| 4 | +namespace SqlServerSimulator; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// End-to-end coverage of the tokenized literal forms ('foo', N'foo', |
| 8 | +/// 0xHEX) and the SQL Server semantics they exercise: ANSI trailing-space |
| 9 | +/// padding for varchar/nvarchar equality, case-insensitive default collation, |
| 10 | +/// accent-sensitive comparison, escape handling for embedded apostrophes, and |
| 11 | +/// no-padding equality for varbinary. |
| 12 | +/// </summary> |
| 13 | +[TestClass] |
| 14 | +public class StringLiteralTests |
| 15 | +{ |
| 16 | + [TestMethod] |
| 17 | + [DataRow("'a' = 'a'", 1)] |
| 18 | + [DataRow("'a' = 'A'", 1)] // case-insensitive default collation |
| 19 | + [DataRow("'a' = 'b'", 0)] |
| 20 | + [DataRow("'a' = 'a '", 1)] // ANSI trailing-space padding |
| 21 | + [DataRow("'a ' = 'a '", 1)] |
| 22 | + [DataRow("'' = ' '", 1)] // empty equals all-spaces under padding |
| 23 | + [DataRow("'a' = ' a'", 0)] // leading spaces are significant |
| 24 | + [DataRow("'café' = 'cafe'", 0)] // accent-sensitive |
| 25 | + [DataRow("'café' = 'CAFÉ'", 1)] // accents preserved across case fold |
| 26 | + public void VarcharEquality(string condition, int expectedRows) => |
| 27 | + AreEqual(expectedRows, new Simulation().ExecuteReader($"select 1 where {condition}").EnumerateRecords().Count()); |
| 28 | + |
| 29 | + [TestMethod] |
| 30 | + [DataRow("N'a' = N'A'", 1)] |
| 31 | + [DataRow("N'Bjørn' = N'BJØRN'", 1)] |
| 32 | + [DataRow("N'Bjørn ' = N'BJØRN'", 1)] |
| 33 | + [DataRow("N'a' = N'b'", 0)] |
| 34 | + public void NVarcharEquality(string condition, int expectedRows) => |
| 35 | + AreEqual(expectedRows, new Simulation().ExecuteReader($"select 1 where {condition}").EnumerateRecords().Count()); |
| 36 | + |
| 37 | + [TestMethod] |
| 38 | + [DataRow("'a' < 'b'", 1)] |
| 39 | + [DataRow("'a' < 'a'", 0)] |
| 40 | + [DataRow("'b' > 'a'", 1)] |
| 41 | + [DataRow("'a' <= 'A '", 1)] // case fold + ANSI padding produce equality, so <= is true |
| 42 | + [DataRow("'a' >= 'A '", 1)] |
| 43 | + [DataRow("'a' < 'A '", 0)] // ... but strict less-than is false |
| 44 | + [DataRow("'apple' < 'banana'", 1)] |
| 45 | + [DataRow("'banana' > 'apple'", 1)] |
| 46 | + public void VarcharOrdering(string condition, int expectedRows) => |
| 47 | + AreEqual(expectedRows, new Simulation().ExecuteReader($"select 1 where {condition}").EnumerateRecords().Count()); |
| 48 | + |
| 49 | + [TestMethod] |
| 50 | + [DataRow("0x0102 < 0x0103", 1)] |
| 51 | + [DataRow("0x0103 > 0x0102", 1)] |
| 52 | + [DataRow("0x01 < 0x0100", 1)] // shorter is less when prefix matches (no padding) |
| 53 | + [DataRow("0x0100 > 0x01", 1)] |
| 54 | + [DataRow("0x01 = 0x01", 1)] |
| 55 | + public void VarbinaryOrdering(string condition, int expectedRows) => |
| 56 | + AreEqual(expectedRows, new Simulation().ExecuteReader($"select 1 where {condition}").EnumerateRecords().Count()); |
| 57 | + |
| 58 | + [TestMethod] |
| 59 | + [DataRow("'a' = null", 0)] // any comparison with NULL is UNKNOWN, no row |
| 60 | + [DataRow("null = 'a'", 0)] |
| 61 | + [DataRow("null = null", 0)] // even NULL = NULL is UNKNOWN |
| 62 | + [DataRow("'' = null", 0)] |
| 63 | + public void NullVsString_AlwaysUnknown(string condition, int expectedRows) => |
| 64 | + AreEqual(expectedRows, new Simulation().ExecuteReader($"select 1 where {condition}").EnumerateRecords().Count()); |
| 65 | + |
| 66 | + [TestMethod] |
| 67 | + [DataRow("0xDEAD = 0xDEAD", 1)] |
| 68 | + [DataRow("0xDEAD = 0xBEEF", 0)] |
| 69 | + [DataRow("0xdead = 0xDEAD", 1)] // hex digits are case-insensitive in the literal |
| 70 | + [DataRow("0x01 = 0x0100", 0)] // no padding for varbinary |
| 71 | + [DataRow("0x = 0x", 0)] // 0x without digits is a syntax error — verified separately below |
| 72 | + public void VarbinaryEquality(string condition, int expectedRows) |
| 73 | + { |
| 74 | + if (condition.Contains("0x =", System.StringComparison.Ordinal)) |
| 75 | + { |
| 76 | + // The "0x =" rows are filtered out below by the syntax-error test; |
| 77 | + // skip them here to keep the data-row table compact. |
| 78 | + return; |
| 79 | + } |
| 80 | + AreEqual(expectedRows, new Simulation().ExecuteReader($"select 1 where {condition}").EnumerateRecords().Count()); |
| 81 | + } |
| 82 | + |
| 83 | + [TestMethod] |
| 84 | + public void HexLiteral_OddLengthZeroPadsHighNibble() |
| 85 | + { |
| 86 | + // 0xABC → 0x0ABC (high nibble defaulted to 0, matching SQL Server). |
| 87 | + AreEqual(1, new Simulation().ExecuteReader("select 1 where 0xABC = 0x0ABC").EnumerateRecords().Count()); |
| 88 | + } |
| 89 | + |
| 90 | + [TestMethod] |
| 91 | + public void HexLiteral_BarePrefix_RaisesSyntaxError() |
| 92 | + { |
| 93 | + var ex = Throws<System.Data.Common.DbException>(() => new Simulation().ExecuteReader("select 1 where 0x = 0x").EnumerateRecords().ToArray()); |
| 94 | + StringAssert.Contains(ex.Message, "syntax"); |
| 95 | + } |
| 96 | + |
| 97 | + [TestMethod] |
| 98 | + public void StringLiteral_EmbeddedApostropheEscape() |
| 99 | + { |
| 100 | + // 'foo''bar' parses as foo'bar. |
| 101 | + using var connection = new Simulation().CreateOpenConnection(); |
| 102 | + _ = connection.CreateCommand("create table t ( v varchar(20) )").ExecuteNonQuery(); |
| 103 | + _ = connection.CreateCommand("insert t values ( 'foo''bar' )").ExecuteNonQuery(); |
| 104 | + AreEqual("foo'bar", connection.CreateCommand("select v from t").ExecuteScalar()); |
| 105 | + } |
| 106 | + |
| 107 | + [TestMethod] |
| 108 | + public void StringLiteral_Unclosed_RaisesError() |
| 109 | + { |
| 110 | + var ex = Throws<System.Data.Common.DbException>(() => new Simulation().ExecuteReader("select 1 where 'unclosed = 1").EnumerateRecords().ToArray()); |
| 111 | + StringAssert.Contains(ex.Message, "Unclosed quotation mark"); |
| 112 | + } |
| 113 | + |
| 114 | + [TestMethod] |
| 115 | + public void Insert_VarcharLiteral_StoresAndReadsBack() |
| 116 | + { |
| 117 | + using var connection = new Simulation().CreateOpenConnection(); |
| 118 | + _ = connection.CreateCommand("create table t ( v varchar(20) )").ExecuteNonQuery(); |
| 119 | + _ = connection.CreateCommand("insert t values ( 'hello' )").ExecuteNonQuery(); |
| 120 | + AreEqual("hello", connection.CreateCommand("select v from t").ExecuteScalar()); |
| 121 | + } |
| 122 | + |
| 123 | + [TestMethod] |
| 124 | + public void Insert_NVarcharLiteral_StoresAndReadsBack() |
| 125 | + { |
| 126 | + using var connection = new Simulation().CreateOpenConnection(); |
| 127 | + _ = connection.CreateCommand("create table t ( v nvarchar(20) )").ExecuteNonQuery(); |
| 128 | + _ = connection.CreateCommand("insert t values ( N'café' )").ExecuteNonQuery(); |
| 129 | + AreEqual("café", connection.CreateCommand("select v from t").ExecuteScalar()); |
| 130 | + } |
| 131 | + |
| 132 | + [TestMethod] |
| 133 | + public void Insert_HexLiteral_StoresAndReadsBack() |
| 134 | + { |
| 135 | + using var connection = new Simulation().CreateOpenConnection(); |
| 136 | + _ = connection.CreateCommand("create table t ( v varbinary(8) )").ExecuteNonQuery(); |
| 137 | + _ = connection.CreateCommand("insert t values ( 0xDEADBEEF )").ExecuteNonQuery(); |
| 138 | + var read = (byte[]?)connection.CreateCommand("select v from t").ExecuteScalar(); |
| 139 | + CollectionAssert.AreEqual(new byte[] { 0xDE, 0xAD, 0xBE, 0xEF }, read); |
| 140 | + } |
| 141 | + |
| 142 | + [TestMethod] |
| 143 | + public void FromTableWhere_FiltersByVarcharLiteral() |
| 144 | + { |
| 145 | + using var connection = new Simulation().CreateOpenConnection(); |
| 146 | + _ = connection.CreateCommand("create table t ( name varchar(20) )").ExecuteNonQuery(); |
| 147 | + _ = connection.CreateCommand("insert t values ( 'Alice' ), ( 'Bob' ), ( 'Carol' )").ExecuteNonQuery(); |
| 148 | + |
| 149 | + // Case-insensitive + ANSI padding: 'BOB ' matches 'Bob'. |
| 150 | + using var reader = connection.CreateCommand("select name from t where name = 'BOB '").ExecuteReader(); |
| 151 | + IsTrue(reader.Read()); |
| 152 | + AreEqual("Bob", reader[0]); |
| 153 | + IsFalse(reader.Read()); |
| 154 | + } |
| 155 | + |
| 156 | + [TestMethod] |
| 157 | + public void FromTableWhere_FiltersByVarbinaryLiteral() |
| 158 | + { |
| 159 | + using var connection = new Simulation().CreateOpenConnection(); |
| 160 | + _ = connection.CreateCommand("create table t ( id int, payload varbinary(4) )").ExecuteNonQuery(); |
| 161 | + _ = connection.CreateCommand("insert t values ( 1, 0xDEAD ), ( 2, 0xBEEF )").ExecuteNonQuery(); |
| 162 | + |
| 163 | + using var reader = connection.CreateCommand("select id from t where payload = 0xBEEF").ExecuteReader(); |
| 164 | + IsTrue(reader.Read()); |
| 165 | + AreEqual(2, reader[0]); |
| 166 | + IsFalse(reader.Read()); |
| 167 | + } |
| 168 | +} |
0 commit comments