|
| 1 | +"""Unit tests for load_table() EVALID and state filtering. |
| 2 | +
|
| 3 | +Verifies that load_table() applies PLT_CN-based EVALID filtering and |
| 4 | +STATECD-based state filtering to any table that has those columns, |
| 5 | +not just a hardcoded allowlist of table names. |
| 6 | +""" |
| 7 | + |
| 8 | +from unittest.mock import MagicMock, patch |
| 9 | + |
| 10 | +import polars as pl |
| 11 | +import pytest |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def mock_fia(): |
| 16 | + """Create a mock FIA instance with the real load_table method.""" |
| 17 | + from pyfia.core.fia import FIA |
| 18 | + |
| 19 | + with patch.object(FIA, "__init__", lambda self: None): |
| 20 | + db = FIA() |
| 21 | + db.tables = {} |
| 22 | + db.evalid = None |
| 23 | + db.state_filter = None |
| 24 | + db._polygon_attributes = None |
| 25 | + db._spatial_plot_cns = None |
| 26 | + db._valid_plot_cns = None |
| 27 | + db._reader = MagicMock() |
| 28 | + return db |
| 29 | + |
| 30 | + |
| 31 | +class TestEVALIDFilteringByColumn: |
| 32 | + """EVALID filtering should apply to any table with PLT_CN, not just TREE/COND.""" |
| 33 | + |
| 34 | + def test_tree_grm_component_gets_evalid_filtered(self, mock_fia): |
| 35 | + """TREE_GRM_COMPONENT has PLT_CN and should be EVALID-filtered.""" |
| 36 | + mock_fia.evalid = [132303] |
| 37 | + mock_fia._valid_plot_cns = ["100", "200", "300"] |
| 38 | + mock_fia._reader.get_table_schema.return_value = { |
| 39 | + "CN": "VARCHAR", |
| 40 | + "TRE_CN": "VARCHAR", |
| 41 | + "PLT_CN": "VARCHAR", |
| 42 | + "COMPONENT": "VARCHAR", |
| 43 | + "TPA_UNADJ": "DOUBLE", |
| 44 | + } |
| 45 | + mock_fia._reader.read_table.return_value = pl.DataFrame( |
| 46 | + {"TRE_CN": ["1"], "PLT_CN": ["100"], "TPA_UNADJ": [1.0]} |
| 47 | + ).lazy() |
| 48 | + |
| 49 | + mock_fia.load_table("TREE_GRM_COMPONENT") |
| 50 | + |
| 51 | + # Verify read_table was called with a PLT_CN IN (...) WHERE clause |
| 52 | + call_args = mock_fia._reader.read_table.call_args |
| 53 | + where_clause = call_args.kwargs.get("where", "") or "" |
| 54 | + assert "PLT_CN IN" in where_clause |
| 55 | + |
| 56 | + def test_table_without_plt_cn_skips_evalid_filter(self, mock_fia): |
| 57 | + """Tables without PLT_CN (e.g. POP_EVAL) should not get EVALID filtering.""" |
| 58 | + mock_fia.evalid = [132303] |
| 59 | + mock_fia._valid_plot_cns = ["100", "200"] |
| 60 | + mock_fia._reader.get_table_schema.return_value = { |
| 61 | + "CN": "VARCHAR", |
| 62 | + "EVALID": "INTEGER", |
| 63 | + "EVAL_DESCR": "VARCHAR", |
| 64 | + } |
| 65 | + mock_fia._reader.read_table.return_value = pl.DataFrame( |
| 66 | + {"CN": ["1"], "EVALID": [132303], "EVAL_DESCR": ["test"]} |
| 67 | + ).lazy() |
| 68 | + |
| 69 | + mock_fia.load_table("POP_EVAL") |
| 70 | + |
| 71 | + # Should use default path without PLT_CN filtering |
| 72 | + call_args = mock_fia._reader.read_table.call_args |
| 73 | + where_clause = call_args.kwargs.get("where", "") or "" |
| 74 | + assert "PLT_CN IN" not in where_clause |
| 75 | + |
| 76 | + def test_no_evalid_set_skips_filter(self, mock_fia): |
| 77 | + """When no EVALID is set, PLT_CN filtering should be skipped.""" |
| 78 | + mock_fia.evalid = None |
| 79 | + mock_fia._reader.get_table_schema.return_value = { |
| 80 | + "CN": "VARCHAR", |
| 81 | + "PLT_CN": "VARCHAR", |
| 82 | + "TPA_UNADJ": "DOUBLE", |
| 83 | + } |
| 84 | + mock_fia._reader.read_table.return_value = pl.DataFrame( |
| 85 | + {"CN": ["1"], "PLT_CN": ["100"], "TPA_UNADJ": [1.0]} |
| 86 | + ).lazy() |
| 87 | + |
| 88 | + mock_fia.load_table("TREE_GRM_COMPONENT") |
| 89 | + |
| 90 | + # Should use default path |
| 91 | + call_args = mock_fia._reader.read_table.call_args |
| 92 | + where_clause = call_args.kwargs.get("where", "") or "" |
| 93 | + assert "PLT_CN IN" not in where_clause |
| 94 | + |
| 95 | + |
| 96 | +class TestStateFilteringByColumn: |
| 97 | + """State filtering should apply to any table with STATECD.""" |
| 98 | + |
| 99 | + def test_table_with_statecd_gets_filtered(self, mock_fia): |
| 100 | + """Any table with STATECD should get state filtering.""" |
| 101 | + mock_fia.state_filter = [13] # Georgia |
| 102 | + mock_fia._reader.get_table_schema.return_value = { |
| 103 | + "CN": "VARCHAR", |
| 104 | + "PLT_CN": "VARCHAR", |
| 105 | + "STATECD": "INTEGER", |
| 106 | + } |
| 107 | + mock_fia._reader.read_table.return_value = pl.DataFrame( |
| 108 | + {"CN": ["1"], "PLT_CN": ["100"], "STATECD": [13]} |
| 109 | + ).lazy() |
| 110 | + |
| 111 | + mock_fia.load_table("SEEDLING") |
| 112 | + |
| 113 | + call_args = mock_fia._reader.read_table.call_args |
| 114 | + where_clause = call_args.kwargs.get("where", "") or "" |
| 115 | + assert "STATECD IN (13)" in where_clause |
| 116 | + |
| 117 | + def test_table_without_statecd_skips_filter(self, mock_fia): |
| 118 | + """Tables without STATECD should not get state filtering.""" |
| 119 | + mock_fia.state_filter = [13] |
| 120 | + mock_fia._reader.get_table_schema.return_value = { |
| 121 | + "CN": "VARCHAR", |
| 122 | + "EVALID": "INTEGER", |
| 123 | + } |
| 124 | + mock_fia._reader.read_table.return_value = pl.DataFrame( |
| 125 | + {"CN": ["1"], "EVALID": [132303]} |
| 126 | + ).lazy() |
| 127 | + |
| 128 | + mock_fia.load_table("POP_EVAL") |
| 129 | + |
| 130 | + call_args = mock_fia._reader.read_table.call_args |
| 131 | + where_clause = call_args.kwargs.get("where", "") or "" |
| 132 | + assert "STATECD" not in where_clause |
| 133 | + |
| 134 | + |
| 135 | +class TestSpatialFilteringByColumn: |
| 136 | + """Spatial filtering should apply to any table with PLT_CN.""" |
| 137 | + |
| 138 | + def test_spatial_filter_applies_to_grm_table(self, mock_fia): |
| 139 | + """Tables with PLT_CN should get spatial filtering when active.""" |
| 140 | + mock_fia._spatial_plot_cns = ["100", "200"] |
| 141 | + mock_fia._reader.get_table_schema.return_value = { |
| 142 | + "CN": "VARCHAR", |
| 143 | + "PLT_CN": "VARCHAR", |
| 144 | + "TPA_UNADJ": "DOUBLE", |
| 145 | + } |
| 146 | + data = pl.DataFrame( |
| 147 | + { |
| 148 | + "CN": ["1", "2", "3"], |
| 149 | + "PLT_CN": ["100", "200", "999"], |
| 150 | + "TPA_UNADJ": [1.0, 2.0, 3.0], |
| 151 | + } |
| 152 | + ).lazy() |
| 153 | + mock_fia._reader.read_table.return_value = data |
| 154 | + |
| 155 | + result = mock_fia.load_table("TREE_GRM_COMPONENT") |
| 156 | + |
| 157 | + # Should filter to only spatial plot CNs |
| 158 | + collected = result.collect() |
| 159 | + assert collected.shape[0] == 2 |
| 160 | + assert set(collected["PLT_CN"].to_list()) == {"100", "200"} |
0 commit comments