Enhance procurement lot estimated value aggregation and add unit tests
- Updated the `AggregateProcurementLotEstimatedValue` function to reject mixed currencies and allow empty lot currencies, ensuring accurate aggregation of estimated values. - Introduced new unit tests in `entity_test.go` to validate the behavior of the aggregation function under various scenarios, including mixed currencies and empty lot currencies. - Refactored budget calculation in `budget_test.go` to utilize the new aggregation logic, improving consistency in budget retrieval. This update improves the handling of estimated values in procurement lots, enhancing the reliability of the tender management system.
This commit is contained in:
@@ -309,18 +309,37 @@ func (t *Tender) HasEstimatedValue() bool {
|
||||
return value > 0
|
||||
}
|
||||
|
||||
// AggregateProcurementLotEstimatedValue sums lot-level estimated values and returns the first lot currency found.
|
||||
// AggregateProcurementLotEstimatedValue sums lot-level estimated values when all non-empty
|
||||
// lot currencies match. Conflicting currencies are not aggregated.
|
||||
func AggregateProcurementLotEstimatedValue(lots []ProcurementLot) (value float64, currency string) {
|
||||
var refCurrency string
|
||||
for _, lot := range lots {
|
||||
if lot.EstimatedValue <= 0 {
|
||||
continue
|
||||
}
|
||||
c := strings.TrimSpace(lot.Currency)
|
||||
if c == "" {
|
||||
continue
|
||||
}
|
||||
if refCurrency == "" {
|
||||
refCurrency = c
|
||||
continue
|
||||
}
|
||||
if c != refCurrency {
|
||||
return 0, ""
|
||||
}
|
||||
}
|
||||
|
||||
for _, lot := range lots {
|
||||
if lot.EstimatedValue <= 0 {
|
||||
continue
|
||||
}
|
||||
value += lot.EstimatedValue
|
||||
if currency == "" {
|
||||
currency = strings.TrimSpace(lot.Currency)
|
||||
}
|
||||
}
|
||||
return value, currency
|
||||
if value <= 0 {
|
||||
return 0, ""
|
||||
}
|
||||
return value, refCurrency
|
||||
}
|
||||
|
||||
// ResolvedEstimatedValueAndCurrency returns the tender-level estimated value when set,
|
||||
|
||||
@@ -15,6 +15,29 @@ func TestAggregateProcurementLotEstimatedValue(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAggregateProcurementLotEstimatedValueRejectsMixedCurrencies(t *testing.T) {
|
||||
value, currency := AggregateProcurementLotEstimatedValue([]ProcurementLot{
|
||||
{EstimatedValue: 100000, Currency: "EUR"},
|
||||
{EstimatedValue: 250000, Currency: "USD"},
|
||||
})
|
||||
if value != 0 || currency != "" {
|
||||
t.Fatalf("expected no aggregation for mixed currencies, got value=%v currency=%q", value, currency)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAggregateProcurementLotEstimatedValueAllowsEmptyLotCurrency(t *testing.T) {
|
||||
value, currency := AggregateProcurementLotEstimatedValue([]ProcurementLot{
|
||||
{EstimatedValue: 100000, Currency: "EUR"},
|
||||
{EstimatedValue: 250000},
|
||||
})
|
||||
if value != 350000 {
|
||||
t.Fatalf("expected total 350000, got %v", value)
|
||||
}
|
||||
if currency != "EUR" {
|
||||
t.Fatalf("expected currency EUR, got %q", currency)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolvedEstimatedValueAndCurrencyUsesLotsWhenTopLevelMissing(t *testing.T) {
|
||||
tender := &Tender{
|
||||
Currency: "EUR",
|
||||
|
||||
Reference in New Issue
Block a user