|
3 | 3 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
4 | 4 | // |
5 | 5 |
|
6 | | -using System; |
7 | | -using System.Collections.Generic; |
8 | | -using System.Data.SqlClient; |
9 | | -using System.Threading.Tasks; |
10 | 6 | using Microsoft.SqlTools.Hosting.Protocol; |
11 | 7 | using Microsoft.SqlTools.ServiceLayer.Connection; |
12 | 8 | using Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility; |
13 | 9 | using Microsoft.SqlTools.ServiceLayer.Metadata; |
14 | 10 | using Microsoft.SqlTools.ServiceLayer.Metadata.Contracts; |
| 11 | +using Microsoft.SqlTools.ServiceLayer.Test.Common; |
15 | 12 | using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts; |
16 | 13 | using Moq; |
| 14 | +using System; |
| 15 | +using System.Collections.Generic; |
| 16 | +using System.Data.SqlClient; |
| 17 | +using System.Linq; |
| 18 | +using System.Threading; |
| 19 | +using System.Threading.Tasks; |
17 | 20 | using Xunit; |
| 21 | +using static Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility.LiveConnectionHelper; |
18 | 22 |
|
19 | 23 | namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Metadata |
20 | 24 | { |
@@ -142,5 +146,124 @@ public async void GetViewInfoReturnsValidResults() |
142 | 146 | requestContext.VerifyAll(); |
143 | 147 | } |
144 | 148 |
|
| 149 | + [Fact] |
| 150 | + public async void VerifyMetadataList() |
| 151 | + { |
| 152 | + string query = @"CREATE TABLE testTable1 (c1 int) |
| 153 | + GO |
| 154 | + CREATE PROCEDURE testSp1 @StartProductID [int] AS BEGIN Select * from sys.all_columns END |
| 155 | + GO |
| 156 | + CREATE VIEW testView1 AS SELECT * from sys.all_columns |
| 157 | + GO |
| 158 | + CREATE FUNCTION testFun1() RETURNS [int] AS BEGIN RETURN 1 END |
| 159 | + GO |
| 160 | + CREATE FUNCTION [testFun2](@CityID int) |
| 161 | + RETURNS TABLE |
| 162 | + WITH SCHEMABINDING |
| 163 | + AS |
| 164 | + RETURN SELECT 1 AS AccessResult |
| 165 | + GO"; |
| 166 | + |
| 167 | + List<ObjectMetadata> expectedMetadataList = new List<ObjectMetadata> |
| 168 | + { |
| 169 | + new ObjectMetadata |
| 170 | + { |
| 171 | + MetadataType = MetadataType.Table, |
| 172 | + MetadataTypeName = "Table", |
| 173 | + Name = "testTable1", |
| 174 | + Schema = "dbo" |
| 175 | + }, |
| 176 | + new ObjectMetadata |
| 177 | + { |
| 178 | + MetadataType = MetadataType.SProc, |
| 179 | + MetadataTypeName = "StoredProcedure", |
| 180 | + Name = "testSp1", |
| 181 | + Schema = "dbo" |
| 182 | + }, |
| 183 | + new ObjectMetadata |
| 184 | + { |
| 185 | + MetadataType = MetadataType.View, |
| 186 | + MetadataTypeName = "View", |
| 187 | + Name = "testView1", |
| 188 | + Schema = "dbo" |
| 189 | + }, |
| 190 | + new ObjectMetadata |
| 191 | + { |
| 192 | + MetadataType = MetadataType.Function, |
| 193 | + MetadataTypeName = "UserDefinedFunction", |
| 194 | + Name = "testFun1", |
| 195 | + Schema = "dbo" |
| 196 | + }, |
| 197 | + new ObjectMetadata |
| 198 | + { |
| 199 | + MetadataType = MetadataType.Function, |
| 200 | + MetadataTypeName = "UserDefinedFunction", |
| 201 | + Name = "testFun2", |
| 202 | + Schema = "dbo" |
| 203 | + } |
| 204 | + }; |
| 205 | + |
| 206 | + await VerifyMetadataList(query, expectedMetadataList); |
| 207 | + } |
| 208 | + |
| 209 | + private async Task VerifyMetadataList(string query, List<ObjectMetadata> expectedMetadataList) |
| 210 | + { |
| 211 | + var testDb = await SqlTestDb.CreateNewAsync(TestServerType.OnPrem, false, null, query, "MetadataTests"); |
| 212 | + try |
| 213 | + { |
| 214 | + var requestContext = new Mock<RequestContext<MetadataQueryResult>>(); |
| 215 | + requestContext.Setup(x => x.SendResult(It.IsAny<MetadataQueryResult>())).Returns(Task.FromResult(new object())); |
| 216 | + ConnectionService connectionService = LiveConnectionHelper.GetLiveTestConnectionService(); |
| 217 | + using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile()) |
| 218 | + { |
| 219 | + //Opening a connection to db to lock the db |
| 220 | + TestConnectionResult connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync(testDb.DatabaseName, queryTempFile.FilePath, ConnectionType.Default); |
| 221 | + |
| 222 | + MetadataService service = new MetadataService(); |
| 223 | + await service.HandleMetadataListRequest(new MetadataQueryParams |
| 224 | + { |
| 225 | + OwnerUri = queryTempFile.FilePath |
| 226 | + }, requestContext.Object); |
| 227 | + Thread.Sleep(2000); |
| 228 | + await service.MetadataListTask; |
| 229 | + |
| 230 | + requestContext.Verify(x => x.SendResult(It.Is<MetadataQueryResult>(r => VerifyResult(r, expectedMetadataList)))); |
| 231 | + connectionService.Disconnect(new ServiceLayer.Connection.Contracts.DisconnectParams |
| 232 | + { |
| 233 | + OwnerUri = queryTempFile.FilePath |
| 234 | + }); |
| 235 | + } |
| 236 | + } |
| 237 | + catch |
| 238 | + { |
| 239 | + throw; |
| 240 | + } |
| 241 | + finally |
| 242 | + { |
| 243 | + await testDb.CleanupAsync(); |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + private static bool VerifyResult(MetadataQueryResult result, List<ObjectMetadata> expectedMetadataList) |
| 248 | + { |
| 249 | + if (expectedMetadataList == null) |
| 250 | + { |
| 251 | + return result.Metadata == null; |
| 252 | + } |
| 253 | + |
| 254 | + if(expectedMetadataList.Count() != result.Metadata.Count()) |
| 255 | + { |
| 256 | + return false; |
| 257 | + } |
| 258 | + foreach (ObjectMetadata expected in expectedMetadataList) |
| 259 | + { |
| 260 | + if (!result.Metadata.Any(x => x.MetadataType == expected.MetadataType && x.MetadataTypeName == expected.MetadataTypeName && x.Name == expected.Name && x.Schema == expected.Schema)) |
| 261 | + { |
| 262 | + return false; |
| 263 | + } |
| 264 | + } |
| 265 | + return true; |
| 266 | + } |
| 267 | + |
145 | 268 | } |
146 | 269 | } |
0 commit comments