Skip to content

Commit 0717bd2

Browse files
authored
Merge pull request #2694 from PolicyEngine/test/modify-varying-your-earnings
Refactor varying your earnings test
2 parents 185c0bb + aca4b07 commit 0717bd2

File tree

4 files changed

+139
-181
lines changed

4 files changed

+139
-181
lines changed

changelog_entry.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- bump: patch
2+
changes:
3+
changed:
4+
- Refactored test_varying_your_earnings to better serve testing needs
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
"""
2+
Test fixtures and constants for axes calculation tests
3+
"""
4+
5+
import pytest
6+
from unittest.mock import Mock, MagicMock, patch
7+
from policyengine_api.endpoints.household import add_yearly_variables
8+
9+
STANDARD_AXES_COUNT = 401 # Not formally defined anywhere, but this value is used throughout the API
10+
SMALL_AXES_COUNT = 5
11+
TEST_YEAR = "2025"
12+
TEST_STATE = "NY"
13+
TEST_COUNTRY_ID = "us"
14+
15+
BASE_HOUSEHOLD = {
16+
"people": {
17+
"adult": {
18+
"age": {TEST_YEAR: 40},
19+
"employment_income": {TEST_YEAR: 30000},
20+
},
21+
},
22+
"tax_units": {"unit": {"members": ["adult"]}},
23+
"households": {
24+
"household": {
25+
"members": ["adult"],
26+
"state_name": {TEST_YEAR: TEST_STATE},
27+
}
28+
},
29+
}
30+
31+
SMALL_AXES_CONFIG = [
32+
[
33+
{
34+
"name": "employment_income",
35+
"period": TEST_YEAR,
36+
"min": 20000,
37+
"max": 40000,
38+
"count": SMALL_AXES_COUNT,
39+
}
40+
]
41+
]
42+
43+
44+
# Pytest fixtures
45+
def create_base_household():
46+
"""Returns a copy of the base household configuration"""
47+
import copy
48+
49+
return copy.deepcopy(BASE_HOUSEHOLD)
50+
51+
52+
def create_small_axes():
53+
"""Returns the small axes configuration for faster tests"""
54+
import copy
55+
56+
return copy.deepcopy(SMALL_AXES_CONFIG)
57+
58+
59+
def create_household_with_axes(base_household, axes_config):
60+
"""Helper function to add axes to a household"""
61+
import copy
62+
63+
household = copy.deepcopy(base_household)
64+
household["axes"] = axes_config
65+
return household
66+
67+
68+
def setup_small_axes_household(base_household, small_axes_config):
69+
"""Fixture to setup a household with small axes for testing"""
70+
household_with_axes = create_household_with_axes(
71+
base_household, small_axes_config
72+
)
73+
household_with_axes = add_yearly_variables(
74+
household_with_axes, TEST_COUNTRY_ID
75+
)
76+
return household_with_axes
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import pytest
2+
from unittest.mock import Mock, patch, MagicMock
3+
import numpy as np
4+
from policyengine_api.country import COUNTRIES
5+
from policyengine_api.endpoints.household import add_yearly_variables
6+
from tests.fixtures.integration.simulations import (
7+
TEST_COUNTRY_ID,
8+
SMALL_AXES_COUNT,
9+
setup_small_axes_household,
10+
create_base_household,
11+
create_small_axes,
12+
)
13+
14+
15+
class TestSimsWithAxes:
16+
17+
def test__given_any_number_of_axes__sim_returns_valid_arrays(
18+
self,
19+
): # , patched_add_yearly_variables, patched_countries_get):
20+
"""Integration test with small axes for speed"""
21+
base_household = create_base_household()
22+
small_axes_config = create_small_axes()
23+
household_with_axes = setup_small_axes_household(
24+
base_household, small_axes_config
25+
)
26+
country = COUNTRIES.get(TEST_COUNTRY_ID)
27+
result = country.calculate(household_with_axes, {})
28+
29+
# This variable does not function like others; it is a list of member names and is not calculated
30+
FORBIDDEN_VARIABLES = ["members"]
31+
32+
# Verify we get array results
33+
for entity_type in result:
34+
print("Entity type: ", entity_type)
35+
if entity_type == "axes":
36+
continue
37+
for entity_id in result[entity_type]:
38+
print("Entity ID: ", entity_id)
39+
for variable_name in result[entity_type][entity_id]:
40+
print("Variable name: ", variable_name)
41+
if variable_name in FORBIDDEN_VARIABLES:
42+
continue
43+
for period in result[entity_type][entity_id][
44+
variable_name
45+
]:
46+
print("Period: ", period)
47+
value = result[entity_type][entity_id][variable_name][
48+
period
49+
]
50+
print(f"Value: {value}")
51+
if isinstance(value, list):
52+
# Assert no Nones
53+
assert all(
54+
v is not None for v in value
55+
), f"None found in {variable_name} for {entity_id} in {period}"
56+
# Assert correct length
57+
assert (
58+
len(value) == SMALL_AXES_COUNT
59+
), f"Expected {SMALL_AXES_COUNT} values for {variable_name}, got {len(value)}"

tests/to_refactor/python/test_varying_your_earnings.py

Lines changed: 0 additions & 181 deletions
This file was deleted.

0 commit comments

Comments
 (0)