Files
sqltoolsservice/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/QueryExecution/DataStorage/StorageDataReaderTests.cs
Connor Quagliana c055c459a0 Update connection logic to handle multiple connections per URI (#176)
* Add CancelTokenKey for uniquely identifying cancelations of Connections associated with an OwnerUri and ConnectionType string.

* Update ConnectionInfo to use ConcurrentDictionary of DbConnection instances. Add wrapper functions for the ConcurrentDictionary. 

* Refactor Connect and Disconnect in ConnectionService.

* Update ConnectionService: Handle multiple connections per ConnectionInfo. Handle cancelation tokens uniquely identified with CancelTokenKey. Add GetOrOpenConnection() for other services to request an existing or create a new DbConnection.

* Add ConnectionType.cs for ConnectionType strings.

* Add ConnectionType string to ConnectParams, ConnectionCompleteNotification, DisconnectParams.

* Update Query ExecuteInternal to use the dedicated query connection and GetOrOpenConnection().

* Update test library to account for multiple connections in ConnectionInfo.

* Write tests ensuring multiple connections don’t create redundant data.

* Write tests ensuring database changes affect all connections of a given ConnectionInfo.

* Write tests for TRANSACTION statements and temp tables.
2017-01-18 17:59:41 -08:00

105 lines
3.7 KiB
C#

//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
using System.Data.Common;
using System.Threading.Tasks;
using Microsoft.SqlTools.ServiceLayer.Connection;
using Microsoft.SqlTools.ServiceLayer.QueryExecution.DataStorage;
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
using Microsoft.SqlTools.Test.Utility;
using Xunit;
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.QueryExecution.DataStorage
{
public class StorageDataReaderTests
{
private async Task<StorageDataReader> GetTestStorageDataReader(string query)
{
var result = await TestObjects.InitLiveConnectionInfo();
DbConnection connection;
result.ConnectionInfo.TryGetConnection(ConnectionType.Default, out connection);
var command = connection.CreateCommand();
command.CommandText = query;
DbDataReader reader = command.ExecuteReader();
return new StorageDataReader(reader);
}
/// <summary>
/// Validate GetBytesWithMaxCapacity
/// </summary>
[Fact]
public async Task GetBytesWithMaxCapacityTest()
{
var storageReader = await GetTestStorageDataReader(
"SELECT CAST([name] as TEXT) As TextName FROM sys.all_columns");
DbDataReader reader = storageReader.DbDataReader;
reader.Read();
Assert.False(storageReader.IsDBNull(0));
byte[] bytes = storageReader.GetBytesWithMaxCapacity(0, 100);
Assert.NotNull(bytes);
}
/// <summary>
/// Validate GetCharsWithMaxCapacity
/// </summary>
[Fact]
public async Task GetCharsWithMaxCapacityTest()
{
var storageReader = await GetTestStorageDataReader(
"SELECT name FROM sys.all_columns");
DbDataReader reader = storageReader.DbDataReader;
reader.Read();
Assert.False(storageReader.IsDBNull(0));
Assert.NotNull(storageReader.GetValue(0));
string shortName = storageReader.GetCharsWithMaxCapacity(0, 2);
Assert.True(shortName.Length == 2);
Assert.Throws<ArgumentOutOfRangeException>(() => storageReader.GetBytesWithMaxCapacity(0, 0));
Assert.Throws<ArgumentOutOfRangeException>(() => storageReader.GetCharsWithMaxCapacity(0, 0));
}
/// <summary>
/// Validate GetXmlWithMaxCapacity
/// </summary>
[Fact]
public async Task GetXmlWithMaxCapacityTest()
{
var storageReader = await GetTestStorageDataReader(
"SELECT CAST('<xml>Test XML context</xml>' AS XML) As XmlColumn");
DbDataReader reader = storageReader.DbDataReader;
reader.Read();
Assert.False(storageReader.IsDBNull(0));
string shortXml = storageReader.GetXmlWithMaxCapacity(0, 2);
Assert.True(shortXml.Length == 3);
}
/// <summary>
/// Validate StringWriterWithMaxCapacity Write test
/// </summary>
[Fact]
public void StringWriterWithMaxCapacityTest()
{
var writer = new StorageDataReader.StringWriterWithMaxCapacity(null, 4);
string output = "...";
writer.Write(output);
Assert.True(writer.ToString().Equals(output));
writer.Write('.');
Assert.True(writer.ToString().Equals(output + '.'));
writer.Write(output);
writer.Write('.');
Assert.True(writer.ToString().Equals(output + '.'));
}
}
}