|
| 1 | +// --------------------------------------------------------------------- |
| 2 | +//This file is part of DotNetWorkQueue |
| 3 | +//Copyright © 2015-2026 Brian Lehnen |
| 4 | +// |
| 5 | +//This library is free software; you can redistribute it and/or |
| 6 | +//modify it under the terms of the GNU Lesser General Public |
| 7 | +//License as published by the Free Software Foundation; either |
| 8 | +//version 2.1 of the License, or (at your option) any later version. |
| 9 | +// |
| 10 | +//This library is distributed in the hope that it will be useful, |
| 11 | +//but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | +//Lesser General Public License for more details. |
| 14 | +// |
| 15 | +//You should have received a copy of the GNU Lesser General Public |
| 16 | +//License along with this library; if not, write to the Free Software |
| 17 | +//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | +// --------------------------------------------------------------------- |
| 19 | +using System.Data; |
| 20 | +using System.Linq; |
| 21 | +using DotNetWorkQueue.Transport.RelationalDatabase; |
| 22 | +using DotNetWorkQueue.Transport.RelationalDatabase.Basic; |
| 23 | +using DotNetWorkQueue.Transport.RelationalDatabase.Basic.Query; |
| 24 | +using DotNetWorkQueue.Transport.RelationalDatabase.Basic.QueryPrepareHandler; |
| 25 | +using NSubstitute; |
| 26 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 27 | + |
| 28 | +namespace DotNetWorkQueue.Transport.RelationalDatabase.Tests.Basic.QueryPrepareHandler |
| 29 | +{ |
| 30 | + [TestClass] |
| 31 | + public class GetQueueCountQueryPrepareHandlerTests |
| 32 | + { |
| 33 | + [TestMethod] |
| 34 | + public void Handle_With_Status_And_StatusEnabled_Binds_Int_Not_Enum() |
| 35 | + { |
| 36 | + // Regression guard for issue #155: the QueueStatusAdmin enum must be cast to its underlying |
| 37 | + // numeric type before being assigned to the @Status parameter. Npgsql 10.x throws |
| 38 | + // InvalidCastException when a raw CLR enum is bound to an integer parameter. |
| 39 | + var handler = new GetQueueCountQueryPrepareHandler(new FakeCommandStringCache(), CreateOptions(enableStatus: true)); |
| 40 | + var command = CreateDbCommand(); |
| 41 | + |
| 42 | + handler.Handle(new GetQueueCountQuery("conn", QueueStatusAdmin.Waiting), command, CommandStringTypes.GetQueueCountStatus); |
| 43 | + |
| 44 | + var parameters = (DataParameterCollection)command.Parameters; |
| 45 | + // Parameter name must match the SQL placeholder casing exactly. System.Data.SQLite binds |
| 46 | + // parameters case-sensitively, so "@Status" against a "@status" placeholder throws |
| 47 | + // "Insufficient parameters supplied to the command" (issue #155 follow-up). |
| 48 | + Assert.AreEqual(1, parameters.Count); |
| 49 | + var status = parameters.First(); |
| 50 | + Assert.AreEqual("@status", status.ParameterName); |
| 51 | + Assert.AreEqual(DbType.Int32, status.DbType); |
| 52 | + Assert.IsInstanceOfType(status.Value, typeof(int), "@status must be bound as an int, not the QueueStatusAdmin enum"); |
| 53 | + Assert.AreEqual((int)QueueStatusAdmin.Waiting, status.Value); |
| 54 | + } |
| 55 | + |
| 56 | + [TestMethod] |
| 57 | + public void Handle_With_Status_Uses_StatusFiltered_Command() |
| 58 | + { |
| 59 | + var handler = new GetQueueCountQueryPrepareHandler(new FakeCommandStringCache(), CreateOptions(enableStatus: true)); |
| 60 | + var command = CreateDbCommand(); |
| 61 | + |
| 62 | + handler.Handle(new GetQueueCountQuery("conn", QueueStatusAdmin.Processing), command, CommandStringTypes.GetQueueCountStatus); |
| 63 | + |
| 64 | + StringAssert.Contains(command.CommandText, "@status"); |
| 65 | + } |
| 66 | + |
| 67 | + [TestMethod] |
| 68 | + public void Handle_When_StatusDisabled_Adds_No_Parameter() |
| 69 | + { |
| 70 | + // When the status table is disabled the handler ignores the filter and counts all rows. |
| 71 | + var handler = new GetQueueCountQueryPrepareHandler(new FakeCommandStringCache(), CreateOptions(enableStatus: false)); |
| 72 | + var command = CreateDbCommand(); |
| 73 | + |
| 74 | + handler.Handle(new GetQueueCountQuery("conn", QueueStatusAdmin.Waiting), command, CommandStringTypes.GetQueueCountStatus); |
| 75 | + |
| 76 | + Assert.IsEmpty((DataParameterCollection)command.Parameters); |
| 77 | + } |
| 78 | + |
| 79 | + private static ITransportOptionsFactory CreateOptions(bool enableStatus) |
| 80 | + { |
| 81 | + var options = Substitute.For<ITransportOptions>(); |
| 82 | + options.EnableStatus.Returns(enableStatus); |
| 83 | + var factory = Substitute.For<ITransportOptionsFactory>(); |
| 84 | + factory.Create().Returns(options); |
| 85 | + return factory; |
| 86 | + } |
| 87 | + |
| 88 | + private static IDbCommand CreateDbCommand() |
| 89 | + { |
| 90 | + var command = Substitute.For<IDbCommand>(); |
| 91 | + var parameters = new DataParameterCollection(); |
| 92 | + command.Parameters.Returns(parameters); |
| 93 | + command.CreateParameter().Returns(_ => Substitute.For<IDbDataParameter>()); |
| 94 | + return command; |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments