Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Domain.LinnApps/PurchaseOrders/PurchaseOrderDetail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,12 @@ public bool ValidPrices()
{
return false;
}


if (this.BaseOrderUnitPrice < 0)
{
return false;
}

return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
namespace Linn.Purchasing.Domain.LinnApps.Tests.PurchaseOrderServiceTests
{
using System;
using System.Collections.Generic;

using FluentAssertions;

using Linn.Purchasing.Domain.LinnApps.Exceptions;
using Linn.Purchasing.Domain.LinnApps.PurchaseLedger;
using Linn.Purchasing.Domain.LinnApps.PurchaseOrders;

using NSubstitute;

using NUnit.Framework;

public class WhenUpdatingAndNegativeBaseUnitPrice : ContextBase
Copy link
Contributor

@lewisrenfrew lewisrenfrew Feb 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this testing a scenario that can ever actually happen? The only code I can see in the service under test (the purchase order service) that actually sets detail.BaseOrderUnitPrice is

current.BaseOrderUnitPrice = Math.Round(
current.OrderUnitPriceCurrency.GetValueOrDefault() / exchangeRate,
5,
MidpointRounding.AwayFromZero);

In this test you are just explicitly setting it to be negative via the detail constructor which I don't think matches reality, so not sure how much value this test adds?

Is this PR to fix a bug?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's happened a few times over the past few months and each time it seems to be that the base order unit price has somehow been set to a negative value. I can remove the test but I do think it should be a thing that we be checking it in that valid prices method.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah - How do we recreate that bug? i.e. under what scenario can the derived BaseOrderUnitPrice field on a detail end up being set to be a negative number by the Sut? Could that exact scenario be arranged by the test? That way the the test will provide full confidence that the bug is squished

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stewart really isn't sure how he's managed to do it and I followed the creation and update logic last week but it's not exactly clear how it could ever get into that state. I personally think it's potentially been the thing we've seen in the past where a user will scroll down the page and the focus is still on the field so it will mark it down to a negative number. Not an ideal scenario but this check should at least report the fault. Really scratching my head how he could have got the form into that state.

{
private readonly int orderNumber = 600179;

private PurchaseOrder updated;

private Action act;

[SetUp]
public void SetUp()
{
this.updated = new PurchaseOrder
{
OrderNumber = this.orderNumber,
Details =
new List<PurchaseOrderDetail>
{
new PurchaseOrderDetail
{
Cancelled = "N",
Line = 1,
BaseNetTotal = 100m,
NetTotalCurrency = 120m,
BaseOrderUnitPrice = -100m,
OrderNumber = this.orderNumber,
OurQty = 99m,
OrderQty = 12m,
PartNumber = "P"
}
}
};

this.MockAuthService.HasPermissionFor(AuthorisedAction.PurchaseOrderUpdate, Arg.Any<IEnumerable<string>>())
.Returns(true);

this.PurchaseLedgerMaster.GetRecord().Returns(new PurchaseLedgerMaster { OkToRaiseOrder = "Y" });

this.act = () => this.Sut.UpdateOrder(new PurchaseOrder(), this.updated, new List<string>());
}


[Test]
public void ShouldThrow()
{
this.act.Should().Throw<PurchaseOrderException>("Prices must be positive numbers");
}
}
}