-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from PolicyEngine/issue5
adding basic Pytest tests
- Loading branch information
Showing
4 changed files
with
160 additions
and
51 deletions.
There are no files selected for viewing
Empty file.
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
39 changes: 39 additions & 0 deletions
39
policyengine/tests/economic_impact/inequality_impact.py/inequality_impact.py
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,39 @@ | ||
import pytest | ||
import yaml | ||
import os | ||
from policyengine import EconomicImpact | ||
|
||
def assert_dict_approx_equal(actual, expected, tolerance=1e-4): | ||
for key in expected: | ||
assert abs(actual[key] - expected[key]) < tolerance, f"Key {key}: expected {expected[key]}, got {actual[key]}" | ||
|
||
# Specify the path to your YAML file | ||
yaml_file_path = "/home/tahseer/Desktop/NewFolder/policyengine.py/policyengine/tests/economic_impact/inequality_impact.py/inequality_impact.yaml" | ||
|
||
# Check if the file exists | ||
if not os.path.exists(yaml_file_path): | ||
raise FileNotFoundError(f"The YAML file does not exist at: {yaml_file_path}") | ||
|
||
with open(yaml_file_path, 'r') as file: | ||
test_cases = yaml.safe_load(file) | ||
|
||
@pytest.mark.parametrize("test_case", test_cases) | ||
def test_economic_impact(test_case): | ||
test_name = list(test_case.keys())[0] | ||
test_data = test_case[test_name] | ||
|
||
economic_impact = EconomicImpact(test_data['reform'], test_data['country']) | ||
|
||
if 'gini' in test_name: | ||
result = economic_impact.calculate("inequality/gini") | ||
elif 'top_1_pct' in test_name: | ||
result = economic_impact.calculate("inequality/top_1_pct_share") | ||
elif 'top_10_pct' in test_name: | ||
result = economic_impact.calculate("inequality/top_10_pct_share") | ||
else: | ||
pytest.fail(f"Unknown test case: {test_name}") | ||
|
||
assert_dict_approx_equal(result, test_data['expected']) | ||
|
||
if __name__ == "__main__": | ||
pytest.main([__file__]) |
33 changes: 33 additions & 0 deletions
33
policyengine/tests/economic_impact/inequality_impact.py/inequality_impact.yaml
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,33 @@ | ||
# economic_impact_tests.yaml | ||
- test_gini_calculator: | ||
reform: | ||
gov.hmrc.income_tax.rates.uk[0].rate: | ||
"2024-01-01.2100-12-31": 0.55 | ||
country: uk | ||
expected: | ||
baseline: 0.42 | ||
reform: 0.40 | ||
change: -0.01 | ||
change_percentage: -2.80 | ||
|
||
- test_top_1_pct_share_calculator: | ||
reform: | ||
gov.hmrc.income_tax.rates.uk[0].rate: | ||
"2024-01-01.2100-12-31": 0.55 | ||
country: uk | ||
expected: | ||
baseline: 0.12 | ||
reform: 0.14 | ||
change: 0.02 | ||
change_percentage: 12.76 | ||
|
||
- test_top_10_pct_share_calculator: | ||
reform: | ||
gov.hmrc.income_tax.rates.uk[0].rate: | ||
"2024-01-01.2100-12-31": 0.55 | ||
country: uk | ||
expected: | ||
baseline: 0.35 | ||
reform: 0.36 | ||
change: 0.01 | ||
change_percentage: 3.11 |