Skip to content

Tests | Expand UDT serialization code coverage #3423

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions src/Microsoft.Data.SqlClient/tests/UnitTests/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# editorconfig.org

# top-most EditorConfig file
root = false

[*.cs]

csharp_style_var_when_type_is_apparent = false:refactor
# IDE0090: Use 'new(...)'
csharp_style_implicit_object_creation_when_type_is_apparent = true:warning

Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@

namespace Microsoft.Data.SqlClient.UnitTests
{
/// <summary>
/// Tests proving that the InternalsVisibleTo attribute works correctly.
/// </summary>
public class InternalsVisibleToTest
{
/// <summary>
/// Creates an instance of an internal class. Verifies that this compiles.
/// </summary>
[Fact]
public void TestInternalsVisible()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<IntermediateOutputPath>$(ObjFolder)$(Configuration).$(Platform).$(AssemblyName)</IntermediateOutputPath>
<OutputPath>$(BinFolder)$(Configuration).$(Platform).$(AssemblyName)</OutputPath>
<IsTestProject>true</IsTestProject>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Nullable>enable</Nullable>
</PropertyGroup>
<!-- Common references -->
<ItemGroup>
Expand All @@ -25,6 +27,7 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.DotNet.XUnitExtensions" />
<PackageReference Include="Microsoft.SqlServer.Server" />
</ItemGroup>
<!-- .NET Framework references -->
<ItemGroup Condition="$(TargetGroup) == 'netfx'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Microsoft.Data.SqlClient.UnitTests
{
#pragma warning disable CS1591 // Test classes do not require XML documentation comments
public class ChannelDbConnectionPoolTest
{
private readonly ChannelDbConnectionPool _pool;
Expand Down Expand Up @@ -152,4 +153,5 @@ public void TestTryGetConnection()
Assert.Throws<NotImplementedException>(() => _pool.TryGetConnection(null!, null!, null!, out _));
}
}
#pragma warning restore CS1591
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.IO;
using Microsoft.Data.SqlClient.Server;
using Microsoft.Data.SqlClient.UnitTests.UdtSerialization.SerializedTypes;
using Microsoft.SqlServer.Server;
using Xunit;

namespace Microsoft.Data.SqlClient.UnitTests.UdtSerialization;

/// <summary>
/// Attempts to serialize types which do not meet the requirements for either user-defined or native serialization.
/// </summary>
public sealed class InvalidSerializationTest : IDisposable
{
private readonly MemoryStream _stream;

/// <summary>
/// Initializes the MemoryStream used for all tests in this class.
/// </summary>
public InvalidSerializationTest()
{
_stream = new MemoryStream();
}

void IDisposable.Dispose()
{
_stream.Dispose();
}

/// <summary>
/// Attempts to serialize a class that does not have the SqlUserDefinedType attribute. Verifies that this fails.
/// </summary>
[Fact]
public void RequiresSqlUserDefinedTypeAttribute()
{
var exception = Assert.Throws<InvalidUdtException>(
() => SerializationHelperSql9.Serialize(_stream, new ClassMissingSqlUserDefinedTypeAttribute()));

Assert.Equal($"'{typeof(ClassMissingSqlUserDefinedTypeAttribute).FullName}' is an invalid user defined type, reason: no UDT attribute.", exception.Message);
}

/// <summary>
/// Attempts to serialize a class that has a SqlUserDefinedType attribute, but specifies a Format enumeration value of
/// Unknown. Verifies that this fails.
/// </summary>
[Fact]
public void CannotSerializeUnknownFormattedType()
{
var exception = Assert.Throws<ArgumentOutOfRangeException>("Format",
() => SerializationHelperSql9.Serialize(_stream, new UnknownFormattedClass()));

#if NET
Assert.Equal("The Format enumeration value, 0, is not supported by the format method. (Parameter 'Format')", exception.Message);
#else
Assert.Equal("The Format enumeration value, Unknown, is not supported by the format method.\r\nParameter name: Format", exception.Message);
#endif
}
}
Loading
Loading