Skip to content

Commit

Permalink
Fix backward_relations in PydanticMeta (#1815)
Browse files Browse the repository at this point in the history
  • Loading branch information
markus-96 authored Dec 20, 2024
1 parent 433e302 commit a5bb80f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Added
Fixed
^^^^^
- Fixed a deadlock in three level nested transactions (#1810)
- Fix backward_relations in PydanticMeta (#1814)

0.22
====
Expand Down
29 changes: 23 additions & 6 deletions tests/contrib/test_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
Employee,
EnumFields,
Event,
ModelTestPydanticMetaBackwardRelations1,
ModelTestPydanticMetaBackwardRelations2,
IntFields,
JSONFields,
Reporter,
Expand All @@ -33,11 +35,17 @@ async def asyncSetUp(self) -> None:
self.Tournament_Pydantic = pydantic_model_creator(Tournament)
self.Team_Pydantic = pydantic_model_creator(Team)
self.Address_Pydantic = pydantic_model_creator(Address)
self.ModelTestPydanticMetaBackwardRelations1_Pydantic = pydantic_model_creator(
ModelTestPydanticMetaBackwardRelations1
)
self.ModelTestPydanticMetaBackwardRelations2_Pydantic = pydantic_model_creator(
ModelTestPydanticMetaBackwardRelations2
)

class PydanticMetaOverride:
backward_relations = False

self.Event_Pydantic_non_backward = pydantic_model_creator(
self.Event_Pydantic_non_backward_from_override = pydantic_model_creator(
Event, meta_override=PydanticMetaOverride, name="Event_non_backward"
)

Expand All @@ -54,15 +62,24 @@ class PydanticMetaOverride:
await self.event2.participants.add(self.team1, self.team2)
self.maxDiff = None

async def test_backward_relations(self):
async def test_backward_relations_with_meta_override(self):
event_schema = copy.deepcopy(dict(self.Event_Pydantic.model_json_schema()))
event_non_backward_schema = copy.deepcopy(
dict(self.Event_Pydantic_non_backward.model_json_schema())
event_non_backward_schema_by_override = copy.deepcopy(
dict(self.Event_Pydantic_non_backward_from_override.model_json_schema())
)
self.assertTrue("address" in event_schema["properties"])
self.assertFalse("address" in event_non_backward_schema["properties"])
self.assertFalse("address" in event_non_backward_schema_by_override["properties"])
del event_schema["properties"]["address"]
self.assertEqual(event_schema["properties"], event_non_backward_schema["properties"])
self.assertEqual(event_schema["properties"], event_non_backward_schema_by_override["properties"])

async def test_backward_relations_with_pydantic_meta(self):
test_model1_schema = self.ModelTestPydanticMetaBackwardRelations1_Pydantic.model_json_schema()
test_model2_schema = self.ModelTestPydanticMetaBackwardRelations2_Pydantic.model_json_schema()
self.assertTrue("threes" in test_model2_schema["properties"])
self.assertFalse("threes" in test_model1_schema["properties"])
del test_model2_schema["properties"]["threes"]
self.assertEqual(test_model2_schema["properties"], test_model1_schema["properties"])
print(test_model2_schema)

def test_event_schema(self):
self.assertEqual(
Expand Down
18 changes: 18 additions & 0 deletions tests/testmodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,24 @@ def __str__(self):
return self.name


class ModelTestPydanticMetaBackwardRelations1(Model):
class PydanticMeta:
backward_relations = False


class ModelTestPydanticMetaBackwardRelations2(Model):
...


class ModelTestPydanticMetaBackwardRelations3(Model):
one: fields.ForeignKeyRelation[ModelTestPydanticMetaBackwardRelations1] = fields.ForeignKeyField(
"models.ModelTestPydanticMetaBackwardRelations1", related_name="threes"
)
two: fields.ForeignKeyRelation[ModelTestPydanticMetaBackwardRelations2] = fields.ForeignKeyField(
"models.ModelTestPydanticMetaBackwardRelations2", related_name="threes"
)


class Node(Model):
name = fields.CharField(max_length=10)

Expand Down
2 changes: 1 addition & 1 deletion tortoise/contrib/pydantic/descriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def get_param_from_pydantic_meta(attr: str, default: Any) -> Any:
exclude = tuple(get_param_from_pydantic_meta("exclude", default_meta.exclude))
computed = tuple(get_param_from_pydantic_meta("computed", default_meta.computed))
backward_relations = bool(
get_param_from_pydantic_meta("backward_relations_raw", default_meta.backward_relations)
get_param_from_pydantic_meta("backward_relations", default_meta.backward_relations)
)
max_recursion = int(
get_param_from_pydantic_meta("max_recursion", default_meta.max_recursion)
Expand Down

0 comments on commit a5bb80f

Please sign in to comment.