Coverage for backend/django/Economics/formulas/builders/native.py: 83%

37 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2026-06-23 21:51 +0000

1from __future__ import annotations 

2 

3from typing import TYPE_CHECKING 

4 

5from Economics.formulas.models import EconomicsMetricFormula 

6 

7from Economics.studies.models import EconomicsStudy 

8from Economics.formulas.builders.native_property_formulas import ( 

9 NativePropertyExpression, 

10 annual_operating_expense_property_expression, 

11 base_capital_cost_property_expression, 

12) 

13from Economics.formulas.native_properties.specs import ( 

14 ANNUAL_OPERATING_EXPENSE, 

15 BASE_CAPITAL_COST, 

16 EconomicsNativePropertySpec, 

17 native_property_specs, 

18) 

19 

20if TYPE_CHECKING: 

21 from Economics.results.services.financial_metrics import FinancialMetricsError, FinancialMetricsResult 

22 

23 

24def native_property_expression( 

25 study: EconomicsStudy, 

26 field_key: str, 

27 *, 

28 financial_metrics: "FinancialMetricsResult | None" = None, 

29 financial_metrics_error: "FinancialMetricsError | None" = None, 

30) -> NativePropertyExpression: 

31 if field_key == BASE_CAPITAL_COST: 

32 return base_capital_cost_property_expression(study) 

33 if field_key == ANNUAL_OPERATING_EXPENSE: 

34 return annual_operating_expense_property_expression(study) 

35 spec = _native_property_spec(study, field_key) 

36 if spec is not None and spec.result_metric_key: 36 ↛ 43line 36 didn't jump to line 43 because the condition on line 36 was always true

37 return _financial_metric_expression( 

38 study, 

39 spec=spec, 

40 financial_metrics=financial_metrics, 

41 financial_metrics_error=financial_metrics_error, 

42 ) 

43 return NativePropertyExpression("", False, f"Unknown economics native property `{field_key}`.") 

44 

45 

46def _native_property_spec(study: EconomicsStudy, field_key: str) -> EconomicsNativePropertySpec | None: 

47 return next((spec for spec in native_property_specs(study) if spec.field_key == field_key), None) 

48 

49 

50def _financial_metric_expression( 

51 study: EconomicsStudy, 

52 *, 

53 spec: EconomicsNativePropertySpec, 

54 financial_metrics: "FinancialMetricsResult | None", 

55 financial_metrics_error: "FinancialMetricsError | None", 

56) -> NativePropertyExpression: 

57 if financial_metrics_error is not None: 57 ↛ 58line 57 didn't jump to line 58 because the condition on line 57 was never true

58 return NativePropertyExpression("", False, financial_metrics_error.message) 

59 if financial_metrics is None: 59 ↛ 60line 59 didn't jump to line 60 because the condition on line 59 was never true

60 return NativePropertyExpression("", False, "Financial metrics have not been calculated.") 

61 metric = financial_metrics.metrics.get(spec.result_metric_key or "") 

62 if metric is None: 

63 return NativePropertyExpression("", False, f"{spec.display_name} is unavailable for this study.") 

64 if metric.formula_record_id is None: 

65 return NativePropertyExpression( 

66 "", 

67 False, 

68 f"{spec.display_name} has no persisted formula.", 

69 metric.value, 

70 ) 

71 formula_record = ( 

72 EconomicsMetricFormula.objects.filter( 

73 pk=metric.formula_record_id, 

74 flowsheet=study.flowsheet, 

75 study=study, 

76 metric_key=metric.key, 

77 ) 

78 .order_by("pk") 

79 .first() 

80 ) 

81 if formula_record is None: 81 ↛ 82line 81 didn't jump to line 82 because the condition on line 81 was never true

82 return NativePropertyExpression( 

83 "", 

84 False, 

85 f"{spec.display_name} formula record is unavailable.", 

86 metric.value, 

87 ) 

88 if formula_record.blocked_reason: 

89 return NativePropertyExpression( 

90 "", 

91 False, 

92 formula_record.blocked_reason, 

93 metric.value, 

94 formula_record.pk, 

95 ) 

96 if formula_record.status != "calculated": 

97 return NativePropertyExpression( 

98 "", 

99 False, 

100 f"{spec.display_name} is incomplete.", 

101 metric.value, 

102 formula_record.pk, 

103 ) 

104 if not formula_record.property_formula: 104 ↛ 105line 104 didn't jump to line 105 because the condition on line 104 was never true

105 return NativePropertyExpression( 

106 "", 

107 False, 

108 f"{spec.display_name} has no solve-visible formula.", 

109 metric.value, 

110 formula_record.pk, 

111 ) 

112 return NativePropertyExpression( 

113 formula_record.property_formula, 

114 True, 

115 "", 

116 metric.value, 

117 formula_record.pk, 

118 )