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
5 changes: 3 additions & 2 deletions tariff_fetch/urdb/arcadia/fixedcharge.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def get_fixed_charge_at_dt(scenario: Scenario, library: Library, dt: datetime) -
master_tariff_id = scenario.master_tariff_id
tariff = library.tariffs.get_tariff_at_date(master_tariff_id, dt.date())
rates = ru.tariff_iter_rates_for_dt(tariff, scenario, library, dt)
rates = list(rates)
return sum(get_rate_fixed_charge_at_dt(scenario, library, rate, dt) for rate in rates)


Expand All @@ -53,14 +54,14 @@ def get_rate_fixed_charge_at_dt(scenario: Scenario, library: Library, rate: Tari
bands = ru.rate_filter_bands(rate, scenario, library)
if rate["charge_type"] != "FIXED_PRICE":
return 0
if not bands:
return 0
if (variable_factor_key := rate.get("variable_factor_key")) is not None and not (
rate["charge_period"] == "MONTHLY" and variable_factor_key == "billingPeriodProrationFactor"
):
raise RateConversionError(rate, "Fixed charges cannot have variable factors")
if rate.get("quantity_key") is not None:
raise RateConversionError(rate, "Rates with quantity_key are not supported for fixed charge conversion")
if not bands:
return 0
band_rate_units = {band["rate_unit"] for band in bands}
if (transaction_type := rate["transaction_type"]) != "BUY":
raise RateConversionError(
Expand Down
45 changes: 42 additions & 3 deletions tests/test_arcadia_urdb_fixedcharge.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,60 @@
from tariff_fetch.arcadia.schema.tariff import TariffExtended
from tariff_fetch.urdb.arcadia.exception import RateConversionError
from tariff_fetch.urdb.arcadia.fixedcharge import build_fixed_charge
from tariff_fetch.urdb.arcadia.library import Library, TariffLibrary, VariablePropertyLibrary
from tariff_fetch.urdb.arcadia.library import Library, PropertyValue, TariffLibrary, VariablePropertyLibrary
from tariff_fetch.urdb.arcadia.scenario import Scenario
from tests.arcadia_urdb_fixtures import BAND, RATE, TARIFF


def make_stub_library(tariffs: list[TariffExtended]) -> Library:
def make_stub_library(tariffs: list[TariffExtended], properties: dict[str, PropertyValue] | None = None) -> Library:
tariff_library = TariffLibrary(None, None, tariffs)
variables_library = VariablePropertyLibrary(None, None, None)
return Library(None, None, None, tariff_library=tariff_library, variables_library=variables_library)
return Library(None, properties, None, tariff_library=tariff_library, variables_library=variables_library)


def make_stub_scenario(tariff: TariffExtended) -> Scenario:
return Scenario(tariff["master_tariff_id"], 2025, False)


def test_build_fixed_charge_inapplicable_bands_does_not_fail():
tariff: TariffExtended = {
**TARIFF,
"rates": [
{
**RATE,
"charge_type": "FIXED_PRICE",
"applicability_key": "APPLICABLE",
"charge_period": "MONTHLY",
"rate_bands": [
{
**BAND,
"applicability_value": "true",
"rate_amount": 2.0,
}
],
"quantity_key": "some_key",
}
],
"properties": [
{
"key_name": "APPLICABLE",
"display_name": "applicable",
"data_type": "BOOLEAN",
"operator": "=",
"keyspace": "tariff",
"family": "service",
"description": "applicability",
"property_types": "APPLICABILITY",
"is_default": False,
}
],
}
scenario = make_stub_scenario(tariff)
library = make_stub_library([tariff], properties={"APPLICABLE": "false"})
result = build_fixed_charge(scenario, library)
assert result.get("fixedchargefirstmeter") == 0


def test_build_fixed_charge_returns_zero_when_no_fixed_rates_apply():
tariff: TariffExtended = {
**TARIFF,
Expand Down
Loading