|
| 1 | +using System.Data; |
| 2 | +using System.Data.SqlTypes; |
| 3 | +using Microsoft.Data.SqlClient; |
| 4 | +using Microsoft.SqlServer.Types; |
| 5 | +using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert; |
| 6 | + |
| 7 | +namespace SqlServerSimulator; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// CLR-UDT RPC parameters (TDS type token 0xF0): <c>geography</c> / |
| 11 | +/// <c>geometry</c> / <c>hierarchyid</c> arrive as a three-B_VARCHAR UDT_INFO |
| 12 | +/// (db / schema / type, only the type filled) plus a PLP value — OrdPath bytes |
| 13 | +/// for hierarchyid (stored verbatim) or the MS spatial binary for the spatial |
| 14 | +/// types (decoded back to WKT via <c>SpatialWkbDecoder</c>). SqlClient serializes |
| 15 | +/// a typed <c>SqlGeography</c> / <c>SqlHierarchyId</c> value or a raw |
| 16 | +/// <c>byte[]</c> to the same wire form; both are exercised. Semantics and error |
| 17 | +/// numbers probe-confirmed against SQL Server 2025 + SqlClient 7.0.2 |
| 18 | +/// (2026-07-19). <c>SqlGeometry.STGeomFromText</c> is not implemented on Linux |
| 19 | +/// (managed <c>Microsoft.SqlServer.Types</c>), so geometry is constructed from |
| 20 | +/// raw WKB bytes — the shape DacFx / bulk-import consumers send. |
| 21 | +/// </summary> |
| 22 | +[TestClass] |
| 23 | +public sealed class UdtRpcParameterTests |
| 24 | +{ |
| 25 | + public TestContext TestContext { get; set; } = null!; |
| 26 | + |
| 27 | + // geometry POINT(3 4): SRID 0 + version 1 + props 0x0C (isValid|isSinglePoint) |
| 28 | + // + x=3.0 + y=4.0, the 22-byte single-point MS spatial binary. |
| 29 | + private const string GeometryPointWkbHex = "00000000010C00000000000008400000000000001040"; |
| 30 | + |
| 31 | + // hierarchyid '/1/2/' canonical OrdPath bytes. |
| 32 | + private static readonly byte[] Node1_2 = [0x5B, 0x40]; |
| 33 | + |
| 34 | + [TestMethod] |
| 35 | + public async Task GeographyParameter_TypedValue_RoundTripsToWkt() |
| 36 | + { |
| 37 | + var simulation = new Simulation(); |
| 38 | + await using var listener = await simulation.ListenAsync(0, TestContext.CancellationToken); |
| 39 | + await using var connection = await Wire.OpenAsync(listener, TestContext.CancellationToken); |
| 40 | + await using var command = new SqlCommand("select @p.ToString()", connection); |
| 41 | + _ = command.Parameters.Add(new SqlParameter("@p", SqlDbType.Udt) |
| 42 | + { |
| 43 | + UdtTypeName = "geography", |
| 44 | + Value = SqlGeography.STGeomFromText(new SqlChars("POINT(-122.3 47.6)"), 4326), |
| 45 | + }); |
| 46 | + |
| 47 | + AreEqual("POINT (-122.3 47.6)", await command.ExecuteScalarAsync(TestContext.CancellationToken)); |
| 48 | + } |
| 49 | + |
| 50 | + [TestMethod] |
| 51 | + public async Task GeographyParameter_Null_ReadsAsNull() |
| 52 | + { |
| 53 | + var simulation = new Simulation(); |
| 54 | + await using var listener = await simulation.ListenAsync(0, TestContext.CancellationToken); |
| 55 | + await using var connection = await Wire.OpenAsync(listener, TestContext.CancellationToken); |
| 56 | + await using var command = new SqlCommand("select @p.ToString()", connection); |
| 57 | + _ = command.Parameters.Add(new SqlParameter("@p", SqlDbType.Udt) { UdtTypeName = "geography", Value = DBNull.Value }); |
| 58 | + |
| 59 | + _ = IsInstanceOfType<DBNull>(await command.ExecuteScalarAsync(TestContext.CancellationToken)); |
| 60 | + } |
| 61 | + |
| 62 | + [TestMethod] |
| 63 | + public async Task GeometryParameter_RawWkbBytes_RoundTripsToWkt() |
| 64 | + { |
| 65 | + var simulation = new Simulation(); |
| 66 | + await using var listener = await simulation.ListenAsync(0, TestContext.CancellationToken); |
| 67 | + await using var connection = await Wire.OpenAsync(listener, TestContext.CancellationToken); |
| 68 | + await using var command = new SqlCommand("select @p.ToString()", connection); |
| 69 | + _ = command.Parameters.Add(new SqlParameter("@p", SqlDbType.Udt) |
| 70 | + { |
| 71 | + UdtTypeName = "geometry", |
| 72 | + Value = Convert.FromHexString(GeometryPointWkbHex), |
| 73 | + }); |
| 74 | + |
| 75 | + AreEqual("POINT (3 4)", await command.ExecuteScalarAsync(TestContext.CancellationToken)); |
| 76 | + } |
| 77 | + |
| 78 | + [TestMethod] |
| 79 | + public async Task HierarchyIdParameter_TypedValue_RoundTripsToPathAndDataLength() |
| 80 | + { |
| 81 | + var simulation = new Simulation(); |
| 82 | + await using var listener = await simulation.ListenAsync(0, TestContext.CancellationToken); |
| 83 | + await using var connection = await Wire.OpenAsync(listener, TestContext.CancellationToken); |
| 84 | + await using var command = new SqlCommand("select @p.ToString(), datalength(@p)", connection); |
| 85 | + _ = command.Parameters.Add(new SqlParameter("@p", SqlDbType.Udt) |
| 86 | + { |
| 87 | + UdtTypeName = "hierarchyid", |
| 88 | + Value = SqlHierarchyId.Parse(new SqlString("/1/2/")), |
| 89 | + }); |
| 90 | + |
| 91 | + await using var reader = await command.ExecuteReaderAsync(TestContext.CancellationToken); |
| 92 | + IsTrue(await reader.ReadAsync(TestContext.CancellationToken)); |
| 93 | + AreEqual("/1/2/", reader.GetString(0)); |
| 94 | + AreEqual(2, reader.GetInt32(1)); |
| 95 | + } |
| 96 | + |
| 97 | + [TestMethod] |
| 98 | + public async Task HierarchyIdParameter_RawOrdPathBytes_RoundTripsToPath() |
| 99 | + { |
| 100 | + var simulation = new Simulation(); |
| 101 | + await using var listener = await simulation.ListenAsync(0, TestContext.CancellationToken); |
| 102 | + await using var connection = await Wire.OpenAsync(listener, TestContext.CancellationToken); |
| 103 | + await using var command = new SqlCommand("select @p.ToString()", connection); |
| 104 | + _ = command.Parameters.Add(new SqlParameter("@p", SqlDbType.Udt) { UdtTypeName = "hierarchyid", Value = Node1_2 }); |
| 105 | + |
| 106 | + AreEqual("/1/2/", await command.ExecuteScalarAsync(TestContext.CancellationToken)); |
| 107 | + } |
| 108 | + |
| 109 | + [TestMethod] |
| 110 | + public async Task HierarchyIdParameter_Null_ReadsAsNull() |
| 111 | + { |
| 112 | + var simulation = new Simulation(); |
| 113 | + await using var listener = await simulation.ListenAsync(0, TestContext.CancellationToken); |
| 114 | + await using var connection = await Wire.OpenAsync(listener, TestContext.CancellationToken); |
| 115 | + await using var command = new SqlCommand("select @p.ToString()", connection); |
| 116 | + _ = command.Parameters.Add(new SqlParameter("@p", SqlDbType.Udt) { UdtTypeName = "hierarchyid", Value = DBNull.Value }); |
| 117 | + |
| 118 | + _ = IsInstanceOfType<DBNull>(await command.ExecuteScalarAsync(TestContext.CancellationToken)); |
| 119 | + } |
| 120 | + |
| 121 | + [TestMethod] |
| 122 | + public async Task UnknownUdtTypeName_RaisesMsg8064() |
| 123 | + { |
| 124 | + var simulation = new Simulation(); |
| 125 | + await using var listener = await simulation.ListenAsync(0, TestContext.CancellationToken); |
| 126 | + await using var connection = await Wire.OpenAsync(listener, TestContext.CancellationToken); |
| 127 | + await using var command = new SqlCommand("select @p", connection); |
| 128 | + _ = command.Parameters.Add(new SqlParameter("@p", SqlDbType.Udt) { UdtTypeName = "nosuchtype", Value = new byte[] { 1, 2 } }); |
| 129 | + |
| 130 | + var ex = await ThrowsExactlyAsync<SqlException>(async () => await command.ExecuteScalarAsync(TestContext.CancellationToken)); |
| 131 | + AreEqual(8064, ex.Number); |
| 132 | + // db segment resolves to the current database; schema stays empty. |
| 133 | + Contains("[simulated].[].[nosuchtype]", ex.Message); |
| 134 | + Contains("The CLR type does not exist", ex.Message); |
| 135 | + } |
| 136 | + |
| 137 | + [TestMethod] |
| 138 | + public async Task InvalidGeographyBytes_RaisesMsg8023() |
| 139 | + { |
| 140 | + var simulation = new Simulation(); |
| 141 | + await using var listener = await simulation.ListenAsync(0, TestContext.CancellationToken); |
| 142 | + await using var connection = await Wire.OpenAsync(listener, TestContext.CancellationToken); |
| 143 | + await using var command = new SqlCommand("select @p.ToString()", connection); |
| 144 | + _ = command.Parameters.Add(new SqlParameter("@p", SqlDbType.Udt) { UdtTypeName = "geography", Value = new byte[] { 0, 1, 2, 3 } }); |
| 145 | + |
| 146 | + var ex = await ThrowsExactlyAsync<SqlException>(async () => await command.ExecuteScalarAsync(TestContext.CancellationToken)); |
| 147 | + AreEqual(8023, ex.Number); |
| 148 | + Contains("not a valid instance of data type geography", ex.Message); |
| 149 | + } |
| 150 | + |
| 151 | + [TestMethod] |
| 152 | + public async Task HierarchyIdParameter_DirectProcedureCall_Binds() |
| 153 | + { |
| 154 | + var simulation = new Simulation(); |
| 155 | + Wire.ExecInProc(simulation, "create procedure dbo.EchoNode @h hierarchyid as select @h.ToString()"); |
| 156 | + |
| 157 | + await using var listener = await simulation.ListenAsync(0, TestContext.CancellationToken); |
| 158 | + await using var connection = await Wire.OpenAsync(listener, TestContext.CancellationToken); |
| 159 | + await using var command = new SqlCommand("dbo.EchoNode", connection) { CommandType = CommandType.StoredProcedure }; |
| 160 | + _ = command.Parameters.Add(new SqlParameter("@h", SqlDbType.Udt) |
| 161 | + { |
| 162 | + UdtTypeName = "hierarchyid", |
| 163 | + Value = SqlHierarchyId.Parse(new SqlString("/3/4/")), |
| 164 | + }); |
| 165 | + |
| 166 | + AreEqual("/3/4/", await command.ExecuteScalarAsync(TestContext.CancellationToken)); |
| 167 | + } |
| 168 | +} |
0 commit comments