Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for the stream exceptions #3

Merged
merged 2 commits into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Data.SqlTypes;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Xml;
using Microsoft.Data.SqlTypes;
Expand Down Expand Up @@ -145,7 +144,5 @@ public bool IsNull
return (_cachedBytes == null) ? true : false;
}
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4393,7 +4393,7 @@ out dataLength
}
else
{
//Debug.Assert(false, "we have read past the column somehow, this is an error");
Debug.Assert(false, "we have read past the column somehow, this is an error");
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
</Compile>
<Compile Include="DataCommon\AADUtility.cs" />
<Compile Include="SQL\ConfigurableIpPreferenceTest\ConfigurableIpPreferenceTest.cs" />
<Compile Include="SQL\DataReaderTest\DataReaderStreamsTest.cs" />
<Compile Include="SQL\DataReaderTest\DataReaderStreamsTest.cs" />
<Compile Include="SQL\RetryLogic\RetryLogicCounterTest.cs" />
<Compile Include="SQL\RetryLogic\RetryLogicConfigHelper.cs" />
<Compile Include="SQL\RetryLogic\RetryLogicTestHelper.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,40 @@ public static async Task GetFieldValueAsync_OfTextReader(CommandBehavior behavio
Assert.Equal(originalText, outputText);
}

[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
[MemberData(nameof(GetCommandBehavioursAndIsAsync))]
public static async Task GetFieldValueAsync_Char_OfTextReader(CommandBehavior behavior, bool isExecuteAsync)
{
const int PacketSize = 512; // force minimun packet size so that the test data spans multiple packets to test sequential access spanning
string connectionString = SetConnectionStringPacketSize(DataTestUtility.TCPConnectionString, PacketSize);
string originalText = new ('c', PacketSize * 4);
string query = CreateCharDataQuery(originalText);

string streamTypeName = null;
string outputText = null;
using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = new SqlCommand(query, connection))
{
connection.Open();
using (SqlDataReader reader = await ExecuteReader(command, behavior, isExecuteAsync))
{
if (await Read(reader, isExecuteAsync))
{
using (TextReader textReader = await reader.GetFieldValueAsync<TextReader>(1))
{
streamTypeName = textReader.GetType().Name;
outputText = await textReader.ReadToEndAsync();
}
}
}
}

Assert.True(behavior != CommandBehavior.SequentialAccess || streamTypeName.Contains("Sequential"));
Assert.NotNull(outputText);
Assert.Equal(originalText.Length, outputText.Length);
Assert.Equal(originalText, outputText);
}

[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
[MemberData(nameof(GetCommandBehavioursAndIsAsync))]
public static async void GetFieldValue_OfXmlReader(CommandBehavior behavior, bool isExecuteAsync)
Expand Down Expand Up @@ -407,7 +441,33 @@ public static void NullStreamProperties(CommandBehavior behavior, AccessorType a
}
}
}
}

[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
[MemberData(nameof(GetCommandBehaviourAndAccessorTypes))]
public static void InvalidCastExceptionStream(CommandBehavior behavior, AccessorType accessorType)
{
string query = "SELECT convert(xml,NULL) AS XmlData, convert(nvarchar(max),NULL) as TextData";

using (SqlConnection connection = new SqlConnection(DataTestUtility.TCPConnectionString))
using (SqlCommand command = new SqlCommand(query, connection))
{
connection.Open();

using (SqlDataReader reader = command.ExecuteReader(behavior))
{
Assert.True(reader.Read(), "It's excpected to read a row.");

InvalidCastException ex = Assert.Throws<InvalidCastException>(() => GetValue<TextReader>(reader, 0, accessorType));
Assert.Contains("The GetTextReader function can only be used on columns of type Char, NChar, NText, NVarChar, Text or VarChar.", ex.Message);

ex = Assert.Throws<InvalidCastException>(() => GetValue<Stream>(reader, 0, accessorType));
Assert.Contains("The GetStream function can only be used on columns of type Binary, Image, Udt or VarBinary.", ex.Message);

ex = Assert.Throws<InvalidCastException>(() => GetValue<XmlReader>(reader, 1, accessorType));
Assert.Contains("The GetXmlReader function can only be used on columns of type Xml.", ex.Message);
}
}
}

private static async Task<SqlDataReader> ExecuteReader(SqlCommand command, CommandBehavior behavior, bool isExecuteAsync)
Expand Down Expand Up @@ -573,6 +633,15 @@ private static string CreateTextDataQuery(string originalText)
return queryBuilder.ToString();
}

private static string CreateCharDataQuery(string originalText)
{
StringBuilder queryBuilder = new StringBuilder(originalText.Length + 128);
queryBuilder.Append($"SELECT 1 as DummyField, convert(char({originalText.Length}),'");
queryBuilder.Append(originalText);
queryBuilder.Append("') AS Data");
return queryBuilder.ToString();
}

private static string GetXmlDocumentContents(XmlReader xmlReader)
{
string outputXml;
Expand Down