Skip to content

Commit 464d709

Browse files
committed
fix(spp_demo_phl_luzon): address review findings on area/population loaders
- test_population_weights: patch file_path at its imported location (odoo.addons.spp_demo_phl_luzon.models.population_weights.file_path) so the stub actually takes effect instead of being a no-op. - area_loader._load_shapes: hoist the shapely import above the feature loop and bail early on ImportError, avoiding one swallowed ImportError (and log warning) per feature when shapely is absent. - population_weights.get_weights: extract pcode inside the try/except so a malformed row skips gracefully instead of aborting the whole load.
1 parent 7836107 commit 464d709

3 files changed

Lines changed: 19 additions & 7 deletions

File tree

spp_demo_phl_luzon/models/area_loader.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,14 @@ def _load_shapes(self):
157157
features = geojson_data.get("features", [])
158158
shapes_loaded = 0
159159

160+
# Import shapely once up front: if it is missing there is no point
161+
# iterating features (each would fail and flood the log with warnings).
162+
try:
163+
from shapely.geometry import shape
164+
except ImportError:
165+
_logger.warning("shapely not installed; skipping Luzon shape loading")
166+
return 0
167+
160168
# Batch-fetch all referenced areas once; a per-feature search would
161169
# issue hundreds of queries for Luzon's administrative areas.
162170
codes = [f.get("properties", {}).get("code") for f in features]
@@ -176,8 +184,6 @@ def _load_shapes(self):
176184
continue
177185

178186
try:
179-
from shapely.geometry import shape
180-
181187
geom = shape(geometry)
182188
area.write({"geo_polygon": geom.wkt})
183189
shapes_loaded += 1

spp_demo_phl_luzon/models/population_weights.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ def get_weights(cls):
2929
with open(csv_path, encoding="utf-8") as f:
3030
reader = csv.DictReader(f)
3131
for row in reader:
32-
pcode = row["pcode"].strip()
3332
try:
33+
pcode = row["pcode"].strip()
3434
population = int(row["population"])
35-
except (ValueError, KeyError):
36-
_logger.warning("Skipping invalid population row for pcode: %s", pcode)
35+
except (ValueError, KeyError, AttributeError):
36+
_logger.warning("Skipping invalid population row: %s", row)
3737
continue
3838
weights[pcode] = population
3939

spp_demo_phl_luzon/tests/test_population_weights.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ def test_get_weights_skips_invalid_rows(self):
5656
)
5757
_reset_cache()
5858

59-
with patch("odoo.tools.misc.file_path", return_value="/fake/path"):
59+
with patch(
60+
"odoo.addons.spp_demo_phl_luzon.models.population_weights.file_path",
61+
return_value="/fake/path",
62+
):
6063
with patch("builtins.open", mock_open(read_data=csv_content)):
6164
weights = DemoPopulationWeights.get_weights()
6265

@@ -68,7 +71,10 @@ def test_get_weights_empty_csv(self):
6871
csv_content = "pcode,name,province_pcode,region_pcode,population\n"
6972
_reset_cache()
7073

71-
with patch("odoo.tools.misc.file_path", return_value="/fake/path"):
74+
with patch(
75+
"odoo.addons.spp_demo_phl_luzon.models.population_weights.file_path",
76+
return_value="/fake/path",
77+
):
7278
with patch("builtins.open", mock_open(read_data=csv_content)):
7379
weights = DemoPopulationWeights.get_weights()
7480

0 commit comments

Comments
 (0)