|
| 1 | +"""Shared fixtures for pypums test suite. |
| 2 | +
|
| 3 | +Provides mock Census API responses, fake API keys, and temporary |
| 4 | +cache directories used across all test files. |
| 5 | +""" |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture() |
| 11 | +def fake_api_key(): |
| 12 | + """A fake Census API key for testing.""" |
| 13 | + return "fake_test_key_0123456789abcdef" |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture() |
| 17 | +def cache_dir(tmp_path): |
| 18 | + """Temporary cache directory for testing.""" |
| 19 | + d = tmp_path / "cache" |
| 20 | + d.mkdir() |
| 21 | + return d |
| 22 | + |
| 23 | + |
| 24 | +# --------------------------------------------------------------------------- |
| 25 | +# Sample Census API JSON responses |
| 26 | +# These mirror the structure returned by api.census.gov so we can mock httpx |
| 27 | +# calls and still test DataFrame construction, column naming, etc. |
| 28 | +# --------------------------------------------------------------------------- |
| 29 | + |
| 30 | +@pytest.fixture() |
| 31 | +def acs_api_response_tidy(): |
| 32 | + """Sample ACS API response (JSON rows) for 2 variables, 2 counties.""" |
| 33 | + return [ |
| 34 | + ["NAME", "B01001_001E", "B01001_001M", "B02001_002E", "B02001_002M", "state", "county"], |
| 35 | + ["Los Angeles County, California", "10014009", "0", "2786755", "9012", "06", "037"], |
| 36 | + ["Orange County, California", "3186989", "0", "1298431", "6234", "06", "059"], |
| 37 | + ] |
| 38 | + |
| 39 | + |
| 40 | +@pytest.fixture() |
| 41 | +def decennial_api_response(): |
| 42 | + """Sample Decennial Census API response for 2 variables, 2 states.""" |
| 43 | + return [ |
| 44 | + ["NAME", "P1_001N", "P1_002N", "state"], |
| 45 | + ["California", "39538223", "39538223", "06"], |
| 46 | + ["Texas", "29145505", "29145505", "48"], |
| 47 | + ] |
| 48 | + |
| 49 | + |
| 50 | +@pytest.fixture() |
| 51 | +def variables_api_response(): |
| 52 | + """Sample variables endpoint response.""" |
| 53 | + return { |
| 54 | + "variables": { |
| 55 | + "B01001_001E": { |
| 56 | + "label": "Estimate!!Total:", |
| 57 | + "concept": "SEX BY AGE", |
| 58 | + "predicateType": "int", |
| 59 | + "group": "B01001", |
| 60 | + }, |
| 61 | + "B01001_002E": { |
| 62 | + "label": "Estimate!!Total:!!Male:", |
| 63 | + "concept": "SEX BY AGE", |
| 64 | + "predicateType": "int", |
| 65 | + "group": "B01001", |
| 66 | + }, |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | +@pytest.fixture() |
| 72 | +def pums_api_response(): |
| 73 | + """Sample PUMS API response with person-level records.""" |
| 74 | + return [ |
| 75 | + ["SERIALNO", "SPORDER", "PWGTP", "AGEP", "SEX", "ST", "PUMA"], |
| 76 | + ["2023HU0000001", "01", "50", "35", "1", "06", "03701"], |
| 77 | + ["2023HU0000001", "02", "45", "33", "2", "06", "03701"], |
| 78 | + ["2023HU0000002", "01", "60", "42", "1", "06", "03701"], |
| 79 | + ] |
| 80 | + |
| 81 | + |
| 82 | +@pytest.fixture() |
| 83 | +def estimates_api_response(): |
| 84 | + """Sample Population Estimates API response.""" |
| 85 | + return [ |
| 86 | + ["NAME", "POP_2023", "DENSITY_2023", "state"], |
| 87 | + ["California", "38965193", "254.3", "06"], |
| 88 | + ["Texas", "30503301", "117.3", "48"], |
| 89 | + ] |
| 90 | + |
| 91 | + |
| 92 | +@pytest.fixture() |
| 93 | +def flows_api_response(): |
| 94 | + """Sample ACS Migration Flows API response.""" |
| 95 | + return [ |
| 96 | + [ |
| 97 | + "FULL1_NAME", "FULL2_NAME", |
| 98 | + "MOVEDIN", "MOVEDIN_M", |
| 99 | + "MOVEDOUT", "MOVEDOUT_M", |
| 100 | + "MOVEDNET", "MOVEDNET_M", |
| 101 | + "state1", "county1", "state2", "county2", |
| 102 | + ], |
| 103 | + [ |
| 104 | + "Los Angeles County, California", |
| 105 | + "Maricopa County, Arizona", |
| 106 | + "15234", "1200", "22456", "1500", "-7222", "1921", |
| 107 | + "06", "037", "04", "013", |
| 108 | + ], |
| 109 | + ] |
0 commit comments