1
- """Module for testing the Formula class ."""
1
+ """Module for testing the Formula classes ."""
2
2
3
3
import pytest
4
4
5
- from blueprints .codes .formula import Formula
5
+ from blueprints .codes .formula import ComparisonFormula , Formula
6
6
7
7
8
8
class FormulaTest (Formula ):
@@ -32,7 +32,6 @@ def _evaluate(
32
32
33
33
def test_raise_error_when_changing_value_after_initialization () -> None :
34
34
"""Test that an error is raised when changing a value after initialization."""
35
- # example values
36
35
first = 1
37
36
second = 2
38
37
dummy_testing_formula = FormulaTest (first = first , second = second )
@@ -42,9 +41,111 @@ def test_raise_error_when_changing_value_after_initialization() -> None:
42
41
43
42
def test_raise_not_implemented_error_detailed_result () -> None :
44
43
"""Test that an error is raised when the detailed result is not implemented."""
45
- # example values
46
44
first = 1
47
45
second = 2
48
46
dummy_testing_formula = FormulaTest (first = first , second = second )
49
47
with pytest .raises (NotImplementedError ):
50
48
_ = dummy_testing_formula .detailed_result
49
+
50
+
51
+ class ComparisonFormulaTest (ComparisonFormula ):
52
+ """Dummy comparison formula for testing purposes."""
53
+
54
+ label = "Dummy testing comparison formula"
55
+ source_document = "Dummy testing document"
56
+
57
+ def __init__ (
58
+ self ,
59
+ a : float ,
60
+ b : float ,
61
+ c : float ,
62
+ ) -> None :
63
+ """Dummy comparison formula for testing purposes."""
64
+ super ().__init__ ()
65
+ self .a = a
66
+ self .b = b
67
+ self .c = c
68
+
69
+ @staticmethod
70
+ def _evaluate (
71
+ a : float ,
72
+ b : float ,
73
+ c : float ,
74
+ ) -> float :
75
+ """Dummy formula for testing purposes."""
76
+ return ComparisonFormulaTest ._evaluate_lhs (a = a , b = b ) <= ComparisonFormulaTest ._evaluate_rhs (c = c )
77
+
78
+ @staticmethod
79
+ def _evaluate_lhs (a : float , b : float , ** _ ) -> float :
80
+ """Left-hand side value of the comparison."""
81
+ return a + b
82
+
83
+ @staticmethod
84
+ def _evaluate_rhs (c : float , ** _ ) -> float :
85
+ """Right-hand side value of the comparison."""
86
+ return c / 2
87
+
88
+ @property
89
+ def unity_check (self ) -> float :
90
+ """Property to present the unity check of the formula."""
91
+ return self .lhs / self .rhs
92
+
93
+
94
+ def test_comparison_formula_evaluation () -> None :
95
+ """Test that the formula returns the correct result."""
96
+ a = 10
97
+ b = 5
98
+ c = 40
99
+
100
+ # check passing condition (lhs <= rhs) = True
101
+ formula = ComparisonFormulaTest (a = a , b = b , c = c )
102
+ assert formula
103
+
104
+ # check failing condition (lhs > rhs) = False
105
+ a = 30
106
+ formula = ComparisonFormulaTest (a = a , b = b , c = c )
107
+ assert not formula
108
+
109
+
110
+ def test_comparison_formula_lhs_property () -> None :
111
+ """Test that the lhs property returns the correct result."""
112
+ a = 10
113
+ b = 5
114
+ c = 40
115
+ formula = ComparisonFormulaTest (a = a , b = b , c = c )
116
+
117
+ # The lhs should be a + b = 10 + 5 = 15
118
+ assert formula .lhs == 15
119
+
120
+
121
+ def test_comparison_formula_rhs_property () -> None :
122
+ """Test that the rhs property returns the correct result."""
123
+ a = 10
124
+ b = 5
125
+ c = 40
126
+ formula = ComparisonFormulaTest (a = a , b = b , c = c )
127
+
128
+ # The rhs should be c / 2 = 40 / 2 = 20
129
+ assert formula .rhs == 20
130
+
131
+
132
+ def test_comparison_formula_unity_check_property () -> None :
133
+ """Test that the unity check property returns the correct result."""
134
+ a = 10
135
+ b = 5
136
+ c = 40
137
+ formula = ComparisonFormulaTest (a = a , b = b , c = c )
138
+
139
+ # The unity check should be lhs / rhs = 15 / 20 = 0.75
140
+ assert formula .unity_check == 0.75
141
+
142
+
143
+ def test_comparison_formula_change_value_after_initialization () -> None :
144
+ """Test that an error is raised when changing a value after initialization."""
145
+ a = 10
146
+ b = 5
147
+ c = 40
148
+ formula = ComparisonFormulaTest (a = a , b = b , c = c )
149
+
150
+ with pytest .raises (AttributeError ):
151
+ formula .a = 30
0 commit comments