|
| 1 | +extern alias mssql; |
| 2 | + |
| 3 | +namespace Open.Database.Extensions.Tests; |
| 4 | + |
| 5 | +#nullable enable |
| 6 | + |
| 7 | +// AddParam("@o", DbType.Int32, ParameterDirection.Output) on the two SQL Server commands. Their database type is SqlDbType, so a DbType used to bind to AddParam(string, object?, ParameterDirection) |
| 8 | +// and the enum itself became the VALUE of an output parameter. The overloads below take the DbType, as the generic commands do, and build a parameter of that type with no value. |
| 9 | +// Each case is written for both packages (System.Data.SqlClient and Microsoft.Data.SqlClient): the second is reached through an extern alias, as both define ExpressiveSqlCommand. |
| 10 | +// A command that declares an AddParam of its own hides the inherited ones that apply to the same call, so the last tests pin the calls that must keep their meaning, a literal 0 above all. |
| 11 | +[ExcludeFromCodeCoverage] |
| 12 | +public static class SqlServerParameterTypeTests |
| 13 | +{ |
| 14 | +#pragma warning disable CS0618 // System.Data.SqlClient is obsolete; it is one of the two packages under test |
| 15 | + static global::Open.Database.Extensions.ExpressiveSqlCommand Legacy() |
| 16 | + => new(new System.Data.SqlClient.SqlConnection(), CommandType.StoredProcedure, "dbo.p"); |
| 17 | +#pragma warning restore CS0618 |
| 18 | + |
| 19 | + static mssql::Open.Database.Extensions.ExpressiveSqlCommand Microsoft() |
| 20 | + => new(new Microsoft.Data.SqlClient.SqlConnection(), CommandType.StoredProcedure, "dbo.p"); |
| 21 | + |
| 22 | + static string Show(object? value) |
| 23 | + => value is null ? "null" : value is DBNull ? "DBNull" : Convert.ToString(value, System.Globalization.CultureInfo.InvariantCulture)!; |
| 24 | + |
| 25 | + // What the command holds: name=value databaseType direction, "-" when no database type was given. |
| 26 | + static string Sent(global::Open.Database.Extensions.ExpressiveSqlCommand command) |
| 27 | + => string.Join("; ", command.Params.Select(p => $"{p.Name}={Show(p.Value)} {(p.Type.HasValue ? p.Type.Value.ToString() : "-")} {p.Direction}")); |
| 28 | + |
| 29 | + static string Sent(mssql::Open.Database.Extensions.ExpressiveSqlCommand command) |
| 30 | + => string.Join("; ", command.Params.Select(p => $"{p.Name}={Show(p.Value)} {(p.Type.HasValue ? p.Type.Value.ToString() : "-")} {p.Direction}")); |
| 31 | + |
| 32 | + static string Only(bool condition, string expected) => condition ? expected : ""; |
| 33 | + |
| 34 | + // ---- a DbType and a direction: a parameter of that type with no value ---- |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public static void ADbTypeAndADirection_AreATypedParameterWithNoValue() |
| 38 | + { |
| 39 | + Assert.Equal("@o=null Int Output", Sent(Legacy().AddParam("@o", DbType.Int32, ParameterDirection.Output))); |
| 40 | + Assert.Equal("@o=null Int Output", Sent(Microsoft().AddParam("@o", DbType.Int32, ParameterDirection.Output))); |
| 41 | + Assert.Equal("@o=null BigInt Output", Sent(Legacy().AddParam("@o", DbType.Int64, ParameterDirection.Output))); |
| 42 | + Assert.Equal("@o=null NVarChar Output", Sent(Microsoft().AddParam("@o", DbType.String, ParameterDirection.Output))); |
| 43 | + Assert.Equal("@o=null VarChar Output", Sent(Legacy().AddParam("@o", DbType.AnsiString, ParameterDirection.Output))); |
| 44 | + Assert.Equal("@o=null DateTime2 Output", Sent(Microsoft().AddParam("@o", DbType.DateTime2, ParameterDirection.Output))); |
| 45 | + Assert.Equal("@o=null Bit Output", Sent(Legacy().AddParam("@o", DbType.Boolean, ParameterDirection.Output))); |
| 46 | + } |
| 47 | + |
| 48 | + [Fact] |
| 49 | + public static void TheOtherDirections_KeepTheirMeaning() |
| 50 | + { |
| 51 | + Assert.Equal("@r=null Int ReturnValue", Sent(Legacy().AddParam("@r", DbType.Int32, ParameterDirection.ReturnValue))); |
| 52 | + Assert.Equal("@r=null Int ReturnValue", Sent(Microsoft().AddParam("@r", DbType.Int32, ParameterDirection.ReturnValue))); |
| 53 | + |
| 54 | + // An input or input-output parameter with no value is a typed NULL, as for a SqlDbType. |
| 55 | + Assert.Equal("@i=DBNull Int Input", Sent(Legacy().AddParam("@i", DbType.Int32, ParameterDirection.Input))); |
| 56 | + Assert.Equal("@io=DBNull Int InputOutput", Sent(Microsoft().AddParam("@io", DbType.Int32, ParameterDirection.InputOutput))); |
| 57 | + } |
| 58 | + |
| 59 | + [Theory] |
| 60 | + [InlineData(true)] |
| 61 | + [InlineData(false)] |
| 62 | + public static void AddParamIf_AddsOnlyWhenTrue(bool condition) |
| 63 | + { |
| 64 | + Assert.Equal(Only(condition, "@o=null Int Output"), Sent(Legacy().AddParamIf(condition, "@o", DbType.Int32, ParameterDirection.Output))); |
| 65 | + Assert.Equal(Only(condition, "@o=null Int Output"), Sent(Microsoft().AddParamIf(condition, "@o", DbType.Int32, ParameterDirection.Output))); |
| 66 | + } |
| 67 | + |
| 68 | + [Fact] |
| 69 | + public static void ADbTypeSqlServerHasNoTypeFor_Throws_AsTheProviderDoes() |
| 70 | + { |
| 71 | + Assert.Throws<ArgumentException>(() => Legacy().AddParam("@o", DbType.UInt16, ParameterDirection.Output)); |
| 72 | + Assert.Throws<ArgumentException>(() => Microsoft().AddParam("@o", DbType.UInt16, ParameterDirection.Output)); |
| 73 | + } |
| 74 | + |
| 75 | + // ---- the calls that must keep their meaning: the new overload is declared on the command, so it hides the inherited overloads of any call it applies to ---- |
| 76 | + // A literal 0 converts implicitly to any enum, so a DbType parameter with a default direction would take AddParam("@count", 0) and add a typed NULL where an int 0 was meant. |
| 77 | + |
| 78 | + [Fact] |
| 79 | + [SuppressMessage("Roslynator", "RCS1118:Mark local variable as const", Justification = "The local is a variable on purpose: a literal 0 converts implicitly to any enum, a variable does not, and the test pins both.")] |
| 80 | + public static void AValueIsStillAValue_AboveAllALiteralZero() |
| 81 | + { |
| 82 | + int zero = 0; |
| 83 | + |
| 84 | + Assert.Equal("@x=0 - Input", Sent(Legacy().AddParam("@x", 0))); |
| 85 | + Assert.Equal("@x=0 - Input", Sent(Microsoft().AddParam("@x", 0))); |
| 86 | + Assert.Equal("@x=0 - Input", Sent(Legacy().AddParam("@x", zero))); |
| 87 | + Assert.Equal("@x=0 - Input", Sent(Microsoft().AddParam("@x", zero))); |
| 88 | + Assert.Equal("@x=0 - Input", Sent(Legacy().AddParam("@x", 0L))); |
| 89 | + Assert.Equal("@x=0 Int Input", Sent(Microsoft().AddParam("@x", 0, SqlDbType.Int))); |
| 90 | + Assert.Equal("@x=0 Int Input", Sent(Legacy().AddParamIf(true, "@x", 0, SqlDbType.Int))); |
| 91 | + Assert.Equal("@x=0 - Input", Sent(Microsoft().AddParamIf(true, "@x", 0))); |
| 92 | + Assert.Equal("@x=c - Input", Sent(Legacy().AddParam("@x", 'c'))); |
| 93 | + Assert.Equal("@x=c - Input", Sent(Microsoft().AddParam("@x", 'c'))); |
| 94 | + Assert.Equal("@x=5 - Output", Sent(Legacy().AddParam("@x", 5, ParameterDirection.Output))); |
| 95 | + Assert.Equal("@x=a - Input", Sent(Microsoft().AddParam("@x", "a"))); |
| 96 | + } |
| 97 | + |
| 98 | + [Fact] |
| 99 | + public static void ASqlDbTypeIsStillASqlDbType() |
| 100 | + { |
| 101 | + Assert.Equal("@x=DBNull Int Input", Sent(Legacy().AddParam("@x", SqlDbType.Int))); |
| 102 | + Assert.Equal("@x=null Int Output", Sent(Microsoft().AddParam("@x", SqlDbType.Int, ParameterDirection.Output))); |
| 103 | + Assert.Equal("@x=5 Int Input", Sent(Legacy().AddParam("@x", 5, SqlDbType.Int))); |
| 104 | + Assert.Equal("@x=null Int ReturnValue", Sent(Microsoft().AddReturnParam("@x", SqlDbType.Int))); |
| 105 | + } |
| 106 | +} |
0 commit comments