|
| 1 | +using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert; |
| 2 | + |
| 3 | +namespace SqlServerSimulator; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Curated subset of <see cref="SelectTests"/>-style queries run against |
| 7 | +/// <see cref="StorageBackend.Pages"/>. Each query exercises the page-row |
| 8 | +/// encoder/decoder end-to-end via the parallel <c>Expression.RunSql</c> |
| 9 | +/// evaluator. Tableless only — table-backed queries arrive in a later step. |
| 10 | +/// </summary> |
| 11 | +[TestClass] |
| 12 | +public sealed class PagesBackendIntegrationTests |
| 13 | +{ |
| 14 | + private static object? ExecuteScalar(string commandText) => |
| 15 | + new Simulation(StorageBackend.Pages).ExecuteScalar(commandText); |
| 16 | + |
| 17 | + [TestMethod] |
| 18 | + [DataRow("select 1", 1)] |
| 19 | + [DataRow("select 0", 0)] |
| 20 | + [DataRow("select 42", 42)] |
| 21 | + [DataRow("select +1", 1)] |
| 22 | + [DataRow("select +0", 0)] |
| 23 | + [DataRow("select - 1", -1)] |
| 24 | + [DataRow("select -1", -1)] |
| 25 | + public void IntegerLiteral(string commandText, int expected) |
| 26 | + => AreEqual(expected, ExecuteScalar(commandText)); |
| 27 | + |
| 28 | + [TestMethod] |
| 29 | + [DataRow("select 1 + 1", 2)] |
| 30 | + [DataRow("select 1 - 1", 0)] |
| 31 | + [DataRow("select 2 * 2", 4)] |
| 32 | + [DataRow("select 2 / 2", 1)] |
| 33 | + [DataRow("select 5 % 2", 1)] |
| 34 | + [DataRow("select 5 % 3", 2)] |
| 35 | + [DataRow("select 5 % 5", 0)] |
| 36 | + [DataRow("select 1 + 2 * 3", 7)] |
| 37 | + [DataRow("select 3 * 2 + 1", 7)] |
| 38 | + public void Arithmetic(string commandText, int expected) |
| 39 | + => AreEqual(expected, ExecuteScalar(commandText)); |
| 40 | + |
| 41 | + [TestMethod] |
| 42 | + [DataRow("select 1 & 3", 1)] |
| 43 | + [DataRow("select 1 | 3", 3)] |
| 44 | + [DataRow("select 1 ^ 3", 2)] |
| 45 | + public void Bitwise(string commandText, int expected) |
| 46 | + => AreEqual(expected, ExecuteScalar(commandText)); |
| 47 | + |
| 48 | + [TestMethod] |
| 49 | + [DataRow("select (1)", 1)] |
| 50 | + [DataRow("select (1) + 1", 2)] |
| 51 | + [DataRow("select (1) + (1)", 2)] |
| 52 | + [DataRow("select (1 + 2) * 3", 9)] |
| 53 | + public void Parenthesized(string commandText, int expected) |
| 54 | + => AreEqual(expected, ExecuteScalar(commandText)); |
| 55 | + |
| 56 | + [TestMethod] |
| 57 | + [DataRow("select abs(1)", 1)] |
| 58 | + [DataRow("select abs(0)", 0)] |
| 59 | + [DataRow("select abs(-1)", 1)] |
| 60 | + [DataRow("select datalength(1)", 4)] |
| 61 | + public void BuiltInFunction(string commandText, int expected) |
| 62 | + => AreEqual(expected, ExecuteScalar(commandText)); |
| 63 | + |
| 64 | + [TestMethod] |
| 65 | + public void AbsOfNull() |
| 66 | + => AreEqual(DBNull.Value, ExecuteScalar("select abs(null)")); |
| 67 | + |
| 68 | + [TestMethod] |
| 69 | + public void DataLengthOfNull() |
| 70 | + => AreEqual(DBNull.Value, ExecuteScalar("select datalength(null)")); |
| 71 | + |
| 72 | + [TestMethod] |
| 73 | + public void ArithmeticPropagatesNull() |
| 74 | + => AreEqual(DBNull.Value, ExecuteScalar("select null + 1")); |
| 75 | + |
| 76 | + [TestMethod] |
| 77 | + [DataRow("select 1 as c", "c", 1)] |
| 78 | + [DataRow("select 1 + 1 as c", "c", 2)] |
| 79 | + [DataRow("select 1 c", "c", 1)] |
| 80 | + public void NamedExpression(string commandText, string name, int value) |
| 81 | + { |
| 82 | + using var reader = new Simulation(StorageBackend.Pages).ExecuteReader(commandText); |
| 83 | + IsTrue(reader.Read()); |
| 84 | + AreEqual(name, reader.GetName(0)); |
| 85 | + AreEqual(value, reader[0]); |
| 86 | + } |
| 87 | + |
| 88 | + [TestMethod] |
| 89 | + public void MultiColumnConstants() |
| 90 | + { |
| 91 | + using var reader = new Simulation(StorageBackend.Pages).ExecuteReader("select 1, 2"); |
| 92 | + IsTrue(reader.Read()); |
| 93 | + AreEqual(2, reader.FieldCount); |
| 94 | + AreEqual(1, reader[0]); |
| 95 | + AreEqual(2, reader[1]); |
| 96 | + IsFalse(reader.Read()); |
| 97 | + } |
| 98 | + |
| 99 | + [TestMethod] |
| 100 | + public void MultiColumnMixedNamed() |
| 101 | + { |
| 102 | + using var reader = new Simulation(StorageBackend.Pages).ExecuteReader("select 1 + 1 as x, 7 as y"); |
| 103 | + IsTrue(reader.Read()); |
| 104 | + AreEqual(2, reader.FieldCount); |
| 105 | + AreEqual("x", reader.GetName(0)); |
| 106 | + AreEqual("y", reader.GetName(1)); |
| 107 | + AreEqual(2, reader[0]); |
| 108 | + AreEqual(7, reader[1]); |
| 109 | + } |
| 110 | + |
| 111 | + [TestMethod] |
| 112 | + [DataRow("select @p0", "p0", 5)] |
| 113 | + [DataRow("select @p0", "@p0", 6)] |
| 114 | + [DataRow("select (@p0)", "p0", 7)] |
| 115 | + [DataRow("select @p0 + 1", "p0", 41)] |
| 116 | + public void ParameterValue(string commandText, string name, int value) |
| 117 | + { |
| 118 | + var result = new Simulation(StorageBackend.Pages) |
| 119 | + .CreateOpenConnection() |
| 120 | + .CreateCommand(commandText, (name, value)) |
| 121 | + .ExecuteScalar(); |
| 122 | + |
| 123 | + AreEqual(commandText.EndsWith("+ 1") ? value + 1 : value, result); |
| 124 | + } |
| 125 | +} |
0 commit comments