()
| 25 | |
| 26 | |
| 27 | def test_computed_fields_get(): |
| 28 | class Rectangle(BaseModel): |
| 29 | width: int |
| 30 | length: int |
| 31 | |
| 32 | @computed_field |
| 33 | def area(self) -> int: |
| 34 | """An awesome area""" |
| 35 | return self.width * self.length |
| 36 | |
| 37 | @computed_field(title='Pikarea', description='Another area') |
| 38 | @property |
| 39 | def area2(self) -> int: |
| 40 | return self.width * self.length |
| 41 | |
| 42 | @property |
| 43 | def double_width(self) -> int: |
| 44 | return self.width * 2 |
| 45 | |
| 46 | rect = Rectangle(width=10, length=5) |
| 47 | assert set(Rectangle.model_fields) == {'width', 'length'} |
| 48 | assert set(Rectangle.model_computed_fields) == {'area', 'area2'} |
| 49 | assert rect.__dict__ == {'width': 10, 'length': 5} |
| 50 | |
| 51 | assert Rectangle.model_computed_fields['area'].description == 'An awesome area' |
| 52 | assert Rectangle.model_computed_fields['area2'].title == 'Pikarea' |
| 53 | assert Rectangle.model_computed_fields['area2'].description == 'Another area' |
| 54 | |
| 55 | assert rect.area == 50 |
| 56 | assert rect.double_width == 20 |
| 57 | assert rect.model_dump() == {'width': 10, 'length': 5, 'area': 50, 'area2': 50} |
| 58 | assert rect.model_dump_json() == '{"width":10,"length":5,"area":50,"area2":50}' |
| 59 | |
| 60 | assert set(Rectangle.model_fields) == {'width', 'length'} |
| 61 | assert set(Rectangle.model_computed_fields) == {'area', 'area2'} |
| 62 | |
| 63 | assert Rectangle.model_computed_fields['area'].description == 'An awesome area' |
| 64 | assert Rectangle.model_computed_fields['area2'].title == 'Pikarea' |
| 65 | assert Rectangle.model_computed_fields['area2'].description == 'Another area' |
| 66 | |
| 67 | |
| 68 | def test_computed_fields_json_schema(): |
nothing calls this directly
no test coverage detected