|
1 | 1 | from datetime import datetime, timezone
|
2 | 2 | from enum import Enum
|
| 3 | +from pathlib import PurePath, PurePosixPath, PureWindowsPath |
3 | 4 |
|
4 | 5 | import pytest
|
5 | 6 | from fastapi.encoders import jsonable_encoder
|
6 |
| -from pydantic import BaseModel, ValidationError |
| 7 | +from pydantic import BaseModel, ValidationError, create_model |
7 | 8 |
|
8 | 9 | try:
|
9 | 10 | from pydantic import Field
|
@@ -69,6 +70,19 @@ class ModelWithAlias(BaseModel):
|
69 | 70 | foo: str = Field(..., alias="Foo")
|
70 | 71 |
|
71 | 72 |
|
| 73 | +@pytest.fixture( |
| 74 | + name="model_with_path", params=[PurePath, PurePosixPath, PureWindowsPath] |
| 75 | +) |
| 76 | +def fixture_model_with_path(request): |
| 77 | + class Config: |
| 78 | + arbitrary_types_allowed = True |
| 79 | + |
| 80 | + ModelWithPath = create_model( |
| 81 | + "ModelWithPath", path=(request.param, ...), __config__=Config |
| 82 | + ) |
| 83 | + return ModelWithPath(path=request.param("/foo", "bar")) |
| 84 | + |
| 85 | + |
72 | 86 | def test_encode_class():
|
73 | 87 | person = Person(name="Foo")
|
74 | 88 | pet = Pet(owner=person, name="Firulais")
|
@@ -120,3 +134,11 @@ class MyModel(BaseModel):
|
120 | 134 | instance, custom_encoder={safe_datetime: lambda o: o.isoformat()}
|
121 | 135 | )
|
122 | 136 | assert encoded_instance["dt_field"] == instance.dt_field.isoformat()
|
| 137 | + |
| 138 | + |
| 139 | +def test_encode_model_with_path(model_with_path): |
| 140 | + if isinstance(model_with_path.path, PureWindowsPath): |
| 141 | + expected = "\\foo\\bar" |
| 142 | + else: |
| 143 | + expected = "/foo/bar" |
| 144 | + assert jsonable_encoder(model_with_path) == {"path": expected} |
0 commit comments