|
| 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 JOIN forms: <c>INNER JOIN</c>, |
| 8 | +/// <c>LEFT [OUTER] JOIN</c>, bare <c>JOIN</c> (treated as INNER), |
| 9 | +/// <c>CROSS JOIN</c>, multi-table chains, self-joins via alias, and the |
| 10 | +/// shared rules — qualifier-aware column resolution (Msg 209 on |
| 11 | +/// unqualified ambiguity), ON-predicate 3VL semantics (UNKNOWN doesn't |
| 12 | +/// match), and parser rejections (INNER JOIN without ON, CROSS JOIN |
| 13 | +/// with ON). RIGHT JOIN is intentionally not modeled — see |
| 14 | +/// <see cref="RightJoin_NotSupported"/>. |
| 15 | +/// </summary> |
| 16 | +[TestClass] |
| 17 | +public sealed class JoinTests |
| 18 | +{ |
| 19 | + private static DbConnection SeededAB() |
| 20 | + { |
| 21 | + var connection = new Simulation().CreateOpenConnection(); |
| 22 | + _ = connection.CreateCommand("create table a (id int, name varchar(20))").ExecuteNonQuery(); |
| 23 | + _ = connection.CreateCommand("create table b (id int, a_id int, val int)").ExecuteNonQuery(); |
| 24 | + _ = connection.CreateCommand("insert into a values (1, 'one'), (2, 'two'), (3, 'three')").ExecuteNonQuery(); |
| 25 | + _ = connection.CreateCommand("insert into b values (10, 1, 100), (11, 1, 200), (12, 2, 300)").ExecuteNonQuery(); |
| 26 | + return connection; |
| 27 | + } |
| 28 | + |
| 29 | + private static List<(int, int)> ReadIntPairs(DbCommand command) |
| 30 | + { |
| 31 | + using var reader = command.ExecuteReader(); |
| 32 | + var rows = new List<(int, int)>(); |
| 33 | + while (reader.Read()) |
| 34 | + rows.Add((reader.GetInt32(0), reader.IsDBNull(1) ? -1 : reader.GetInt32(1))); |
| 35 | + return rows; |
| 36 | + } |
| 37 | + |
| 38 | + // === INNER JOIN === |
| 39 | + |
| 40 | + [TestMethod] |
| 41 | + public void InnerJoin_BasicMatch() |
| 42 | + { |
| 43 | + using var connection = SeededAB(); |
| 44 | + var rows = ReadIntPairs(connection.CreateCommand( |
| 45 | + "select a.id, b.val from a inner join b on a.id = b.a_id")); |
| 46 | + CollectionAssert.AreEquivalent(new[] { (1, 100), (1, 200), (2, 300) }, rows); |
| 47 | + } |
| 48 | + |
| 49 | + [TestMethod] |
| 50 | + public void InnerJoin_BareJoinKeyword_TreatedAsInner() |
| 51 | + { |
| 52 | + using var connection = SeededAB(); |
| 53 | + var rows = ReadIntPairs(connection.CreateCommand( |
| 54 | + "select a.id, b.val from a join b on a.id = b.a_id")); |
| 55 | + CollectionAssert.AreEquivalent(new[] { (1, 100), (1, 200), (2, 300) }, rows); |
| 56 | + } |
| 57 | + |
| 58 | + [TestMethod] |
| 59 | + public void InnerJoin_UnmatchedRowsExcluded() |
| 60 | + { |
| 61 | + // a.id=3 has no matching b row, so the row is excluded from INNER JOIN. |
| 62 | + using var connection = SeededAB(); |
| 63 | + var matched = new List<int>(); |
| 64 | + using var reader = connection.CreateCommand( |
| 65 | + "select a.id from a inner join b on a.id = b.a_id").ExecuteReader(); |
| 66 | + while (reader.Read()) matched.Add(reader.GetInt32(0)); |
| 67 | + // 1 (matches b twice), 1, 2. |
| 68 | + CollectionAssert.AreEquivalent(new[] { 1, 1, 2 }, matched); |
| 69 | + } |
| 70 | + |
| 71 | + [TestMethod] |
| 72 | + public void InnerJoin_MissingOn_RaisesSyntaxError() |
| 73 | + { |
| 74 | + var simulation = new Simulation(); |
| 75 | + _ = simulation.ExecuteNonQuery("create table a (id int)"); |
| 76 | + _ = simulation.ExecuteNonQuery("create table b (id int)"); |
| 77 | + _ = Throws<DbException>(() => _ = simulation.ExecuteScalar("select 1 from a inner join b")); |
| 78 | + } |
| 79 | + |
| 80 | + // === LEFT JOIN === |
| 81 | + |
| 82 | + [TestMethod] |
| 83 | + public void LeftJoin_NullFillsUnmatchedRight() |
| 84 | + { |
| 85 | + using var connection = SeededAB(); |
| 86 | + var rows = ReadIntPairs(connection.CreateCommand( |
| 87 | + "select a.id, b.val from a left join b on a.id = b.a_id")); |
| 88 | + // a.id=3 has no match; b.val is NULL (we map to -1 for the test pair). |
| 89 | + CollectionAssert.AreEquivalent(new[] { (1, 100), (1, 200), (2, 300), (3, -1) }, rows); |
| 90 | + } |
| 91 | + |
| 92 | + [TestMethod] |
| 93 | + public void LeftJoin_LeftOuterSpelling_Equivalent() |
| 94 | + { |
| 95 | + using var connection = SeededAB(); |
| 96 | + var rows = ReadIntPairs(connection.CreateCommand( |
| 97 | + "select a.id, b.val from a left outer join b on a.id = b.a_id")); |
| 98 | + CollectionAssert.AreEquivalent(new[] { (1, 100), (1, 200), (2, 300), (3, -1) }, rows); |
| 99 | + } |
| 100 | + |
| 101 | + [TestMethod] |
| 102 | + public void LeftJoin_IsNullPattern_FindsUnmatched() |
| 103 | + { |
| 104 | + // The classic "find rows in A with no match in B" pattern. |
| 105 | + using var connection = SeededAB(); |
| 106 | + var matched = new List<int>(); |
| 107 | + using var reader = connection.CreateCommand( |
| 108 | + "select a.id from a left join b on a.id = b.a_id where b.val is null").ExecuteReader(); |
| 109 | + while (reader.Read()) matched.Add(reader.GetInt32(0)); |
| 110 | + CollectionAssert.AreEqual(new[] { 3 }, matched); |
| 111 | + } |
| 112 | + |
| 113 | + // === CROSS JOIN === |
| 114 | + |
| 115 | + [TestMethod] |
| 116 | + public void CrossJoin_CartesianProduct() |
| 117 | + { |
| 118 | + using var connection = SeededAB(); |
| 119 | + // a has 3 rows, b has 3 rows → 9. |
| 120 | + AreEqual(9, connection.CreateCommand("select count(*) from a cross join b").ExecuteScalar()); |
| 121 | + } |
| 122 | + |
| 123 | + [TestMethod] |
| 124 | + public void CrossJoin_WithOn_RaisesSyntaxError() |
| 125 | + { |
| 126 | + var simulation = new Simulation(); |
| 127 | + _ = simulation.ExecuteNonQuery("create table a (id int)"); |
| 128 | + _ = simulation.ExecuteNonQuery("create table b (id int)"); |
| 129 | + _ = Throws<DbException>(() => _ = simulation.ExecuteScalar("select 1 from a cross join b on 1=1")); |
| 130 | + } |
| 131 | + |
| 132 | + // === Multi-table chain === |
| 133 | + |
| 134 | + [TestMethod] |
| 135 | + public void Chain_InnerThenLeft_Composes() |
| 136 | + { |
| 137 | + var simulation = new Simulation(); |
| 138 | + _ = simulation.ExecuteNonQuery("create table a (id int, name varchar(20))"); |
| 139 | + _ = simulation.ExecuteNonQuery("create table b (id int, a_id int, val int)"); |
| 140 | + _ = simulation.ExecuteNonQuery("create table c (id int, b_id int, label varchar(20))"); |
| 141 | + _ = simulation.ExecuteNonQuery("insert into a values (1, 'one'), (2, 'two')"); |
| 142 | + _ = simulation.ExecuteNonQuery("insert into b values (10, 1, 100), (11, 1, 200), (12, 2, 300)"); |
| 143 | + _ = simulation.ExecuteNonQuery("insert into c values (20, 10, 'first'), (21, 12, 'second')"); |
| 144 | + |
| 145 | + using var connection = simulation.CreateOpenConnection(); |
| 146 | + using var reader = connection.CreateCommand( |
| 147 | + "select a.name, b.val, c.label from a inner join b on a.id = b.a_id left join c on b.id = c.b_id").ExecuteReader(); |
| 148 | + var rows = new List<(string, int, string?)>(); |
| 149 | + while (reader.Read()) |
| 150 | + rows.Add((reader.GetString(0), reader.GetInt32(1), reader.IsDBNull(2) ? null : reader.GetString(2))); |
| 151 | + CollectionAssert.AreEquivalent(new (string, int, string?)[] |
| 152 | + { |
| 153 | + ("one", 100, "first"), |
| 154 | + ("one", 200, null), |
| 155 | + ("two", 300, "second"), |
| 156 | + }, rows); |
| 157 | + } |
| 158 | + |
| 159 | + // === Self-join via aliases === |
| 160 | + |
| 161 | + [TestMethod] |
| 162 | + public void SelfJoin_DifferentAliases_DistinguishCopies() |
| 163 | + { |
| 164 | + var simulation = new Simulation(); |
| 165 | + _ = simulation.ExecuteNonQuery("create table a (id int, name varchar(20))"); |
| 166 | + _ = simulation.ExecuteNonQuery("insert into a values (1, 'one'), (2, 'two')"); |
| 167 | + |
| 168 | + using var connection = simulation.CreateOpenConnection(); |
| 169 | + using var reader = connection.CreateCommand( |
| 170 | + "select t1.name, t2.name from a t1 inner join a t2 on t1.id <> t2.id").ExecuteReader(); |
| 171 | + var pairs = new List<(string, string)>(); |
| 172 | + while (reader.Read()) pairs.Add((reader.GetString(0), reader.GetString(1))); |
| 173 | + CollectionAssert.AreEquivalent(new[] { ("one", "two"), ("two", "one") }, pairs); |
| 174 | + } |
| 175 | + |
| 176 | + // === Column resolution === |
| 177 | + |
| 178 | + [TestMethod] |
| 179 | + public void Ambiguous_UnqualifiedColumn_RaisesMsg209() |
| 180 | + { |
| 181 | + using var connection = SeededAB(); |
| 182 | + // Both a and b have an `id` column; bare `id` is ambiguous. |
| 183 | + var ex = Throws<DbException>(() => _ = connection.CreateCommand( |
| 184 | + "select id from a inner join b on a.id = b.a_id").ExecuteScalar()); |
| 185 | + AreEqual("209", ex.Data["HelpLink.EvtID"]); |
| 186 | + AreEqual("Ambiguous column name 'id'.", ex.Message); |
| 187 | + } |
| 188 | + |
| 189 | + [TestMethod] |
| 190 | + public void OnPredicate_NullEqualsNull_ExcludesRow() |
| 191 | + { |
| 192 | + // Two-table inner join where both columns can be NULL. ON `x.k = y.k` |
| 193 | + // with NULLs on both sides → UNKNOWN → excluded (3VL). |
| 194 | + var simulation = new Simulation(); |
| 195 | + _ = simulation.ExecuteNonQuery("create table x (k int null)"); |
| 196 | + _ = simulation.ExecuteNonQuery("create table y (k int null)"); |
| 197 | + _ = simulation.ExecuteNonQuery("insert into x values (1), (null)"); |
| 198 | + _ = simulation.ExecuteNonQuery("insert into y values (1), (null)"); |
| 199 | + |
| 200 | + using var connection = simulation.CreateOpenConnection(); |
| 201 | + var rows = ReadIntPairs(connection.CreateCommand( |
| 202 | + "select x.k, y.k from x inner join y on x.k = y.k")); |
| 203 | + CollectionAssert.AreEqual(new[] { (1, 1) }, rows); |
| 204 | + } |
| 205 | + |
| 206 | + // === RIGHT JOIN — explicitly not supported === |
| 207 | + |
| 208 | + [TestMethod] |
| 209 | + public void RightJoin_NotSupported() |
| 210 | + { |
| 211 | + var simulation = new Simulation(); |
| 212 | + _ = simulation.ExecuteNonQuery("create table a (id int)"); |
| 213 | + _ = simulation.ExecuteNonQuery("create table b (id int)"); |
| 214 | + _ = Throws<NotSupportedException>(() => |
| 215 | + _ = simulation.ExecuteScalar("select 1 from a right join b on a.id = b.id")); |
| 216 | + } |
| 217 | +} |
0 commit comments