Avoid duplicate Best Value rank crashes
This commit is contained in:
@@ -213,6 +213,27 @@ final class FuelSavingsEstimateTests: XCTestCase {
|
||||
"baseline must be recalculated from the latest journey values when routes arrive")
|
||||
}
|
||||
|
||||
func testRankingToleratesDuplicateStationIDs() {
|
||||
let baseline = station("baseline", price: 145.9)
|
||||
let duplicateA = station("duplicate", price: 138.9, lat: 51.55)
|
||||
let duplicateB = station("duplicate", price: 139.9, lat: 51.56)
|
||||
let pool = [baseline, duplicateA, duplicateB]
|
||||
|
||||
let ranked = FuelSavingsEstimator.ranked(
|
||||
pool,
|
||||
fuel: .e10,
|
||||
baseline: baseline,
|
||||
settings: settings,
|
||||
amount: amount,
|
||||
journeyMiles: { s in
|
||||
s.id == "baseline" ? self.straightLine(2.0) : self.straightLine(7.0)
|
||||
}
|
||||
)
|
||||
|
||||
XCTAssertEqual(ranked.count, pool.count)
|
||||
XCTAssertEqual(ranked.filter { $0.id == "duplicate" }.count, 2)
|
||||
}
|
||||
|
||||
func testStationWithoutAPriceIsUnavailable() {
|
||||
let baseline = station("baseline", price: 145.9)
|
||||
let noPrice = station("no-price", price: nil)
|
||||
|
||||
@@ -499,11 +499,12 @@ enum FuelSavingsEstimator {
|
||||
pricePencePerLitre: station.prices[fuel] ?? .infinity
|
||||
)
|
||||
}
|
||||
let order = Dictionary(
|
||||
uniqueKeysWithValues: FuelSavingsCalculator.sortedForWorthTheDrive(ranked)
|
||||
.enumerated()
|
||||
.map { ($0.element.stationID, $0.offset) }
|
||||
)
|
||||
var order: [String: Int] = [:]
|
||||
for (offset, rankedStation) in FuelSavingsCalculator.sortedForWorthTheDrive(ranked).enumerated() {
|
||||
if order[rankedStation.stationID] == nil {
|
||||
order[rankedStation.stationID] = offset
|
||||
}
|
||||
}
|
||||
return stations.sorted { lhs, rhs in
|
||||
let left = order[lhs.id] ?? Int.max
|
||||
let right = order[rhs.id] ?? Int.max
|
||||
|
||||
Reference in New Issue
Block a user