|
| 1 | +using System.Data.Common; |
| 2 | +using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert; |
| 3 | + |
| 4 | +namespace SqlServerSimulator; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Behavioral tests for the <c>WITH name [(col, …)] AS (SELECT …)</c> |
| 8 | +/// non-recursive CTE prefix that scopes to one following SELECT / INSERT |
| 9 | +/// / UPDATE / DELETE / MERGE statement. Recursive CTEs are not modeled. |
| 10 | +/// </summary> |
| 11 | +[TestClass] |
| 12 | +public sealed class CommonTableExpressionTests |
| 13 | +{ |
| 14 | + private static Simulation WithSourceTable() |
| 15 | + { |
| 16 | + var simulation = new Simulation(); |
| 17 | + _ = simulation.ExecuteNonQuery(""" |
| 18 | + create table src (id int, score int); |
| 19 | + insert src (id, score) values (1, 10), (2, 20), (3, 30) |
| 20 | + """); |
| 21 | + return simulation; |
| 22 | + } |
| 23 | + |
| 24 | + [TestMethod] |
| 25 | + public void Cte_Vanilla_ProjectsBodyRows() |
| 26 | + { |
| 27 | + var simulation = WithSourceTable(); |
| 28 | + using var reader = simulation.ExecuteReader("with c as (select id, score from src) select id, score from c order by id"); |
| 29 | + var rows = new List<(int id, int score)>(); |
| 30 | + while (reader.Read()) |
| 31 | + rows.Add((reader.GetInt32(0), reader.GetInt32(1))); |
| 32 | + CollectionAssert.AreEqual(new[] { (1, 10), (2, 20), (3, 30) }, rows); |
| 33 | + } |
| 34 | + |
| 35 | + [TestMethod] |
| 36 | + public void Cte_RenameList_RenamesProjection() |
| 37 | + { |
| 38 | + var simulation = WithSourceTable(); |
| 39 | + using var reader = simulation.ExecuteReader("with c (a, b) as (select id, score from src) select a, b from c order by a"); |
| 40 | + IsTrue(reader.Read()); |
| 41 | + AreEqual("a", reader.GetName(0)); |
| 42 | + AreEqual("b", reader.GetName(1)); |
| 43 | + AreEqual(1, reader.GetInt32(0)); |
| 44 | + AreEqual(10, reader.GetInt32(1)); |
| 45 | + } |
| 46 | + |
| 47 | + [TestMethod] |
| 48 | + public void Cte_MultipleBindings_LaterReferencesEarlier() |
| 49 | + => AreEqual(2, WithSourceTable().ExecuteScalar(""" |
| 50 | + with a as (select id, score from src), |
| 51 | + b as (select id from a where score > 10) |
| 52 | + select count(*) from b |
| 53 | + """)); |
| 54 | + |
| 55 | + [TestMethod] |
| 56 | + public void Cte_MultipleReferences_EachReExecutesPlan() |
| 57 | + { |
| 58 | + var simulation = WithSourceTable(); |
| 59 | + using var reader = simulation.ExecuteReader(""" |
| 60 | + with c as (select id from src) |
| 61 | + select x.id, y.id |
| 62 | + from c x cross join c y |
| 63 | + where x.id < y.id |
| 64 | + order by x.id, y.id |
| 65 | + """); |
| 66 | + var rows = new List<(int x, int y)>(); |
| 67 | + while (reader.Read()) |
| 68 | + rows.Add((reader.GetInt32(0), reader.GetInt32(1))); |
| 69 | + CollectionAssert.AreEqual(new[] { (1, 2), (1, 3), (2, 3) }, rows); |
| 70 | + } |
| 71 | + |
| 72 | + [TestMethod] |
| 73 | + public void Cte_AliasOnCteReference_QualifiesColumns() |
| 74 | + => AreEqual(20, WithSourceTable().ExecuteScalar("with c as (select id, score from src) select alias.score from c as alias where alias.id = 2")); |
| 75 | + |
| 76 | + [TestMethod] |
| 77 | + public void Cte_WhereReferencesCteColumns() |
| 78 | + => AreEqual(2, WithSourceTable().ExecuteScalar("with c as (select id, score from src) select count(*) from c where score >= 20")); |
| 79 | + |
| 80 | + [TestMethod] |
| 81 | + public void Cte_StarProjectionInBody_ExpandsBeforeRename() |
| 82 | + { |
| 83 | + var simulation = WithSourceTable(); |
| 84 | + using var reader = simulation.ExecuteReader("with c as (select * from src) select id, score from c order by id"); |
| 85 | + IsTrue(reader.Read()); |
| 86 | + AreEqual(1, reader.GetInt32(0)); |
| 87 | + AreEqual(10, reader.GetInt32(1)); |
| 88 | + } |
| 89 | + |
| 90 | + [TestMethod] |
| 91 | + public void Cte_UnionInBody_Works() |
| 92 | + => AreEqual(6, new Simulation().ExecuteScalar(""" |
| 93 | + with c as (select 1 as v union all select 2 union all select 3) |
| 94 | + select sum(v) from c |
| 95 | + """)); |
| 96 | + |
| 97 | + // CTE shadows a real table of the same name for the prefixed statement. |
| 98 | + [TestMethod] |
| 99 | + public void Cte_ShadowsRealTable() |
| 100 | + { |
| 101 | + var simulation = WithSourceTable(); |
| 102 | + AreEqual(42, simulation.ExecuteScalar("with src as (select 42 as score) select score from src")); |
| 103 | + } |
| 104 | + |
| 105 | + [TestMethod] |
| 106 | + public void Cte_PrefixesInsertSelect_RowsLandInDestination() |
| 107 | + { |
| 108 | + var simulation = WithSourceTable(); |
| 109 | + _ = simulation.ExecuteNonQuery("create table dst (a int)"); |
| 110 | + AreEqual(2, simulation.ExecuteNonQuery("with c as (select score from src where score >= 20) insert dst (a) select score from c")); |
| 111 | + AreEqual(2, simulation.ExecuteScalar("select count(*) from dst")); |
| 112 | + AreEqual(50, simulation.ExecuteScalar("select sum(a) from dst")); |
| 113 | + } |
| 114 | + |
| 115 | + [TestMethod] |
| 116 | + public void Cte_PrefixesUpdate_FromJoin() |
| 117 | + { |
| 118 | + var simulation = WithSourceTable(); |
| 119 | + _ = simulation.ExecuteNonQuery("create table u (id int, score int); insert u select id, score from src"); |
| 120 | + AreEqual(2, simulation.ExecuteNonQuery("with c as (select id from src where score >= 20) update u set score = score * 10 from u inner join c on c.id = u.id")); |
| 121 | + AreEqual(10, simulation.ExecuteScalar("select score from u where id = 1")); |
| 122 | + AreEqual(200, simulation.ExecuteScalar("select score from u where id = 2")); |
| 123 | + AreEqual(300, simulation.ExecuteScalar("select score from u where id = 3")); |
| 124 | + } |
| 125 | + |
| 126 | + [TestMethod] |
| 127 | + public void Cte_PrefixesDelete_FromJoin() |
| 128 | + { |
| 129 | + var simulation = WithSourceTable(); |
| 130 | + _ = simulation.ExecuteNonQuery("create table d (id int, score int); insert d select id, score from src"); |
| 131 | + AreEqual(2, simulation.ExecuteNonQuery("with c as (select id from src where score >= 20) delete d from d inner join c on c.id = d.id")); |
| 132 | + AreEqual(1, simulation.ExecuteScalar("select count(*) from d")); |
| 133 | + } |
| 134 | + |
| 135 | + [TestMethod] |
| 136 | + public void Cte_DuplicateName_RaisesMsg239() |
| 137 | + => new Simulation().AssertSqlError("with a as (select 1 as v), a as (select 2 as v) select * from a", 239, |
| 138 | + "Duplicate common table expression name 'a' was specified."); |
| 139 | + |
| 140 | + [TestMethod] |
| 141 | + public void Cte_RenameTooFew_RaisesMsg8158() |
| 142 | + => WithSourceTable().AssertSqlError("with c (x) as (select id, score from src) select * from c", 8158, |
| 143 | + "'c' has more columns than were specified in the column list."); |
| 144 | + |
| 145 | + [TestMethod] |
| 146 | + public void Cte_RenameTooMany_RaisesMsg8159() |
| 147 | + => WithSourceTable().AssertSqlError("with c (x, y, z) as (select id, score from src) select * from c", 8159, |
| 148 | + "'c' has fewer columns than were specified in the column list."); |
| 149 | + |
| 150 | + [TestMethod] |
| 151 | + public void Cte_OrderByWithoutTopOrOffset_RaisesMsg1033() |
| 152 | + => WithSourceTable().AssertSqlError("with c as (select id from src order by id) select * from c", 1033, |
| 153 | + "The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified."); |
| 154 | + |
| 155 | + [TestMethod] |
| 156 | + public void Cte_OrderByWithTop_Allowed() |
| 157 | + => AreEqual(3, WithSourceTable().ExecuteScalar("with c as (select top 1 id from src order by id desc) select id from c")); |
| 158 | + |
| 159 | + [TestMethod] |
| 160 | + public void Cte_OrderByWithOffsetFetch_Allowed() |
| 161 | + => AreEqual(2, WithSourceTable().ExecuteScalar("with c as (select id from src order by id desc offset 1 rows fetch next 1 rows only) select id from c")); |
| 162 | + |
| 163 | + [TestMethod] |
| 164 | + public void Cte_RecursiveSelfReference_RaisesNotSupported() |
| 165 | + => Throws<NotSupportedException>(() => WithSourceTable().ExecuteNonQuery(""" |
| 166 | + with a as ( |
| 167 | + select 1 as n |
| 168 | + union all |
| 169 | + select n + 1 from a where n < 5 |
| 170 | + ) select * from a |
| 171 | + """)); |
| 172 | + |
| 173 | + [TestMethod] |
| 174 | + public void Cte_BindingClearsBetweenStatements() |
| 175 | + { |
| 176 | + var simulation = WithSourceTable(); |
| 177 | + // First statement uses a CTE binding named 'c'; the second statement |
| 178 | + // should NOT see 'c' anymore — it must fail with a missing-table error. |
| 179 | + _ = simulation.ExecuteNonQuery("with c as (select id from src) select count(*) from c"); |
| 180 | + _ = Throws<DbException>(() => simulation.ExecuteNonQuery("select count(*) from c")); |
| 181 | + } |
| 182 | + |
| 183 | + [TestMethod] |
| 184 | + public void Cte_WithParameter() |
| 185 | + { |
| 186 | + var simulation = WithSourceTable(); |
| 187 | + using var connection = simulation.CreateOpenConnection(); |
| 188 | + var command = connection.CreateCommand("with c as (select id, score from src where id >= @minId) select count(*) from c", ("minId", 2)); |
| 189 | + AreEqual(2, command.ExecuteScalar()); |
| 190 | + } |
| 191 | + |
| 192 | + // CTE is reachable from a scalar subquery inside the prefixed statement |
| 193 | + // (the binding lives in ParserContext.CteBindings for the whole statement). |
| 194 | + [TestMethod] |
| 195 | + public void Cte_ReachableFromScalarSubqueryInProjection() |
| 196 | + { |
| 197 | + var simulation = WithSourceTable(); |
| 198 | + using var reader = simulation.ExecuteReader(""" |
| 199 | + with c as (select id, score from src) |
| 200 | + select id, (select max(score) from c) as max_score |
| 201 | + from c |
| 202 | + order by id |
| 203 | + """); |
| 204 | + var rows = new List<(int id, int max)>(); |
| 205 | + while (reader.Read()) |
| 206 | + rows.Add((reader.GetInt32(0), reader.GetInt32(1))); |
| 207 | + CollectionAssert.AreEqual(new[] { (1, 30), (2, 30), (3, 30) }, rows); |
| 208 | + } |
| 209 | +} |
0 commit comments