| 1 |
from typing import Set, Tuple, Union |
| 2 |
|
| 3 |
import pytest |
| 4 |
from pydantic import Field, ValidationError |
| 5 |
|
| 6 |
from geojson_pydantic.base import _GeoJsonBase |
| 7 |
|
| 8 |
BBOXES = ( |
| 9 |
(0, 100, 0, 0), |
| 10 |
(0, 0, 100, 0, 0, 0), |
| 11 |
(0, "a", 0, 0), # Invalid Type |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
@pytest.mark.parametrize("values", BBOXES) |
| 16 |
def test_bbox_validation(values: Tuple) -> None: |
| 17 |
# Ensure validation is happening correctly on the base model |
| 18 |
with pytest.raises(ValidationError): |
| 19 |
_GeoJsonBase(bbox=values) |
| 20 |
|
| 21 |
|
| 22 |
def test_bbox_antimeridian() -> None: |
| 23 |
with pytest.warns(UserWarning): |
| 24 |
_GeoJsonBase(bbox=(100, 0, 0, 0)) |
| 25 |
|
| 26 |
|
| 27 |
@pytest.mark.parametrize("values", BBOXES) |
| 28 |
def test_bbox_validation_subclass(values: Tuple) -> None: |
| 29 |
# Ensure validation is happening correctly when subclassed |
| 30 |
class TestClass(_GeoJsonBase): |
| 31 |
test_field: str = None |
| 32 |
|
| 33 |
with pytest.raises(ValidationError): |
| 34 |
TestClass(bbox=values) |
| 35 |
|
| 36 |
|
| 37 |
@pytest.mark.parametrize("values", BBOXES) |
| 38 |
def test_bbox_validation_field(values: Tuple) -> None: |
| 39 |
# Ensure validation is happening correctly when used as a field |
| 40 |
class TestClass(_GeoJsonBase): |
| 41 |
geo: _GeoJsonBase |
| 42 |
|
| 43 |
with pytest.raises(ValidationError): |
| 44 |
TestClass(geo={"bbox": values}) |
| 45 |
|
| 46 |
|
| 47 |
def test_exclude_if_none() -> None: |
| 48 |
model = _GeoJsonBase() |
| 49 |
# Included in default dump |
| 50 |
assert model.model_dump() == {"bbox": None} |
| 51 |
# Not included when in json mode |
| 52 |
assert model.model_dump(mode="json") == {} |
| 53 |
# And not included in the output json string. |
| 54 |
assert model.model_dump_json() == "{}" |
| 55 |
|
| 56 |
# Included if it has a value |
| 57 |
model = _GeoJsonBase(bbox=(0, 0, 0, 0)) |
| 58 |
assert model.model_dump() == {"bbox": (0, 0, 0, 0)} |
| 59 |
assert model.model_dump(mode="json") == {"bbox": [0, 0, 0, 0]} |
| 60 |
assert model.model_dump_json() == '{"bbox":[0.0,0.0,0.0,0.0]}' |
| 61 |
|
| 62 |
# Since `validate_assignment` is not set, you can do this without an error. |
| 63 |
# The solution should handle this and not just look at if the field is set. |
| 64 |
model.bbox = None |
| 65 |
assert model.model_dump() == {"bbox": None} |
| 66 |
assert model.model_dump(mode="json") == {} |
| 67 |
assert model.model_dump_json() == "{}" |
| 68 |
|
| 69 |
|
| 70 |
def test_exclude_if_none_subclass() -> None: |
| 71 |
# Create a subclass that adds a field, and ensure it works. |
| 72 |
class TestClass(_GeoJsonBase): |
| 73 |
test_field: str = None |
| 74 |
__geojson_exclude_if_none__: Set[str] = {"bbox", "test_field"} |
| 75 |
|
| 76 |
assert TestClass().model_dump_json() == "{}" |
| 77 |
assert TestClass(test_field="a").model_dump_json() == '{"test_field":"a"}' |
| 78 |
assert ( |
| 79 |
TestClass(bbox=(0, 0, 0, 0)).model_dump_json() == '{"bbox":[0.0,0.0,0.0,0.0]}' |
| 80 |
) |
| 81 |
|
| 82 |
|
| 83 |
def test_exclude_if_none_kwargs() -> None: |
| 84 |
# Create a subclass that adds fields and dumps it with kwargs to ensure |
| 85 |
# the kwargs are still being utilized. |
| 86 |
class TestClass(_GeoJsonBase): |
| 87 |
test_field: str = Field(default="test", alias="field") |
| 88 |
null_field: Union[str, None] = None |
| 89 |
|
| 90 |
model = TestClass(bbox=(0, 0, 0, 0)) |
| 91 |
assert ( |
| 92 |
model.model_dump_json(indent=2, by_alias=True, exclude_none=True) |
| 93 |
== """{ |
| 94 |
"bbox": [ |
| 95 |
0.0, |
| 96 |
0.0, |
| 97 |
0.0, |
| 98 |
0.0 |
| 99 |
], |
| 100 |
"field": "test" |
| 101 |
}""" |
| 102 |
) |