|
| 1 | +using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert; |
| 2 | + |
| 3 | +namespace SqlServerSimulator; |
| 4 | + |
| 5 | +[TestClass] |
| 6 | +public class OpenQueryTests |
| 7 | +{ |
| 8 | + private static Simulation LocalWithRemote(out Simulation remote, string remoteSetup) |
| 9 | + { |
| 10 | + remote = new Simulation(); |
| 11 | + _ = remote.ExecuteNonQuery(remoteSetup); |
| 12 | + |
| 13 | + var local = new Simulation(); |
| 14 | + local.AddRemoteSimulation("RMT", remote); |
| 15 | + _ = local.ExecuteNonQuery("exec sp_addlinkedserver 'RMT', 'SQL Server'"); |
| 16 | + return local; |
| 17 | + } |
| 18 | + |
| 19 | + [TestMethod] |
| 20 | + public void Basic_ReturnsRemoteRows() |
| 21 | + { |
| 22 | + var local = LocalWithRemote(out _, "create table dbo.t (id int not null primary key, name varchar(20) not null); insert t values (1, 'a'), (2, 'b')"); |
| 23 | + AreEqual("b", local.ExecuteScalar("select name from OPENQUERY(RMT, 'select id, name from dbo.t where id = 2')")); |
| 24 | + } |
| 25 | + |
| 26 | + [TestMethod] |
| 27 | + public void Basic_CountAllRows() |
| 28 | + { |
| 29 | + var local = LocalWithRemote(out _, "create table dbo.t (id int not null primary key); insert t values (1), (2), (3)"); |
| 30 | + AreEqual(3, local.ExecuteScalar("select count(*) from OPENQUERY(RMT, 'select id from dbo.t')")); |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// The pass-through query is arbitrary T-SQL run on the remote, not just a |
| 35 | + /// table name: WHERE / expressions / aggregation all execute remotely. |
| 36 | + /// </summary> |
| 37 | + [TestMethod] |
| 38 | + public void Passthrough_AggregationRunsRemotely() |
| 39 | + { |
| 40 | + var local = LocalWithRemote(out _, "create table dbo.t (id int not null primary key, qty int not null); insert t values (1, 5), (2, 10), (3, 7)"); |
| 41 | + AreEqual(22, local.ExecuteScalar("select total from OPENQUERY(RMT, 'select sum(qty) as total from dbo.t')")); |
| 42 | + } |
| 43 | + |
| 44 | + [TestMethod] |
| 45 | + public void Passthrough_ExpressionColumn() |
| 46 | + { |
| 47 | + var local = LocalWithRemote(out _, "create table dbo.t (id int not null primary key); insert t values (10)"); |
| 48 | + AreEqual(20, local.ExecuteScalar("select doubled from OPENQUERY(RMT, 'select id * 2 as doubled from dbo.t')")); |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// OPENQUERY returns only the FIRST result set of a multi-statement |
| 53 | + /// pass-through batch. |
| 54 | + /// </summary> |
| 55 | + [TestMethod] |
| 56 | + public void FirstResultSetOnly() |
| 57 | + { |
| 58 | + var local = LocalWithRemote(out _, "create table dbo.a (v int not null); insert a values (100); create table dbo.b (v int not null); insert b values (200), (300)"); |
| 59 | + // First SELECT yields one row (100); the second SELECT is ignored. |
| 60 | + AreEqual(1, local.ExecuteScalar("select count(*) from OPENQUERY(RMT, 'select v from dbo.a; select v from dbo.b')")); |
| 61 | + AreEqual(100, local.ExecuteScalar("select v from OPENQUERY(RMT, 'select v from dbo.a; select v from dbo.b')")); |
| 62 | + } |
| 63 | + |
| 64 | + [TestMethod] |
| 65 | + public void Position_InJoin() |
| 66 | + { |
| 67 | + var local = LocalWithRemote(out _, "create table dbo.parts (part_id int not null primary key, name varchar(20) not null); insert parts values (1, 'widget'), (2, 'gadget')"); |
| 68 | + _ = local.ExecuteNonQuery("create table dbo.orders (order_id int not null primary key, part_id int not null, qty int not null); insert orders values (1, 1, 5), (2, 2, 10), (3, 1, 7)"); |
| 69 | + |
| 70 | + var qty = local.ExecuteScalar(""" |
| 71 | + select sum(o.qty) |
| 72 | + from dbo.orders o |
| 73 | + inner join OPENQUERY(RMT, 'select part_id, name from dbo.parts') p on p.part_id = o.part_id |
| 74 | + where p.name = 'widget' |
| 75 | + """); |
| 76 | + AreEqual(12, qty); |
| 77 | + } |
| 78 | + |
| 79 | + [TestMethod] |
| 80 | + public void Position_InDerivedTable() |
| 81 | + { |
| 82 | + var local = LocalWithRemote(out _, "create table dbo.t (id int not null primary key, v int not null); insert t values (1, 10), (2, 20), (3, 30)"); |
| 83 | + AreEqual(50, local.ExecuteScalar("select sum(v) from (select v from OPENQUERY(RMT, 'select id, v from dbo.t where id >= 2')) d")); |
| 84 | + } |
| 85 | + |
| 86 | + [TestMethod] |
| 87 | + public void Alias_WithoutAs() |
| 88 | + { |
| 89 | + var local = LocalWithRemote(out _, "create table dbo.t (id int not null primary key); insert t values (7)"); |
| 90 | + AreEqual(7, local.ExecuteScalar("select q.id from OPENQUERY(RMT, 'select id from dbo.t') q")); |
| 91 | + } |
| 92 | + |
| 93 | + [TestMethod] |
| 94 | + public void Alias_WithAs() |
| 95 | + { |
| 96 | + var local = LocalWithRemote(out _, "create table dbo.t (id int not null primary key); insert t values (7)"); |
| 97 | + AreEqual(7, local.ExecuteScalar("select q.id from OPENQUERY(RMT, 'select id from dbo.t') as q")); |
| 98 | + } |
| 99 | + |
| 100 | + [TestMethod] |
| 101 | + public void KeywordIsCaseInsensitive() |
| 102 | + { |
| 103 | + var local = LocalWithRemote(out _, "create table dbo.t (id int not null primary key); insert t values (5)"); |
| 104 | + AreEqual(5, local.ExecuteScalar("select id from openquery(RMT, 'select id from dbo.t')")); |
| 105 | + } |
| 106 | + |
| 107 | + [TestMethod] |
| 108 | + public void BracketedServerName_Resolves() |
| 109 | + { |
| 110 | + var local = LocalWithRemote(out _, "create table dbo.t (id int not null primary key); insert t values (9)"); |
| 111 | + AreEqual(9, local.ExecuteScalar("select id from OPENQUERY([RMT], 'select id from dbo.t')")); |
| 112 | + } |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// A pass-through payload that produces no result set (empty string, a |
| 116 | + /// non-SELECT statement) surfaces a clear <see cref="NotSupportedException"/> |
| 117 | + /// naming the condition — the exact real-server Msg isn't probed. |
| 118 | + /// </summary> |
| 119 | + [TestMethod] |
| 120 | + public void EmptyQuery_NoResultSet_NotSupported() |
| 121 | + { |
| 122 | + var local = LocalWithRemote(out _, "create table dbo.t (id int)"); |
| 123 | + var ex = Throws<NotSupportedException>(() => local.ExecuteScalar("select * from OPENQUERY(RMT, '')")); |
| 124 | + Contains("no result set", ex.Message); |
| 125 | + } |
| 126 | + |
| 127 | + [TestMethod] |
| 128 | + public void NonSelectQuery_NoResultSet_NotSupported() |
| 129 | + { |
| 130 | + var local = LocalWithRemote(out _, "create table dbo.t (id int)"); |
| 131 | + var ex = Throws<NotSupportedException>(() => local.ExecuteScalar("select * from OPENQUERY(RMT, 'declare @x int = 5')")); |
| 132 | + Contains("no result set", ex.Message); |
| 133 | + } |
| 134 | + |
| 135 | + [TestMethod] |
| 136 | + public void UnknownServer_Msg7202() |
| 137 | + { |
| 138 | + var local = new Simulation(); |
| 139 | + var ex = local.AssertSqlError("select * from OPENQUERY(NOPE, 'select 1 as x')", 7202); |
| 140 | + Contains("Could not find server 'NOPE' in sys.servers", ex.Message); |
| 141 | + } |
| 142 | + |
| 143 | + [TestMethod] |
| 144 | + public void VariableQueryArg_Msg102() |
| 145 | + { |
| 146 | + var local = LocalWithRemote(out _, "create table dbo.t (id int)"); |
| 147 | + _ = local.AssertSqlError("declare @q varchar(100) = 'select id from dbo.t'; select * from OPENQUERY(RMT, @q)", 102); |
| 148 | + } |
| 149 | + |
| 150 | + [TestMethod] |
| 151 | + public void StringLiteralServerArg_Msg102() |
| 152 | + { |
| 153 | + var local = LocalWithRemote(out _, "create table dbo.t (id int)"); |
| 154 | + _ = local.AssertSqlError("select * from OPENQUERY('RMT', 'select id from dbo.t')", 102); |
| 155 | + } |
| 156 | + |
| 157 | + [TestMethod] |
| 158 | + public void TooFewArgs_Msg102() |
| 159 | + { |
| 160 | + var local = LocalWithRemote(out _, "create table dbo.t (id int)"); |
| 161 | + _ = local.AssertSqlError("select * from OPENQUERY(RMT)", 102); |
| 162 | + } |
| 163 | + |
| 164 | + [TestMethod] |
| 165 | + public void TooManyArgs_Msg102() |
| 166 | + { |
| 167 | + var local = LocalWithRemote(out _, "create table dbo.t (id int)"); |
| 168 | + _ = local.AssertSqlError("select * from OPENQUERY(RMT, 'select id from dbo.t', 'extra')", 102); |
| 169 | + } |
| 170 | + |
| 171 | + [TestMethod] |
| 172 | + public void ConcatenatedQueryArg_Msg102() |
| 173 | + { |
| 174 | + var local = LocalWithRemote(out _, "create table dbo.t (id int)"); |
| 175 | + _ = local.AssertSqlError("select * from OPENQUERY(RMT, 'select ' + 'id from dbo.t')", 102); |
| 176 | + } |
| 177 | + |
| 178 | + /// <summary> |
| 179 | + /// A column-alias list on OPENQUERY is rejected (real SQL Server: Msg 102 |
| 180 | + /// near the first alias identifier; the simulator raises Msg 102 near the |
| 181 | + /// opening <c>(</c>). Without the guard the general FROM parser tolerates |
| 182 | + /// and ignores the list, silently keeping the remote column names. |
| 183 | + /// </summary> |
| 184 | + [TestMethod] |
| 185 | + public void ColumnAliasList_Msg102() |
| 186 | + { |
| 187 | + var local = LocalWithRemote(out _, "create table dbo.t (id int not null primary key); insert t values (1)"); |
| 188 | + _ = local.AssertSqlError("select c1 from OPENQUERY(RMT, 'select id from dbo.t') q(c1)", 102); |
| 189 | + } |
| 190 | +} |
0 commit comments