using System.Reflection;
using Microsoft.Data.SqlClient;
namespace ZB.MOM.WW.ScadaBridge.AuditLog.Tests.TestSupport;
///
/// Fabricates a with a given error number via the driver's internal
/// CreateException factory (the type has no public constructor) — same technique as
/// ExternalSystemGateway.Tests' FabricateSqlException.
///
internal static class SqlExceptionFactory
{
/// Creates a carrying one error with .
/// The SQL Server error number.
/// The error message.
/// The fabricated exception.
public static SqlException Create(int number, string message = "simulated")
{
var errorCtor = typeof(SqlError).GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic)
.First(c => c.GetParameters().Length == 8 && c.GetParameters()[7].ParameterType == typeof(Exception));
var sqlError = (SqlError)errorCtor.Invoke(
new object?[] { number, (byte)0, (byte)0, "server", message, "procedure", 0, null });
var collection = (SqlErrorCollection)typeof(SqlErrorCollection)
.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic).First()
.Invoke(Array.Empty