Skip to content

Commit 2655083

Browse files
authored
Tests | Expand UDT serialization code coverage (#3423)
* Expand code coverage for UDT serialization * Address test failures on 32-bit OSes * Make Microsoft.SqlServer.Server a PackageReference * Reformat SerializedTypes.cs * Switch to implicit object creation Also enforce this via editorconfig * Add and correct comments Also mandating this by generating a documentation XML file * Enable nullable for project * Declare and assign variables together * Moved MemoryStream to an instance variable * Post-merge build corrections * Reapply IDE0090 recommendations, disable CS1591 on newly-added tests * Next code review (first response) * Switch to using TheoryData * Nit: => movement * Rename tests * Split delegate for Assert.Throws into separate arrange/act/assert segment of each method * Disable CS1591 on new tests * Disable mandatory XML documentation * Change formatting of test data * Remove trailing XML documentation override
1 parent e472245 commit 2655083

File tree

8 files changed

+1031
-7
lines changed

8 files changed

+1031
-7
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# editorconfig.org
2+
3+
# top-most EditorConfig file
4+
root = false
5+
6+
[*.cs]
7+
8+
csharp_style_var_when_type_is_apparent = false:refactor
9+
# IDE0090: Use 'new(...)'
10+
csharp_style_implicit_object_creation_when_type_is_apparent = true:warning
11+

src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft.Data.SqlClient.UnitTests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<IntermediateOutputPath>$(ObjFolder)$(Configuration).$(Platform).$(AssemblyName)</IntermediateOutputPath>
88
<OutputPath>$(BinFolder)$(Configuration).$(Platform).$(AssemblyName)</OutputPath>
99
<IsTestProject>true</IsTestProject>
10+
<Nullable>enable</Nullable>
1011
</PropertyGroup>
1112
<!-- Common references -->
1213
<ItemGroup>
@@ -25,6 +26,7 @@
2526
<PrivateAssets>all</PrivateAssets>
2627
</PackageReference>
2728
<PackageReference Include="Microsoft.DotNet.XUnitExtensions" />
29+
<PackageReference Include="Microsoft.SqlServer.Server" />
2830
</ItemGroup>
2931
<!-- .NET Framework references -->
3032
<ItemGroup Condition="$(TargetGroup) == 'netfx'">
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.IO;
7+
using Microsoft.Data.SqlClient.Server;
8+
using Microsoft.Data.SqlClient.UnitTests.UdtSerialization.SerializedTypes;
9+
using Microsoft.SqlServer.Server;
10+
using Xunit;
11+
12+
namespace Microsoft.Data.SqlClient.UnitTests.UdtSerialization;
13+
14+
/// <summary>
15+
/// Attempts to serialize types which do not meet the requirements for either user-defined or native serialization.
16+
/// </summary>
17+
public sealed class InvalidSerializationTest : IDisposable
18+
{
19+
private readonly MemoryStream _stream;
20+
21+
/// <summary>
22+
/// Initializes the MemoryStream used for all tests in this class.
23+
/// </summary>
24+
public InvalidSerializationTest()
25+
{
26+
_stream = new MemoryStream();
27+
}
28+
29+
void IDisposable.Dispose()
30+
{
31+
_stream.Dispose();
32+
}
33+
34+
/// <summary>
35+
/// Attempts to serialize a class that does not have the SqlUserDefinedType attribute. Verifies that this fails.
36+
/// </summary>
37+
[Fact]
38+
public void Serialize_MissingSqlUserDefinedTypeAttribute_Throws()
39+
{
40+
Action serialize = () => SerializationHelperSql9.Serialize(_stream, new ClassMissingSqlUserDefinedTypeAttribute());
41+
var exception = Assert.Throws<InvalidUdtException>(serialize);
42+
43+
Assert.Equal($"'{typeof(ClassMissingSqlUserDefinedTypeAttribute).FullName}' is an invalid user defined type, reason: no UDT attribute.", exception.Message);
44+
}
45+
46+
/// <summary>
47+
/// Attempts to serialize a class that has a SqlUserDefinedType attribute, but specifies a Format enumeration value of
48+
/// Unknown. Verifies that this fails.
49+
/// </summary>
50+
[Fact]
51+
public void Serialize_UnknownFormattedType_Throws()
52+
{
53+
Action serialize = () => SerializationHelperSql9.Serialize(_stream, new UnknownFormattedClass());
54+
var exception = Assert.Throws<ArgumentOutOfRangeException>("Format", serialize);
55+
56+
#if NET
57+
Assert.Equal("The Format enumeration value, 0, is not supported by the format method. (Parameter 'Format')", exception.Message);
58+
#else
59+
Assert.Equal("The Format enumeration value, Unknown, is not supported by the format method.\r\nParameter name: Format", exception.Message);
60+
#endif
61+
}
62+
}

0 commit comments

Comments
 (0)