Skip to content

Commit 7c96ce2

Browse files
committed
Rename class var and add more tests.
1 parent 58db343 commit 7c96ce2

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

geojson_pydantic/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class _GeoJsonBase(BaseModel):
1313

1414
# These fields will not be included when serializing in json mode
1515
# `.model_dump_json()` or `.model_dump(mode="json")`
16-
__exclude_if_none__: Set[str] = {"bbox"}
16+
__geojson_exclude_if_none__: Set[str] = {"bbox"}
1717

1818
@property
1919
def __geo_interface__(self) -> Dict[str, Any]:
@@ -62,7 +62,7 @@ def clean_model(self, serializer: Any, _info: SerializationInfo) -> Dict[str, An
6262
# We want to avoid forcing values in `exclude_none` or `exclude_unset` which could
6363
# cause issues or unexpected behavior for downstream users.
6464
data: Dict[str, Any] = serializer(self)
65-
for field in self.__exclude_if_none__:
65+
for field in self.__geojson_exclude_if_none__:
6666
if field in data and data[field] is None:
6767
del data[field]
6868
return data

geojson_pydantic/features.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Feature(_GeoJsonBase, Generic[Geom, Props]):
1919
properties: Union[Props, None] = Field(...)
2020
id: Optional[Union[StrictInt, StrictStr]] = None
2121

22-
__exclude_if_none__ = {"bbox", "id"}
22+
__geojson_exclude_if_none__ = {"bbox", "id"}
2323

2424
@field_validator("geometry", mode="before")
2525
def set_geometry(cls, geometry: Any) -> Any:

tests/test_base.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,41 @@
55

66
from geojson_pydantic.base import _GeoJsonBase
77

8-
9-
@pytest.mark.parametrize(
10-
"values",
11-
(
12-
(100, 0, 0, 0), # Incorrect Order
13-
(0, 100, 0, 0),
14-
(0, 0, 100, 0, 0, 0),
15-
(0, "a", 0, 0), # Invalid Type
16-
),
8+
BBOXES = (
9+
(100, 0, 0, 0), # Incorrect Order
10+
(0, 100, 0, 0),
11+
(0, 0, 100, 0, 0, 0),
12+
(0, "a", 0, 0), # Invalid Type
1713
)
14+
15+
16+
@pytest.mark.parametrize("values", BBOXES)
1817
def test_bbox_validation(values: Tuple) -> None:
18+
# Ensure validation is happening correctly on the base model
1919
with pytest.raises(ValidationError):
2020
_GeoJsonBase(bbox=values)
2121

2222

23+
@pytest.mark.parametrize("values", BBOXES)
24+
def test_bbox_validation_subclass(values: Tuple) -> None:
25+
# Ensure validation is happening correctly when subclassed
26+
class TestClass(_GeoJsonBase):
27+
test_field: str = None
28+
29+
with pytest.raises(ValidationError):
30+
TestClass(bbox=values)
31+
32+
33+
@pytest.mark.parametrize("values", BBOXES)
34+
def test_bbox_validation_field(values: Tuple) -> None:
35+
# Ensure validation is happening correctly when used as a field
36+
class TestClass(_GeoJsonBase):
37+
geo: _GeoJsonBase
38+
39+
with pytest.raises(ValidationError):
40+
TestClass(geo={"bbox": values})
41+
42+
2343
def test_exclude_if_none() -> None:
2444
model = _GeoJsonBase()
2545
# Included in default dump

0 commit comments

Comments
 (0)