-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add attribute min_value, max_value in variable.py
- Loading branch information
1 parent
1ab99f7
commit a6b1a8b
Showing
3 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
- bump: minor | ||
changes: | ||
added: | ||
- max_value and min_value in Variable class. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import policyengine_core.country_template as country_template | ||
from policyengine_core.country_template.entities import Person | ||
from policyengine_core.variables import Variable | ||
from policyengine_core.periods import YEAR | ||
from test_variables import get_message | ||
|
||
tax_benefit_system = country_template.CountryTaxBenefitSystem() | ||
|
||
|
||
class variable__bound(Variable): | ||
value_type = int | ||
entity = Person | ||
definition_period = YEAR | ||
label = "Variable with bound." | ||
min_value = 0 | ||
max_value = 100 | ||
|
||
|
||
tax_benefit_system.add_variable(variable__bound) | ||
|
||
|
||
def test_variable__bound(): | ||
variable = tax_benefit_system.variables["variable__bound"] | ||
assert variable.min_value == 0 | ||
assert variable.max_value == 100 | ||
|
||
|
||
class variable__no_bound(Variable): | ||
value_type = int | ||
entity = Person | ||
definition_period = YEAR | ||
label = "Variable with no bound." | ||
|
||
|
||
tax_benefit_system.add_variable(variable__no_bound) | ||
|
||
|
||
def test_variable__no_bound(): | ||
variable = tax_benefit_system.variables["variable__no_bound"] | ||
assert variable.min_value is None | ||
assert variable.max_value is None | ||
|
||
|
||
class variable__small_max(Variable): | ||
value_type = int | ||
entity = Person | ||
definition_period = YEAR | ||
label = "Variable with max value smaller than min value." | ||
min_value = 100 | ||
max_value = 99 | ||
|
||
|
||
def test_variable__small_max(): | ||
try: | ||
tax_benefit_system.add_variable(variable__small_max) | ||
except ValueError as e: | ||
message = get_message(e) | ||
assert message.startswith("min_value cannot be greater than max_value") | ||
assert not tax_benefit_system.variables.get("variable__small_max") | ||
|
||
|
||
class variable__wrong_type(Variable): | ||
value_type = int | ||
entity = Person | ||
definition_period = YEAR | ||
label = "Variable with wrong max value data type." | ||
max_value = "string" | ||
|
||
|
||
def test_variable__wrong_type(): | ||
try: | ||
tax_benefit_system.add_variable(variable__wrong_type) | ||
except ValueError as e: | ||
message = get_message(e) | ||
assert message.startswith( | ||
"Invalid value 'string' for attribute 'max_value' in variable 'variable__wrong_type'. " | ||
"Must be of type '(<class 'float'>, <class 'int'>)'." | ||
) | ||
assert not tax_benefit_system.variables.get("variable__wrong_type") |