Skip to content

Commit

Permalink
Add more for describe of ManyToManyField
Browse files Browse the repository at this point in the history
  • Loading branch information
long2ice committed Feb 3, 2021
1 parent 2739267 commit a25c0fc
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 14 deletions.
93 changes: 79 additions & 14 deletions tests/utils/test_describe_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ def test_describe_models_some(self):

def test_describe_model_straight(self):
val = StraightFields.describe()

self.assertEqual(
val,
{
Expand Down Expand Up @@ -315,6 +314,13 @@ def test_describe_model_straight(self):
"description": "M2M to myself",
"docstring": None,
"constraints": {},
"model_name": "models.StraightFields",
"related_name": "rel_from",
"forward_key": "straightfields_id",
"backward_key": "straightfields_rel_id",
"through": "straightfields_straightfields",
"on_delete": "CASCADE",
"_generated": False,
},
{
"name": "rel_from",
Expand All @@ -328,14 +334,20 @@ def test_describe_model_straight(self):
"description": "M2M to myself",
"docstring": None,
"constraints": {},
"model_name": "models.StraightFields",
"related_name": "rel_to",
"forward_key": "straightfields_rel_id",
"backward_key": "straightfields_id",
"through": "straightfields_straightfields",
"on_delete": "CASCADE",
"_generated": True,
},
],
},
)

def test_describe_model_straight_native(self):
val = StraightFields.describe(serializable=False)

self.assertEqual(
val,
{
Expand Down Expand Up @@ -515,6 +527,13 @@ def test_describe_model_straight_native(self):
"description": "M2M to myself",
"docstring": None,
"constraints": {},
"model_name": "models.StraightFields",
"related_name": "rel_from",
"forward_key": "straightfields_id",
"backward_key": "straightfields_rel_id",
"through": "straightfields_straightfields",
"on_delete": "CASCADE",
"_generated": False,
},
{
"name": "rel_from",
Expand All @@ -528,14 +547,20 @@ def test_describe_model_straight_native(self):
"description": "M2M to myself",
"docstring": None,
"constraints": {},
"model_name": "models.StraightFields",
"related_name": "rel_to",
"forward_key": "straightfields_rel_id",
"backward_key": "straightfields_id",
"through": "straightfields_straightfields",
"on_delete": "CASCADE",
"_generated": True,
},
],
},
)

def test_describe_model_source(self):
val = SourceFields.describe()

self.assertEqual(
val,
{
Expand Down Expand Up @@ -715,6 +740,13 @@ def test_describe_model_source(self):
"description": "M2M to myself",
"docstring": None,
"constraints": {},
"model_name": "models.SourceFields",
"related_name": "rel_from",
"forward_key": "sts_forward",
"backward_key": "backward_sts",
"through": "sometable_self",
"on_delete": "CASCADE",
"_generated": False,
},
{
"name": "rel_from",
Expand All @@ -728,14 +760,20 @@ def test_describe_model_source(self):
"description": "M2M to myself",
"docstring": None,
"constraints": {},
"model_name": "models.SourceFields",
"related_name": "rel_to",
"forward_key": "backward_sts",
"backward_key": "sts_forward",
"through": "sometable_self",
"on_delete": "CASCADE",
"_generated": True,
},
],
},
)

def test_describe_model_source_native(self):
val = SourceFields.describe(serializable=False)

self.assertEqual(
val,
{
Expand Down Expand Up @@ -915,6 +953,13 @@ def test_describe_model_source_native(self):
"description": "M2M to myself",
"docstring": None,
"constraints": {},
"model_name": "models.SourceFields",
"related_name": "rel_from",
"forward_key": "sts_forward",
"backward_key": "backward_sts",
"through": "sometable_self",
"on_delete": "CASCADE",
"_generated": False,
},
{
"name": "rel_from",
Expand All @@ -928,6 +973,13 @@ def test_describe_model_source_native(self):
"description": "M2M to myself",
"docstring": None,
"constraints": {},
"model_name": "models.SourceFields",
"related_name": "rel_to",
"forward_key": "backward_sts",
"backward_key": "sts_forward",
"through": "sometable_self",
"on_delete": "CASCADE",
"_generated": True,
},
],
},
Expand Down Expand Up @@ -982,25 +1034,31 @@ def test_describe_model_uuidpk(self):
"backward_o2o_fields": [],
"m2m_fields": [
{
"name": "peers",
"_generated": True,
"backward_key": "uuidpkmodel_id",
"constraints": {},
"default": None,
"description": None,
"docstring": None,
"field_type": "ManyToManyFieldInstance",
"python_type": "models.UUIDM2MRelatedModel",
"forward_key": "uuidm2mrelatedmodel_id",
"generated": False,
"indexed": False,
"model_name": "models.UUIDM2MRelatedModel",
"name": "peers",
"nullable": False,
"on_delete": "CASCADE",
"python_type": "models.UUIDM2MRelatedModel",
"related_name": "models",
"through": "uuidm2mrelatedmodel_uuidpkmodel",
"unique": False,
"indexed": False,
"default": None,
"description": None,
"docstring": None,
"constraints": {},
}
],
},
)

def test_describe_model_uuidpk_native(self):
val = UUIDPkModel.describe(serializable=False)

self.assertEqual(
val,
{
Expand Down Expand Up @@ -1048,16 +1106,23 @@ def test_describe_model_uuidpk_native(self):
"m2m_fields": [
{
"name": "peers",
"field_type": ManyToManyFieldInstance,
"python_type": UUIDM2MRelatedModel,
"generated": False,
"nullable": False,
"field_type": ManyToManyFieldInstance,
"python_type": UUIDM2MRelatedModel,
"unique": False,
"indexed": False,
"default": None,
"description": None,
"docstring": None,
"constraints": {},
"model_name": "models.UUIDM2MRelatedModel",
"related_name": "models",
"forward_key": "uuidm2mrelatedmodel_id",
"backward_key": "uuidpkmodel_id",
"through": "uuidm2mrelatedmodel_uuidpkmodel",
"on_delete": "CASCADE",
"_generated": True,
}
],
},
Expand Down
11 changes: 11 additions & 0 deletions tortoise/fields/relational.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,17 @@ def __init__(
self._generated: bool = False
self.on_delete = on_delete

def describe(self, serializable: bool) -> dict:
desc = super().describe(serializable)
desc["model_name"] = self.model_name
desc["related_name"] = self.related_name
desc["forward_key"] = self.forward_key
desc["backward_key"] = self.backward_key
desc["through"] = self.through
desc["on_delete"] = self.on_delete
desc["_generated"] = self._generated
return desc


def OneToOneField(
model_name: str,
Expand Down

0 comments on commit a25c0fc

Please sign in to comment.