Coverage for backend/ahuora-builder-types/src/ahuora_builder_types/units.py: 100%

10 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2026-07-22 05:22 +0000

1"""Shared project unit definitions. 

2 

3Keep Ahuora-specific units in this module so Django conversion code and the 

4builder expression parser register the same units with their Pint registries. 

5""" 

6 

7from __future__ import annotations 

8 

9from collections.abc import Callable 

10 

11CUSTOM_UNIT_DEFINITIONS = ( 

12 "dollar = [currency]", 

13 "megadollar = 1e6 * dollar", 

14 "NZD = dollar", 

15) 

16 

17# Fixed FX assumptions used by unit registries. The rates are anchored to NZD 

18# so existing generic ``dollar`` units remain equivalent to the app's default 

19# project currency. 

20FIXED_FX_RATES_PER_NZD = { 

21 "NZD": 1.0, 

22 "USD": 0.5849305057151759, 

23 "AUD": 0.8203239576440228, 

24 "EUR": 0.5039856740506515, 

25 "GBP": 0.4352380389574088, 

26} 

27 

28 

29def project_unit_definitions() -> tuple[str, ...]: 

30 """Return Pint definition strings for all Ahuora-specific units.""" 

31 

32 currency_definitions = tuple( 

33 f"{currency_code} = {1 / units_per_nzd} * NZD" 

34 for currency_code, units_per_nzd in FIXED_FX_RATES_PER_NZD.items() 

35 if currency_code != "NZD" 

36 ) 

37 return CUSTOM_UNIT_DEFINITIONS + currency_definitions 

38 

39 

40def register_project_units(define: Callable[[str], object]) -> None: 

41 """Register Ahuora-specific units with a Pint-compatible ``define`` call.""" 

42 

43 for definition in project_unit_definitions(): 

44 define(definition)